From 27665e35460186d26052f4a869c483b31d655eae Mon Sep 17 00:00:00 2001 From: Harrison Chase Date: Sat, 27 Jan 2024 15:16:22 -0800 Subject: [PATCH] [community] fix anthropic streaming (#16682) --- .../langchain_community/chat_models/anthropic.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/libs/community/langchain_community/chat_models/anthropic.py b/libs/community/langchain_community/chat_models/anthropic.py index 57d7dc7707..682f361679 100644 --- a/libs/community/langchain_community/chat_models/anthropic.py +++ b/libs/community/langchain_community/chat_models/anthropic.py @@ -142,9 +142,10 @@ class ChatAnthropic(BaseChatModel, _AnthropicCommon): stream_resp = self.client.completions.create(**params, stream=True) for data in stream_resp: delta = data.completion - yield ChatGenerationChunk(message=AIMessageChunk(content=delta)) + chunk = ChatGenerationChunk(message=AIMessageChunk(content=delta)) + yield chunk if run_manager: - run_manager.on_llm_new_token(delta) + run_manager.on_llm_new_token(delta, chunk=chunk) async def _astream( self, @@ -161,9 +162,10 @@ class ChatAnthropic(BaseChatModel, _AnthropicCommon): stream_resp = await self.async_client.completions.create(**params, stream=True) async for data in stream_resp: delta = data.completion - yield ChatGenerationChunk(message=AIMessageChunk(content=delta)) + chunk = ChatGenerationChunk(message=AIMessageChunk(content=delta)) + yield chunk if run_manager: - await run_manager.on_llm_new_token(delta) + await run_manager.on_llm_new_token(delta, chunk=chunk) def _generate( self,