Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes token logging in callbacks when streaming=True is used. #241

Merged
merged 1 commit into from
Oct 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions libs/aws/langchain_aws/chat_models/bedrock.py
Original file line number Diff line number Diff line change
Expand Up @@ -477,14 +477,19 @@ def _stream(
**kwargs,
):
if isinstance(chunk, AIMessageChunk):
yield ChatGenerationChunk(message=chunk)
generation_chunk = ChatGenerationChunk(message=chunk)
if run_manager:
3coins marked this conversation as resolved.
Show resolved Hide resolved
run_manager.on_llm_new_token(
generation_chunk.text, chunk=generation_chunk
)
yield generation_chunk
else:
delta = chunk.text
if generation_info := chunk.generation_info:
usage_metadata = generation_info.pop("usage_metadata", None)
else:
usage_metadata = None
yield ChatGenerationChunk(
generation_chunk = ChatGenerationChunk(
message=AIMessageChunk(
content=delta,
response_metadata=chunk.generation_info,
Expand All @@ -493,6 +498,11 @@ def _stream(
if chunk.generation_info is not None
else AIMessageChunk(content=delta)
)
if run_manager:
run_manager.on_llm_new_token(
3coins marked this conversation as resolved.
Show resolved Hide resolved
generation_chunk.text, chunk=generation_chunk
)
yield generation_chunk

def _generate(
self,
Expand Down
Loading