Skip to content

Commit

Permalink
feat: 支持azure openai (#81)
Browse files Browse the repository at this point in the history
Co-authored-by: qiaocc <[email protected]>
  • Loading branch information
qiaocco and qiaocc authored Mar 28, 2024
1 parent bd1432e commit 319aa13
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
15 changes: 14 additions & 1 deletion app/api/chat/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
7 changes: 7 additions & 0 deletions app/utils/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

0 comments on commit 319aa13

Please sign in to comment.