Hi,
When I use a specific agent I use this moneypatch:
import litellm
Patch unsupported parameters
original_completion = litellm.completion
def patched_completion(*args, **kwargs):
kwargs.pop(‘stop’, None) # Remove problematic parameter
return original_completion(*args, **kwargs)
litellm.completion = patched_completion
Now use LiteLLM normally
perplexity_agent = Agent(
role=‘Researcher’,
llm=‘perplexity/sonar-pro’
)
At the moment I dont use a perplexity agent anymore but have defined it as a tool:
import os
import logging
from typing import Optional, Dict, Any
import requests
from tenacity import retry, wait_exponential, stop_after_attempt, retry_if_exception_type
from crewai.tools import BaseTool
logger = logging.getLogger(name)
class PerplexitySearchTool(BaseTool):
“”"Tool that performs research using Perplexity API with citation tracking.
This tool allows agents to search the web for information on a given topic
and returns results with proper citations.
"""
name: str = "perplexity_search"
description: str = "Performs in-depth research on topics with citation tracking"
api_key: Optional[str] = None
max_results: int = 5
result_count: int = 3
class Config:
extra = "allow"
def __init__(
self,
api_key: Optional[str] = None,
max_results: int = 5,
result_count: int = 3
) -> None:
"""Initialize the Perplexity search tool.
Args:
api_key: Perplexity API key. Defaults to PERPLEXITY_API_KEY env variable.
max_results: Maximum number of results to return.
result_count: Number of results to include in Perplexity API query.
"""
super().__init__()
self.api_key = api_key or os.getenv("PERPLEXITY_API_KEY")
self.max_results = max_results
self.result_count = result_count
if not self.api_key:
logger.warning("PERPLEXITY_API_KEY not found in environment variables")
@retry(
wait=wait_exponential(multiplier=1, min=4, max=60),
stop=stop_after_attempt(3),
retry=retry_if_exception_type(requests.exceptions.RequestException)
)
def _run(self, query: str) -> str:
"""Run research query through Perplexity API and format results with citations.
Args:
query: The search query.
Returns:
str: Research results with citations.
"""
if not self.api_key:
return "Error: Perplexity API key not found in environment variables."
try:
headers = {
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
}
data = {
"query": query,
"max_results": self.result_count
}
logger.info(f"Sending query to Perplexity API: {query}")
response = requests.post(
"https://api.perplexity.ai/search",
headers=headers,
json=data,
timeout=30
)
response.raise_for_status()
results = response.json()
logger.debug(f"Received response from Perplexity API for query: {query}")
# Format the response with citations
formatted_response = f"## Research Results for: {query}\n\n"
# Add results with citations
for i, result in enumerate(results.get("results", [])):
formatted_response += f"### Source {i+1}: {result.get('title', 'Untitled')}\n"
formatted_response += f"URL: {result.get('url', 'No URL')}\n\n"
formatted_response += f"{result.get('snippet', 'No content available')}\n\n"
# Add a summary section
formatted_response += "## Summary\n\n"
formatted_response += results.get("summary", "No summary available")
return formatted_response
except requests.exceptions.RequestException as e:
error_msg = f"Error performing Perplexity search: {str(e)}"
logger.error(error_msg)
# Fallback to a more generic response when the API fails
return f"""Research results for: {query}