Skip to content

Commit

Permalink
✨ feat: support Moonshot AI Provider (lobehub#1232)
Browse files Browse the repository at this point in the history
* ✨ feat: support moonshot

* 📝 docs: update documents

* 🎨 chore: clean code
  • Loading branch information
arvinxx authored Feb 6, 2024
1 parent 2b7b0ad commit a6de202
Show file tree
Hide file tree
Showing 29 changed files with 367 additions and 4 deletions.
6 changes: 6 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ OPENAI_API_KEY=sk-xxxxxxxxx

#ZHIPU_API_KEY=xxxxxxxxxxxxxxxxxxx.xxxxxxxxxxxxx

########################################
########## Moonshot AI Service #########
########################################

#MOONSHOT_API_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

########################################
########### Google AI Service ##########
########################################
Expand Down
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,7 @@ ENV GOOGLE_API_KEY ""
# Zhipu
ENV ZHIPU_API_KEY ""

# Moonshot
ENV MOONSHOT_API_KEY ""

CMD ["node", "server.js"]
10 changes: 10 additions & 0 deletions docs/Deployment/Environment-Variable.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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`
Expand Down
10 changes: 10 additions & 0 deletions docs/Deployment/Environment-Variable.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
- [插件服务](#插件服务)
Expand Down Expand Up @@ -106,6 +107,15 @@ LobeChat 在部署时提供了一些额外的配置项,使用环境变量进
- 默认值:-
- 示例:`4582d332441a313f5c2ed9824d1798ca.rC8EcTAhgbOuAuVT`

### Moonshot AI

#### `MOONSHOT_API_KEY`

- 类型:必选
- 描述:这是你在 Moonshot AI 服务中申请的 API 密钥
- 默认值:-
- 示例:`Y2xpdGhpMzNhZXNoYjVtdnZjMWc6bXNrLWIxQlk3aDNPaXpBWnc0V1RaMDhSRmRFVlpZUWY=`

### Google AI

#### `GOOGLE_API_KEY`
Expand Down
14 changes: 14 additions & 0 deletions src/app/api/chat/[provider]/agentRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
LobeAzureOpenAI,
LobeBedrockAI,
LobeGoogleAI,
LobeMoonshotAI,
LobeOpenAI,
LobeRuntimeAI,
LobeZhipuAI,
Expand All @@ -16,6 +17,7 @@ interface AzureOpenAIParams {
model: string;
useAzure?: boolean;
}

class AgentRuntime {
private _runtime: LobeRuntimeAI;

Expand Down Expand Up @@ -57,6 +59,11 @@ class AgentRuntime {
break;
}

case ModelProvider.Moonshot: {
runtimeModel = this.initMoonshot(payload);
break;
}

case ModelProvider.Bedrock: {
runtimeModel = this.initBedrock(payload);
}
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion src/app/api/config/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
},
};
Expand Down
4 changes: 3 additions & 1 deletion src/app/api/errorResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ const getStatus = (errorType: ILobeAgentRuntimeErrorType | ErrorType) => {
case AgentRuntimeErrorType.GoogleBizError: {
return 475;
}
case AgentRuntimeErrorType.MoonshotBizError: {
return 476;
}
}

return errorType as number;
};

Expand Down
77 changes: 77 additions & 0 deletions src/app/settings/llm/Moonshot/index.tsx
Original file line number Diff line number Diff line change
@@ -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: (
<Input.Password
autoComplete={'new-password'}
placeholder={t('llm.Moonshot.token.placeholder')}
/>
),
desc: t('llm.Moonshot.token.desc'),
label: t('llm.Moonshot.token.title'),
name: [LLMProviderConfigKey, providerKey, LLMProviderApiTokenKey],
},
{
children: <Checker model={'moonshot-v1-8k'} provider={ModelProvider.Moonshot} />,
desc: t('llm.checker.desc'),
label: t('llm.checker.title'),
minWidth: undefined,
},
],
defaultActive: enabled,
extra: (
<Switch
onChange={(enabled) => {
toggleProviderEnabled(providerKey, enabled);
}}
value={enabled}
/>
),
title: (
<Flexbox align={'center'} gap={8} horizontal>
<Moonshot.Combine
color={theme.isDarkMode ? theme.colorText : Moonshot.colorPrimary}
size={24}
/>
</Flexbox>
),
};

return (
<Form form={form} items={[model]} onValuesChange={debounce(setSettings, 100)} {...FORM_STYLE} />
);
});

export default MoonshotProvider;
2 changes: 2 additions & 0 deletions src/app/settings/llm/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -23,6 +24,7 @@ export default memo(() => {
<OpenAI />
{/*<AzureOpenAI />*/}
<Zhipu />
<Moonshot />
<Google />
<Bedrock />
<Footer>
Expand Down
2 changes: 2 additions & 0 deletions src/components/ModelIcon/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
Meta,
Minimax,
Mistral,
Moonshot,
OpenAI,
Tongyi,
} from '@lobehub/icons';
Expand All @@ -29,6 +30,7 @@ const ModelIcon = memo<ModelProviderIconProps>(({ model, size = 12 }) => {
if (model.includes('gemini')) return <Gemini.Avatar size={size} />;
if (model.includes('qwen')) return <Tongyi.Avatar background={Tongyi.colorPrimary} size={size} />;
if (model.includes('minmax')) return <Minimax.Avatar size={size} />;
if (model.includes('moonshot')) return <Moonshot.Avatar size={size} />;
if (model.includes('baichuan'))
return <Baichuan.Avatar background={Baichuan.colorPrimary} size={size} />;
if (model.includes('mistral')) return <Mistral.Avatar size={size} />;
Expand Down
6 changes: 5 additions & 1 deletion src/components/ModelProviderIcon/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Azure, Bedrock, Google, OpenAI, Zhipu } from '@lobehub/icons';
import { Azure, Bedrock, Google, Moonshot, OpenAI, Zhipu } from '@lobehub/icons';
import { memo } from 'react';
import { Center } from 'react-layout-kit';

Expand Down Expand Up @@ -34,6 +34,10 @@ const ModelProviderIcon = memo<ModelProviderIconProps>(({ provider }) => {
);
}

case ModelProvider.Moonshot: {
return <Moonshot size={20} />;
}

case ModelProvider.OpenAI: {
return <OpenAI size={20} />;
}
Expand Down
2 changes: 2 additions & 0 deletions src/components/ModelTag/ModelIcon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
Meta,
Minimax,
Mistral,
Moonshot,
OpenAI,
Tongyi,
} from '@lobehub/icons';
Expand All @@ -26,6 +27,7 @@ const ModelIcon = memo<ModelIconProps>(({ model, size = 12 }) => {
if (model.includes('titan')) return <Aws size={size} />;
if (model.includes('llama')) return <Meta size={size} />;
if (model.includes('gemini')) return <Gemini size={size} />;
if (model.includes('moonshot')) return <Moonshot size={size} />;
if (model.includes('qwen')) return <Tongyi size={size} />;
if (model.includes('minmax')) return <Minimax size={size} />;
if (model.includes('baichuan')) return <Baichuan size={size} />;
Expand Down
3 changes: 3 additions & 0 deletions src/config/modelProviders/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ChatModelCard } from '@/types/llm';

import BedrockProvider from './bedrock';
import GoogleProvider from './google';
import MoonshotProvider from './moonshot';
import OpenAIProvider from './openai';
import ZhiPuProvider from './zhipu';

Expand All @@ -10,9 +11,11 @@ export const LOBE_DEFAULT_MODEL_LIST: ChatModelCard[] = [
ZhiPuProvider.chatModels,
BedrockProvider.chatModels,
GoogleProvider.chatModels,
MoonshotProvider.chatModels,
].flat();

export { default as BedrockProvider } from './bedrock';
export { default as GoogleProvider } from './google';
export { default as MoonshotProvider } from './moonshot';
export { default as OpenAIProvider } from './openai';
export { default as ZhiPuProvider } from './zhipu';
24 changes: 24 additions & 0 deletions src/config/modelProviders/moonshot.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { ModelProviderCard } from '@/types/llm';

const Moonshot: ModelProviderCard = {
chatModels: [
{
displayName: 'Moonshot V1 8K',
id: 'moonshot-v1-8k',
tokens: 8192,
},
{
displayName: 'Moonshot V1 32K',
id: 'moonshot-v1-32k',
tokens: 32_768,
},
{
displayName: 'Moonshot V1 128K',
id: 'moonshot-v1-128k',
tokens: 128_000,
},
],
id: 'moonshot',
};

export default Moonshot;
10 changes: 10 additions & 0 deletions src/config/server/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ declare global {
// Google Provider
GOOGLE_API_KEY?: string;

// Moonshot Provider
MOONSHOT_API_KEY?: string;
MOONSHOT_PROXY_URL?: string;

// AWS Credentials
AWS_REGION?: string;
AWS_ACCESS_KEY_ID?: string;
Expand All @@ -44,6 +48,8 @@ export const getProviderConfig = () => {

const GOOGLE_API_KEY = process.env.GOOGLE_API_KEY || '';

const MOONSHOT_API_KEY = process.env.MOONSHOT_API_KEY || '';

// region format: iad1,sfo1
let regions: string[] = [];
if (process.env.OPENAI_FUNCTION_REGIONS) {
Expand All @@ -63,6 +69,10 @@ export const getProviderConfig = () => {
ENABLED_GOOGLE: !!GOOGLE_API_KEY,
GOOGLE_API_KEY,

ENABLED_MOONSHOT: !!MOONSHOT_API_KEY,
MOONSHOT_API_KEY,
MOONSHOT_PROXY_URL: process.env.MOONSHOT_PROXY_URL,

ENABLED_AWS_BEDROCK: !!AWS_ACCESS_KEY_ID,
AWS_REGION: process.env.AWS_REGION,
AWS_ACCESS_KEY_ID: AWS_ACCESS_KEY_ID,
Expand Down
4 changes: 4 additions & 0 deletions src/const/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ export const DEFAULT_LLM_CONFIG: GlobalLLMConfig = {
apiKey: '',
enabled: false,
},
moonshot: {
apiKey: '',
enabled: false,
},
openAI: {
OPENAI_API_KEY: '',
models: [],
Expand Down
Loading

0 comments on commit a6de202

Please sign in to comment.