mirror of
https://github.com/hwchase17/langchain.git
synced 2025-06-20 22:03:52 +00:00
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
This commit is contained in:
parent
371bec79bc
commit
eb04d0d3e2
@ -309,9 +309,9 @@ class Anthropic(LLM, _AnthropicCommon):
|
|||||||
prompt=self._wrap_prompt(prompt), stop_sequences=stop, stream=True, **params
|
prompt=self._wrap_prompt(prompt), stop_sequences=stop, stream=True, **params
|
||||||
):
|
):
|
||||||
chunk = GenerationChunk(text=token.completion)
|
chunk = GenerationChunk(text=token.completion)
|
||||||
yield chunk
|
|
||||||
if run_manager:
|
if run_manager:
|
||||||
run_manager.on_llm_new_token(chunk.text, chunk=chunk)
|
run_manager.on_llm_new_token(chunk.text, chunk=chunk)
|
||||||
|
yield chunk
|
||||||
|
|
||||||
async def _astream(
|
async def _astream(
|
||||||
self,
|
self,
|
||||||
@ -345,9 +345,9 @@ class Anthropic(LLM, _AnthropicCommon):
|
|||||||
**params,
|
**params,
|
||||||
):
|
):
|
||||||
chunk = GenerationChunk(text=token.completion)
|
chunk = GenerationChunk(text=token.completion)
|
||||||
yield chunk
|
|
||||||
if run_manager:
|
if run_manager:
|
||||||
await run_manager.on_llm_new_token(chunk.text, chunk=chunk)
|
await run_manager.on_llm_new_token(chunk.text, chunk=chunk)
|
||||||
|
yield chunk
|
||||||
|
|
||||||
def get_num_tokens(self, text: str) -> int:
|
def get_num_tokens(self, text: str) -> int:
|
||||||
"""Calculate number of tokens."""
|
"""Calculate number of tokens."""
|
||||||
|
Loading…
Reference in New Issue
Block a user