Skip to content

Commit

Permalink
Merge pull request #219 from rockagen/master
Browse files Browse the repository at this point in the history
Support Cloudflare AI Gateway
  • Loading branch information
TBXark authored Oct 13, 2023
2 parents 33f94a3 + 7094b3c commit 3713d4d
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 8 deletions.
2 changes: 1 addition & 1 deletion dist/buildinfo.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"sha": "cca7cf8", "timestamp": 1696754346}
{"sha": "3cce4ce", "timestamp": 1697203145}
13 changes: 9 additions & 4 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ var ENV = {
// 检查更新的分支
UPDATE_BRANCH: "master",
// 当前版本
BUILD_TIMESTAMP: 1696754346,
BUILD_TIMESTAMP: 1697203145,
// 当前版本 commit id
BUILD_VERSION: "cca7cf8",
BUILD_VERSION: "3cce4ce",
I18N: null,
LANGUAGE: "zh-cn",
// 使用流模式
Expand All @@ -58,6 +58,8 @@ var ENV = {
TELEGRAM_API_DOMAIN: "https://api.telegram.org",
// OpenAI API Domain 可替换兼容openai api的其他服务商
OPENAI_API_DOMAIN: "https://api.openai.com",
// OpenAI API BASE `https://api.openai.com/v1`
OPENAI_API_BASE: "",
// Azure API Key
AZURE_API_KEY: null,
// Azure Completions API
Expand Down Expand Up @@ -121,6 +123,9 @@ function initEnv(env, i18n2) {
}
ENV.TELEGRAM_AVAILABLE_TOKENS.push(env.TELEGRAM_TOKEN);
}
if (!ENV.OPENAI_API_BASE) {
ENV.OPENAI_API_BASE = `${ENV.OPENAI_API_DOMAIN}/v1`;
}
}
ENV.I18N = i18n2((ENV.LANGUAGE || "cn").toLowerCase());
ENV.SYSTEM_INIT_MESSAGE = ENV.I18N.env.system_init_message;
Expand Down Expand Up @@ -741,7 +746,7 @@ async function requestCompletionsFromOpenAI(message, history, context, onStream)
const { signal } = controller;
const timeout = 1e3 * 60 * 5;
setTimeout(() => controller.abort(), timeout);
let url = `${ENV.OPENAI_API_DOMAIN}/v1/chat/completions`;
let url = `${ENV.OPENAI_API_BASE}/chat/completions`;
const header = {
"Content-Type": "application/json",
"Authorization": `Bearer ${key}`
Expand Down Expand Up @@ -802,7 +807,7 @@ async function requestImageFromOpenAI(prompt, context) {
n: 1,
size: "512x512"
};
const resp = await fetch(`${ENV.OPENAI_API_DOMAIN}/v1/images/generations`, {
const resp = await fetch(`${ENV.OPENAI_API_BASE}/images/generations`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Expand Down
2 changes: 1 addition & 1 deletion dist/timestamp
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1696754346
1697203145
8 changes: 8 additions & 0 deletions src/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ export const ENV = {
TELEGRAM_API_DOMAIN: 'https://api.telegram.org',
// OpenAI API Domain 可替换兼容openai api的其他服务商
OPENAI_API_DOMAIN: 'https://api.openai.com',
// OpenAI API BASE `https://api.openai.com/v1`
OPENAI_API_BASE: '',

// Azure API Key
AZURE_API_KEY: null,
Expand Down Expand Up @@ -196,6 +198,12 @@ export function initEnv(env, i18n) {
}
ENV.TELEGRAM_AVAILABLE_TOKENS.push(env.TELEGRAM_TOKEN);
}

// AUTO SET VALUES
if (!ENV.OPENAI_API_BASE) {
ENV.OPENAI_API_BASE=`${ENV.OPENAI_API_DOMAIN}/v1`
}

}
ENV.I18N = i18n((ENV.LANGUAGE || 'cn').toLowerCase());
ENV.SYSTEM_INIT_MESSAGE = ENV.I18N.env.system_init_message;
Expand Down
4 changes: 2 additions & 2 deletions src/openai.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export async function requestCompletionsFromOpenAI(message, history, context, on
const timeout = 1000 * 60 * 5;
setTimeout(() => controller.abort(), timeout);

let url = `${ENV.OPENAI_API_DOMAIN}/v1/chat/completions`;
let url = `${ENV.OPENAI_API_BASE}/chat/completions`;
const header = {
'Content-Type': 'application/json',
'Authorization': `Bearer ${key}`,
Expand Down Expand Up @@ -101,7 +101,7 @@ export async function requestImageFromOpenAI(prompt, context) {
n: 1,
size: '512x512',
};
const resp = await fetch(`${ENV.OPENAI_API_DOMAIN}/v1/images/generations`, {
const resp = await fetch(`${ENV.OPENAI_API_BASE}/images/generations`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Expand Down

0 comments on commit 3713d4d

Please sign in to comment.