import os
import anthropic
import requests
from dotenv import load_dotenv
import time
import json

os.environ["http_proxy"] = "http://localhost:7890"
os.environ["https_proxy"] = "http://localhost:7890"

class LLMHandler:
    def __init__(self):
        load_dotenv()
        self.api_key = os.getenv("ANTHROPIC_API_KEY")
        if not self.api_key:
            raise ValueError("ANTHROPIC_API_KEY environment variable not found, please make sure it is set")
        
        self.client = anthropic.Anthropic(api_key=self.api_key)
        self.default_model = "claude-3-7-sonnet-20250219"
        self.max_tokens = 3000
    
    def call_llm(self, system_prompt, messages, max_tokens=None, model=None, retries=3):
        model = model or self.default_model
        
        max_tokens = max_tokens or self.max_tokens
        
        print(f"Request model: {model}")
        print(f"max tokens: {max_tokens}")
        
        try:
            response = self.client.messages.create(
                model=model,
                max_tokens=max_tokens,
                system=system_prompt,
                messages=messages
            )
            return response
        except Exception as e:
            print(f"Error calling LLM: {e}")
            raise 