langchain-ai / langchain-aws

Build LangChain Applications on AWS
MIT License
71 stars 54 forks source link

Remove tool_use requests from the content field of AIMessage #100

Open thiagotps opened 3 weeks ago

thiagotps commented 3 weeks ago

Description

This pull request removes any tool_use request from the content variable of the resulting AIMessage, since all those requests are already stored in the tool_call variable.

Example answer before the change:

AIMessage(content=[{'type': 'text', 'text': '<thinking>\nThe relevant tool for performing mathematical operations is the "math_tool". It requires 3 parameters: "op" (the operation to perform), "x" (the first operand), and "y" (the second operand).\n\nThe user has provided the operation to perform (multiplication) and the operands (3 and 3). I have all the required parameters to invoke the "math_tool".\n</thinking>'}, {'type': 'tool_use', 'name': 'math_tool', 'input': {'op': 'mul', 'x': 3, 'y': 3}, 'id': 'tooluse_MhEvzYGyQVCqN5N-S0mPgA'}], response_metadata={'ResponseMetadata': {'RequestId': '02d1d951-cba6-483e-9b05-bf91df46816a', 'HTTPStatusCode': 200, 'HTTPHeaders': {'date': 'Wed, 03 Jul 2024 15:22:49 GMT', 'content-type': 'application/json', 'content-length': '678', 'connection': 'keep-alive', 'x-amzn-requestid': '02d1d951-cba6-483e-9b05-bf91df46816a'}, 'RetryAttempts': 0}, 'stopReason': 'tool_use', 'metrics': {'latencyMs': 2394}}, id='run-fd37bb62-07c3-4f3c-83dc-d3e4f5725668-0', tool_calls=[{'name': 'math_tool', 'args': {'op': 'mul', 'x': 3, 'y': 3}, 'id': 'tooluse_MhEvzYGyQVCqN5N-S0mPgA'}], usage_metadata={'input_tokens': 563, 'output_tokens': 182, 'total_tokens': 745})

Example answer after the change:

 AIMessage(content="<thinking>\nThe relevant tool to perform mathematical operations is the 'math_tool'. The required parameters for the math_tool are 'op' (the operation to perform), 'x' (the first operand), and 'y' (the second operand).\n\nThe user has provided the information necessary to invoke the math_tool. The operation they would like to perform is multiplication ('mul'), the first operand is 3, and the second operand is also 3.\n</thinking>", response_metadata={'ResponseMetadata': {'RequestId': 'e67bd992-06f4-4804-9518-7ff701066630', 'HTTPStatusCode': 200, 'HTTPHeaders': {'date': 'Wed, 03 Jul 2024 15:24:29 GMT', 'content-type': 'application/json', 'content-length': '729', 'connection': 'keep-alive', 'x-amzn-requestid': 'e67bd992-06f4-4804-9518-7ff701066630'}, 'RetryAttempts': 0}, 'stopReason': 'tool_use', 'metrics': {'latencyMs': 2434}}, id='run-2c3c909a-2a2e-4ce9-ad0e-9ec808d1a010-0', tool_calls=[{'name': 'math_tool', 'args': {'op': 'mul', 'x': 3, 'y': 3}, 'id': 'tooluse_0Vdp8gKUSOGvw8SviH0WnQ'}], usage_metadata={'input_tokens': 563, 'output_tokens': 196, 'total_tokens': 759})