import OpenAI from "openai";
const cloudflareToken = "CF_AIG_TOKEN";
const accountId = "{account_id}";
const gatewayId = "{gateway_id}";
const baseURL = `https://gateway.ai.cloudflare.com/v1/${accountId}/${gatewayId}/compat`;
const openai = new OpenAI({
apiKey: cloudflareToken,
baseURL,
});
try {
const model = "dynamic/<your-dynamic-route-name>";
const messages = [{ role: "user", content: "What is a neuron?" }];
const chatCompletion = await openai.chat.completions.create({
model,
messages,
});
const response = chatCompletion.choices[0].message;
console.log(response);
} catch (e) {
console.error(e);
}curl -X POST https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/compat/chat/completions \
--header 'cf-aig-authorization: Bearer {CF_AIG_TOKEN}' \
--header 'Content-Type: application/json' \
--data '{
"model": "dynamic/<your-dynamic-route-name>",
"messages": [
{
"role": "user",
"content": "What is Cloudflare?"
}
]
}'export interface Env {
AI: Ai;
}
export default {
async fetch(request: Request, env: Env) {
const response = await env.AI.gateway("default").run({
provider: "compat",
endpoint: "chat/completions",
headers: {},
query: {
model: "dynamic/<your-dynamic-route-name>",
messages: [
{
role: "user",
content: "What is Cloudflare?",
},
],
},
});
return Response(response);
},
};The response from a dynamic route is the same as the response from a model. There is additional metadata used to notify the model and provider used, you can check the following headers
cf-aig-model- The model usedcf-aig-provider- The slug of provider used