Spaces:
Running
Running
File size: 1,550 Bytes
45a414e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
export default {
async fetch(request, env) {
const corsHeaders = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "POST, OPTIONS",
"Access-Control-Allow-Headers": "Content-Type"
};
if (request.method === "OPTIONS") {
return new Response(null, { headers: corsHeaders });
}
if (request.method !== "POST") {
return new Response("CreateAI server is running!", {
headers: corsHeaders
});
}
try {
const { prompt } = await request.json();
if (!prompt) {
return Response.json(
{ error: "Please enter a prompt." },
{ status: 400, headers: corsHeaders }
);
}
const response = await fetch(
"https://router.huggingface.co/v1/chat/completions",
{
method: "POST",
headers: {
"Authorization": `Bearer ${env.HF_TOKEN}`,
"Content-Type": "application/json"
},
body: JSON.stringify({
model: "Qwen/Qwen2.5-7B-Instruct",
messages: [
{
role: "user",
content: prompt
}
],
max_tokens: 800
})
}
);
const data = await response.json();
return Response.json(data, {
status: response.status,
headers: corsHeaders
});
} catch (error) {
return Response.json(
{ error: error.message },
{ status: 500, headers: corsHeaders }
);
}
}
};
|