import logging import httpx import asyncio from typing import Dict, Any, List, Union, Optional from fastapi import HTTPException from modules.shared.configuration import APP_CONFIG # Configure logger logger = logging.getLogger(__name__) def loadConfigData(): """Load configuration data for Perplexity connector""" return { "apiKey": APP_CONFIG.get('Connector_AiPerplexity_API_SECRET'), "apiUrl": APP_CONFIG.get('Connector_AiPerplexity_API_URL'), "modelName": APP_CONFIG.get('Connector_AiPerplexity_MODEL_NAME'), "temperature": float(APP_CONFIG.get('Connector_AiPerplexity_TEMPERATURE')), "maxTokens": int(APP_CONFIG.get('Connector_AiPerplexity_MAX_TOKENS')) } class AiPerplexity: """Connector for communication with the Perplexity API.""" def __init__(self): # Load configuration self.config = loadConfigData() self.apiKey = self.config["apiKey"] self.apiUrl = self.config["apiUrl"] self.modelName = self.config["modelName"] # HttpClient for API calls self.httpClient = httpx.AsyncClient( timeout=120.0, # Longer timeout for complex requests headers={ "Authorization": f"Bearer {self.apiKey}", "Content-Type": "application/json", "Accept": "application/json" } ) logger.info(f"Perplexity Connector initialized with model: {self.modelName}") async def callAiBasic(self, messages: List[Dict[str, Any]], temperature: float = None, maxTokens: int = None) -> str: """ Calls the Perplexity API with the given messages. Args: messages: List of messages in OpenAI format (role, content) temperature: Temperature for response generation (0.0-1.0) maxTokens: Maximum number of tokens in the response Returns: The response from the Perplexity API Raises: HTTPException: For errors in API communication """ try: # Use parameters from configuration if none were overridden if temperature is None: temperature = self.config.get("temperature", 0.2) if maxTokens is None: maxTokens = self.config.get("maxTokens", 2000) payload = { "model": self.modelName, "messages": messages, "temperature": temperature, "max_tokens": maxTokens } response = await self.httpClient.post( self.apiUrl, json=payload ) if response.status_code != 200: error_detail = f"Perplexity API error: {response.status_code} - {response.text}" logger.error(error_detail) # Provide more specific error messages based on status code if response.status_code == 429: error_message = "Rate limit exceeded. Please wait before making another request." elif response.status_code == 401: error_message = "Invalid API key. Please check your Perplexity API configuration." elif response.status_code == 400: error_message = f"Invalid request to Perplexity API: {response.text}" else: error_message = f"Perplexity API error ({response.status_code}): {response.text}" raise HTTPException(status_code=500, detail=error_message) responseJson = response.json() content = responseJson["choices"][0]["message"]["content"] return content except Exception as e: logger.error(f"Error calling Perplexity API: {str(e)}") raise HTTPException(status_code=500, detail=f"Error calling Perplexity API: {str(e)}") async def callAiWithWebSearch(self, query: str, temperature: float = None, maxTokens: int = None) -> str: """ Calls Perplexity API with web search capabilities for research. Args: query: The research query or question temperature: Temperature for response generation (0.0-1.0) maxTokens: Maximum number of tokens in the response Returns: The response from Perplexity with web search context """ try: # Use parameters from configuration if none were overridden if temperature is None: temperature = self.config.get("temperature", 0.2) if maxTokens is None: maxTokens = self.config.get("maxTokens", 2000) # For web search, we use the configured model name webSearchModel = self.modelName payload = { "model": webSearchModel, "messages": [ { "role": "user", "content": query } ], "temperature": temperature, "max_tokens": maxTokens } response = await self.httpClient.post( self.apiUrl, json=payload ) if response.status_code != 200: error_detail = f"Perplexity Web Search API error: {response.status_code} - {response.text}" logger.error(error_detail) if response.status_code == 429: error_message = "Rate limit exceeded for web search. Please wait before making another request." elif response.status_code == 401: error_message = "Invalid API key for web search. Please check your Perplexity API configuration." elif response.status_code == 400: error_message = f"Invalid request to Perplexity Web Search API: {response.text}" else: error_message = f"Perplexity Web Search API error ({response.status_code}): {response.text}" raise HTTPException(status_code=500, detail=error_message) responseJson = response.json() content = responseJson["choices"][0]["message"]["content"] return content except Exception as e: logger.error(f"Error calling Perplexity Web Search API: {str(e)}") raise HTTPException(status_code=500, detail=f"Error calling Perplexity Web Search API: {str(e)}") async def researchTopic(self, topic: str, depth: str = "basic") -> str: """ Research a topic using Perplexity's web search capabilities. Args: topic: The topic to research depth: Research depth - "basic", "detailed", or "comprehensive" Returns: Comprehensive research results on the topic """ try: # Create research prompts based on depth if depth == "basic": prompt = f"Provide a basic overview of: {topic}" elif depth == "detailed": prompt = f"Provide a detailed analysis of: {topic}. Include recent developments, key facts, and important information." else: # comprehensive prompt = f"Provide a comprehensive research report on: {topic}. Include recent developments, key facts, statistics, expert opinions, and current trends." return await self.callAiWithWebSearch(prompt) except Exception as e: logger.error(f"Error researching topic: {str(e)}") raise HTTPException(status_code=500, detail=f"Error researching topic: {str(e)}") async def answerQuestion(self, question: str, context: str = None) -> str: """ Answer a question using web search for current information. Args: question: The question to answer context: Optional context to provide Returns: Answer with web search context """ try: if context: prompt = f"Context: {context}\n\nQuestion: {question}\n\nPlease provide a comprehensive answer using current information from the web." else: prompt = f"Question: {question}\n\nPlease provide a comprehensive answer using current information from the web." return await self.callAiWithWebSearch(prompt) except Exception as e: logger.error(f"Error answering question: {str(e)}") raise HTTPException(status_code=500, detail=f"Error answering question: {str(e)}") async def getCurrentNews(self, topic: str = None, limit: int = 5) -> str: """ Get current news on a specific topic. Args: topic: The topic to get news about (optional) limit: Number of news items to retrieve Returns: Current news information """ try: if topic: prompt = f"Get the latest news about {topic}. Provide {limit} recent news items with sources and dates." else: prompt = f"Get the latest news. Provide {limit} recent news items with sources and dates." return await self.callAiWithWebSearch(prompt) except Exception as e: logger.error(f"Error getting current news: {str(e)}") raise HTTPException(status_code=500, detail=f"Error getting current news: {str(e)}") async def _testConnection(self) -> bool: """ Tests the connection to the Perplexity API. Returns: True if connection is successful, False otherwise """ try: # Try a simple test message testMessages = [ {"role": "user", "content": "Hello, please respond with just 'OK' to confirm the connection works."} ] response = await self.callAiBasic(testMessages) return response and len(response.strip()) > 0 except Exception as e: logger.error(f"Perplexity connection test failed: {str(e)}") return False