sonar

This documentation is valid for the following list of our models: perplexity/sonar

Model Overview

A model built on top of Llama 3.3 70B and optimized for Perplexity search. Fast, cost-effective, everyday search and Q&A. Ideal for simple queries, topic summaries, and fact-checking.

How to Make a Call

Step-by-Step Instructions

1️ Setup You Can’t Skip

▪️ Create an Account: Visit the AI/ML API website and create an account (if you don’t have one yet). ▪️ Generate an API Key: After logging in, navigate to your account dashboard and generate your API key. Ensure that key is enabled on UI.

2️ Copy the code example

At the bottom of this page, you'll find a code example that shows how to structure the request. Choose the code snippet in your preferred programming language and copy it into your development environment.

3️ Modify the code example

▪️ Replace <YOUR_AIMLAPI_KEY> with your actual AI/ML API key from your account. ▪️ Insert your question or request into the content field—this is what the model will respond to.

4️ (Optional) Adjust other optional parameters if needed

Only model and messages are required parameters for this model (and we’ve already filled them in for you in the example), but you can include optional parameters if needed to adjust the model’s behavior. Below, you can find the corresponding API schema, which lists all available parameters along with notes on how to use them.

5️ Run your modified code

Run your modified code in your development environment. Response time depends on various factors, but for simple prompts it rarely exceeds a few seconds.

API Schema

Generate a conversational response using a language model.

post

Creates a chat completion using a language model, allowing interactive conversation by predicting the next response based on the given chat history. This is useful for AI-driven dialogue systems and virtual assistants.

Authorizations
Body
modelundefined · enumRequiredPossible values:
max_tokensnumber · min: 1Optional

The maximum number of tokens that can be generated in the chat completion. This value can be used to control costs for text generated via API.

Default: 512
streambooleanOptional

If set to True, the model response data will be streamed to the client as it is generated using server-sent events.

Default: false
temperaturenumber · max: 2Optional

What sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic. We generally recommend altering this or top_p but not both.

top_pnumber · min: 0.01 · max: 1Optional

An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered. We generally recommend altering this or temperature but not both.

logprobsboolean | nullableOptional

Whether to return log probabilities of the output tokens or not. If True, returns the log probabilities of each output token returned in the content of message.

top_logprobsnumber | nullableOptional

An integer between 0 and 20 specifying the number of most likely tokens to return at each token position, each with an associated log probability. logprobs must be set to True if this parameter is used.

frequency_penaltynumber | nullableOptional

Number between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model's likelihood to repeat the same line verbatim.

presence_penaltynumber | nullableOptional

Positive values penalize new tokens based on whether they appear in the text so far, increasing the model's likelihood to talk about new topics.

seedinteger · min: 1Optional

This feature is in Beta. If specified, our system will make a best effort to sample deterministically, such that repeated requests with the same seed and parameters should return the same result.

response_formatone ofOptional

An object specifying the format that the model must output.

or
or
top_knumberOptional

Only sample from the top K options for each subsequent token. Used to remove "long tail" low probability responses. Recommended for advanced use cases only. You usually only need to use temperature.

search_modestring · enumOptional

Controls the search mode used for the request. When set to 'academic', results will prioritize scholarly sources like peer-reviewed papers and academic journals.

Default: academicPossible values:
search_domain_filterstring[]Optional

A list of domains to limit search results to. Currently limited to 10 domains for Allowlisting and Denylisting. For Denylisting, add a - at the beginning of the domain string.

return_imagesbooleanOptional

Determines whether search results should include images.

Default: false
return_related_questionsbooleanOptional

Determines whether related questions should be returned.

Default: false
search_recency_filterstringOptional

Filters search results based on time (e.g., 'week', 'day').

search_after_date_filterstringOptional

Filters search results to only include content published after this date. Format should be %m/%d/%Y (e.g. 3/1/2025)

search_before_date_filterstringOptional

Filters search results to only include content published before this date. Format should be %m/%d/%Y (e.g. 3/1/2025)

last_updated_after_filterstringOptional

Filters search results to only include content last updated after this date. Format should be %m/%d/%Y (e.g. 3/1/2025)

last_updated_before_filterstringOptional

Filters search results to only include content last updated before this date. Format should be %m/%d/%Y (e.g. 3/1/2025)

Responses
201Success
post
POST /v1/chat/completions HTTP/1.1
Host: api.aimlapi.com
Authorization: Bearer <YOUR_AIMLAPI_KEY>
Content-Type: application/json
Accept: */*
Content-Length: 812

{
  "model": "perplexity/sonar",
  "messages": [
    {
      "role": "user",
      "content": "text",
      "name": "text"
    }
  ],
  "max_tokens": 512,
  "stream": false,
  "stream_options": {
    "include_usage": true
  },
  "temperature": 1,
  "top_p": 1,
  "logprobs": true,
  "top_logprobs": 1,
  "logit_bias": {
    "ANY_ADDITIONAL_PROPERTY": 1
  },
  "frequency_penalty": 1,
  "presence_penalty": 1,
  "seed": 1,
  "response_format": {
    "type": "text"
  },
  "web_search_options": {
    "search_context_size": "low",
    "user_location": {
      "approximate": {
        "city": "text",
        "country": "text",
        "region": "text",
        "timezone": "text"
      },
      "type": "approximate"
    }
  },
  "top_k": 1,
  "search_mode": "academic",
  "search_domain_filter": [
    "text"
  ],
  "return_images": false,
  "return_related_questions": false,
  "search_recency_filter": "text",
  "search_after_date_filter": "text",
  "search_before_date_filter": "text",
  "last_updated_after_filter": "text",
  "last_updated_before_filter": "text"
}
201Success

No content

Code Example

import requests
import json   # for getting a structured output with indentation

response = requests.post(
    "https://api.aimlapi.com/v1/chat/completions",
    headers={
        "Content-Type":"application/json", 

        # Insert your AIML API Key instead of <YOUR_AIMLAPI_KEY>:
        "Authorization":"Bearer <YOUR_AIMLAPI_KEY>",
        "Content-Type":"application/json"
    },
    json={
        "model":"perplexity/sonar",
        "messages":[
            {
                "role":"user",

                # Insert your question for the model here, instead of Hello:
                "content":"Hello"
            }
        ]
    }
)

data = response.json()
print(json.dumps(data, indent=2, ensure_ascii=False))
Response
{
  "id": "541db1f4-d5ef-4e65-9474-a10843fa92ab",
  "object": "chat.completion",
  "choices": [
    {
      "index": 0,
      "finish_reason": "stop",
      "message": {
        "role": "assistant",
        "content": "Hello is a common English salutation or greeting, first recorded in writing in 1826 in the United States. It has since become widely used in spoken and written communication as a way to say \"hi\" or initiate conversation[1]. \n\nAdditionally, \"Hello\" is the title of well-known songs, such as Adele's 2015 hit and Lionel Richie's classic, both of which have contributed to the cultural popularity of the word[2][3]. \n\nIn other contexts, \"Hello\" is a brand name used by companies such as a vegan-friendly personal care products line and the meal kit service HelloFresh, showing its versatile use beyond just a greeting[4][5]."
      },
      "delta": {
        "role": "assistant",
        "content": ""
      }
    }
  ],
  "created": 1753461943,
  "model": "sonar",
  "usage": {
    "prompt_tokens": 10502,
    "completion_tokens": 292,
    "total_tokens": 10794,
    "search_context_size": "low"
  },
  "citations": [
    "https://en.wikipedia.org/wiki/Hello",
    "https://en.wikipedia.org/wiki/Hello_(Adele_song)",
    "https://www.youtube.com/watch?v=mHONNcZbwDY",
    "https://www.hello-products.com",
    "https://www.hellofresh.com"
  ],
  "search_results": [
    {
      "title": "Hello - Wikipedia",
      "url": "https://en.wikipedia.org/wiki/Hello",
      "date": "2002-06-09",
      "last_updated": "2025-07-23"
    },
    {
      "title": "Hello (Adele song) - Wikipedia",
      "url": "https://en.wikipedia.org/wiki/Hello_(Adele_song)",
      "date": "2015-10-22",
      "last_updated": "2025-06-13"
    },
    {
      "title": "Lionel Richie - Hello (Official Music Video) - YouTube",
      "url": "https://www.youtube.com/watch?v=mHONNcZbwDY",
      "date": "2020-11-20",
      "last_updated": "2025-07-07"
    },
    {
      "title": "Hello Products",
      "url": "https://www.hello-products.com",
      "date": "2025-06-04",
      "last_updated": "2025-06-16"
    },
    {
      "title": "HelloFresh® Meal Kits | Get 10 Free Meals + Free Breakfast For Life",
      "url": "https://www.hellofresh.com",
      "date": "2024-09-19",
      "last_updated": "2025-05-13"
    }
  ]
}

Last updated

Was this helpful?