删除重试策略
This commit is contained in:
+13
-38
@@ -167,11 +167,9 @@ class OpenAiLLM:
|
|||||||
|
|
||||||
self._kwargs = kwargs
|
self._kwargs = kwargs
|
||||||
|
|
||||||
def invoke(self, user_prompt="你是谁?", need_retry=True, api_key:str = None, **extra_kwargs):
|
def invoke(self, user_prompt="你是谁?", api_key:str = None, **extra_kwargs):
|
||||||
# 初始化 OpenAI 客户端
|
# 初始化 OpenAI 客户端
|
||||||
|
|
||||||
max_retries = 3
|
|
||||||
retry_count = 0
|
|
||||||
# 合并额外的kwargs与self._kwargs
|
# 合并额外的kwargs与self._kwargs
|
||||||
kwargs = {**self._kwargs}
|
kwargs = {**self._kwargs}
|
||||||
if extra_kwargs:
|
if extra_kwargs:
|
||||||
@@ -183,44 +181,21 @@ class OpenAiLLM:
|
|||||||
if api_key is None:
|
if api_key is None:
|
||||||
api_key = APIKeyManager.get_api_key()
|
api_key = APIKeyManager.get_api_key()
|
||||||
|
|
||||||
if need_retry:
|
try:
|
||||||
while retry_count < max_retries:
|
# 创建 Completion 请求. 超时120s
|
||||||
try:
|
# 使用with语句创建客户端,确保资源会被正确释放
|
||||||
|
with OpenAI(api_key=api_key, base_url=self._url) as client:
|
||||||
# 使用with语句创建客户端,确保资源会被正确释放
|
completion = client.chat.completions.create(
|
||||||
with OpenAI(api_key=api_key, base_url=self._url) as client:
|
model=self._model,
|
||||||
# 创建 Completion 请求. 超时120s
|
messages=[{'role': 'user', 'content': user_prompt}],
|
||||||
completion = client.chat.completions.create(
|
**self._kwargs
|
||||||
model=self._model,
|
)
|
||||||
messages=[{'role': 'user', 'content': user_prompt}],
|
return completion.choices[0].message
|
||||||
**self._kwargs
|
except Exception as e:
|
||||||
)
|
raise RuntimeError(f"OpenAiLLM:invoke:error:{str(e)}") from e
|
||||||
return completion.choices[0].message
|
|
||||||
|
|
||||||
except Exception as e:
|
|
||||||
retry_count += 1
|
|
||||||
if retry_count == max_retries:
|
|
||||||
raise RuntimeError(f"OpenAiLLM:invoke:error:{str(e)}") from e
|
|
||||||
else:
|
|
||||||
time.sleep(5*retry_count) # 重试前等待5秒*重试次数
|
|
||||||
else:
|
|
||||||
try:
|
|
||||||
# 创建 Completion 请求. 超时120s
|
|
||||||
# 使用with语句创建客户端,确保资源会被正确释放
|
|
||||||
with OpenAI(api_key=api_key, base_url=self._url) as client:
|
|
||||||
completion = client.chat.completions.create(
|
|
||||||
model=self._model,
|
|
||||||
messages=[{'role': 'user', 'content': user_prompt}],
|
|
||||||
**self._kwargs
|
|
||||||
)
|
|
||||||
return completion.choices[0].message
|
|
||||||
except Exception as e:
|
|
||||||
raise RuntimeError(f"OpenAiLLM:invoke:error:{str(e)}") from e
|
|
||||||
|
|
||||||
async def ainvoke(self, user_prompt="你是谁?", **extra_kwargs):
|
async def ainvoke(self, user_prompt="你是谁?", **extra_kwargs):
|
||||||
"""异步调用OpenAI API"""
|
"""异步调用OpenAI API"""
|
||||||
max_retries = 3
|
|
||||||
retry_count = 0
|
|
||||||
|
|
||||||
# 合并额外的kwargs与self._kwargs
|
# 合并额外的kwargs与self._kwargs
|
||||||
kwargs = {**self._kwargs}
|
kwargs = {**self._kwargs}
|
||||||
|
|||||||
Reference in New Issue
Block a user