优化DifyCompareTest和ModelTool中的API调用逻辑,增加重试机制以提高稳定性,更新模型名称获取方式为使用环境变量。
This commit is contained in:
@@ -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):
|
||||
"""处理单个工作流调用"""
|
||||
try:
|
||||
response = client.create_chat_message(
|
||||
inputs=inputs, query=query, user="AutoCodeRun", response_mode="blocking"
|
||||
)
|
||||
result = response.json()
|
||||
answer = result.get('answer', "")
|
||||
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}")
|
||||
return '', ''
|
||||
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:
|
||||
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")
|
||||
]
|
||||
|
||||
|
||||
Reference in New Issue
Block a user