优化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
+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(