myagent-ai 1.9.1 → 1.9.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/core/llm.py +28 -20
- package/package.json +1 -1
package/core/llm.py
CHANGED
|
@@ -649,28 +649,36 @@ class LLMClient:
|
|
|
649
649
|
logger.error(f"流式 LLM 调用失败: {e}")
|
|
650
650
|
|
|
651
651
|
async def _stream_openai(self, kwargs: dict) -> AsyncGenerator[str, None]:
|
|
652
|
-
"""OpenAI / 兼容接口 (含 Zhipu) 流式调用
|
|
653
|
-
loop = asyncio.get_running_loop()
|
|
654
|
-
|
|
655
|
-
def _create_stream():
|
|
656
|
-
return self._client.chat.completions.create(**kwargs)
|
|
657
|
-
|
|
658
|
-
stream = await loop.run_in_executor(None, _create_stream)
|
|
652
|
+
"""OpenAI / 兼容接口 (含 Zhipu) 流式调用
|
|
659
653
|
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
654
|
+
支持两种客户端:
|
|
655
|
+
- AsyncOpenAI: 使用 async for 直接异步迭代
|
|
656
|
+
- 同步 OpenAI: 在 executor 中同步迭代
|
|
657
|
+
"""
|
|
658
|
+
# 判断客户端类型,选择合适的流式迭代方式
|
|
659
|
+
is_async = hasattr(self._client, '__aenter__')
|
|
660
|
+
|
|
661
|
+
if is_async:
|
|
662
|
+
# AsyncOpenAI: 直接异步迭代
|
|
663
|
+
stream = await self._client.chat.completions.create(**kwargs)
|
|
664
|
+
async for chunk in stream:
|
|
665
|
+
if chunk.choices and chunk.choices[0].delta.content:
|
|
666
|
+
yield chunk.choices[0].delta.content
|
|
667
|
+
else:
|
|
668
|
+
# 同步 OpenAI: 在 executor 中迭代
|
|
669
|
+
loop = asyncio.get_running_loop()
|
|
666
670
|
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
671
|
+
def _create_and_iterate():
|
|
672
|
+
stream = self._client.chat.completions.create(**kwargs)
|
|
673
|
+
results = []
|
|
674
|
+
for chunk in stream:
|
|
675
|
+
if chunk.choices and chunk.choices[0].delta.content:
|
|
676
|
+
results.append(chunk.choices[0].delta.content)
|
|
677
|
+
return results
|
|
678
|
+
|
|
679
|
+
chunks = await loop.run_in_executor(None, _create_and_iterate)
|
|
680
|
+
for content in chunks:
|
|
681
|
+
yield content
|
|
674
682
|
|
|
675
683
|
async def _stream_anthropic(
|
|
676
684
|
self, messages: List[Message], kwargs: dict
|