From d8f7718511f371a7e0732617870c6093bc86eb33 Mon Sep 17 00:00:00 2001 From: zy123 <646228430@qq.com> Date: Wed, 4 Dec 2024 13:36:53 +0800 Subject: [PATCH] 12.4 --- flask_app/general/doubao.py | 56 ++++++++++++++++++++++++------------- 1 file changed, 36 insertions(+), 20 deletions(-) diff --git a/flask_app/general/doubao.py b/flask_app/general/doubao.py index 772f7a4..30491d2 100644 --- a/flask_app/general/doubao.py +++ b/flask_app/general/doubao.py @@ -57,36 +57,52 @@ def read_txt_to_string(file_path): return "错误:文件未找到。" except Exception as e: return f"错误:读取文件时发生错误。详细信息:{e}" + + def doubao_model(full_user_query): print("call doubao...") # 相关参数 url = "https://ark.cn-beijing.volces.com/api/v3/chat/completions" api_key = "31dca679-c579-40be-834f-2f13143fd9fa" - model_name = "ep-20241119121710-425g6" # ep-20241119121710-425g6 豆包Pro 32k模型 ep-20241119121743-xt6wg 128k - # 大模型提取页码 + # 定义主模型和备用模型 + models = [ + "ep-20241119121710-425g6", # 豆包Pro 32k模型 + "ep-20241119121743-xt6wg" # 128k模型 + ] + # 请求头 headers = { "Content-Type": "application/json", "Authorization": "Bearer " + api_key } - data = { - "model": model_name, - "messages": [ - { - "role": "user", - "content": full_user_query - } - ], - "temperature":0.2 - } - response = requests.post(url, headers=headers, json=data) - - if response.status_code == 200: - # print(response.json()["choices"][0]["message"]["content"]) - return response.json()["choices"][0]["message"]["content"] - else: - print(f"请求失败,状态码:{response.status_code},错误信息:{response.text}") - + max_retries = 1 # 最大重试次数 + attempt = 0 + while attempt <= max_retries and attempt < len(models): + # 设置当前使用的模型 + model_name = models[attempt] + # 请求数据 + data = { + "model": model_name, + "messages": [ + { + "role": "user", + "content": full_user_query + } + ], + "temperature": 0.2 + } + try: + response = requests.post(url, headers=headers, json=data) # 设置超时时间为10秒 + response.raise_for_status() # 如果响应状态码不是200,将引发HTTPError + # 返回模型的回复内容 + return response.json()["choices"][0]["message"]["content"] + except requests.exceptions.RequestException as e: + attempt += 1 + if attempt > max_retries or attempt >= len(models): + print(f"请求失败,尝试了 {attempt} 次。错误信息:{e}") + return None # 或者根据需要返回其他默认值 + else: + print(f"请求出错:{e}。正在尝试第 {attempt} 次重试,使用备用模型...") def generate_full_user_query(file_path, prompt_template): """