优化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.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.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):
|
def llm_judge_answer(self, old_answer: str, now_answer: str):
|
||||||
user_prompt = f"""
|
user_prompt = f"""
|
||||||
@@ -76,7 +76,6 @@ class DifyCompareTest:
|
|||||||
response.content = response.content.strip()
|
response.content = response.content.strip()
|
||||||
clean_output = re.sub(r'<think>.*?</think>', '', response.content, flags=re.DOTALL)
|
clean_output = re.sub(r'<think>.*?</think>', '', response.content, flags=re.DOTALL)
|
||||||
result = JsonOutputParser().parse(clean_output)
|
result = JsonOutputParser().parse(clean_output)
|
||||||
result = json.loads(clean_output)
|
|
||||||
return "回答基本相同" if result.get("is_same", False) else "回答基本不相同"
|
return "回答基本相同" if result.get("is_same", False) else "回答基本不相同"
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
retry_count += 1
|
retry_count += 1
|
||||||
@@ -91,17 +90,28 @@ class DifyCompareTest:
|
|||||||
|
|
||||||
def process_workflow(self, workflow_name, client, inputs, query, old_answer):
|
def process_workflow(self, workflow_name, client, inputs, query, old_answer):
|
||||||
"""处理单个工作流调用"""
|
"""处理单个工作流调用"""
|
||||||
|
max_retries = 3
|
||||||
|
retry_count = 0
|
||||||
|
|
||||||
|
while retry_count < max_retries:
|
||||||
try:
|
try:
|
||||||
response = client.create_chat_message(
|
response = client.create_chat_message(
|
||||||
inputs=inputs, query=query, user="AutoCodeRun", response_mode="blocking"
|
inputs=inputs, query=query, user="AutoCodeRun", response_mode="blocking"
|
||||||
)
|
)
|
||||||
result = response.json()
|
result = response.json()
|
||||||
answer = result.get('answer', "")
|
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)
|
judge_result = self.llm_judge_answer(old_answer=old_answer, now_answer=answer)
|
||||||
return answer, judge_result
|
return answer, judge_result
|
||||||
except Exception as e:
|
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 '', ''
|
return '', ''
|
||||||
|
else:
|
||||||
|
import time
|
||||||
|
time.sleep(1) # 等待1秒后重试
|
||||||
|
|
||||||
def process_single_row(self, index, row):
|
def process_single_row(self, index, row):
|
||||||
"""处理单行数据的方法,用于多线程执行"""
|
"""处理单行数据的方法,用于多线程执行"""
|
||||||
@@ -247,7 +257,7 @@ if __name__ == "__main__":
|
|||||||
|
|
||||||
# 处理第一个文件
|
# 处理第一个文件
|
||||||
excel_files = [
|
excel_files = [
|
||||||
("data/excel/5月.xlsx", "data/excel/5月问答对比.xlsx"),
|
# ("data/excel/5月.xlsx", "data/excel/5月问答对比.xlsx"),
|
||||||
("data/excel/其他月.xlsx", "data/excel/其他月问答对比.xlsx")
|
("data/excel/其他月.xlsx", "data/excel/其他月问答对比.xlsx")
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|||||||
@@ -240,7 +240,7 @@ class OpenAiLLM:
|
|||||||
|
|
||||||
self._kwargs = kwargs
|
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 客户端
|
# 初始化 OpenAI 客户端
|
||||||
|
|
||||||
max_retries = 3
|
max_retries = 3
|
||||||
@@ -253,10 +253,13 @@ class OpenAiLLM:
|
|||||||
timeout = httpx.Timeout(300.0)
|
timeout = httpx.Timeout(300.0)
|
||||||
self._kwargs["timeout"] = timeout
|
self._kwargs["timeout"] = timeout
|
||||||
|
|
||||||
|
if api_key is None:
|
||||||
|
api_key = APIKeyManager.get_api_key()
|
||||||
|
|
||||||
if need_retry:
|
if need_retry:
|
||||||
while retry_count < max_retries:
|
while retry_count < max_retries:
|
||||||
try:
|
try:
|
||||||
api_key = APIKeyManager.get_api_key()
|
|
||||||
# 使用with语句创建客户端,确保资源会被正确释放
|
# 使用with语句创建客户端,确保资源会被正确释放
|
||||||
with OpenAI(api_key=api_key, base_url=self._url) as client:
|
with OpenAI(api_key=api_key, base_url=self._url) as client:
|
||||||
# 创建 Completion 请求. 超时120s
|
# 创建 Completion 请求. 超时120s
|
||||||
@@ -276,7 +279,6 @@ class OpenAiLLM:
|
|||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
# 创建 Completion 请求. 超时120s
|
# 创建 Completion 请求. 超时120s
|
||||||
api_key = APIKeyManager.get_api_key()
|
|
||||||
# 使用with语句创建客户端,确保资源会被正确释放
|
# 使用with语句创建客户端,确保资源会被正确释放
|
||||||
with OpenAI(api_key=api_key, base_url=self._url) as client:
|
with OpenAI(api_key=api_key, base_url=self._url) as client:
|
||||||
completion = client.chat.completions.create(
|
completion = client.chat.completions.create(
|
||||||
|
|||||||
Reference in New Issue
Block a user