104 lines
3.9 KiB
Python
104 lines
3.9 KiB
Python
# services/llm/claude_service.py
|
|
"""
|
|
Claude (Anthropic) service implementation
|
|
"""
|
|
import anthropic
|
|
from typing import Dict, List
|
|
import json
|
|
from .base import LLMService
|
|
from config.api_keys import APIKeyManager
|
|
from utils.logger import setup_logger
|
|
|
|
|
|
class ClaudeService(LLMService):
|
|
def __init__(
|
|
self,
|
|
model: str = "claude-3-7-sonnet-20250219", # Debemos usar el modelo claude-3-7-sonnet-20250219
|
|
temperature: float = 0.3,
|
|
max_tokens: int = 16000,
|
|
):
|
|
api_key = APIKeyManager.get_claude_key()
|
|
if not api_key:
|
|
raise ValueError(
|
|
"Claude API key not found. Please set the CLAUDE_API_KEY environment variable."
|
|
)
|
|
|
|
self.client = anthropic.Anthropic(api_key=api_key)
|
|
self.model = model
|
|
self.temperature = temperature
|
|
self.max_tokens = max_tokens
|
|
self.logger = setup_logger("claude")
|
|
|
|
def generate_text(self, prompt: str) -> str:
|
|
self.logger.info(f"--- PROMPT ---\n{prompt}")
|
|
try:
|
|
message = self.client.messages.create(
|
|
model=self.model,
|
|
max_tokens=self.max_tokens,
|
|
temperature=self.temperature,
|
|
messages=[
|
|
{
|
|
"role": "user",
|
|
"content": prompt,
|
|
}
|
|
],
|
|
)
|
|
response_content = message.content[0].text
|
|
self.logger.info(f"--- RESPONSE ---\n{response_content}")
|
|
return response_content
|
|
except Exception as e:
|
|
self.logger.error(f"Error in Claude API call: {e}")
|
|
print(f"Error in Claude API call: {e}")
|
|
return None
|
|
|
|
def get_similarity_scores(self, texts_pairs: Dict[str, List[str]]) -> List[float]:
|
|
# Claude's API doesn't have a dedicated similarity or JSON mode endpoint as straightforward as others.
|
|
# We will instruct it to return JSON.
|
|
system_prompt = (
|
|
"You are an expert in semantic analysis. Evaluate the semantic similarity between the pairs of texts provided. "
|
|
"Return your response ONLY as a JSON object containing a single key 'similarity_scores' with a list of floats from 0.0 to 1.0. "
|
|
"Do not include any other text, explanation, or markdown formatting. The output must be a valid JSON."
|
|
)
|
|
|
|
request_payload = json.dumps(texts_pairs)
|
|
|
|
try:
|
|
message = self.client.messages.create(
|
|
model=self.model,
|
|
max_tokens=self.max_tokens,
|
|
temperature=self.temperature,
|
|
system=system_prompt,
|
|
messages=[
|
|
{
|
|
"role": "user",
|
|
"content": request_payload,
|
|
}
|
|
],
|
|
)
|
|
|
|
response_content = message.content[0].text
|
|
|
|
try:
|
|
# Find the JSON part of the response
|
|
json_start = response_content.find("{")
|
|
json_end = response_content.rfind("}") + 1
|
|
if json_start == -1 or json_end == 0:
|
|
raise ValueError("No JSON object found in the response.")
|
|
|
|
json_str = response_content[json_start:json_end]
|
|
scores_data = json.loads(json_str)
|
|
|
|
if isinstance(scores_data, dict) and "similarity_scores" in scores_data:
|
|
return scores_data["similarity_scores"]
|
|
else:
|
|
raise ValueError("Unexpected JSON format from Claude.")
|
|
except (json.JSONDecodeError, ValueError) as e:
|
|
print(f"Error decoding Claude JSON response: {e}")
|
|
raise ValueError(
|
|
"Could not decode or parse similarity scores from Claude response."
|
|
)
|
|
|
|
except Exception as e:
|
|
print(f"Error in Claude similarity calculation: {e}")
|
|
return None
|