Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions config/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 6 additions & 0 deletions config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 16 additions & 3 deletions modules/llm_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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:
Expand All @@ -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)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand Down
Loading