Skip to content

Commit fc685bc

Browse files
committed
chore: add Cloudflare AI gateway authentication token
1 parent 05e3057 commit fc685bc

File tree

3 files changed

+9
-1
lines changed

3 files changed

+9
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ You need to create a personal telegram bot and connect it to your worker.
3131
1. First of all you need to create a bot with [BotFather](https://t.me/BotFather).
3232
2. Go to _Cloudflare > Workers & Pages > Your Worker > Settings > Variables > Add Encrypted Environment Variables_
3333
- `AI_GATEWAY`: (optional) set the Cloudflare AI gateway ID.
34+
- `CF_AIG_AUTHORIZATION`: (optional) set the Cloudflare AI gateway authentication token.
3435
- `OPENAI_API_KEY`: (optional) set the OpenAI API KEY. If available GPT will be used, otherwise Llama4.
3536
- `OPENAI_BASE_URL`: (optional) set a custom OpenAI compatible BASE URL. If available it will be used, otherwise the openai default one.
3637
- `TELEGRAM_BOT_API_KEY`: (mandatory) set the API KEY of the telegram bot.

src/env.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ export interface Env {
66
TZ: string;
77
OPENAI_API_KEY: string;
88
OPENAI_BASE_URL: string | undefined;
9+
CF_AIG_AUTHORIZATION: string | undefined;
910
}

src/llm.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,13 @@ export async function llm(messages: MessageParam[], env: Env) : Promise<MessageP
2727
// Try to make a call to the OpenAI API.
2828
try {
2929
// Initialize OpenAI.
30-
const openai = new OpenAI({ apiKey: env.OPENAI_API_KEY, baseURL: env.OPENAI_BASE_URL });
30+
const openai = new OpenAI({
31+
apiKey: env.OPENAI_API_KEY,
32+
baseURL: env.OPENAI_BASE_URL,
33+
defaultHeaders: {
34+
"cf-aig-authorization": `Bearer ${env.CF_AIG_AUTHORIZATION}`,
35+
}
36+
});
3137
// Call the OpenAI API.
3238
const chatCompletion = await openai.chat.completions.create({ model: 'gpt-4o-mini', messages: messages });
3339
const response = chatCompletion.choices[0].message;

0 commit comments

Comments
 (0)