What It Does
Instantly searches the web for real-time answers using the Perplexity AI API (Sonar Pro). Just ask anything — “What’s the weather in Tokyo?” or “Latest news on AI?” — and it returns a concise, spoken-friendly answer without symbols or jargon.Suggested Trigger Words
- “search”
- “tell me about”
- “what is”
- “who is”
Key Features
Real-Time Search
Access current information from the web, not just LLM training data
Clean Answers
Automatically removes citations and symbols for natural speech
Fast Response
Optimized for quick answers with max_tokens=150
Sonar Pro Model
Uses Perplexity’s most advanced search model
How It Works
API Requirements
Setup Instructions
- Visit perplexity.ai/settings/api
- Create an API key
- Replace
YOUR_API_KEYinmain.pywith your actual key:
Code Walkthrough
API Configuration
Optimized for quick, spoken-friendly responses:Model Choice:
sonar-pro provides more accurate results. Use sonar for faster/cheaper queries.API Request with Timing
Logs response time for debugging:Response Extraction and Cleaning
Removes citations and cleans up formatting:Delivery
Example Conversations
Advanced Configuration
Adjusting Response Length
Temperature Control
Model Selection
- sonar-pro
- sonar
Best for: Accurate, detailed answersCost: Higher per queryUse when: User needs reliable, in-depth information
Extending This Ability
Follow-up Questions
Add conversation loop to allow multi-turn research sessions
Source Citations
Parse and speak source URLs for fact-checking
Category Detection
Route different query types to different models or parameters
Search History
Store and recall previous searches across sessions
Why Use Perplexity Instead of LLM?
Real-Time Information
Real-Time Information
Perplexity searches the web for current information, while base LLMs only know information up to their training cutoff date.Example: “What’s the current price of Bitcoin?” needs live data.
Factual Accuracy
Factual Accuracy
Perplexity cites and verifies sources, reducing hallucinations for factual queries.Example: “When was the last SpaceX launch?” requires verified data.
News and Events
News and Events
Perfect for queries about recent news, sports scores, stock prices, or weather.Example: “Who won the game last night?” needs current information.
Troubleshooting
Error: 401 Unauthorized
Error: 401 Unauthorized
Error: 429 Rate Limited
Error: 429 Rate Limited
You’ve exceeded your API quota. Check your usage at perplexity.ai/settings/api
Empty or Incomplete Answers
Empty or Incomplete Answers
Try increasing
max_tokens from 150 to 250 for longer responses.Citation Numbers in Speech
Citation Numbers in Speech
The cleaning regex should remove
[1], [2] markers. Check that this line is present: