Created
January 11, 2025 05:28
Llama3.3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const API_KEY='123456'; | |
export default { | |
async fetch(request, env) { | |
let url = new URL(request.url); | |
const path = url.pathname; | |
const authHeader = request.headers.get("authorization") || request.headers.get("x-api-key"); | |
const apiKey = authHeader?.startsWith("Bearer ") ? authHeader.slice(7) : null; | |
if (API_KEY && apiKey !== API_KEY) { | |
return new Response(JSON.stringify({ | |
error: { | |
message: "Invalid API key. Use 'Authorization: Bearer your-api-key' header", | |
type: "invalid_request_error", | |
param: null, | |
code: "invalid_api_key" | |
} | |
}), { | |
status: 401, | |
headers: { | |
"Content-Type": "application/json", | |
} | |
}); | |
} | |
if (path === "/v1/chat/completions") { | |
const requestBody = await request.json(); | |
// messages - chat style input | |
let chat = { | |
messages: requestBody | |
}; | |
let response = await env.AI.run('@cf/meta/llama-3.3-70b-instruct-fp8-fast', requestBody); | |
let resdata={ | |
choices:[{"message":{"content":response.response}}] | |
} | |
return Response.json(resdata); | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment