Skip to content

Commit

Permalink
✨ feat: 支持自定义 OpenAI 代理地址
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinxx committed Aug 4, 2023
1 parent 5127f1b commit 33a111c
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/const/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export const OPENAI_END_POINT = 'X-OPENAI-END_POINT';

export const OPENAI_API_KEY_HEADER_KEY = 'X-OPENAI-API-KEY';

export const LOBE_CHAT_ACCESS_CODE = 'X-LOBE_CHAT_ACCESS_CODE';
5 changes: 3 additions & 2 deletions src/pages/api/openai.api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { LOBE_CHAT_ACCESS_CODE, OPENAI_API_KEY_HEADER_KEY } from '@/const/fetch';
import { LOBE_CHAT_ACCESS_CODE, OPENAI_API_KEY_HEADER_KEY, OPENAI_END_POINT } from '@/const/fetch';
import { ErrorType } from '@/types/fetch';
import { OpenAIStreamPayload } from '@/types/openai';

Expand All @@ -12,12 +12,13 @@ export default async function handler(req: Request) {
const payload = (await req.json()) as OpenAIStreamPayload;
const apiKey = req.headers.get(OPENAI_API_KEY_HEADER_KEY);
const accessCode = req.headers.get(LOBE_CHAT_ACCESS_CODE);
const endpoint = req.headers.get(OPENAI_END_POINT);

const result = checkAuth({ accessCode, apiKey });

if (!result.auth) {
return createErrorResponse(result.error as ErrorType);
}

return createChatCompletion({ OPENAI_API_KEY: apiKey, payload });
return createChatCompletion({ OPENAI_API_KEY: apiKey, endpoint, payload });
}
11 changes: 8 additions & 3 deletions src/pages/api/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,35 @@ import { OpenAIStreamPayload } from '@/types/openai';
const isDev = process.env.NODE_ENV === 'development';

// 创建 OpenAI 实例
export const createOpenAI = (userApiKey: string | null) => {
export const createOpenAI = (userApiKey: string | null, endpoint?: string | null) => {
const { OPENAI_API_KEY, OPENAI_PROXY_URL } = getServerConfig();

const config = new Configuration({
apiKey: !userApiKey ? OPENAI_API_KEY : userApiKey,
});

return new OpenAIApi(config, isDev && OPENAI_PROXY_URL ? OPENAI_PROXY_URL : undefined);
return new OpenAIApi(
config,
endpoint ? endpoint : isDev && OPENAI_PROXY_URL ? OPENAI_PROXY_URL : undefined,
);
};

interface CreateChatCompletionOptions {
OPENAI_API_KEY: string | null;
callbacks?: (payload: OpenAIStreamPayload) => OpenAIStreamCallbacks;
endpoint?: string | null;
payload: OpenAIStreamPayload;
}

export const createChatCompletion = async ({
payload,
callbacks,
OPENAI_API_KEY,
endpoint,
}: CreateChatCompletionOptions) => {
// ============ 0.创建 OpenAI 实例 ============ //

const openai = createOpenAI(OPENAI_API_KEY);
const openai = createOpenAI(OPENAI_API_KEY, endpoint);

const { messages, plugins: enabledPlugins, ...params } = payload;

Expand Down
3 changes: 2 additions & 1 deletion src/services/chatModel.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { merge } from 'lodash-es';

import { LOBE_CHAT_ACCESS_CODE, OPENAI_API_KEY_HEADER_KEY } from '@/const/fetch';
import { LOBE_CHAT_ACCESS_CODE, OPENAI_API_KEY_HEADER_KEY, OPENAI_END_POINT } from '@/const/fetch';
import { initialLobeAgentConfig } from '@/store/session';
import { useSettings } from '@/store/settings';
import type { OpenAIStreamPayload } from '@/types/openai';
Expand Down Expand Up @@ -33,6 +33,7 @@ export const fetchChatModel = (
'Content-Type': 'application/json',
[LOBE_CHAT_ACCESS_CODE]: useSettings.getState().settings.password || '',
[OPENAI_API_KEY_HEADER_KEY]: useSettings.getState().settings.OPENAI_API_KEY || '',
[OPENAI_END_POINT]: useSettings.getState().settings.endpoint || '',
},
method: 'POST',
signal: options?.signal,
Expand Down

0 comments on commit 33a111c

Please sign in to comment.