From 30878a2fed630adc4156f33d85681df4bc598587 Mon Sep 17 00:00:00 2001 From: Debanjum Date: Thu, 19 Jun 2025 15:22:19 -0700 Subject: [PATCH] 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. --- .../processor/conversation/anthropic/anthropic_chat.py | 2 +- src/khoj/processor/conversation/anthropic/utils.py | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/khoj/processor/conversation/anthropic/anthropic_chat.py b/src/khoj/processor/conversation/anthropic/anthropic_chat.py index 1c3b7166..4885c1a2 100644 --- a/src/khoj/processor/conversation/anthropic/anthropic_chat.py +++ b/src/khoj/processor/conversation/anthropic/anthropic_chat.py @@ -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="", diff --git a/src/khoj/processor/conversation/anthropic/utils.py b/src/khoj/processor/conversation/anthropic/utils.py index 97175253..2842a270 100644 --- a/src/khoj/processor/conversation/anthropic/utils.py +++ b/src/khoj/processor/conversation/anthropic/utils.py @@ -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: