From 54a78b63346fb863530688b892fd7685c542034c Mon Sep 17 00:00:00 2001 From: Arvin Xu Date: Tue, 6 Feb 2024 16:03:38 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:=20support=20Moonshot=20AI=20P?= =?UTF-8?q?rovider=20(#1232)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * ✨ feat: support moonshot * 📝 docs: update documents * 🎨 chore: clean code --- .env.example | 6 ++ Dockerfile | 3 + docs/Deployment/Environment-Variable.md | 10 +++ docs/Deployment/Environment-Variable.zh-CN.md | 10 +++ src/app/api/chat/[provider]/agentRuntime.ts | 14 ++++ src/app/api/config/route.ts | 4 +- src/app/api/errorResponse.ts | 4 +- src/app/settings/llm/Moonshot/index.tsx | 77 +++++++++++++++++ src/app/settings/llm/page.tsx | 2 + src/components/ModelIcon/index.tsx | 2 + src/components/ModelProviderIcon/index.tsx | 6 +- src/components/ModelTag/ModelIcon.tsx | 2 + src/config/modelProviders/index.ts | 3 + src/config/modelProviders/moonshot.ts | 24 ++++++ src/config/server/provider.ts | 10 +++ src/const/settings.ts | 4 + .../Error/APIKeyForm/Moonshot.tsx | 60 ++++++++++++++ .../Conversation/Error/APIKeyForm/index.tsx | 5 ++ src/features/Conversation/Error/index.tsx | 3 +- src/libs/agent-runtime/error.ts | 3 + src/libs/agent-runtime/index.ts | 1 + src/libs/agent-runtime/moonshot/index.ts | 82 +++++++++++++++++++ src/libs/agent-runtime/types/type.ts | 1 + src/locales/default/common.ts | 1 + src/locales/default/error.ts | 7 ++ src/locales/default/setting.ts | 8 ++ src/services/_auth.ts | 4 + .../settings/selectors/modelProvider.ts | 9 ++ src/types/settings/modelProvider.ts | 6 ++ 29 files changed, 367 insertions(+), 4 deletions(-) create mode 100644 src/app/settings/llm/Moonshot/index.tsx create mode 100644 src/config/modelProviders/moonshot.ts create mode 100644 src/features/Conversation/Error/APIKeyForm/Moonshot.tsx create mode 100644 src/libs/agent-runtime/moonshot/index.ts diff --git a/.env.example b/.env.example index a4ed7b5943ef..80c71c6b77e4 100644 --- a/.env.example +++ b/.env.example @@ -39,6 +39,12 @@ OPENAI_API_KEY=sk-xxxxxxxxx #ZHIPU_API_KEY=xxxxxxxxxxxxxxxxxxx.xxxxxxxxxxxxx +######################################## +########## Moonshot AI Service ######### +######################################## + +#MOONSHOT_API_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx + ######################################## ########### Google AI Service ########## ######################################## diff --git a/Dockerfile b/Dockerfile index 6b57aff14492..34ad12d2c921 100644 --- a/Dockerfile +++ b/Dockerfile @@ -75,4 +75,7 @@ ENV GOOGLE_API_KEY "" # Zhipu ENV ZHIPU_API_KEY "" +# Moonshot +ENV MOONSHOT_API_KEY "" + CMD ["node", "server.js"] diff --git a/docs/Deployment/Environment-Variable.md b/docs/Deployment/Environment-Variable.md index f2b616a8a617..658888bfcddb 100644 --- a/docs/Deployment/Environment-Variable.md +++ b/docs/Deployment/Environment-Variable.md @@ -10,6 +10,7 @@ LobeChat provides additional configuration options during deployment, which can - [OpenAI](#openai) - [Azure OpenAI](#azure-openai) - [Zhipu AI](#zhipu-ai) + - [Moonshot AI](#moonshot-ai) - [Google AI](#google-ai) - [AWS Bedrock](#aws-bedrock) - [Plugin Service](#plugin-service) @@ -108,6 +109,15 @@ If you need to use Azure OpenAI to provide model services, you can refer to the - Default Value: - - Example: `4582d332441a313f5c2ed9824d1798ca.rC8EcTAhgbOuAuVT` +### Moonshot AI + +#### `MOONSHOT_API_KEY` + +- Type: Required +- Description: This is the API key you applied for in the Zhipu AI service +- Default Value: - +- Example: `Y2xpdGhpMzNhZXNoYjVtdnZjMWc6bXNrLWIxQlk3aDNPaXpBWnc0V1RaMDhSRmRFVlpZUWY=` + ### Google AI #### `GOOGLE_API_KEY` diff --git a/docs/Deployment/Environment-Variable.zh-CN.md b/docs/Deployment/Environment-Variable.zh-CN.md index 6d7f95ac8c94..c36212c681ad 100644 --- a/docs/Deployment/Environment-Variable.zh-CN.md +++ b/docs/Deployment/Environment-Variable.zh-CN.md @@ -10,6 +10,7 @@ LobeChat 在部署时提供了一些额外的配置项,使用环境变量进 - [OpenAI](#openai) - [Azure OpenAI](#azure-openai) - [智谱 AI](#智谱-ai) + - [Moonshot AI](#moonshot-ai) - [Google AI](#google-ai) - [AWS Bedrock](#aws-bedrock) - [插件服务](#插件服务) @@ -106,6 +107,15 @@ LobeChat 在部署时提供了一些额外的配置项,使用环境变量进 - 默认值:- - 示例:`4582d332441a313f5c2ed9824d1798ca.rC8EcTAhgbOuAuVT` +### Moonshot AI + +#### `MOONSHOT_API_KEY` + +- 类型:必选 +- 描述:这是你在 Moonshot AI 服务中申请的 API 密钥 +- 默认值:- +- 示例:`Y2xpdGhpMzNhZXNoYjVtdnZjMWc6bXNrLWIxQlk3aDNPaXpBWnc0V1RaMDhSRmRFVlpZUWY=` + ### Google AI #### `GOOGLE_API_KEY` diff --git a/src/app/api/chat/[provider]/agentRuntime.ts b/src/app/api/chat/[provider]/agentRuntime.ts index fcf2dbaef2b2..0afa81debb56 100644 --- a/src/app/api/chat/[provider]/agentRuntime.ts +++ b/src/app/api/chat/[provider]/agentRuntime.ts @@ -5,6 +5,7 @@ import { LobeAzureOpenAI, LobeBedrockAI, LobeGoogleAI, + LobeMoonshotAI, LobeOpenAI, LobeRuntimeAI, LobeZhipuAI, @@ -16,6 +17,7 @@ interface AzureOpenAIParams { model: string; useAzure?: boolean; } + class AgentRuntime { private _runtime: LobeRuntimeAI; @@ -57,6 +59,11 @@ class AgentRuntime { break; } + case ModelProvider.Moonshot: { + runtimeModel = this.initMoonshot(payload); + break; + } + case ModelProvider.Bedrock: { runtimeModel = this.initBedrock(payload); } @@ -102,6 +109,13 @@ class AgentRuntime { return LobeZhipuAI.fromAPIKey(apiKey); } + private static initMoonshot(payload: JWTPayload) { + const { MOONSHOT_API_KEY, MOONSHOT_PROXY_URL } = getServerConfig(); + const apiKey = payload?.apiKey || MOONSHOT_API_KEY; + + return new LobeMoonshotAI(apiKey, MOONSHOT_PROXY_URL); + } + private static initGoogle(payload: JWTPayload) { const { GOOGLE_API_KEY } = getServerConfig(); const apiKey = payload?.apiKey || GOOGLE_API_KEY; diff --git a/src/app/api/config/route.ts b/src/app/api/config/route.ts index 5975b27c524d..99e7ade94fb0 100644 --- a/src/app/api/config/route.ts +++ b/src/app/api/config/route.ts @@ -7,13 +7,15 @@ export const runtime = 'edge'; * get Server config to client */ export const GET = async () => { - const { CUSTOM_MODELS, ENABLED_ZHIPU, ENABLED_AWS_BEDROCK, ENABLED_GOOGLE } = getServerConfig(); + const { CUSTOM_MODELS, ENABLED_MOONSHOT, ENABLED_ZHIPU, ENABLED_AWS_BEDROCK, ENABLED_GOOGLE } = + getServerConfig(); const config: GlobalServerConfig = { customModelName: CUSTOM_MODELS, languageModel: { bedrock: { enabled: ENABLED_AWS_BEDROCK }, google: { enabled: ENABLED_GOOGLE }, + moonshot: { enabled: ENABLED_MOONSHOT }, zhipu: { enabled: ENABLED_ZHIPU }, }, }; diff --git a/src/app/api/errorResponse.ts b/src/app/api/errorResponse.ts index c864a080b387..9dde96feb6ab 100644 --- a/src/app/api/errorResponse.ts +++ b/src/app/api/errorResponse.ts @@ -34,8 +34,10 @@ const getStatus = (errorType: ILobeAgentRuntimeErrorType | ErrorType) => { case AgentRuntimeErrorType.GoogleBizError: { return 475; } + case AgentRuntimeErrorType.MoonshotBizError: { + return 476; + } } - return errorType as number; }; diff --git a/src/app/settings/llm/Moonshot/index.tsx b/src/app/settings/llm/Moonshot/index.tsx new file mode 100644 index 000000000000..830fd2a3bd5a --- /dev/null +++ b/src/app/settings/llm/Moonshot/index.tsx @@ -0,0 +1,77 @@ +import { Moonshot } from '@lobehub/icons'; +import { Form, type ItemGroup } from '@lobehub/ui'; +import { Form as AntForm, Input, Switch } from 'antd'; +import { useTheme } from 'antd-style'; +import { debounce } from 'lodash-es'; +import { memo } from 'react'; +import { useTranslation } from 'react-i18next'; +import { Flexbox } from 'react-layout-kit'; + +import { FORM_STYLE } from '@/const/layoutTokens'; +import { ModelProvider } from '@/libs/agent-runtime'; +import { useGlobalStore } from '@/store/global'; +import { modelProviderSelectors } from '@/store/global/selectors'; + +import Checker from '../Checker'; +import { LLMProviderApiTokenKey, LLMProviderConfigKey } from '../const'; +import { useSyncSettings } from '../useSyncSettings'; + +const providerKey = 'moonshot'; + +const MoonshotProvider = memo(() => { + const { t } = useTranslation('setting'); + const [form] = AntForm.useForm(); + const theme = useTheme(); + const [toggleProviderEnabled, setSettings] = useGlobalStore((s) => [ + s.toggleProviderEnabled, + s.setSettings, + ]); + const enabled = useGlobalStore(modelProviderSelectors.enableMoonshot); + + useSyncSettings(form); + + const model: ItemGroup = { + children: [ + { + children: ( + + ), + desc: t('llm.Moonshot.token.desc'), + label: t('llm.Moonshot.token.title'), + name: [LLMProviderConfigKey, providerKey, LLMProviderApiTokenKey], + }, + { + children: , + desc: t('llm.checker.desc'), + label: t('llm.checker.title'), + minWidth: undefined, + }, + ], + defaultActive: enabled, + extra: ( + { + toggleProviderEnabled(providerKey, enabled); + }} + value={enabled} + /> + ), + title: ( + + + + ), + }; + + return ( +
+ ); +}); + +export default MoonshotProvider; diff --git a/src/app/settings/llm/page.tsx b/src/app/settings/llm/page.tsx index 69f5b378074a..4cd41d3e9101 100644 --- a/src/app/settings/llm/page.tsx +++ b/src/app/settings/llm/page.tsx @@ -11,6 +11,7 @@ import { SettingsTabs } from '@/store/global/initialState'; import Bedrock from './Bedrock'; import Google from './Google'; +import Moonshot from './Moonshot'; import OpenAI from './OpenAI'; import Zhipu from './Zhipu'; @@ -23,6 +24,7 @@ export default memo(() => { {/**/} +