From eb04d0d3e20bfa339a10689f567d376f80115433 Mon Sep 17 00:00:00 2001 From: William De Vena <60664495+williamdevena@users.noreply.github.com> Date: Sun, 3 Mar 2024 23:13:41 +0100 Subject: [PATCH] community[patch]: Invoke callback prior to yielding token (#18452) ## PR title community[patch]: Invoke callback prior to yielding token ## PR message - Description: Invoke callback prior to yielding token in _stream and _astream methods in llms/anthropic. - Issue: https://github.com/langchain-ai/langchain/issues/16913 - Dependencies: None --- libs/community/langchain_community/llms/anthropic.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libs/community/langchain_community/llms/anthropic.py b/libs/community/langchain_community/llms/anthropic.py index f5674a07667..ad37bd9e4ad 100644 --- a/libs/community/langchain_community/llms/anthropic.py +++ b/libs/community/langchain_community/llms/anthropic.py @@ -309,9 +309,9 @@ class Anthropic(LLM, _AnthropicCommon): prompt=self._wrap_prompt(prompt), stop_sequences=stop, stream=True, **params ): chunk = GenerationChunk(text=token.completion) - yield chunk if run_manager: run_manager.on_llm_new_token(chunk.text, chunk=chunk) + yield chunk async def _astream( self, @@ -345,9 +345,9 @@ class Anthropic(LLM, _AnthropicCommon): **params, ): chunk = GenerationChunk(text=token.completion) - yield chunk if run_manager: await run_manager.on_llm_new_token(chunk.text, chunk=chunk) + yield chunk def get_num_tokens(self, text: str) -> int: """Calculate number of tokens."""