撤回之前使用langchain_openai调用模型的逻辑。因为暂时无法解决调用Qwen3禁用思考模式的问题
This commit is contained in:
+13
-34
@@ -217,7 +217,7 @@ class OpenAiLLM:
|
||||
except Exception as e:
|
||||
raise RuntimeError(f"OpenAiLLM:invoke:error:{str(e)}.api_key:{api_key}") from e
|
||||
|
||||
async def invoke_async(self, user_prompt="你是谁?", need_retry=True, **extra_kwargs):
|
||||
async def ainvoke(self, user_prompt="你是谁?", **extra_kwargs):
|
||||
"""异步调用OpenAI API"""
|
||||
max_retries = 3
|
||||
retry_count = 0
|
||||
@@ -231,38 +231,17 @@ class OpenAiLLM:
|
||||
timeout = httpx.Timeout(300.0)
|
||||
kwargs["timeout"] = timeout
|
||||
|
||||
if need_retry:
|
||||
while retry_count < max_retries:
|
||||
try:
|
||||
api_key = APIKeyManager.get_api_key()
|
||||
# 使用异步客户端
|
||||
async with AsyncOpenAI(api_key=api_key, base_url=self._url) as client:
|
||||
# 创建异步Completion请求
|
||||
completion = await client.chat.completions.create(
|
||||
model=self._model,
|
||||
messages=[{'role': 'user', 'content': user_prompt}],
|
||||
**kwargs
|
||||
)
|
||||
return completion.choices[0].message
|
||||
|
||||
except Exception as e:
|
||||
retry_count += 1
|
||||
if retry_count == max_retries:
|
||||
raise RuntimeError(f"OpenAiLLM:invoke_async:error:{str(e)}.api_key:{api_key}") from e
|
||||
else:
|
||||
await asyncio.sleep(5*retry_count) # 异步等待
|
||||
else:
|
||||
try:
|
||||
api_key = APIKeyManager.get_api_key()
|
||||
async with AsyncOpenAI(api_key=api_key, base_url=self._url) as client:
|
||||
completion = await client.chat.completions.create(
|
||||
model=self._model,
|
||||
messages=[{'role': 'user', 'content': user_prompt}],
|
||||
**kwargs
|
||||
)
|
||||
return completion.choices[0].message
|
||||
except Exception as e:
|
||||
raise RuntimeError(f"OpenAiLLM:invoke_async:error:{str(e)}.api_key:{api_key}") from e
|
||||
try:
|
||||
api_key = APIKeyManager.get_api_key()
|
||||
async with AsyncOpenAI(api_key=api_key, base_url=self._url) as client:
|
||||
completion = await client.chat.completions.create(
|
||||
model=self._model,
|
||||
messages=[{'role': 'user', 'content': user_prompt}],
|
||||
**kwargs
|
||||
)
|
||||
return completion.choices[0].message
|
||||
except Exception as e:
|
||||
raise RuntimeError(f"OpenAiLLM:ainvoke:error:{str(e)}") from e
|
||||
|
||||
if __name__ == "__main__":
|
||||
# 测试重排模型
|
||||
@@ -291,7 +270,7 @@ if __name__ == "__main__":
|
||||
|
||||
# 测试异步LLM调用
|
||||
llm = OpenAiLLM()
|
||||
response = await llm.invoke_async("你好,请简单介绍一下自己")
|
||||
response = await llm.ainvoke("你好,请简单介绍一下自己")
|
||||
print(f"异步LLM响应: {response.content}")
|
||||
|
||||
# 如果需要运行异步测试,取消下面的注释
|
||||
|
||||
Reference in New Issue
Block a user