Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions backend/ai_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,22 @@ def generate_response(self, query: str,
# Handle tool execution if needed
if response.stop_reason == "tool_use" and tool_manager:
return self._handle_tool_execution(response, api_params, tool_manager)

# Return direct response
return response.content[0].text
return self._extract_text(response)

@staticmethod
def _extract_text(response) -> str:
"""Safely extract text from a Claude response.

The model can return an empty content list (e.g. an end_turn with no
text) or lead with a non-text block. Guard against both rather than
assuming content[0] is always a text block.
"""
for block in response.content:
if getattr(block, "type", None) == "text":
return block.text
return ""

def _handle_tool_execution(self, initial_response, base_params: Dict[str, Any], tool_manager):
"""
Expand Down Expand Up @@ -132,4 +145,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
return self._extract_text(final_response)
2 changes: 1 addition & 1 deletion backend/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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-4-6"

# Embedding model settings
EMBEDDING_MODEL: str = "all-MiniLM-L6-v2"
Expand Down