-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 💄 style: 拆分独立的 LLM Tab * ✨ feat: 支持 Azure OpenAI 调用 * 🚨 ci: fix types * 🗃️ fix: 补充数据迁移逻辑 * 🚸 style: 优化对用户的表达感知 * 💄 style: fix layout * 🚨 ci: fix circular dependencies * ✅ test: fix test * 🎨 chore: clean storage
- Loading branch information
Showing
35 changed files
with
1,006 additions
and
186 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,20 @@ | ||
export const OPENAI_END_POINT = 'X-OPENAI-END_POINT'; | ||
|
||
export const OPENAI_API_KEY_HEADER_KEY = 'X-OPENAI-API-KEY'; | ||
|
||
export const USE_AZURE_OPENAI = 'X-USE_AZURE_OPENAI'; | ||
|
||
export const AZURE_OPENAI_API_VERSION = 'X-AZURE_OPENAI_API_VERSION'; | ||
|
||
export const LOBE_CHAT_ACCESS_CODE = 'X-LOBE_CHAT_ACCESS_CODE'; | ||
|
||
export const LOBE_PLUGIN_SETTINGS = 'X-LOBE_PLUGIN_SETTINGS'; | ||
export const getOpenAIAuthFromRequest = (req: Request) => { | ||
const apiKey = req.headers.get(OPENAI_API_KEY_HEADER_KEY); | ||
const endpoint = req.headers.get(OPENAI_END_POINT); | ||
const accessCode = req.headers.get(LOBE_CHAT_ACCESS_CODE); | ||
const useAzureStr = req.headers.get(USE_AZURE_OPENAI); | ||
const apiVersion = req.headers.get(AZURE_OPENAI_API_VERSION); | ||
|
||
const useAzure = !!useAzureStr; | ||
|
||
return { accessCode, apiKey, apiVersion, endpoint, useAzure }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/** | ||
* A white list of language models that are allowed to display and be used in the app. | ||
*/ | ||
export const LanguageModelWhiteList = [ | ||
// OpenAI | ||
'gpt-3.5-turbo', | ||
'gpt-3.5-turbo-16k', | ||
'gpt-4', | ||
'gpt-4-32k', | ||
]; | ||
|
||
export const DEFAULT_OPENAI_MODEL_LIST = [ | ||
'gpt-3.5-turbo', | ||
'gpt-3.5-turbo-16k', | ||
'gpt-4', | ||
'gpt-4-32k', | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import OpenAI from 'openai'; | ||
|
||
import { getOpenAIAuthFromRequest } from '@/const/fetch'; | ||
import { ChatErrorType, ErrorType } from '@/types/fetch'; | ||
import { OpenAIStreamPayload } from '@/types/openai'; | ||
|
||
import { checkAuth } from '../auth'; | ||
import { createChatCompletion } from '../createChatCompletion'; | ||
import { createErrorResponse } from '../error'; | ||
import { createAzureOpenai } from './createAzureOpenai'; | ||
import { createOpenai } from './createOpenai'; | ||
|
||
export const runtime = 'edge'; | ||
|
||
export default async function handler(req: Request) { | ||
const payload = (await req.json()) as OpenAIStreamPayload; | ||
|
||
const { apiKey, accessCode, endpoint, useAzure, apiVersion } = getOpenAIAuthFromRequest(req); | ||
|
||
const result = checkAuth({ accessCode, apiKey }); | ||
|
||
if (!result.auth) { | ||
return createErrorResponse(result.error as ErrorType); | ||
} | ||
|
||
let openai: OpenAI; | ||
if (useAzure) { | ||
if (!apiVersion) return createErrorResponse(ChatErrorType.BadRequest); | ||
|
||
// `https://test-001.openai.azure.com/openai/deployments/gpt-35-turbo`, | ||
const url = `${endpoint}/openai/deployments/${payload.model.replace('.', '')}`; | ||
|
||
openai = createAzureOpenai({ | ||
apiVersion, | ||
endpoint: url, | ||
userApiKey: apiKey, | ||
}); | ||
} else { | ||
openai = createOpenai(apiKey, endpoint); | ||
} | ||
|
||
return createChatCompletion({ openai, payload }); | ||
} |
Oops, something went wrong.