From 0dda581c8e37f1745fcadf42c2e569ecceb55ee3 Mon Sep 17 00:00:00 2001 From: ouyangyouzhang Date: Mon, 21 Jul 2025 09:44:42 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96DifyCompareTest=E5=92=8CModel?= =?UTF-8?q?Tool=E4=B8=AD=E7=9A=84API=E8=B0=83=E7=94=A8=E9=80=BB=E8=BE=91?= =?UTF-8?q?=EF=BC=8C=E5=A2=9E=E5=8A=A0=E9=87=8D=E8=AF=95=E6=9C=BA=E5=88=B6?= =?UTF-8?q?=E4=BB=A5=E6=8F=90=E9=AB=98=E7=A8=B3=E5=AE=9A=E6=80=A7=EF=BC=8C?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=E6=A8=A1=E5=9E=8B=E5=90=8D=E7=A7=B0=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E6=96=B9=E5=BC=8F=E4=B8=BA=E4=BD=BF=E7=94=A8=E7=8E=AF?= =?UTF-8?q?=E5=A2=83=E5=8F=98=E9=87=8F=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rag2_0/dify/DifyCompareTest.py | 38 +++++++++++++++++++++------------- rag2_0/tool/ModelTool.py | 8 ++++--- 2 files changed, 29 insertions(+), 17 deletions(-) diff --git a/rag2_0/dify/DifyCompareTest.py b/rag2_0/dify/DifyCompareTest.py index 93bbd29..587c53e 100755 --- a/rag2_0/dify/DifyCompareTest.py +++ b/rag2_0/dify/DifyCompareTest.py @@ -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'.*?', '', 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") ] diff --git a/rag2_0/tool/ModelTool.py b/rag2_0/tool/ModelTool.py index d59f489..c197f6b 100755 --- a/rag2_0/tool/ModelTool.py +++ b/rag2_0/tool/ModelTool.py @@ -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(