Perplexity ↗ 是一个 AI 驱动的问答引擎。
https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/perplexity-ai向 Perplexity 发送请求时,请确保具备以下条件:
- 你的 AI Gateway Account ID。
- 你的 AI Gateway gateway 名称。
- 有效的 Perplexity API 令牌。
- 要使用的 Perplexity 模型名称。
curl https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/perplexity-ai/chat/completions \
--header 'accept: application/json' \
--header 'content-type: application/json' \
--header 'Authorization: Bearer {perplexity_token}' \
--data '{
"model": "mistral-7b-instruct",
"messages": [
{
"role": "user",
"content": "What is Cloudflare?"
}
]
}'Perplexity 没有自己的 SDK,但与 OpenAI SDK 兼容。你可以使用 OpenAI SDK 通过 AI Gateway 调用 Perplexity,如下所示:
import OpenAI from "openai";
const apiKey = env.PERPLEXITY_API_KEY;
const accountId = "{account_id}";
const gatewayId = "{gateway_id}";
const baseURL = `https://gateway.ai.cloudflare.com/v1/${accountId}/${gatewayId}/perplexity-ai`;
const perplexity = new OpenAI({
apiKey,
baseURL,
});
const model = "mistral-7b-instruct";
const messages = [{ role: "user", content: "What is Cloudflare?" }];
const maxTokens = 20;
const chatCompletion = await perplexity.chat.completions.create({
model,
messages,
max_tokens: maxTokens,
});你也可以通过 REST API 使用 OpenAI API schema 访问 Perplexity 模型。将请求发送至:
https://api.cloudflare.com/client/v4/accounts/{account_id}/ai/v1/chat/completions指定:
{
"model": "perplexity/{model}"
}