Perplexity ↗ 是一个 AI 驱动的答案引擎。
https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/perplexity-ai
在向 Perplexity 发出请求时,确保您具有以下内容:
- 您的 AI 网关账户 ID。
- 您的 AI 网关网关名称。
- 一个有效的 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 网关进行 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,});
You can also use the OpenAI-compatible endpoint (/ai-gateway/chat-completion/
) to access Perplexity models using the OpenAI API schema. To do so, send your requests to:
https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/compat/chat/completions
Specify:
{"model": "perplexity/{model}"}
- @2025 Cloudflare Ubitools
- Cf Repo