Perplexity ↗ は、AI を使った回答エンジンです。
https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/perplexity-aiPerplexity へリクエストを送るときは、次を用意します。
- AI Gateway の Account ID。
- AI 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 スキーマを使って Perplexity モデルにもアクセスできます。リクエストの送信先は次のとおりです。
https://api.cloudflare.com/client/v4/accounts/{account_id}/ai/v1/chat/completions次を指定します。
{
"model": "perplexity/{model}"
}