跳转到内容
搜索文档

Perplexity

最后更新 查看 MarkdownAgent 设置

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

Example fetch requestbash
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?"
        }
      ]
    }'

通过 OpenAI SDK 使用 Perplexity 与 JavaScript

Perplexity 没有自己的 SDK,但与 OpenAI SDK 兼容。你可以使用 OpenAI SDK 通过 AI Gateway 调用 Perplexity,如下所示:

JavaScriptjs
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,
});

OpenAI 兼容端点

你也可以通过 REST API 使用 OpenAI API schema 访问 Perplexity 模型。将请求发送至:

https://api.cloudflare.com/client/v4/accounts/{account_id}/ai/v1/chat/completions

指定:


{
"model": "perplexity/{model}"
}

这篇文档对您有帮助吗?