import re, ast, requests import math, json def save_dict_to_json(dictionary, filename): with open(filename, 'w', encoding='utf-8') as file: json.dump(dictionary, file, ensure_ascii=False, indent=4) def load_dict_from_json(filename): with open(filename, 'r', encoding='utf-8') as file: return json.load(file) def split_dataframe_to_dict(df, chunk_size=100): # 计算需要切割的份数 num_chunks = math.ceil(len(df) / chunk_size) # 用于存储结果的字典 result_dict = {} for i in range(num_chunks): # 切割 DataFrame start = i * chunk_size end = min((i + 1) * chunk_size, len(df)) chunk = df.iloc[start:end] # 将切割后的 DataFrame 转换为字典并存储 result_dict[f'chunk_{i+1}'] = chunk.to_dict(orient='records') return result_dict def extract_list_from_string(input_string): # 使用正则表达式查找列表部分 list_pattern = r'\[.*?\]' match = re.search(list_pattern, input_string, re.DOTALL) if match: list_string = match.group() try: # 使用 ast.literal_eval 安全地解析字符串 result = ast.literal_eval(list_string) # 检查结果是否为列表 if isinstance(result, list): return result else: print("解析结果不是列表") return None except Exception as e: print(f"解析错误: {e}") return None else: print("未找到列表结构") return None def post_openai(messages): Baseurl = "https://fast.bemore.lol" Skey = "sk-dxl4rt2wWswbdrCr1c7b8500B68c43F5B6175b90F7D672C4" payload = json.dumps({ "model": "gpt-4", "messages": messages }) url = Baseurl + "/v1/chat/completions" headers = { 'Accept': 'application/json', 'Authorization': f'Bearer {Skey}', 'User-Agent': 'Apifox/1.0.0 (https://apifox.com)', 'Content-Type': 'application/json' } response = requests.request("POST", url, headers=headers, data=payload) # 解析 JSON 数据为 Python 字典 print(response) data = response.json() # 获取 content 字段的值 content = data['choices'][0]['message']['content'] return content