コンテンツにスキップ

OpenAI

OpenAI は、ChatGPTを使って構築する手助けをします。

エンドポイント

https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/openai

URL構造

OpenAIにリクエストを送る際は、現在使用しているURLの https://api.openai.com/v1https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/openai に置き換えてください。

リクエスト
curl https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/openai/chat/completions \
--header 'Authorization: Bearer {openai_token}' \
--header 'Content-Type: application/json' \
--data ' {
"model": "gpt-4o-mini",
"messages": [
{
"role": "user",
"content": "Cloudflareとは何ですか"
}
]
}
'

openai-nodeのようなライブラリを使用している場合は、次のように baseURL をOpenAIのエンドポイントに設定します:

JavaScript
import OpenAI from "openai";
const apiKey = "my api key"; // デフォルトは process.env["OPENAI_API_KEY"]
const accountId = "{account_id}";
const gatewayId = "{gateway_id}";
const baseURL = `https://gateway.ai.cloudflare.com/v1/${accountId}/${gatewayId}/openai`;
const openai = new OpenAI({
apiKey,
baseURL,
});
try {
const model = "gpt-3.5-turbo-0613";
const messages = [{ role: "user", content: "ニューロンとは何ですか?" }];
const maxTokens = 100;
const chatCompletion = await openai.chat.completions.create({
model,
messages,
max_tokens: maxTokens,
});
const response = chatCompletion.choices[0].message;
return new Response(JSON.stringify(response));
} catch (e) {
return new Response(e);
}