langchain-ai / langchain-aws

Build LangChain Applications on AWS
MIT License
63 stars 46 forks source link

Add text extraction to tool call processing in BedrockLLM #96

Open Cerrix opened 3 days ago

Cerrix commented 3 days ago

Description

This pull request enhances the functionality of the BedrockLLM class by modifying how tool calls and text are extracted from the model's response. Previously, when tools were provided, only tool calls were extracted, and any text content was ignored. This change allows for both tool calls and text to be processed and returned.

Here an example answer before the change:

content='' additional_kwargs={'usage': {'prompt_tokens': 411, 'completion_tokens': 89, 'total_tokens': 500}, 'stop_reason': 'tool_use', 'model_id': 'anthropic.claude-3-sonnet-20240229-v1:0'} response_metadata={'usage': {'prompt_tokens': 411, 'completion_tokens': 89, 'total_tokens': 500}, 'stop_reason': 'tool_use', 'model_id': 'anthropic.claude-3-sonnet-20240229-v1:0'} id='run-7e8d1ca5-7556-414c-9c29-9a51e79aced1-0' tool_calls=[{'name': 'multiply', 'args': {'first_int': 23, 'second_int': 7}, 'id': 'toolu_bdrk_01MqueVwRGpJuKh6mC8mU5Zm'}] usage_metadata={'input_tokens': 411, 'output_tokens': 89, 'total_tokens': 500}

Here after the change:

content="Okay, let's multiply 5 and 42 using the tool:", additional_kwargs={'usage': {'prompt_tokens': 246, 'completion_tokens': 90, 'total_tokens': 336}, 'stop_reason': 'tool_use', 'model_id': 'anthropic.claude-3-sonnet-20240229-v1:0'}, response_metadata={'usage': {'prompt_tokens': 246, 'completion_tokens': 90, 'total_tokens': 336}, 'stop_reason': 'tool_use', 'model_id': 'anthropic.claude-3-sonnet-20240229-v1:0'}, id='run-7d3ae303-ce91-4994-881a-31ecc241994d-0', tool_calls=[{'name': 'multiply', 'args': {'first_int': 5, 'second_int': 42}, 'id': 'toolu_bdrk_01S5BvS4Ws2GFhVxKUQ7UDDo'}], usage_metadata={'input_tokens': 246, 'output_tokens': 90, 'total_tokens': 336}

Changes

Motivation

This change allows users to receive both tool calls and text content when using the BedrockLLM with tools, providing a more complete and flexible interaction with the model.

Additional Notes

This change maintains backwards compatibility with existing usage while providing enhanced functionality for users who need access to both tool calls and text content.