@lobehub/chat 0.123.4 → 0.124.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 (31) hide show
  1. package/.env.example +6 -0
  2. package/CHANGELOG.md +25 -0
  3. package/Dockerfile +3 -0
  4. package/docs/Deployment/Environment-Variable.md +10 -0
  5. package/docs/Deployment/Environment-Variable.zh-CN.md +10 -0
  6. package/package.json +1 -1
  7. package/src/app/api/chat/[provider]/agentRuntime.ts +14 -0
  8. package/src/app/api/config/route.ts +3 -1
  9. package/src/app/api/errorResponse.ts +3 -1
  10. package/src/app/settings/llm/Moonshot/index.tsx +77 -0
  11. package/src/app/settings/llm/page.tsx +2 -0
  12. package/src/components/ModelIcon/index.tsx +2 -0
  13. package/src/components/ModelProviderIcon/index.tsx +5 -1
  14. package/src/components/ModelTag/ModelIcon.tsx +2 -0
  15. package/src/config/modelProviders/index.ts +3 -0
  16. package/src/config/modelProviders/moonshot.ts +24 -0
  17. package/src/config/server/provider.ts +10 -0
  18. package/src/const/settings.ts +4 -0
  19. package/src/features/Conversation/Error/APIKeyForm/Moonshot.tsx +60 -0
  20. package/src/features/Conversation/Error/APIKeyForm/index.tsx +5 -0
  21. package/src/features/Conversation/Error/index.tsx +2 -1
  22. package/src/libs/agent-runtime/error.ts +3 -0
  23. package/src/libs/agent-runtime/index.ts +1 -0
  24. package/src/libs/agent-runtime/moonshot/index.ts +82 -0
  25. package/src/libs/agent-runtime/types/type.ts +1 -0
  26. package/src/locales/default/common.ts +1 -0
  27. package/src/locales/default/error.ts +7 -0
  28. package/src/locales/default/setting.ts +8 -0
  29. package/src/services/_auth.ts +4 -0
  30. package/src/store/global/slices/settings/selectors/modelProvider.ts +9 -0
  31. package/src/types/settings/modelProvider.ts +6 -0
package/.env.example CHANGED
@@ -39,6 +39,12 @@ OPENAI_API_KEY=sk-xxxxxxxxx
39
39
 
40
40
  #ZHIPU_API_KEY=xxxxxxxxxxxxxxxxxxx.xxxxxxxxxxxxx
41
41
 
42
+ ########################################
43
+ ########## Moonshot AI Service #########
44
+ ########################################
45
+
46
+ #MOONSHOT_API_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
47
+
42
48
  ########################################
43
49
  ########### Google AI Service ##########
44
50
  ########################################
package/CHANGELOG.md CHANGED
@@ -2,6 +2,31 @@
2
2
 
3
3
  # Changelog
4
4
 
5
+ ## [Version 0.124.0](https://github.com/lobehub/lobe-chat/compare/v0.123.4...v0.124.0)
6
+
7
+ <sup>Released on **2024-02-06**</sup>
8
+
9
+ #### ✨ Features
10
+
11
+ - **misc**: Support Moonshot AI Provider.
12
+
13
+ <br/>
14
+
15
+ <details>
16
+ <summary><kbd>Improvements and Fixes</kbd></summary>
17
+
18
+ #### What's improved
19
+
20
+ - **misc**: Support Moonshot AI Provider, closes [#1232](https://github.com/lobehub/lobe-chat/issues/1232) ([a6de202](https://github.com/lobehub/lobe-chat/commit/a6de202))
21
+
22
+ </details>
23
+
24
+ <div align="right">
25
+
26
+ [![](https://img.shields.io/badge/-BACK_TO_TOP-151515?style=flat-square)](#readme-top)
27
+
28
+ </div>
29
+
5
30
  ### [Version 0.123.4](https://github.com/lobehub/lobe-chat/compare/v0.123.3...v0.123.4)
6
31
 
7
32
  <sup>Released on **2024-02-06**</sup>
package/Dockerfile CHANGED
@@ -75,4 +75,7 @@ ENV GOOGLE_API_KEY ""
75
75
  # Zhipu
76
76
  ENV ZHIPU_API_KEY ""
77
77
 
78
+ # Moonshot
79
+ ENV MOONSHOT_API_KEY ""
80
+
78
81
  CMD ["node", "server.js"]
@@ -10,6 +10,7 @@ LobeChat provides additional configuration options during deployment, which can
10
10
  - [OpenAI](#openai)
11
11
  - [Azure OpenAI](#azure-openai)
12
12
  - [Zhipu AI](#zhipu-ai)
13
+ - [Moonshot AI](#moonshot-ai)
13
14
  - [Google AI](#google-ai)
14
15
  - [AWS Bedrock](#aws-bedrock)
15
16
  - [Plugin Service](#plugin-service)
@@ -108,6 +109,15 @@ If you need to use Azure OpenAI to provide model services, you can refer to the
108
109
  - Default Value: -
109
110
  - Example: `4582d332441a313f5c2ed9824d1798ca.rC8EcTAhgbOuAuVT`
110
111
 
112
+ ### Moonshot AI
113
+
114
+ #### `MOONSHOT_API_KEY`
115
+
116
+ - Type: Required
117
+ - Description: This is the API key you applied for in the Zhipu AI service
118
+ - Default Value: -
119
+ - Example: `Y2xpdGhpMzNhZXNoYjVtdnZjMWc6bXNrLWIxQlk3aDNPaXpBWnc0V1RaMDhSRmRFVlpZUWY=`
120
+
111
121
  ### Google AI
112
122
 
113
123
  #### `GOOGLE_API_KEY`
@@ -10,6 +10,7 @@ LobeChat 在部署时提供了一些额外的配置项,使用环境变量进
10
10
  - [OpenAI](#openai)
11
11
  - [Azure OpenAI](#azure-openai)
12
12
  - [智谱 AI](#智谱-ai)
13
+ - [Moonshot AI](#moonshot-ai)
13
14
  - [Google AI](#google-ai)
14
15
  - [AWS Bedrock](#aws-bedrock)
15
16
  - [插件服务](#插件服务)
@@ -106,6 +107,15 @@ LobeChat 在部署时提供了一些额外的配置项,使用环境变量进
106
107
  - 默认值:-
107
108
  - 示例:`4582d332441a313f5c2ed9824d1798ca.rC8EcTAhgbOuAuVT`
108
109
 
110
+ ### Moonshot AI
111
+
112
+ #### `MOONSHOT_API_KEY`
113
+
114
+ - 类型:必选
115
+ - 描述:这是你在 Moonshot AI 服务中申请的 API 密钥
116
+ - 默认值:-
117
+ - 示例:`Y2xpdGhpMzNhZXNoYjVtdnZjMWc6bXNrLWIxQlk3aDNPaXpBWnc0V1RaMDhSRmRFVlpZUWY=`
118
+
109
119
  ### Google AI
110
120
 
111
121
  #### `GOOGLE_API_KEY`
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lobehub/chat",
3
- "version": "0.123.4",
3
+ "version": "0.124.0",
4
4
  "description": "Lobe Chat - an open-source, high-performance chatbot framework that supports speech synthesis, multimodal, and extensible Function Call plugin system. Supports one-click free deployment of your private ChatGPT/LLM web application.",
5
5
  "keywords": [
6
6
  "framework",
@@ -5,6 +5,7 @@ import {
5
5
  LobeAzureOpenAI,
6
6
  LobeBedrockAI,
7
7
  LobeGoogleAI,
8
+ LobeMoonshotAI,
8
9
  LobeOpenAI,
9
10
  LobeRuntimeAI,
10
11
  LobeZhipuAI,
@@ -16,6 +17,7 @@ interface AzureOpenAIParams {
16
17
  model: string;
17
18
  useAzure?: boolean;
18
19
  }
20
+
19
21
  class AgentRuntime {
20
22
  private _runtime: LobeRuntimeAI;
21
23
 
@@ -57,6 +59,11 @@ class AgentRuntime {
57
59
  break;
58
60
  }
59
61
 
62
+ case ModelProvider.Moonshot: {
63
+ runtimeModel = this.initMoonshot(payload);
64
+ break;
65
+ }
66
+
60
67
  case ModelProvider.Bedrock: {
61
68
  runtimeModel = this.initBedrock(payload);
62
69
  }
@@ -102,6 +109,13 @@ class AgentRuntime {
102
109
  return LobeZhipuAI.fromAPIKey(apiKey);
103
110
  }
104
111
 
112
+ private static initMoonshot(payload: JWTPayload) {
113
+ const { MOONSHOT_API_KEY, MOONSHOT_PROXY_URL } = getServerConfig();
114
+ const apiKey = payload?.apiKey || MOONSHOT_API_KEY;
115
+
116
+ return new LobeMoonshotAI(apiKey, MOONSHOT_PROXY_URL);
117
+ }
118
+
105
119
  private static initGoogle(payload: JWTPayload) {
106
120
  const { GOOGLE_API_KEY } = getServerConfig();
107
121
  const apiKey = payload?.apiKey || GOOGLE_API_KEY;
@@ -7,13 +7,15 @@ export const runtime = 'edge';
7
7
  * get Server config to client
8
8
  */
9
9
  export const GET = async () => {
10
- const { CUSTOM_MODELS, ENABLED_ZHIPU, ENABLED_AWS_BEDROCK, ENABLED_GOOGLE } = getServerConfig();
10
+ const { CUSTOM_MODELS, ENABLED_MOONSHOT, ENABLED_ZHIPU, ENABLED_AWS_BEDROCK, ENABLED_GOOGLE } =
11
+ getServerConfig();
11
12
 
12
13
  const config: GlobalServerConfig = {
13
14
  customModelName: CUSTOM_MODELS,
14
15
  languageModel: {
15
16
  bedrock: { enabled: ENABLED_AWS_BEDROCK },
16
17
  google: { enabled: ENABLED_GOOGLE },
18
+ moonshot: { enabled: ENABLED_MOONSHOT },
17
19
  zhipu: { enabled: ENABLED_ZHIPU },
18
20
  },
19
21
  };
@@ -34,8 +34,10 @@ const getStatus = (errorType: ILobeAgentRuntimeErrorType | ErrorType) => {
34
34
  case AgentRuntimeErrorType.GoogleBizError: {
35
35
  return 475;
36
36
  }
37
+ case AgentRuntimeErrorType.MoonshotBizError: {
38
+ return 476;
39
+ }
37
40
  }
38
-
39
41
  return errorType as number;
40
42
  };
41
43
 
@@ -0,0 +1,77 @@
1
+ import { Moonshot } from '@lobehub/icons';
2
+ import { Form, type ItemGroup } from '@lobehub/ui';
3
+ import { Form as AntForm, Input, Switch } from 'antd';
4
+ import { useTheme } from 'antd-style';
5
+ import { debounce } from 'lodash-es';
6
+ import { memo } from 'react';
7
+ import { useTranslation } from 'react-i18next';
8
+ import { Flexbox } from 'react-layout-kit';
9
+
10
+ import { FORM_STYLE } from '@/const/layoutTokens';
11
+ import { ModelProvider } from '@/libs/agent-runtime';
12
+ import { useGlobalStore } from '@/store/global';
13
+ import { modelProviderSelectors } from '@/store/global/selectors';
14
+
15
+ import Checker from '../Checker';
16
+ import { LLMProviderApiTokenKey, LLMProviderConfigKey } from '../const';
17
+ import { useSyncSettings } from '../useSyncSettings';
18
+
19
+ const providerKey = 'moonshot';
20
+
21
+ const MoonshotProvider = memo(() => {
22
+ const { t } = useTranslation('setting');
23
+ const [form] = AntForm.useForm();
24
+ const theme = useTheme();
25
+ const [toggleProviderEnabled, setSettings] = useGlobalStore((s) => [
26
+ s.toggleProviderEnabled,
27
+ s.setSettings,
28
+ ]);
29
+ const enabled = useGlobalStore(modelProviderSelectors.enableMoonshot);
30
+
31
+ useSyncSettings(form);
32
+
33
+ const model: ItemGroup = {
34
+ children: [
35
+ {
36
+ children: (
37
+ <Input.Password
38
+ autoComplete={'new-password'}
39
+ placeholder={t('llm.Moonshot.token.placeholder')}
40
+ />
41
+ ),
42
+ desc: t('llm.Moonshot.token.desc'),
43
+ label: t('llm.Moonshot.token.title'),
44
+ name: [LLMProviderConfigKey, providerKey, LLMProviderApiTokenKey],
45
+ },
46
+ {
47
+ children: <Checker model={'moonshot-v1-8k'} provider={ModelProvider.Moonshot} />,
48
+ desc: t('llm.checker.desc'),
49
+ label: t('llm.checker.title'),
50
+ minWidth: undefined,
51
+ },
52
+ ],
53
+ defaultActive: enabled,
54
+ extra: (
55
+ <Switch
56
+ onChange={(enabled) => {
57
+ toggleProviderEnabled(providerKey, enabled);
58
+ }}
59
+ value={enabled}
60
+ />
61
+ ),
62
+ title: (
63
+ <Flexbox align={'center'} gap={8} horizontal>
64
+ <Moonshot.Combine
65
+ color={theme.isDarkMode ? theme.colorText : Moonshot.colorPrimary}
66
+ size={24}
67
+ />
68
+ </Flexbox>
69
+ ),
70
+ };
71
+
72
+ return (
73
+ <Form form={form} items={[model]} onValuesChange={debounce(setSettings, 100)} {...FORM_STYLE} />
74
+ );
75
+ });
76
+
77
+ export default MoonshotProvider;
@@ -11,6 +11,7 @@ import { SettingsTabs } from '@/store/global/initialState';
11
11
 
12
12
  import Bedrock from './Bedrock';
13
13
  import Google from './Google';
14
+ import Moonshot from './Moonshot';
14
15
  import OpenAI from './OpenAI';
15
16
  import Zhipu from './Zhipu';
16
17
 
@@ -23,6 +24,7 @@ export default memo(() => {
23
24
  <OpenAI />
24
25
  {/*<AzureOpenAI />*/}
25
26
  <Zhipu />
27
+ <Moonshot />
26
28
  <Google />
27
29
  <Bedrock />
28
30
  <Footer>
@@ -7,6 +7,7 @@ import {
7
7
  Meta,
8
8
  Minimax,
9
9
  Mistral,
10
+ Moonshot,
10
11
  OpenAI,
11
12
  Tongyi,
12
13
  } from '@lobehub/icons';
@@ -29,6 +30,7 @@ const ModelIcon = memo<ModelProviderIconProps>(({ model, size = 12 }) => {
29
30
  if (model.includes('gemini')) return <Gemini.Avatar size={size} />;
30
31
  if (model.includes('qwen')) return <Tongyi.Avatar background={Tongyi.colorPrimary} size={size} />;
31
32
  if (model.includes('minmax')) return <Minimax.Avatar size={size} />;
33
+ if (model.includes('moonshot')) return <Moonshot.Avatar size={size} />;
32
34
  if (model.includes('baichuan'))
33
35
  return <Baichuan.Avatar background={Baichuan.colorPrimary} size={size} />;
34
36
  if (model.includes('mistral')) return <Mistral.Avatar size={size} />;
@@ -1,4 +1,4 @@
1
- import { Azure, Bedrock, Google, OpenAI, Zhipu } from '@lobehub/icons';
1
+ import { Azure, Bedrock, Google, Moonshot, OpenAI, Zhipu } from '@lobehub/icons';
2
2
  import { memo } from 'react';
3
3
  import { Center } from 'react-layout-kit';
4
4
 
@@ -34,6 +34,10 @@ const ModelProviderIcon = memo<ModelProviderIconProps>(({ provider }) => {
34
34
  );
35
35
  }
36
36
 
37
+ case ModelProvider.Moonshot: {
38
+ return <Moonshot size={20} />;
39
+ }
40
+
37
41
  case ModelProvider.OpenAI: {
38
42
  return <OpenAI size={20} />;
39
43
  }
@@ -7,6 +7,7 @@ import {
7
7
  Meta,
8
8
  Minimax,
9
9
  Mistral,
10
+ Moonshot,
10
11
  OpenAI,
11
12
  Tongyi,
12
13
  } from '@lobehub/icons';
@@ -26,6 +27,7 @@ const ModelIcon = memo<ModelIconProps>(({ model, size = 12 }) => {
26
27
  if (model.includes('titan')) return <Aws size={size} />;
27
28
  if (model.includes('llama')) return <Meta size={size} />;
28
29
  if (model.includes('gemini')) return <Gemini size={size} />;
30
+ if (model.includes('moonshot')) return <Moonshot size={size} />;
29
31
  if (model.includes('qwen')) return <Tongyi size={size} />;
30
32
  if (model.includes('minmax')) return <Minimax size={size} />;
31
33
  if (model.includes('baichuan')) return <Baichuan size={size} />;
@@ -2,6 +2,7 @@ import { ChatModelCard } from '@/types/llm';
2
2
 
3
3
  import BedrockProvider from './bedrock';
4
4
  import GoogleProvider from './google';
5
+ import MoonshotProvider from './moonshot';
5
6
  import OpenAIProvider from './openai';
6
7
  import ZhiPuProvider from './zhipu';
7
8
 
@@ -10,9 +11,11 @@ export const LOBE_DEFAULT_MODEL_LIST: ChatModelCard[] = [
10
11
  ZhiPuProvider.chatModels,
11
12
  BedrockProvider.chatModels,
12
13
  GoogleProvider.chatModels,
14
+ MoonshotProvider.chatModels,
13
15
  ].flat();
14
16
 
15
17
  export { default as BedrockProvider } from './bedrock';
16
18
  export { default as GoogleProvider } from './google';
19
+ export { default as MoonshotProvider } from './moonshot';
17
20
  export { default as OpenAIProvider } from './openai';
18
21
  export { default as ZhiPuProvider } from './zhipu';
@@ -0,0 +1,24 @@
1
+ import { ModelProviderCard } from '@/types/llm';
2
+
3
+ const Moonshot: ModelProviderCard = {
4
+ chatModels: [
5
+ {
6
+ displayName: 'Moonshot V1 8K',
7
+ id: 'moonshot-v1-8k',
8
+ tokens: 8192,
9
+ },
10
+ {
11
+ displayName: 'Moonshot V1 32K',
12
+ id: 'moonshot-v1-32k',
13
+ tokens: 32_768,
14
+ },
15
+ {
16
+ displayName: 'Moonshot V1 128K',
17
+ id: 'moonshot-v1-128k',
18
+ tokens: 128_000,
19
+ },
20
+ ],
21
+ id: 'moonshot',
22
+ };
23
+
24
+ export default Moonshot;
@@ -24,6 +24,10 @@ declare global {
24
24
  // Google Provider
25
25
  GOOGLE_API_KEY?: string;
26
26
 
27
+ // Moonshot Provider
28
+ MOONSHOT_API_KEY?: string;
29
+ MOONSHOT_PROXY_URL?: string;
30
+
27
31
  // AWS Credentials
28
32
  AWS_REGION?: string;
29
33
  AWS_ACCESS_KEY_ID?: string;
@@ -44,6 +48,8 @@ export const getProviderConfig = () => {
44
48
 
45
49
  const GOOGLE_API_KEY = process.env.GOOGLE_API_KEY || '';
46
50
 
51
+ const MOONSHOT_API_KEY = process.env.MOONSHOT_API_KEY || '';
52
+
47
53
  // region format: iad1,sfo1
48
54
  let regions: string[] = [];
49
55
  if (process.env.OPENAI_FUNCTION_REGIONS) {
@@ -63,6 +69,10 @@ export const getProviderConfig = () => {
63
69
  ENABLED_GOOGLE: !!GOOGLE_API_KEY,
64
70
  GOOGLE_API_KEY,
65
71
 
72
+ ENABLED_MOONSHOT: !!MOONSHOT_API_KEY,
73
+ MOONSHOT_API_KEY,
74
+ MOONSHOT_PROXY_URL: process.env.MOONSHOT_PROXY_URL,
75
+
66
76
  ENABLED_AWS_BEDROCK: !!AWS_ACCESS_KEY_ID,
67
77
  AWS_REGION: process.env.AWS_REGION,
68
78
  AWS_ACCESS_KEY_ID: AWS_ACCESS_KEY_ID,
@@ -62,6 +62,10 @@ export const DEFAULT_LLM_CONFIG: GlobalLLMConfig = {
62
62
  apiKey: '',
63
63
  enabled: false,
64
64
  },
65
+ moonshot: {
66
+ apiKey: '',
67
+ enabled: false,
68
+ },
65
69
  openAI: {
66
70
  OPENAI_API_KEY: '',
67
71
  models: [],
@@ -0,0 +1,60 @@
1
+ import { Moonshot } from '@lobehub/icons';
2
+ import { Input } from 'antd';
3
+ import { memo } from 'react';
4
+ import { useTranslation } from 'react-i18next';
5
+
6
+ import { ModelProvider } from '@/libs/agent-runtime';
7
+ import { useGlobalStore } from '@/store/global';
8
+ import { modelProviderSelectors } from '@/store/global/selectors';
9
+
10
+ import { FormAction } from '../style';
11
+
12
+ const MoonshotForm = memo(() => {
13
+ const { t } = useTranslation('error');
14
+ // const [showProxy, setShow] = useState(false);
15
+
16
+ const [apiKey, setConfig] = useGlobalStore((s) => [
17
+ modelProviderSelectors.moonshotAPIKey(s),
18
+ s.setModelProviderConfig,
19
+ ]);
20
+
21
+ return (
22
+ <FormAction
23
+ avatar={<Moonshot size={56} />}
24
+ description={t('unlock.apikey.Moonshot.description')}
25
+ title={t('unlock.apikey.Moonshot.title')}
26
+ >
27
+ <Input.Password
28
+ autoComplete={'new-password'}
29
+ onChange={(e) => {
30
+ setConfig(ModelProvider.Moonshot, { apiKey: e.target.value });
31
+ }}
32
+ placeholder={'*********************************'}
33
+ type={'block'}
34
+ value={apiKey}
35
+ />
36
+ {/*{showProxy ? (*/}
37
+ {/* <Input*/}
38
+ {/* onChange={(e) => {*/}
39
+ {/* setConfig({ endpoint: e.target.value });*/}
40
+ {/* }}*/}
41
+ {/* placeholder={'https://api.openai.com/v1'}*/}
42
+ {/* type={'block'}*/}
43
+ {/* value={proxyUrl}*/}
44
+ {/* />*/}
45
+ {/*) : (*/}
46
+ {/* <Button*/}
47
+ {/* icon={<Icon icon={Network} />}*/}
48
+ {/* onClick={() => {*/}
49
+ {/* setShow(true);*/}
50
+ {/* }}*/}
51
+ {/* type={'text'}*/}
52
+ {/* >*/}
53
+ {/* {t('unlock.apikey.addProxyUrl')}*/}
54
+ {/* </Button>*/}
55
+ {/*)}*/}
56
+ </FormAction>
57
+ );
58
+ });
59
+
60
+ export default MoonshotForm;
@@ -8,6 +8,7 @@ import { useChatStore } from '@/store/chat';
8
8
 
9
9
  import BedrockForm from './Bedrock';
10
10
  import GoogleForm from './Google';
11
+ import MoonshotForm from './Moonshot';
11
12
  import OpenAIForm from './OpenAI';
12
13
  import ZhipuForm from './Zhipu';
13
14
 
@@ -35,6 +36,10 @@ const APIKeyForm = memo<APIKeyFormProps>(({ id, provider }) => {
35
36
  return <ZhipuForm />;
36
37
  }
37
38
 
39
+ case ModelProvider.Moonshot: {
40
+ return <MoonshotForm />;
41
+ }
42
+
38
43
  default:
39
44
  case ModelProvider.OpenAI: {
40
45
  return <OpenAIForm />;
@@ -31,7 +31,7 @@ export const getErrorAlertConfig = (
31
31
  };
32
32
  }
33
33
 
34
- case ChatErrorType.InvalidAccessCode: {
34
+ case AgentRuntimeErrorType.NoOpenAIAPIKey: {
35
35
  return {
36
36
  extraDefaultExpand: true,
37
37
  extraIsolate: true,
@@ -64,6 +64,7 @@ const ErrorMessageExtra = memo<{ data: ChatMessage }>(({ data }) => {
64
64
 
65
65
  case AgentRuntimeErrorType.InvalidBedrockCredentials:
66
66
  case AgentRuntimeErrorType.InvalidZhipuAPIKey:
67
+ case AgentRuntimeErrorType.InvalidMoonshotAPIKey:
67
68
  case AgentRuntimeErrorType.InvalidGoogleAPIKey:
68
69
  case AgentRuntimeErrorType.NoOpenAIAPIKey: {
69
70
  return <InvalidAPIKey id={data.id} provider={data.error?.body?.provider} />;
@@ -19,6 +19,9 @@ export const AgentRuntimeErrorType = {
19
19
 
20
20
  InvalidBedrockCredentials: 'InvalidBedrockCredentials',
21
21
  BedrockBizError: 'BedrockBizError',
22
+
23
+ InvalidMoonshotAPIKey: 'InvalidMoonshotAPIKey',
24
+ MoonshotBizError: 'MoonshotBizError',
22
25
  } as const;
23
26
 
24
27
  export type ILobeAgentRuntimeErrorType =
@@ -3,6 +3,7 @@ export * from './BaseAI';
3
3
  export { LobeBedrockAI } from './bedrock';
4
4
  export * from './error';
5
5
  export { LobeGoogleAI } from './google';
6
+ export { LobeMoonshotAI } from './moonshot';
6
7
  export { LobeOpenAI } from './openai';
7
8
  export * from './types';
8
9
  export { AgentRuntimeError } from './utils/createError';
@@ -0,0 +1,82 @@
1
+ import { OpenAIStream, StreamingTextResponse } from 'ai';
2
+ import OpenAI from 'openai';
3
+
4
+ import { LobeRuntimeAI } from '../BaseAI';
5
+ import { AgentRuntimeErrorType } from '../error';
6
+ import { ChatStreamPayload, ModelProvider } from '../types';
7
+ import { AgentRuntimeError } from '../utils/createError';
8
+ import { debugStream } from '../utils/debugStream';
9
+ import { desensitizeUrl } from '../utils/desensitizeUrl';
10
+ import { DEBUG_CHAT_COMPLETION } from '../utils/env';
11
+ import { handleOpenAIError } from '../utils/handleOpenAIError';
12
+
13
+ const DEFAULT_BASE_URL = 'https://api.moonshot.cn/v1';
14
+
15
+ export class LobeMoonshotAI implements LobeRuntimeAI {
16
+ private _llm: OpenAI;
17
+
18
+ baseURL: string;
19
+
20
+ constructor(apiKey?: string, baseURL: string = DEFAULT_BASE_URL) {
21
+ if (!apiKey) throw AgentRuntimeError.createError(AgentRuntimeErrorType.InvalidMoonshotAPIKey);
22
+
23
+ const header = { Authorization: `Bearer ${apiKey}` };
24
+
25
+ this._llm = new OpenAI({ apiKey, baseURL, defaultHeaders: header });
26
+ this.baseURL = this._llm.baseURL;
27
+ }
28
+
29
+ async chat(payload: ChatStreamPayload) {
30
+ try {
31
+ const response = await this._llm.chat.completions.create(
32
+ payload as unknown as OpenAI.ChatCompletionCreateParamsStreaming,
33
+ );
34
+
35
+ const stream = OpenAIStream(response);
36
+
37
+ const [debug, returnStream] = stream.tee();
38
+
39
+ if (DEBUG_CHAT_COMPLETION) {
40
+ debugStream(debug).catch(console.error);
41
+ }
42
+
43
+ return new StreamingTextResponse(returnStream);
44
+ } catch (error) {
45
+ let desensitizedEndpoint = this.baseURL;
46
+
47
+ if (this.baseURL !== DEFAULT_BASE_URL) {
48
+ desensitizedEndpoint = desensitizeUrl(this.baseURL);
49
+ }
50
+
51
+ if ('status' in (error as any)) {
52
+ switch ((error as Response).status) {
53
+ case 401: {
54
+ throw AgentRuntimeError.chat({
55
+ endpoint: desensitizedEndpoint,
56
+ error: error as any,
57
+ errorType: AgentRuntimeErrorType.InvalidMoonshotAPIKey,
58
+ provider: ModelProvider.Moonshot,
59
+ });
60
+ }
61
+
62
+ default: {
63
+ break;
64
+ }
65
+ }
66
+ }
67
+
68
+ const { errorResult, RuntimeError } = handleOpenAIError(error);
69
+
70
+ const errorType = RuntimeError || AgentRuntimeErrorType.MoonshotBizError;
71
+
72
+ throw AgentRuntimeError.chat({
73
+ endpoint: desensitizedEndpoint,
74
+ error: errorResult,
75
+ errorType,
76
+ provider: ModelProvider.Moonshot,
77
+ });
78
+ }
79
+ }
80
+ }
81
+
82
+ export default LobeMoonshotAI;
@@ -28,6 +28,7 @@ export enum ModelProvider {
28
28
  ChatGLM = 'chatglm',
29
29
  Google = 'google',
30
30
  Mistral = 'mistral',
31
+ Moonshot = 'moonshot',
31
32
  OpenAI = 'openai',
32
33
  Tongyi = 'tongyi',
33
34
  ZhiPu = 'zhipu',
@@ -102,6 +102,7 @@ export default {
102
102
  azure: 'Azure',
103
103
  bedrock: 'AWS Bedrock',
104
104
  google: 'Google',
105
+ moonshot: 'Moonshot AI',
105
106
  oneapi: 'One API',
106
107
  openai: 'OpenAI',
107
108
  zhipu: '智谱AI',
@@ -61,6 +61,9 @@ export default {
61
61
  ZhipuBizError: '请求智谱服务出错,请根据以下信息排查或重试',
62
62
  InvalidZhipuAPIKey: 'Zhipu API Key 不正确或为空,请检查 Zhipu API Key 后重试',
63
63
 
64
+ MoonshotBizError: '请求月之暗面服务出错,请根据以下信息排查或重试',
65
+ InvalidMoonshotAPIKey: 'Moonshot AI API Key 不正确或为空,请检查 Moonshot API Key 后重试',
66
+
64
67
  GoogleBizError: '请求 Google 服务出错,请根据以下信息排查或重试',
65
68
  InvalidGoogleAPIKey: 'Google API Key 不正确或为空,请检查 Google API Key 后重试',
66
69
 
@@ -91,6 +94,10 @@ export default {
91
94
  description: '输入你的 Google API Key 即可开始会话。应用不会记录你的 API Key',
92
95
  title: '使用自定义 Google API Key',
93
96
  },
97
+ Moonshot: {
98
+ description: '输入你的 Moonshot AI API Key 即可开始会话。应用不会记录你的 API Key',
99
+ title: '使用自定义 Moonshot AI API Key',
100
+ },
94
101
  OpenAI: {
95
102
  addProxyUrl: '添加 OpenAI 代理地址(可选)',
96
103
  description: '输入你的 OpenAI API Key 即可开始会话。应用不会记录你的 API Key',
@@ -77,6 +77,14 @@ export default {
77
77
  title: 'API Key',
78
78
  },
79
79
  },
80
+ Moonshot: {
81
+ title: '月之暗面',
82
+ token: {
83
+ desc: '填入来自 Moonshot AI 的 API Key',
84
+ placeholder: 'Moonshot AI API Key',
85
+ title: 'API Key',
86
+ },
87
+ },
80
88
  OpenAI: {
81
89
  azureApiVersion: {
82
90
  desc: 'Azure 的 API 版本,遵循 YYYY-MM-DD 格式,查阅[最新版本](https://learn.microsoft.com/zh-cn/azure/ai-services/openai/reference#chat-completions)',
@@ -10,6 +10,10 @@ const getProviderAuthPayload = (provider: string) => {
10
10
  return { apiKey: modelProviderSelectors.zhipuAPIKey(useGlobalStore.getState()) };
11
11
  }
12
12
 
13
+ case ModelProvider.Moonshot: {
14
+ return { apiKey: modelProviderSelectors.moonshotAPIKey(useGlobalStore.getState()) };
15
+ }
16
+
13
17
  case ModelProvider.Google: {
14
18
  return { apiKey: modelProviderSelectors.googleAPIKey(useGlobalStore.getState()) };
15
19
  }
@@ -4,6 +4,7 @@ import {
4
4
  BedrockProvider,
5
5
  GoogleProvider,
6
6
  LOBE_DEFAULT_MODEL_LIST,
7
+ MoonshotProvider,
7
8
  OpenAIProvider,
8
9
  ZhiPuProvider,
9
10
  } from '@/config/modelProviders';
@@ -33,6 +34,9 @@ const googleProxyUrl = (s: GlobalStore) => modelProvider(s).google.endpoint;
33
34
  const enableAzure = (s: GlobalStore) => modelProvider(s).openAI.useAzure;
34
35
  const azureConfig = (s: GlobalStore) => modelProvider(s).azure;
35
36
 
37
+ const enableMoonshot = (s: GlobalStore) => modelProvider(s).moonshot.enabled;
38
+ const moonshotAPIKey = (s: GlobalStore) => modelProvider(s).moonshot.apiKey;
39
+
36
40
  // const azureModelList = (s: GlobalStore): ModelProviderCard => {
37
41
  // const azure = azureConfig(s);
38
42
  // return {
@@ -105,6 +109,7 @@ const modelSelectList = (s: GlobalStore): ModelProviderCard[] => {
105
109
  },
106
110
  // { ...azureModelList(s), enabled: enableAzure(s) },
107
111
  { ...ZhiPuProvider, enabled: enableZhipu(s) },
112
+ { ...MoonshotProvider, enabled: enableMoonshot(s) },
108
113
  { ...GoogleProvider, enabled: enableGoogle(s) },
109
114
  { ...BedrockProvider, enabled: enableBedrock(s) },
110
115
  ];
@@ -165,4 +170,8 @@ export const modelProviderSelectors = {
165
170
  // Bedrock
166
171
  enableBedrock,
167
172
  bedrockConfig,
173
+
174
+ // Moonshot
175
+ enableMoonshot,
176
+ moonshotAPIKey,
168
177
  };
@@ -29,6 +29,11 @@ export interface ZhiPuConfig {
29
29
  endpoint?: string;
30
30
  }
31
31
 
32
+ export interface MoonshotConfig {
33
+ apiKey?: string;
34
+ enabled: boolean;
35
+ }
36
+
32
37
  export interface GoogleConfig {
33
38
  apiKey?: string;
34
39
  enabled: boolean;
@@ -46,6 +51,7 @@ export interface GlobalLLMConfig {
46
51
  azure: AzureOpenAIConfig;
47
52
  bedrock: AWSBedrockConfig;
48
53
  google: GoogleConfig;
54
+ moonshot: MoonshotConfig;
49
55
  openAI: OpenAIConfig;
50
56
  zhipu: ZhiPuConfig;
51
57
  }