Skip to content

Commit

Permalink
community[patch]: Invoke callback prior to yielding token (langchain-…
Browse files Browse the repository at this point in the history
…ai#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: langchain-ai#16913
- Dependencies: None
  • Loading branch information
williamdevena authored and gkorland committed Mar 30, 2024
1 parent 32ca3a8 commit ebc6282
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions libs/community/langchain_community/llms/anthropic.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,9 +309,9 @@ def _stream(
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,
Expand Down Expand Up @@ -345,9 +345,9 @@ async def _astream(
**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."""
Expand Down

0 comments on commit ebc6282

Please sign in to comment.