From 319aa133398569b54a8a9b5def50341acae55399 Mon Sep 17 00:00:00 2001 From: Qiao Cc Date: Thu, 28 Mar 2024 16:52:44 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81azure=20openai=20(#81?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: qiaocc --- app/api/chat/route.ts | 15 ++++++++++++++- app/utils/env.ts | 7 +++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/app/api/chat/route.ts b/app/api/chat/route.ts index 34937dbd..7b1e3284 100644 --- a/app/api/chat/route.ts +++ b/app/api/chat/route.ts @@ -28,11 +28,24 @@ export async function POST(req: NextRequest) { const apiKey = getApiKey(cookies().get('apiKey')?.value); - const fetchResult = await fetch(`https://${env.CHATGPT_NEXT_API_HOST}/v1/chat/completions`, { + let apiURL = ''; + switch (env.CHATGPT_NEXT_API_PROVIDER) { + case 'openai': + apiURL = `https://${env.CHATGPT_NEXT_API_HOST}/v1/chat/completions`; + break; + case 'azure': + apiURL = `${env.CHATGPT_NEXT_API_AzureAPIURL}/${env.CHATGPT_NEXT_API_AzureAPIURLPath}`; + break; + default: + apiURL = 'unknown'; + } + + const fetchResult = await fetch(apiURL, { method: HttpMethod.POST, headers: { ...HttpHeaderJson, Authorization: `Bearer ${apiKey}`, + 'api-key': apiKey ?? '', }, // 直接透传,组装逻辑完全由前端实现 body: req.body, diff --git a/app/utils/env.ts b/app/utils/env.ts index b3d07dfd..a9dc90fb 100644 --- a/app/utils/env.ts +++ b/app/utils/env.ts @@ -9,4 +9,11 @@ export const env = { /** 配置 API 请求的 host(包含端口) */ CHATGPT_NEXT_API_HOST: process.env.CHATGPT_NEXT_API_HOST ?? 'api.openai.com', + + /** API提供商: openai/azure */ + CHATGPT_NEXT_API_PROVIDER: process.env.CHATGPT_NEXT_API_PROVIDER ?? 'openai', + /** azure api url */ + CHATGPT_NEXT_API_AzureAPIURL: process.env.CHATGPT_NEXT_API_AzureAPIURL, + /** azure api url path */ + CHATGPT_NEXT_API_AzureAPIURLPath: process.env.CHATGPT_NEXT_API_AzureAPIURLPath, };