Show thoughts and text response in thoughts on anthropic tool use

Previously if anthropic models were using tools, the models text
response accompanying the tool use wouldn't be shown as they were
overwritten in aggregated response with the tool call json.

This changes appends the text response to the thoughts portion on tool
use to still show model's thinking. Thinking and text response are
delineated by italics vs normal text for such cases.
This commit is contained in:
Debanjum
2025-06-19 15:22:19 -07:00
parent c2ab75efef
commit 30878a2fed
2 changed files with 9 additions and 1 deletions

View File

@@ -35,7 +35,7 @@ def anthropic_send_message_to_model(
"""
Send message to model
"""
# Get Response from GPT. Don't use response_type because Anthropic doesn't support it.
# Get response from model. Don't use response_type because Anthropic doesn't support it.
return anthropic_completion_with_backoff(
messages=messages,
system_prompt="",

View File

@@ -129,6 +129,14 @@ def anthropic_completion_with_backoff(
if item.type == "tool_use"
]
if tool_calls:
# If there are tool calls, aggregate thoughts and responses into thoughts
if thoughts and aggregated_response:
# wrap each line of thought in italics
thoughts = "\n".join([f"*{line.strip()}*" for line in thoughts.splitlines() if line.strip()])
thoughts = f"{thoughts}\n\n{aggregated_response}"
else:
thoughts = thoughts or aggregated_response
# Json dump tool calls into aggregated response
aggregated_response = json.dumps(tool_calls)
# If response schema is used, return the first tool call's input
elif response_schema: