Skip to content

Commit

Permalink
🐛 fix: fix CUSTOM_MODEL don't work with docker deployment
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinxx committed Dec 23, 2023
1 parent c29f116 commit 777e5d4
Show file tree
Hide file tree
Showing 14 changed files with 61 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
ACCESS_CODE=lobe66

# add your custom model name, multi model separate by comma. for example gpt-3.5-1106,gpt-4-1106
# NEXT_PUBLIC_CUSTOM_MODELS=model1,model2,model3
# CUSTOM_MODELS=model1,model2,model3

# ---- only choose one from OpenAI Service and Azure OpenAI Service ---- #

Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ ENV PORT=3210

# General Variables
ENV ACCESS_CODE "lobe66"
ENV NEXT_PUBLIC_CUSTOM_MODELS ""
ENV CUSTOM_MODELS ""

# OpenAI
ENV OPENAI_API_KEY ""
Expand Down
4 changes: 2 additions & 2 deletions docs/Deployment/Environment-Variable.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ LobeChat provides additional configuration options during deployment, which can

- [General Variables](#general-variables)
- [`ACCESS_CODE`](#access_code)
- [`NEXT_PUBLIC_CUSTOM_MODELS`](#next_public_custom_models)
- [`CUSTOM_MODELS`](#custom_models)
- [OpenAI](#openai)
- [`OPENAI_API_KEY`](#openai_api_key)
- [`OPENAI_PROXY_URL`](#openai_proxy_url)
Expand All @@ -31,7 +31,7 @@ LobeChat provides additional configuration options during deployment, which can
- Default: `-`
- Example: `awCTe)re_r74` or `rtrt_ewee3@09!` or `code1,code2,code3`

### `NEXT_PUBLIC_CUSTOM_MODELS`
### `CUSTOM_MODELS`

- Type: Optional
- Description: Used to control the model list. Use `+` to add a model, `-` to hide a model, and `model_name=display_name` to customize the display name of a model, separated by commas.
Expand Down
4 changes: 2 additions & 2 deletions docs/Deployment/Environment-Variable.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ LobeChat 在部署时提供了一些额外的配置项,使用环境变量进

- [通用变量](#通用变量)
- [`ACCESS_CODE`](#access_code)
- [`NEXT_PUBLIC_CUSTOM_MODELS`](#next_public_custom_models)
- [`CUSTOM_MODELS`](#custom_models)
- [OpenAI](#openai)
- [`OPENAI_API_KEY`](#openai_api_key)
- [`OPENAI_PROXY_URL`](#openai_proxy_url)
Expand All @@ -31,7 +31,7 @@ LobeChat 在部署时提供了一些额外的配置项,使用环境变量进
- 默认值:-
- 示例:`awCTe)re_r74` or `rtrt_ewee3@09!`

### `NEXT_PUBLIC_CUSTOM_MODELS`
### `CUSTOM_MODELS`

- 类型:可选
- 描述:用来控制模型列表,使用 `+` 增加一个模型,使用 `-` 来隐藏一个模型,使用 `模型名=展示名` 来自定义模型的展示名,用英文逗号隔开。
Expand Down
15 changes: 15 additions & 0 deletions src/app/api/config/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { getServerConfig } from '@/config/server';
import { GlobalServerConfig } from '@/types/settings';

export const runtime = 'edge';

/**
* get Server config to client
*/
export const GET = async () => {
const { CUSTOM_MODELS } = getServerConfig();

const config: GlobalServerConfig = { customModelName: CUSTOM_MODELS };

return new Response(JSON.stringify(config));
};
5 changes: 5 additions & 0 deletions src/layout/GlobalLayout/StoreHydration.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@ import { useEffectAfterSessionHydrated, useSessionStore } from '@/store/session'
const StoreHydration = memo(() => {
const router = useRouter();

const useFetchGlobalConfig = useGlobalStore((s) => s.useFetchGlobalConfig);

useEffect(() => {
// refs: https://github.com/pmndrs/zustand/blob/main/docs/integrations/persisting-store-data.md#hashydrated
useSessionStore.persist.rehydrate();
useGlobalStore.persist.rehydrate();
}, []);

useFetchGlobalConfig();

const { mobile } = useResponsive();

useEffectAfterSessionHydrated(
Expand All @@ -29,6 +33,7 @@ const StoreHydration = memo(() => {
},
[router],
);

useEffect(() => {
router.prefetch('/chat');
router.prefetch('/market');
Expand Down
2 changes: 1 addition & 1 deletion src/services/__tests__/chat.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { LobeChatPluginManifest } from '@lobehub/chat-plugin-sdk';
import { act } from '@testing-library/react';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { describe, expect, it, vi } from 'vitest';

import { VISION_MODEL_WHITE_LIST } from '@/const/llm';
import { DEFAULT_AGENT_CONFIG } from '@/const/settings';
Expand Down
1 change: 1 addition & 0 deletions src/services/_url.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export const URLS = {
config: '/api/config',
market: '/api/market',
plugins: '/api/plugins',
};
Expand Down
9 changes: 9 additions & 0 deletions src/services/global.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { URLS } from '@/services/_url';
import { GlobalServerConfig } from '@/types/settings';

const VERSION_URL = 'https://registry.npmmirror.com/@lobehub/chat';

class GlobalService {
Expand All @@ -10,6 +13,12 @@ class GlobalService {

return data['dist-tags']?.latest;
};

getGlobalConfig = async (): Promise<GlobalServerConfig> => {
const res = await fetch(URLS.config);

return res.json();
};

Check warning on line 21 in src/services/global.ts

View check run for this annotation

Codecov / codecov/patch

src/services/global.ts#L18-L21

Added lines #L18 - L21 were not covered by tests
}

export const globalService = new GlobalService();
4 changes: 3 additions & 1 deletion src/store/global/initialState.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DEFAULT_SETTINGS } from '@/const/settings';
import type { GlobalSettings } from '@/types/settings';
import type { GlobalServerConfig, GlobalSettings } from '@/types/settings';

export enum SidebarTabKey {
Chat = 'chat',
Expand Down Expand Up @@ -38,6 +38,7 @@ export interface GlobalState {
* @localStorage
*/
preference: GlobalPreference;
serverConfig: GlobalServerConfig;
/**
* @localStorage
* 用户设置
Expand All @@ -58,6 +59,7 @@ export const initialState: GlobalState = {
showSessionPanel: true,
showSystemRole: false,
},
serverConfig: {},
settings: DEFAULT_SETTINGS,
settingsTab: SettingsTabs.Common,
sidebarKey: SidebarTabKey.Chat,
Expand Down
11 changes: 7 additions & 4 deletions src/store/global/selectors/settings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,13 @@ describe('settingsSelectors', () => {
describe('CUSTOM_MODELS', () => {
it('custom deletion, addition, and renaming of models', () => {
const s = {
serverConfig: {
customModelName:
'-all,+llama,+claude-2,-gpt-3.5-turbo,gpt-4-1106-preview=gpt-4-turbo,gpt-4-1106-preview=gpt-4-32k',
},
settings: {
languageModel: {
openAI: {
customModelName:
'-all,+llama,+claude-2,-gpt-3.5-turbo,gpt-4-1106-preview=gpt-4-turbo,gpt-4-1106-preview=gpt-4-32k',
},
openAI: {},
},
},
} as unknown as GlobalStore;
Expand All @@ -78,6 +79,7 @@ describe('settingsSelectors', () => {

it('duplicate naming model', () => {
const s = {
serverConfig: {},
settings: {
languageModel: {
openAI: {
Expand All @@ -94,6 +96,7 @@ describe('settingsSelectors', () => {

it('only add the model', () => {
const s = {
serverConfig: {},
settings: {
languageModel: {
openAI: {
Expand Down
1 change: 1 addition & 0 deletions src/store/global/selectors/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const modelListSelectors = (s: GlobalStore) => {

const modelNames = [
...DEFAULT_OPENAI_MODEL_LIST,
...(s.serverConfig.customModelName || '').split(/[,,]/).filter(Boolean),
...(s.settings.languageModel.openAI.customModelName || '').split(/[,,]/).filter(Boolean),
];

Expand Down
9 changes: 9 additions & 0 deletions src/store/global/slices/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { StateCreator } from 'zustand/vanilla';

import { CURRENT_VERSION } from '@/const/version';
import { globalService } from '@/services/global';
import type { GlobalServerConfig } from '@/types/settings';
import { merge } from '@/utils/merge';
import { setNamespace } from '@/utils/storeDebug';

Expand All @@ -28,6 +29,7 @@ export interface CommonAction {
updateGuideState: (guide: Partial<Guide>) => void;
updatePreference: (preference: Partial<GlobalPreference>, action?: string) => void;
useCheckLatestVersion: () => SWRResponse<string>;
useFetchGlobalConfig: () => SWRResponse;
}

export const createCommonSlice: StateCreator<
Expand Down Expand Up @@ -78,4 +80,11 @@ export const createCommonSlice: StateCreator<
set({ hasNewVersion: true, latestVersion: data }, false, n('checkLatestVersion'));
},
}),
useFetchGlobalConfig: () =>
useSWR<GlobalServerConfig>('fetchGlobalConfig', globalService.getGlobalConfig, {
onSuccess: (data) => {
set({ serverConfig: data });
},
revalidateOnFocus: false,
}),

Check warning on line 89 in src/store/global/slices/common.ts

View check run for this annotation

Codecov / codecov/patch

src/store/global/slices/common.ts#L84-L89

Added lines #L84 - L89 were not covered by tests
});
4 changes: 4 additions & 0 deletions src/types/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,7 @@ export interface GlobalSettings extends GlobalBaseSettings {
}

export type ConfigKeys = keyof GlobalSettings;

export interface GlobalServerConfig {
customModelName?: string;
}

0 comments on commit 777e5d4

Please sign in to comment.