gateway/modules/aicore/aicorePluginPerplexity.py

364 lines
15 KiB
Python

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
from modules.aicore.aicoreBase import BaseConnectorAi
from modules.datamodels.datamodelAi import AiModel, ModelTags
# 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')),
}
class AiPerplexity(BaseConnectorAi):
"""Connector for communication with the Perplexity API."""
def __init__(self):
super().__init__()
# 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}")
def getConnectorType(self) -> str:
"""Get the connector type identifier."""
return "perplexity"
def getModels(self) -> List[AiModel]:
"""Get all available Perplexity models."""
return [
AiModel(
name="perplexity_callAiBasic",
displayName="Llama 3.1 Sonar Large 128k",
connectorType="perplexity",
maxTokens=128000,
contextLength=128000,
costPer1kTokens=0.005,
costPer1kTokensOutput=0.005,
speedRating=8,
qualityRating=8,
capabilities=["text_generation", "chat", "reasoning", "web_search"],
tags=[ModelTags.TEXT, ModelTags.CHAT, ModelTags.REASONING, ModelTags.WEB, ModelTags.SEARCH, ModelTags.COST_EFFECTIVE],
functionCall=self.callAiBasic,
priority="balanced",
processingMode="advanced",
preferredFor=["general", "web_research"],
version="llama-3.1-sonar-large-128k-online"
),
AiModel(
name="perplexity_callAiWithWebSearch",
displayName="Sonar Pro",
connectorType="perplexity",
maxTokens=128000,
contextLength=128000,
costPer1kTokens=0.01,
costPer1kTokensOutput=0.01,
speedRating=7,
qualityRating=9,
capabilities=["text_generation", "web_search", "research"],
tags=[ModelTags.TEXT, ModelTags.WEB, ModelTags.SEARCH, ModelTags.RESEARCH, ModelTags.HIGH_QUALITY],
functionCall=self.callAiWithWebSearch,
priority="quality",
processingMode="detailed",
preferredFor=["web_research"],
version="sonar-pro"
),
AiModel(
name="perplexity_researchTopic",
displayName="Mistral 7B Instruct",
connectorType="perplexity",
maxTokens=32000,
contextLength=32000,
costPer1kTokens=0.002,
costPer1kTokensOutput=0.002,
speedRating=8,
qualityRating=8,
capabilities=["web_search", "research", "information_gathering"],
tags=[ModelTags.WEB, ModelTags.SEARCH, ModelTags.RESEARCH, ModelTags.INFORMATION, ModelTags.COST_EFFECTIVE],
functionCall=self.researchTopic,
priority="cost",
processingMode="basic",
preferredFor=["web_research"],
version="mistral-7b-instruct"
),
AiModel(
name="perplexity_answerQuestion",
displayName="Mistral 7B Instruct QA",
connectorType="perplexity",
maxTokens=32000,
contextLength=32000,
costPer1kTokens=0.002,
costPer1kTokensOutput=0.002,
speedRating=8,
qualityRating=8,
capabilities=["web_search", "question_answering", "research"],
tags=[ModelTags.WEB, ModelTags.SEARCH, ModelTags.RESEARCH, ModelTags.COST_EFFECTIVE],
functionCall=self.answerQuestion,
priority="cost",
processingMode="basic",
preferredFor=["web_research"],
version="mistral-7b-instruct"
),
AiModel(
name="perplexity_getCurrentNews",
displayName="Mistral 7B Instruct News",
connectorType="perplexity",
maxTokens=32000,
contextLength=32000,
costPer1kTokens=0.002,
costPer1kTokensOutput=0.002,
speedRating=8,
qualityRating=8,
capabilities=["web_search", "news", "current_events"],
tags=[ModelTags.WEB, ModelTags.SEARCH, ModelTags.COST_EFFECTIVE],
functionCall=self.getCurrentNews,
priority="cost",
processingMode="basic",
preferredFor=["web_research"],
version="mistral-7b-instruct"
)
]
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)
# Don't set maxTokens from config - let the model use its full context length
# Our continuation system handles stopping early via prompt engineering
payload = {
"model": self.modelName,
"messages": messages,
"temperature": temperature
}
# Add max_tokens - use provided value or throw error
if maxTokens is None:
raise ValueError("maxTokens must be provided for Perplexity API calls")
payload["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)
# Don't set maxTokens from config - let the model use its full context length
# Our continuation system handles stopping early via prompt engineering
# For web search, we use the configured model name
webSearchModel = self.modelName
payload = {
"model": webSearchModel,
"messages": [
{
"role": "user",
"content": query
}
],
"temperature": temperature
}
# Add max_tokens - use provided value or throw error
if maxTokens is None:
raise ValueError("maxTokens must be provided for Perplexity API calls")
payload["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