community[patch]: callback before yield for _stream/_astream (#17907)

- Description: callback on_llm_new_token before yield chunk for
_stream/_astream for some chat models, make all chat models in a
consistent behaviour.
- Issue: N/A
- Dependencies: N/A
This commit is contained in:
mackong
2024-02-23 08:15:21 +08:00
committed by GitHub
parent 15e42f1799
commit 9678797625
22 changed files with 66 additions and 55 deletions

View File

@@ -342,9 +342,9 @@ class ChatTongyi(BaseChatModel):
chunk = ChatGenerationChunk(
**self._chat_generation_from_qwen_resp(stream_resp, is_chunk=True)
)
yield chunk
if run_manager:
run_manager.on_llm_new_token(chunk.text, chunk=chunk)
yield chunk
async def _astream(
self,
@@ -360,9 +360,9 @@ class ChatTongyi(BaseChatModel):
chunk = ChatGenerationChunk(
**self._chat_generation_from_qwen_resp(stream_resp, is_chunk=True)
)
yield chunk
if run_manager:
await run_manager.on_llm_new_token(chunk.text, chunk=chunk)
yield chunk
def _invocation_params(
self, messages: List[BaseMessage], stop: Any, **kwargs: Any