Perplexity

1. 概述

Perplexity AI 是一个由人工智能驱动的对话式搜索引擎,旨在通过自然语言处理技术为用户提供直接、准确的答案。

模型列表:

  • llama-3.1-sonar-small-128k-online

  • llama-3.1-sonar-large-128k-online

  • llama-3.1-sonar-huge-128k-online

说明:

  1. 本 API 与 OpenAI 接口格式兼容。

2. 请求说明

  • 请求方法:POST

  • 请求地址:

    https://gateway.theturbo.ai/v1/chat/completions


3. 请求参数

3.1 Header 参数

参数名称
类型
必填
说明
示例值

Content-Type

string

设置请求头类型,必须为 application/json

application/json

Accept

string

设置响应类型,建议统一为 application/json

application/json

Authorization

string

身份验证所需的 API_KEY,格式 Bearer $YOUR_API_KEY

Bearer $YOUR_API_KEY


3.2 Body 参数 (application/json)

参数名称
类型
必填
说明
示例

model

string

要使用的模型 ID。详见概述列出的可用版本,如 llama-3.1-sonar-small-128k-online

llama-3.1-sonar-small-128k-online

messages

array

聊天消息列表,格式与 OpenAI 兼容。数组中的每个对象包含 role(角色) 与 content(内容)。

[{"role": "user","content": "你好"}]

role

string

消息角色,可选值:systemuserassistant

user

content

string

消息的具体内容。

你好,请给我讲个笑话。

temperature

integer

采样温度,取值 0~2。数值越大,输出越随机;数值越小,输出越集中和确定。

0.7

top_p

integer

另一种调节采样分布的方式,取值 0~1。和 temperature 通常二选一设置。

0.9

n

integer

为每条输入消息生成多少条回复。

1

stream

boolean

是否开启流式输出。设置为 true 时,返回类似 ChatGPT 的流式数据。

false

max_tokens

integer

单次回复可生成的最大 token 数量,受模型上下文长度限制。

1024

presence_penalty

number

-2.0 ~ 2.0。正值会鼓励模型输出更多新话题,负值会降低输出新话题的概率。

0

frequency_penalty

number

-2.0 ~ 2.0。正值会降低模型重复字句的频率,负值会提高重复字句出现的概率。

0

search_recency_filter

string

返回指定时间间隔内的搜索结果。可选值包括:monthweekdayhour

month


4. 请求示例

POST /v1/chat/completions
Content-Type: application/json
Accept: application/json
Authorization: Bearer $YOUR_API_KEY

{
	"model": "llama-3.1-sonar-small-128k-online",
	"messages": [
		{
			"role": "system",
			"content": "Be precise and concise."
		},
		{
			"role": "user",
			"content": "How many stars are there in our galaxy?"
		}
	],
	"max_tokens": 1024,
	"temperature": 0.2,
	"search_recency_filter": "month"
}

5. 响应示例

{
	"id": "3c90c3cc-0d44-4b50-8888-8dd25736052a",
	"model": "llama-3.1-sonar-small-128k-online",
	"object": "chat.completion",
	"created": 1724369245,
	"citations": [
		"https://www.astronomy.com/science/astro-for-kids-how-many-stars-are-there-in-space/",
		"https://www.esa.int/Science_Exploration/Space_Science/Herschel/How_many_stars_are_there_in_the_Universe",
		"https://www.space.com/25959-how-many-stars-are-in-the-milky-way.html",
		"https://www.space.com/26078-how-many-stars-are-there.html",
		"https://en.wikipedia.org/wiki/Milky_Way",
		"https://www.littlepassports.com/blog/space/how-many-stars-are-in-the-universe/?srsltid=AfmBOoqWVymRloolU4KZBI9-LotDIoTnzhKYKCw7vVkaIifhjrEU66_5"
	],
	"choices": [
		{
			"index": 0,
			"finish_reason": "stop",
			"message": {
				"role": "assistant",
				"content": "The number of stars in the Milky Way galaxy is estimated to be between 100 billion and 400 billion stars. The most recent estimates from the Gaia mission suggest that there are approximately 100 to 400 billion stars in the Milky Way, with significant uncertainties remaining due to the difficulty in detecting faint red dwarfs and brown dwarfs."
			},
			"delta": {
				"role": "assistant",
				"content": ""
			}
		}
	],
	"usage": {
		"prompt_tokens": 14,
		"completion_tokens": 70,
		"total_tokens": 84
	}
}

最后更新于