Perplexity AI has launched the Sonar API, designed to provide real-time, accurate, and citation-backed answers for AI applications. Unlike other models that rely solely on pre-trained data, the Sonar API connects to the internet, ensuring responses are based on the latest information. This article highlights its features, benefits, and what’s new for Pro subscribers.
The Sonar API is a versatile solution for handling a wide range of queries, from simple questions to more complex, multi-step research tasks. It offers real-time web connectivity, citations, and customizable search filters, making it suitable for applications where accuracy and factuality are critical.
Also Read: Perplexity AI vs Chatgpt : Which is a Better Option?
from openai import OpenAI
client = OpenAI(api_key="YOUR_API_KEY", base_url="https://api.perplexity.ai")
response = client.chat.completions.create(
model="sonar",
messages=[
{"role": "system", "content": "Be precise and concise."},
{"role": "user", "content": "How many stars are in the Milky Way?"}
]
)
print(response)
Time needed: 1 minute
Getting started with the Perplexity Sonar API is quick and easy. Follow these steps:
Visit the Perplexity API documentation to register and obtain an API key.
Setup your card details to get the API access
curl --location 'https://api.perplexity.ai/chat/completions' \
--header 'accept: application/json' \
--header 'content-type: application/json' \
--header 'Authorization: Bearer {API_KEY}' \
--data '{
"model": "sonar-pro ",
"messages": [
{
"role": "system",
"content": "Be precise and concise."
},
{
"role": "user",
"content": "How many stars are there in our galaxy?"
}
]
}'
The API is conveniently OpenAI client-compatible for easy integration with existing applications.
from openai import OpenAI
YOUR_API_KEY = "INSERT API KEY HERE"
messages = [
{
"role": "system",
"content": (
"You are an artificial intelligence assistant and you need to "
"engage in a helpful, detailed, polite conversation with a user."
),
},
{
"role": "user",
"content": (
"How many stars are in the universe?"
),
},
]
client = OpenAI(api_key=YOUR_API_KEY, base_url="https://api.perplexity.ai")
# chat completion without streaming
response = client.chat.completions.create(
model="sonar-pro",
messages=messages,
)
print(response)
# chat completion with streaming
response_stream = client.chat.completions.create(
model="sonar-pro",
messages=messages,
stream=True,
)
for response in response_stream:
print(response)
Also Read: ChatGPT vs Perplexity vs Google vs Goover: Who Nails your Search Queries?
To make the most of the Perplexity Sonar API, here are some practical tips to help you optimize your integration and avoid common pitfalls:
usage
field in the API response to monitor token consumption.search_domain_filter
search_domain_filter
to restrict searches to trusted or industry-specific domains. For example, limit medical queries to domains like nih.gov
or who.int
.Authorization
header. Use the format: Bearer <API_KEY>
.model
, messages
, and search_domain_filter
to ensure they are correctly formatted and within allowed limits.stream
parameter for real-time applications to receive responses incrementally and improve user experience.response_format
to enable JSON mode for structured outputs, making it easier to parse and integrate responses into your application.For users who need more advanced capabilities, Perplexity offers a Pro subscription for the Sonar API. Here’s what Pro subscribers gain access to:
Feature | Description |
---|---|
Extended Context Window | Handles longer, detailed queries for in-depth research and follow-ups. |
Double the Citations | Provides twice the citations per search for more verifiable answers. |
Advanced Search Filters | Filters searches by specific domains or timeframes (e.g., past month, week). |
Higher Usage Limits | Increased rate limits for more queries per minute and higher overall usage. |
Priority Support | Priority access to customer support for faster issue resolution. |
The Perplexity Sonar API is a reliable tool for building AI applications that require accurate, real-time information. With features like real-time web access, citations, and customizable search filters, it is well-suited for a variety of use cases, from enterprise research to healthcare and content creation. For those needing advanced capabilities, the Pro subscription offers extended context windows, double the citations, and priority support. Start using the Sonar API today to enhance your AI-powered applications.
Stay tuned for more updates on Analytics Vidhya News!