This commit is contained in:
zy123 2024-12-04 13:36:53 +08:00
parent dc65210f70
commit d8f7718511

View File

@ -57,18 +57,30 @@ def read_txt_to_string(file_path):
return "错误:文件未找到。" return "错误:文件未找到。"
except Exception as e: except Exception as e:
return f"错误:读取文件时发生错误。详细信息:{e}" return f"错误:读取文件时发生错误。详细信息:{e}"
def doubao_model(full_user_query): def doubao_model(full_user_query):
print("call doubao...") print("call doubao...")
# 相关参数 # 相关参数
url = "https://ark.cn-beijing.volces.com/api/v3/chat/completions" url = "https://ark.cn-beijing.volces.com/api/v3/chat/completions"
api_key = "31dca679-c579-40be-834f-2f13143fd9fa" 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 = { headers = {
"Content-Type": "application/json", "Content-Type": "application/json",
"Authorization": "Bearer " + api_key "Authorization": "Bearer " + api_key
} }
max_retries = 1 # 最大重试次数
attempt = 0
while attempt <= max_retries and attempt < len(models):
# 设置当前使用的模型
model_name = models[attempt]
# 请求数据
data = { data = {
"model": model_name, "model": model_name,
"messages": [ "messages": [
@ -79,14 +91,18 @@ def doubao_model(full_user_query):
], ],
"temperature": 0.2 "temperature": 0.2
} }
response = requests.post(url, headers=headers, json=data) try:
response = requests.post(url, headers=headers, json=data) # 设置超时时间为10秒
if response.status_code == 200: response.raise_for_status() # 如果响应状态码不是200将引发HTTPError
# print(response.json()["choices"][0]["message"]["content"]) # 返回模型的回复内容
return response.json()["choices"][0]["message"]["content"] 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: else:
print(f"请求失败,状态码:{response.status_code},错误信息:{response.text}") print(f"请求出错:{e}。正在尝试第 {attempt} 次重试,使用备用模型...")
def generate_full_user_query(file_path, prompt_template): def generate_full_user_query(file_path, prompt_template):
""" """