diff --git a/backend/ai_generator.py b/backend/ai_generator.py index 0363ca90c..fc3675bbf 100644 --- a/backend/ai_generator.py +++ b/backend/ai_generator.py @@ -34,11 +34,18 @@ def __init__(self, api_key: str, model: str): self.model = model # Pre-build base API parameters + # Sonnet 5.5 rejects non-default temperature; "between_tools" is its lowest + # thinking setting (no extended thinking), closest to the previous behavior self.base_params = { "model": self.model, - "temperature": 0, - "max_tokens": 800 + "max_tokens": 800, + "thinking": {"type": "between_tools"} } + + @staticmethod + def _extract_text(response) -> str: + """Join text blocks; the response may also contain thinking blocks.""" + return "".join(block.text for block in response.content if block.type == "text") def generate_response(self, query: str, conversation_history: Optional[str] = None, @@ -84,7 +91,7 @@ def generate_response(self, query: str, return self._handle_tool_execution(response, api_params, tool_manager) # Return direct response - return response.content[0].text + return self._extract_text(response) def _handle_tool_execution(self, initial_response, base_params: Dict[str, Any], tool_manager): """ @@ -101,8 +108,12 @@ def _handle_tool_execution(self, initial_response, base_params: Dict[str, Any], # Start with existing messages messages = base_params["messages"].copy() - # Add AI's tool use response - messages.append({"role": "assistant", "content": initial_response.content}) + # Add AI's tool use response; thinking blocks are dropped because the final + # call omits tools, and replaying them after that change can be rejected + messages.append({ + "role": "assistant", + "content": [block for block in initial_response.content if block.type != "thinking"] + }) # Execute all tool calls and collect results tool_results = [] @@ -132,4 +143,4 @@ def _handle_tool_execution(self, initial_response, base_params: Dict[str, Any], # Get final response final_response = self.client.messages.create(**final_params) - return final_response.content[0].text \ No newline at end of file + return self._extract_text(final_response) \ No newline at end of file diff --git a/backend/config.py b/backend/config.py index d9f6392ef..55b4999fb 100644 --- a/backend/config.py +++ b/backend/config.py @@ -10,7 +10,7 @@ class Config: """Configuration settings for the RAG system""" # Anthropic API settings ANTHROPIC_API_KEY: str = os.getenv("ANTHROPIC_API_KEY", "") - ANTHROPIC_MODEL: str = "claude-sonnet-4-20250514" + ANTHROPIC_MODEL: str = "claude-sonnet-5-5" # Embedding model settings EMBEDDING_MODEL: str = "all-MiniLM-L6-v2"