From 7094b3c1ef2eadc04ee2df2a330acd096ee186b8 Mon Sep 17 00:00:00 2001 From: Johnny <> Date: Fri, 13 Oct 2023 21:20:19 +0800 Subject: [PATCH] support cloudflare AI Gateway --- dist/buildinfo.json | 2 +- dist/index.js | 13 +++++++++---- dist/timestamp | 2 +- src/env.js | 8 ++++++++ src/openai.js | 4 ++-- 5 files changed, 21 insertions(+), 8 deletions(-) diff --git a/dist/buildinfo.json b/dist/buildinfo.json index c351a10b..ec71e180 100644 --- a/dist/buildinfo.json +++ b/dist/buildinfo.json @@ -1 +1 @@ -{"sha": "cca7cf8", "timestamp": 1696754346} +{"sha": "3cce4ce", "timestamp": 1697203145} diff --git a/dist/index.js b/dist/index.js index 33691815..08d57847 100644 --- a/dist/index.js +++ b/dist/index.js @@ -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", // 使用流模式 @@ -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 @@ -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; @@ -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}` @@ -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", diff --git a/dist/timestamp b/dist/timestamp index 840e7af6..745108d3 100644 --- a/dist/timestamp +++ b/dist/timestamp @@ -1 +1 @@ -1696754346 +1697203145 diff --git a/src/env.js b/src/env.js index 4ad0cae6..b27e4cd3 100644 --- a/src/env.js +++ b/src/env.js @@ -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, @@ -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; diff --git a/src/openai.js b/src/openai.js index d1ffbc43..cb6ff226 100644 --- a/src/openai.js +++ b/src/openai.js @@ -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}`, @@ -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',