优化DifyCompareTest和ModelTool中的API调用逻辑,增加重试机制以提高稳定性,更新模型名称获取方式为使用环境变量。

This commit is contained in:
2025-07-21 09:44:42 +08:00
parent 6243fedc4e
commit 0dda581c8e
2 changed files with 29 additions and 17 deletions
+14 -4
View File
@@ -46,7 +46,7 @@ class DifyCompareTest:
self.first_wiki_client = ChatClient(api_key="app-gocvuqduBnJptYNPpnW9V9R6", base_url=os.getenv("DIFY_BSAE_URL"))
# 词条与工单同时检索
self.both_wiki_worker_client = ChatClient(api_key="app-CPoOMaGDsLRPAe9TW7Xjhszy", base_url=os.getenv("DIFY_BSAE_URL"))
self.llm = OpenAiLLM(base_url=os.getenv("OPENAI_API_BASE"), model="deepseek-ai/DeepSeek-R1")
self.llm = OpenAiLLM(base_url=os.getenv("OPENAI_API_BASE"), model=os.getenv("MODEL_NAME"))
def llm_judge_answer(self, old_answer: str, now_answer: str):
user_prompt = f"""
@@ -76,7 +76,6 @@ class DifyCompareTest:
response.content = response.content.strip()
clean_output = re.sub(r'<think>.*?</think>', '', response.content, flags=re.DOTALL)
result = JsonOutputParser().parse(clean_output)
result = json.loads(clean_output)
return "回答基本相同" if result.get("is_same", False) else "回答基本不相同"
except Exception as e:
retry_count += 1
@@ -91,17 +90,28 @@ class DifyCompareTest:
def process_workflow(self, workflow_name, client, inputs, query, old_answer):
"""处理单个工作流调用"""
max_retries = 3
retry_count = 0
while retry_count < max_retries:
try:
response = client.create_chat_message(
inputs=inputs, query=query, user="AutoCodeRun", response_mode="blocking"
)
result = response.json()
answer = result.get('answer', "")
if len(answer) == 0:
raise Exception(f"回答为空: {result}")
judge_result = self.llm_judge_answer(old_answer=old_answer, now_answer=answer)
return answer, judge_result
except Exception as e:
logging.error(f"{workflow_name}调用失败: {e}")
retry_count += 1
if retry_count >= max_retries:
logging.error(f"{workflow_name}调用失败 (尝试 {max_retries} 次后): {e}")
return '', ''
else:
import time
time.sleep(1) # 等待1秒后重试
def process_single_row(self, index, row):
"""处理单行数据的方法,用于多线程执行"""
@@ -247,7 +257,7 @@ if __name__ == "__main__":
# 处理第一个文件
excel_files = [
("data/excel/5月.xlsx", "data/excel/5月问答对比.xlsx"),
# ("data/excel/5月.xlsx", "data/excel/5月问答对比.xlsx"),
("data/excel/其他月.xlsx", "data/excel/其他月问答对比.xlsx")
]
+5 -3
View File
@@ -240,7 +240,7 @@ class OpenAiLLM:
self._kwargs = kwargs
def invoke(self, user_prompt="你是谁?", need_retry=True,**extra_kwargs):
def invoke(self, user_prompt="你是谁?", need_retry=True, api_key:str = None, **extra_kwargs):
# 初始化 OpenAI 客户端
max_retries = 3
@@ -253,10 +253,13 @@ class OpenAiLLM:
timeout = httpx.Timeout(300.0)
self._kwargs["timeout"] = timeout
if api_key is None:
api_key = APIKeyManager.get_api_key()
if need_retry:
while retry_count < max_retries:
try:
api_key = APIKeyManager.get_api_key()
# 使用with语句创建客户端,确保资源会被正确释放
with OpenAI(api_key=api_key, base_url=self._url) as client:
# 创建 Completion 请求. 超时120s
@@ -276,7 +279,6 @@ class OpenAiLLM:
else:
try:
# 创建 Completion 请求. 超时120s
api_key = APIKeyManager.get_api_key()
# 使用with语句创建客户端,确保资源会被正确释放
with OpenAI(api_key=api_key, base_url=self._url) as client:
completion = client.chat.completions.create(