npm create cloudflare@latest
export interface Env {
AI: Ai;
}
export default {
async fetch(request, env): Promise<Response> {
const messages = [
{ role: "system", content: "You are a friendly assistant" },
{
role: "user",
content: "对比Vue和React,并给出未来前端的趋势",
},
];
const response = await env.AI.run("@cf/qwen/qwen1.5-14b-chat-awq", { messages });
return Response.json(response);
},
} satisfies ExportedHandler<Env>;
# account_id 在 左侧 workers ai => 使用 REST API => 获取帐户 ID
# token 在 左侧 workers ai => 使用 REST API => 创建 Workers AI API 令牌
curl https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/ai/run/@cf/qwen/qwen1.5-14b-chat-awq \
-X POST \
-H "Authorization: Bearer $CLOUDFLARE_AUTH_TOKEN" \
-d '{ "messages": [{ "role": "system", "content": "You are a friendly assistant" }, { "role": "user", "content": "对比Vue和React,并给出未来前端的趋势" }]}'
const axios = require('axios');
const account_id = 'xx'
const token = 'xx'
const model_name = '@cf/qwen/qwen1.5-14b-chat-awq'
const options = {
method: 'POST',
url: `https://api.cloudflare.com/client/v4/accounts/${account_id}/ai/run/${model_name}`,
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`
},
data: {
text: '比较一下Vue和React,以及未来web前端趋势'
}
};
axios.request(options).then(function(response) {
console.log(response.data);
}).catch(function(error) {
console.error(error);
});
curl https://gateway.ai.cloudflare.com/v1/${account_id}/ai-test/workers-ai/@cf/qwen/qwen1.5-14b-chat-awq \
-X POST \
-H "Authorization: Bearer ${token}" \
-d '{ "messages": [{ "role": "system", "content": "You are a friendly assistant" }, { "role": "user", "content": "分析在编程领域为什么偏向使用英语而不是其他语言" }]}'