diff --git a/config/.env.example b/config/.env.example index bef32a9..d7b0593 100644 --- a/config/.env.example +++ b/config/.env.example @@ -12,3 +12,6 @@ # Grok API Key (required for Grok provider) # GROK_API_KEY=your_grok_key_here + +# Perplexity API Key (required for Perplexity provider) +PERPLEXITY_API_KEY=your_perplexity_key_here diff --git a/config/config.py b/config/config.py index cebc032..f711ff9 100644 --- a/config/config.py +++ b/config/config.py @@ -57,6 +57,12 @@ "api_key": "", # User must provide "models": ["grok-1", "grok-2"], }, + "perplexity": { + "name": "Perplexity AI", + "base_url": "https://api.perplexity.ai", + "api_key": "", # User must provide + "models": ["sonar", "sonar-pro"], # Example model names + }, } # Topic list diff --git a/modules/llm_service.py b/modules/llm_service.py index 8641df3..fca07de 100644 --- a/modules/llm_service.py +++ b/modules/llm_service.py @@ -33,6 +33,7 @@ # Grok uses the OpenAI client with a different base URL GROK_AVAILABLE = OPENAI_AVAILABLE +PERPLEXITY_AVAILABLE = OPENAI_AVAILABLE from config import config @@ -58,7 +59,7 @@ def __init__( Initialize the LLM service. Args: - provider: LLM provider (e.g., 'lmstudio', 'openai', 'anthropic', 'google', 'grok') + provider: LLM provider (e.g., 'lmstudio', 'openai', 'anthropic', 'google', 'grok', 'perplexity') base_url: Base URL for the LLM API api_key: API key for authentication (if None, will use provider-specific key) model: Model identifier to use @@ -79,6 +80,8 @@ def __init__( self.api_key = config.GOOGLE_API_KEY elif provider == "grok": self.api_key = config.GROK_API_KEY + elif provider == "perplexity": + self.api_key = config.PERPLEXITY_API_KEY else: # lmstudio or other self.api_key = config.LLM_API_KEY else: @@ -87,7 +90,7 @@ def __init__( # Initialize client based on provider self.client = None - if provider in ["lmstudio", "openai", "grok"]: + if provider in ["lmstudio", "openai", "grok", "perplexity"]: if OPENAI_AVAILABLE: try: self.client = OpenAI(base_url=base_url, api_key=self.api_key) @@ -126,6 +129,16 @@ def __init__( logger.warning( "Google Generative AI package not installed or API key missing. Falling back to direct API calls." ) + elif provider == "perplexity": + if PERPLEXITY_AVAILABLE and self.api_key: + try: + self.client = OpenAI(api_key=self.api_key, base_url=self.base_url) + print(self.base_url) + logger.info("Initialized Perplexity client with model %s", model) + except Exception as e: + logger.warning("Error initializing Perplexity client: %s", e) + logger.info("Falling back to direct API calls via requests") + def generate_response( self, @@ -161,7 +174,7 @@ def generate_response( for attempt in range(max_retries): try: # Handle different providers - if self.provider in ["lmstudio", "openai", "grok"]: + if self.provider in ["lmstudio", "openai", "grok", "perplexity"]: if self.client is not None: # Use the OpenAI client response = self.client.chat.completions.create(