Skip to content

Commit

Permalink
community[patch]: Invoke callback prior to yielding token (#18452)
Browse files Browse the repository at this point in the history
## 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: #16913
- Dependencies: None
  • Loading branch information
williamdevena authored and hinthornw committed Mar 4, 2024
1 parent 35ba0b7 commit fdca1bb
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 fdca1bb

Please sign in to comment.