Replace print statements with proper logging
Fixed inconsistent error handling in ai_provider.py and memory.py by: - Adding optional logger parameter to PerplexityProvider and ConversationMemory classes - Replacing all print() statements with proper logger.error() calls - Updating chatbot.py to pass logger instance to both components - Ensuring consistent logging architecture across the codebase This improves error visibility and integrates with the existing logging infrastructure.
This commit is contained in:
parent
a1f30c31c6
commit
2b15ceeaa1
3 changed files with 22 additions and 12 deletions
|
|
@ -5,13 +5,14 @@ Handles communication with Perplexity Sonar API
|
|||
import httpx
|
||||
import json
|
||||
import time
|
||||
import logging
|
||||
from typing import List, Dict, Optional
|
||||
|
||||
|
||||
class PerplexityProvider:
|
||||
"""Communicates with Perplexity API for AI responses"""
|
||||
|
||||
def __init__(self, api_key, model="sonar-pro", max_tokens=450, temperature=0.7):
|
||||
def __init__(self, api_key, model="sonar-pro", max_tokens=450, temperature=0.7, logger=None):
|
||||
"""
|
||||
Initialize Perplexity API provider
|
||||
|
||||
|
|
@ -20,12 +21,14 @@ class PerplexityProvider:
|
|||
model (str): Model to use (default: sonar-pro)
|
||||
max_tokens (int): Maximum tokens in response
|
||||
temperature (float): Sampling temperature
|
||||
logger: Optional logger instance for error reporting
|
||||
"""
|
||||
self.api_key = api_key
|
||||
self.model = model
|
||||
self.max_tokens = max_tokens
|
||||
self.temperature = temperature
|
||||
self.base_url = "https://api.perplexity.ai"
|
||||
self.logger = logger or logging.getLogger(__name__)
|
||||
|
||||
# Statistics
|
||||
self.total_requests = 0
|
||||
|
|
@ -85,16 +88,16 @@ class PerplexityProvider:
|
|||
else:
|
||||
self.total_errors += 1
|
||||
error_msg = f"API Error {response.status_code}: {response.text}"
|
||||
print(error_msg)
|
||||
self.logger.error(error_msg)
|
||||
return None
|
||||
|
||||
except httpx.TimeoutException:
|
||||
self.total_errors += 1
|
||||
print("API Timeout: Request took too long")
|
||||
self.logger.error("API Timeout: Request took too long")
|
||||
return None
|
||||
except Exception as e:
|
||||
self.total_errors += 1
|
||||
print(f"API Error: {str(e)}")
|
||||
self.logger.error(f"API Error: {str(e)}")
|
||||
return None
|
||||
|
||||
async def validate_api_key(self):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue