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

Add text extraction to tool call processing in BedrockLLM #96

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Cerrix
Copy link

@Cerrix Cerrix commented Jul 2, 2024

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

  • Created a new function extract_tool_calls_and_text which replaces the existing extract_tool_calls function.
  • The new function returns both a list of ToolCall objects and a string containing any text content from the response.
  • Modified the relevant parts of the BedrockLLM class to utilize this new function and handle the returned text.

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.

@3coins
Copy link
Collaborator

3coins commented Jul 12, 2024

@Cerrix
Thanks for submitting the change. Can you provide some sample code to test this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants