Perplexity
Search the web with filtering, recency, and domain restrictions using Perplexity Search API.
PerplexitySearch enables an Agent to search the web using the Perplexity Search API and return ranked results with titles, URLs, snippets, and publication dates.
Prerequisites
The following example requires the httpx library and an API key from Perplexity.
1uv pip install -U httpx1export PERPLEXITY_API_KEY=***Examples
Basic search:
1from kern.agent import Agent2from kern.tools.perplexity import PerplexitySearch34agent = Agent(tools=[PerplexitySearch()], markdown=True)5agent.print_response("What are the latest developments in AI?")Search with filters (news from the past week, specific domains):
1agent_filtered = Agent(2 tools=[3 PerplexitySearch(4 max_results=10,5 search_recency_filter="week",6 search_domain_filter=["cnbc.com", "reuters.com", "bloomberg.com"],7 )8 ],9 markdown=True,10)11agent_filtered.print_response("Latest AI industry developments")When to Use
Use PerplexitySearch when you need:
- Current, ranked web search results with publication dates
- Filtering by recency (past day, week, month, or year)
- Restriction to specific domains or languages
- Integration with agents that need up-to-date information
Toolkit Params
| Parameter | Type | Default | Description |
|---|---|---|---|
api_key | Optional[str] | None | Perplexity API key. If not provided, uses PERPLEXITY_API_KEY environment variable. |
max_results | int | 5 | Maximum number of results to return per query. |
max_tokens_per_page | int | 2048 | Maximum content length per result in tokens. |
search_recency_filter | Optional[str] | None | Filter results by recency: day, week, month, or year. |
search_domain_filter | Optional[List[str]] | None | List of domains to restrict search results to. |
search_language_filter | Optional[List[str]] | None | List of ISO language codes to filter results by language. |
show_results | bool | False | Log search results for debugging. |
Toolkit Functions
| Function | Description |
|---|---|
search | Search the web using the Perplexity Search API. Takes a query (str) and optional max_results (int). Returns a JSON array of results with url, title, snippet, and date. |
asearch | Async variant of search. Uses httpx.AsyncClient for non-blocking requests. |