@lobehub/chat 0.72.3 → 0.73.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. package/CHANGELOG.md +50 -0
  2. package/next.config.mjs +6 -2
  3. package/package.json +5 -10
  4. package/src/config/server.ts +2 -0
  5. package/src/const/fetch.ts +15 -2
  6. package/src/const/llm.ts +17 -0
  7. package/src/const/settings.ts +16 -8
  8. package/src/const/url.ts +2 -1
  9. package/src/locales/default/setting.ts +59 -13
  10. package/src/locales/options.ts +1 -1
  11. package/src/pages/api/{openai.ts → createChatCompletion.ts} +7 -36
  12. package/src/pages/api/openai/chat.api.ts +43 -0
  13. package/src/pages/api/openai/createAzureOpenai.ts +26 -0
  14. package/src/pages/api/openai/createOpenai.ts +17 -0
  15. package/src/pages/api/openai/models.api.ts +17 -0
  16. package/src/pages/settings/features/Settings/Common.tsx +4 -28
  17. package/src/pages/settings/features/Settings/LLM/Checker.tsx +85 -0
  18. package/src/pages/settings/features/Settings/LLM/ModelList.tsx +51 -0
  19. package/src/pages/settings/features/Settings/LLM/getModelList.ts +9 -0
  20. package/src/pages/settings/features/Settings/LLM/index.tsx +152 -0
  21. package/src/pages/settings/features/Settings/index.tsx +13 -12
  22. package/src/services/_header.ts +33 -0
  23. package/src/services/_url.ts +17 -0
  24. package/src/services/chatModel.ts +4 -10
  25. package/src/services/modelList.ts +15 -0
  26. package/src/services/plugin.ts +1 -1
  27. package/src/services/pluginMarket.ts +1 -1
  28. package/src/store/global/hooks/index.ts +1 -0
  29. package/src/store/global/hooks/useEffectAfterHydrated.ts +22 -0
  30. package/src/store/global/initialState.ts +4 -0
  31. package/src/store/global/selectors.test.ts +264 -0
  32. package/src/store/global/selectors.ts +4 -5
  33. package/src/store/global/slices/settings.ts +16 -4
  34. package/src/store/global/store.ts +34 -5
  35. package/src/types/settings.ts +45 -8
  36. package/src/utils/fetch.ts +4 -12
  37. package/src/pages/api/openai.api.ts +0 -24
  38. package/src/services/url.ts +0 -8
@@ -0,0 +1,51 @@
1
+ import { ActionIcon } from '@lobehub/ui';
2
+ import { Button, Dropdown, Skeleton } from 'antd';
3
+ import { MenuItemType } from 'antd/lib/menu/hooks/useItems';
4
+ import { RotateCwIcon } from 'lucide-react';
5
+ import { memo } from 'react';
6
+ import { useTranslation } from 'react-i18next';
7
+ import { Flexbox } from 'react-layout-kit';
8
+
9
+ import { getModelList } from './getModelList';
10
+
11
+ interface ModelListProps {
12
+ value?: string[];
13
+ }
14
+ const ModelList = memo<ModelListProps>(({ value }) => {
15
+ const { t } = useTranslation('setting');
16
+
17
+ const isLoading = !value;
18
+
19
+ if (isLoading)
20
+ return (
21
+ <Skeleton active paragraph={false} title={{ style: { marginBottom: 0 }, width: '50%' }} />
22
+ );
23
+
24
+ const isEmpty = value?.length === 0;
25
+
26
+ return isEmpty ? (
27
+ <Button
28
+ onClick={() => {
29
+ getModelList('openAI');
30
+ }}
31
+ >
32
+ {t('llm.OpenAI.models.fetch')}
33
+ </Button>
34
+ ) : (
35
+ <Dropdown
36
+ menu={{
37
+ items: (value || []).map<MenuItemType>((v) => ({
38
+ key: v,
39
+ label: v,
40
+ })),
41
+ }}
42
+ >
43
+ <Flexbox align={'center'} gap={8} horizontal>
44
+ {t('llm.OpenAI.models.count', { count: value.length })}
45
+ <ActionIcon icon={RotateCwIcon} size={'small'} title={t('llm.OpenAI.models.refetch')} />
46
+ </Flexbox>
47
+ </Dropdown>
48
+ );
49
+ });
50
+
51
+ export default ModelList;
@@ -0,0 +1,9 @@
1
+ import { fetchModelList } from '@/services/modelList';
2
+ import { useGlobalStore } from '@/store/global';
3
+
4
+ export const getModelList = async (brand = 'openAI') => {
5
+ const setSettings = useGlobalStore.getState().setSettings;
6
+ const models = await fetchModelList();
7
+
8
+ setSettings({ languageModel: { [brand]: { models } } });
9
+ };
@@ -0,0 +1,152 @@
1
+ import { Form, Markdown } from '@lobehub/ui';
2
+ import { Form as AntForm, AutoComplete, Input, Switch } from 'antd';
3
+ import { createStyles } from 'antd-style';
4
+ import { debounce } from 'lodash-es';
5
+ import { Webhook } from 'lucide-react';
6
+ import Link from 'next/link';
7
+ import { memo } from 'react';
8
+ import { Trans, useTranslation } from 'react-i18next';
9
+ import { Flexbox } from 'react-layout-kit';
10
+
11
+ import { FORM_STYLE } from '@/const/layoutTokens';
12
+ import { globalSelectors, useEffectAfterGlobalHydrated, useGlobalStore } from '@/store/global';
13
+
14
+ import Checker from './Checker';
15
+
16
+ const useStyles = createStyles(({ css, token }) => ({
17
+ markdown: css`
18
+ a {
19
+ font-size: 12px !important;
20
+ }
21
+
22
+ p {
23
+ font-size: 12px !important;
24
+ color: ${token.colorTextDescription} !important;
25
+ }
26
+ `,
27
+ plan: css`
28
+ color: ${token.colorTextDescription};
29
+ `,
30
+ tip: css`
31
+ font-size: 12px;
32
+ color: ${token.colorTextDescription};
33
+ `,
34
+ }));
35
+
36
+ const configKey = 'languageModel';
37
+
38
+ const LLM = memo(() => {
39
+ const { t } = useTranslation('setting');
40
+ const [form] = AntForm.useForm();
41
+ const { styles } = useStyles();
42
+ const [setSettings] = useGlobalStore((s) => [s.setSettings]);
43
+
44
+ useEffectAfterGlobalHydrated((store) => {
45
+ const settings = globalSelectors.currentSettings(store.getState());
46
+
47
+ form.setFieldsValue(settings);
48
+ }, []);
49
+
50
+ const useAzure = useGlobalStore((s) => s.settings.languageModel.openAI.useAzure);
51
+
52
+ const openAI = {
53
+ children: [
54
+ {
55
+ children: (
56
+ <Input.Password
57
+ placeholder={
58
+ useAzure ? t('llm.AzureOpenAI.token.placeholder') : t('llm.OpenAI.token.placeholder')
59
+ }
60
+ />
61
+ ),
62
+ desc: useAzure ? t('llm.AzureOpenAI.token.desc') : t('llm.OpenAI.token.desc'),
63
+ label: useAzure ? t('llm.AzureOpenAI.token.title') : t('llm.OpenAI.token.title'),
64
+ name: [configKey, 'openAI', 'OPENAI_API_KEY'],
65
+ },
66
+ {
67
+ children: (
68
+ <Input
69
+ allowClear
70
+ placeholder={
71
+ useAzure
72
+ ? t('llm.AzureOpenAI.endpoint.placeholder')
73
+ : t('llm.OpenAI.endpoint.placeholder')
74
+ }
75
+ />
76
+ ),
77
+ desc: useAzure ? t('llm.AzureOpenAI.endpoint.desc') : t('llm.OpenAI.endpoint.desc'),
78
+
79
+ label: useAzure ? t('llm.AzureOpenAI.endpoint.title') : t('llm.OpenAI.endpoint.title'),
80
+
81
+ name: [configKey, 'openAI', 'endpoint'],
82
+ },
83
+ {
84
+ children: <Switch />,
85
+ desc: t('llm.OpenAI.useAzure.desc'),
86
+ label: t('llm.OpenAI.useAzure.title'),
87
+ name: [configKey, 'openAI', 'useAzure'],
88
+ valuePropName: 'checked',
89
+ },
90
+ {
91
+ children: (
92
+ <AutoComplete
93
+ options={[
94
+ '2023-08-01-preview',
95
+ '2023-07-01-preview',
96
+ '2023-06-01-preview',
97
+ '2023-05-15',
98
+ '2023-03-15-preview',
99
+ ].map((i) => ({
100
+ label: i,
101
+ value: i,
102
+ }))}
103
+ placeholder={'20XX-XX-XX'}
104
+ />
105
+ ),
106
+ desc: (
107
+ <Markdown className={styles.markdown}>{t('llm.OpenAI.azureApiVersion.desc')}</Markdown>
108
+ ),
109
+ hidden: !useAzure,
110
+ label: t('llm.OpenAI.azureApiVersion.title'),
111
+ name: [configKey, 'openAI', 'azureApiVersion'],
112
+ },
113
+ {
114
+ children: <Checker checkModel={!useAzure} />,
115
+ desc: t('llm.OpenAI.check.desc'),
116
+ label: t('llm.OpenAI.check.title'),
117
+ },
118
+ // {
119
+ // children: useAzure ? <Flexbox>{t('llm.OpenAI.models.notSupport')}</Flexbox> : <ModelList />,
120
+ // desc: useAzure ? t('llm.OpenAI.models.notSupportTip') : t('llm.OpenAI.models.desc'),
121
+ // label: t('llm.OpenAI.models.title'),
122
+ // name: [configKey, 'openAI', 'models'],
123
+ // },
124
+ ],
125
+ icon: Webhook,
126
+ title: t('llm.OpenAI.title'),
127
+ };
128
+
129
+ return (
130
+ <Flexbox align={'center'} gap={24} width={'100%'}>
131
+ <Form
132
+ form={form}
133
+ items={[openAI]}
134
+ onValuesChange={debounce(setSettings, 100)}
135
+ {...FORM_STYLE}
136
+ />
137
+ <Flexbox align={'center'} className={styles.plan}>
138
+ <div>
139
+ <Trans i18nKey="llm.waitingForMore" ns={'setting'}>
140
+ 更多模型正在
141
+ <Link href="https://github.com/lobehub/lobe-chat/issues/151" target="_blank">
142
+ 计划接入
143
+ </Link>
144
+ 中 ,敬请期待 ✨
145
+ </Trans>
146
+ </div>
147
+ </Flexbox>
148
+ </Flexbox>
149
+ );
150
+ });
151
+
152
+ export default LLM;
@@ -2,13 +2,16 @@ import { GridBackground, Icon, Logo, TabsNav } from '@lobehub/ui';
2
2
  import { createStyles } from 'antd-style';
3
3
  import { PackageCheck } from 'lucide-react';
4
4
  import { rgba } from 'polished';
5
- import { memo, useState } from 'react';
5
+ import { memo } from 'react';
6
6
  import { useTranslation } from 'react-i18next';
7
7
  import { Center, Flexbox } from 'react-layout-kit';
8
8
 
9
+ import { useGlobalStore } from '@/store/global';
10
+
9
11
  import pkg from '../../../../../package.json';
10
12
  import Agent from './Agent';
11
13
  import Common from './Common';
14
+ import LLM from './LLM';
12
15
 
13
16
  const useStyles = createStyles(({ css, token, isDarkMode }) => ({
14
17
  background: css`
@@ -55,16 +58,12 @@ const useStyles = createStyles(({ css, token, isDarkMode }) => ({
55
58
  `,
56
59
  }));
57
60
 
58
- enum Tabs {
59
- agent = 'agent',
60
- common = 'common',
61
- }
62
-
63
61
  const Settings = memo(() => {
64
- const [tab, setTab] = useState<Tabs>(Tabs.common);
65
62
  const { styles, theme } = useStyles();
66
63
  const { t } = useTranslation('setting');
67
64
 
65
+ const [tab, setTab] = useGlobalStore((s) => [s.settingsTab, s.switchSettingTabs]);
66
+
68
67
  return (
69
68
  <Center gap={16} width={'100%'}>
70
69
  <Flexbox align={'flex-end'} className={styles.tabs} horizontal justify={'center'}>
@@ -78,10 +77,11 @@ const Settings = memo(() => {
78
77
  <TabsNav
79
78
  activeKey={tab}
80
79
  items={[
81
- { key: Tabs.common, label: t('tab.common') },
82
- { key: Tabs.agent, label: t('tab.agent') },
80
+ { key: 'common', label: t('tab.common') },
81
+ { key: 'llm', label: t('tab.llm') },
82
+ { key: 'agent', label: t('tab.agent') },
83
83
  ]}
84
- onChange={(e) => setTab(e as Tabs)}
84
+ onChange={(e) => setTab(e as any)}
85
85
  />
86
86
  <div className={styles.logo}>
87
87
  <Logo extra={'Chat'} type={'text'} />
@@ -91,8 +91,9 @@ const Settings = memo(() => {
91
91
  <div>{`${pkg.version}`}</div>
92
92
  </Flexbox>
93
93
  </Flexbox>
94
- {tab === Tabs.common && <Common />}
95
- {tab === Tabs.agent && <Agent />}
94
+ {tab === 'common' && <Common />}
95
+ {tab === 'llm' && <LLM />}
96
+ {tab === 'agent' && <Agent />}
96
97
  </Center>
97
98
  );
98
99
  });
@@ -0,0 +1,33 @@
1
+ import {
2
+ AZURE_OPENAI_API_VERSION,
3
+ LOBE_CHAT_ACCESS_CODE,
4
+ OPENAI_API_KEY_HEADER_KEY,
5
+ OPENAI_END_POINT,
6
+ USE_AZURE_OPENAI,
7
+ } from '@/const/fetch';
8
+ import { useGlobalStore } from '@/store/global';
9
+
10
+ // eslint-disable-next-line no-undef
11
+ export const createHeaderWithOpenAI = (header?: HeadersInit): HeadersInit => {
12
+ const openai = useGlobalStore.getState().settings.languageModel.openAI;
13
+
14
+ const apiKey = openai.OPENAI_API_KEY || useGlobalStore.getState().settings.OPENAI_API_KEY || '';
15
+ const endpoint = openai.endpoint || useGlobalStore.getState().settings.endpoint || '';
16
+
17
+ // eslint-disable-next-line no-undef
18
+ const result: HeadersInit = {
19
+ ...header,
20
+ [LOBE_CHAT_ACCESS_CODE]: useGlobalStore.getState().settings.password || '',
21
+ [OPENAI_API_KEY_HEADER_KEY]: apiKey,
22
+ [OPENAI_END_POINT]: endpoint,
23
+ };
24
+
25
+ if (openai.useAzure) {
26
+ Object.assign(result, {
27
+ [AZURE_OPENAI_API_VERSION]: openai.azureApiVersion,
28
+ [USE_AZURE_OPENAI]: '1',
29
+ });
30
+ }
31
+
32
+ return result;
33
+ };
@@ -0,0 +1,17 @@
1
+ const isDev = process.env.NODE_ENV === 'development';
2
+
3
+ const prefix = isDev ? '-dev' : '';
4
+
5
+ export const URLS = {
6
+ plugins: '/api/plugins' + prefix,
7
+ };
8
+
9
+ export const OPENAI_URLS = {
10
+ chat: '/api/openai/chat' + prefix,
11
+ models: '/api/openai/models' + prefix,
12
+ };
13
+
14
+ export const AZURE_OPENAI_URLS = {
15
+ chat: '/api/azure-openai/chat' + prefix,
16
+ models: '/api/azure-openai/models' + prefix,
17
+ };
@@ -1,12 +1,11 @@
1
1
  import { merge } from 'lodash-es';
2
2
 
3
- import { LOBE_CHAT_ACCESS_CODE, OPENAI_API_KEY_HEADER_KEY, OPENAI_END_POINT } from '@/const/fetch';
4
- import { useGlobalStore } from '@/store/global';
5
3
  import { pluginSelectors, usePluginStore } from '@/store/plugin';
6
4
  import { initialLobeAgentConfig } from '@/store/session/initialState';
7
5
  import type { ChatCompletionFunctions, OpenAIStreamPayload } from '@/types/openai';
8
6
 
9
- import { URLS } from './url';
7
+ import { createHeaderWithOpenAI } from './_header';
8
+ import { OPENAI_URLS } from './_url';
10
9
 
11
10
  interface FetchChatModelOptions {
12
11
  signal?: AbortSignal | undefined;
@@ -35,14 +34,9 @@ export const fetchChatModel = (
35
34
 
36
35
  const functions = filterFunctions.length === 0 ? undefined : filterFunctions;
37
36
 
38
- return fetch(URLS.openai, {
37
+ return fetch(OPENAI_URLS.chat, {
39
38
  body: JSON.stringify({ ...payload, functions }),
40
- headers: {
41
- 'Content-Type': 'application/json',
42
- [LOBE_CHAT_ACCESS_CODE]: useGlobalStore.getState().settings.password || '',
43
- [OPENAI_API_KEY_HEADER_KEY]: useGlobalStore.getState().settings.OPENAI_API_KEY || '',
44
- [OPENAI_END_POINT]: useGlobalStore.getState().settings.endpoint || '',
45
- },
39
+ headers: createHeaderWithOpenAI({ 'Content-Type': 'application/json' }),
46
40
  method: 'POST',
47
41
  signal: options?.signal,
48
42
  });
@@ -0,0 +1,15 @@
1
+ import { LanguageModelWhiteList } from '@/const/llm';
2
+
3
+ import { createHeaderWithOpenAI } from './_header';
4
+ import { OPENAI_URLS } from './_url';
5
+
6
+ export const fetchModelList = async (): Promise<string[]> => {
7
+ const res = await fetch(OPENAI_URLS.models, {
8
+ headers: createHeaderWithOpenAI(),
9
+ method: 'POST',
10
+ });
11
+
12
+ const modelList: string[] = await res.json();
13
+
14
+ return LanguageModelWhiteList.filter((i) => modelList.includes(i));
15
+ };
@@ -3,7 +3,7 @@ import { PluginRequestPayload, createHeadersWithPluginSettings } from '@lobehub/
3
3
  import { pluginSelectors, usePluginStore } from '@/store/plugin';
4
4
  import { getMessageError } from '@/utils/fetch';
5
5
 
6
- import { URLS } from './url';
6
+ import { URLS } from './_url';
7
7
 
8
8
  interface FetchChatModelOptions {
9
9
  signal?: AbortSignal | undefined;
@@ -2,7 +2,7 @@ import { getPluginIndexJSON } from '@/const/url';
2
2
  import { useGlobalStore } from '@/store/global';
3
3
 
4
4
  /**
5
- * 请求插件列表
5
+ * fetch Plugin Market List
6
6
  */
7
7
  export const getPluginList = async () => {
8
8
  const url = getPluginIndexJSON(useGlobalStore.getState().settings.language);
@@ -1,3 +1,4 @@
1
+ export * from './useEffectAfterHydrated';
1
2
  export * from './useHydrated';
2
3
  export * from './useOnFinishHydrationGlobal';
3
4
  export * from './useSwitchSideBarOnInit';
@@ -0,0 +1,22 @@
1
+ import { useEffect } from 'react';
2
+
3
+ import { useGlobalStore } from '../store';
4
+
5
+ export const useEffectAfterGlobalHydrated = (
6
+ fn: (store: typeof useGlobalStore) => void,
7
+ deps: any[] = [],
8
+ ) => {
9
+ useEffect(() => {
10
+ const hasRehydrated = useGlobalStore.persist.hasHydrated();
11
+
12
+ if (hasRehydrated) {
13
+ // 等价 useEffect 多次触发
14
+ fn(useGlobalStore);
15
+ } else {
16
+ // 等价于 useEffect 第一次触发
17
+ useGlobalStore.persist.onFinishHydration(() => {
18
+ fn(useGlobalStore);
19
+ });
20
+ }
21
+ }, deps);
22
+ };
@@ -3,6 +3,8 @@ import type { GlobalSettings } from '@/types/settings';
3
3
 
4
4
  export type SidebarTabKey = 'chat' | 'market' | 'settings';
5
5
 
6
+ export type SettingsTabs = 'agent' | 'common' | 'llm';
7
+
6
8
  export interface Guide {
7
9
  // Topic 引导
8
10
  topic?: boolean;
@@ -19,6 +21,7 @@ export interface GlobalState {
19
21
  * 用户设置
20
22
  */
21
23
  settings: GlobalSettings;
24
+ settingsTab: SettingsTabs;
22
25
  sidebarKey: SidebarTabKey;
23
26
  }
24
27
 
@@ -41,5 +44,6 @@ export const initialState: GlobalState = {
41
44
  showSessionPanel: true,
42
45
  },
43
46
  settings: DEFAULT_SETTINGS,
47
+ settingsTab: 'common',
44
48
  sidebarKey: 'chat',
45
49
  };