Skip to content

Commit

Permalink
🚧 wip: wip for model select
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinxx committed Jan 31, 2024
1 parent 7b024c3 commit 509b064
Show file tree
Hide file tree
Showing 29 changed files with 259 additions and 162 deletions.
6 changes: 6 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ OPENAI_API_KEY=sk-xxxxxxxxx
# Azure's API version, follows the YYYY-MM-DD format
# AZURE_API_VERSION=2023-08-01-preview

########################################
############ ZhiPu AI Service ##########
########################################

# ZHIPU_API_KEY=xxxxxxxxxxxxxxxxxxx.xxxxxxxxxxxxx

########################################
############ Market Service ############
########################################
Expand Down
2 changes: 1 addition & 1 deletion src/app/api/chat/[provider]/route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getPreferredRegion } from '@/app/api/config';
import { createErrorResponse } from '@/app/api/openai/errorResponse';
import { createErrorResponse } from '@/app/api/errorResponse';
import {
CompletionError,
LobeOpenAI,
Expand Down
3 changes: 2 additions & 1 deletion src/app/api/config/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ export const runtime = 'edge';
* get Server config to client
*/
export const GET = async () => {
const { CUSTOM_MODELS } = getServerConfig();
const { CUSTOM_MODELS, ENABLED_ZHIPU } = getServerConfig();

const config: GlobalServerConfig = {
customModelName: CUSTOM_MODELS,
languageModel: { zhipu: { enabled: ENABLED_ZHIPU } },
};

return new Response(JSON.stringify(config));
Expand Down
34 changes: 22 additions & 12 deletions src/app/chat/(desktop)/features/ChatHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,27 @@ const Left = memo(() => {

const router = useRouter();

const [init, isInbox, title, description, avatar, backgroundColor, model, plugins] =
useSessionStore((s) => [
sessionSelectors.isSomeSessionActive(s),
sessionSelectors.isInboxSession(s),
agentSelectors.currentAgentTitle(s),
agentSelectors.currentAgentDescription(s),
agentSelectors.currentAgentAvatar(s),
agentSelectors.currentAgentBackgroundColor(s),
agentSelectors.currentAgentModel(s),
agentSelectors.currentAgentPlugins(s),
]);
const [
init,
isInbox,
title,
description,
avatar,
backgroundColor,
model,
modelProvider,
plugins,
] = useSessionStore((s) => [
sessionSelectors.isSomeSessionActive(s),
sessionSelectors.isInboxSession(s),
agentSelectors.currentAgentTitle(s),
agentSelectors.currentAgentDescription(s),
agentSelectors.currentAgentAvatar(s),
agentSelectors.currentAgentBackgroundColor(s),
agentSelectors.currentAgentModel(s),
agentSelectors.currentAgentModelProvider(s),
agentSelectors.currentAgentPlugins(s),
]);

const displayTitle = isInbox ? t('inbox.title') : title;
const displayDesc = isInbox ? t('inbox.desc') : description;
Expand Down Expand Up @@ -63,7 +73,7 @@ const Left = memo(() => {
desc={displayDesc}
tag={
<>
<ModelTag name={model} />
<ModelTag name={model} provider={modelProvider} />
{plugins?.length > 0 && <PluginTag plugins={plugins} />}
</>
}
Expand Down
31 changes: 20 additions & 11 deletions src/app/chat/features/ChatHeader/ShareButton/Preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,25 @@ import { FieldType } from './type';

const Preview = memo<FieldType & { title?: string }>(
({ title, withSystemRole, withBackground, withFooter }) => {
const [isInbox, description, avatar, backgroundColor, model, plugins, systemRole] =
useSessionStore((s) => [
sessionSelectors.isInboxSession(s),
agentSelectors.currentAgentDescription(s),
agentSelectors.currentAgentAvatar(s),
agentSelectors.currentAgentBackgroundColor(s),
agentSelectors.currentAgentModel(s),
agentSelectors.currentAgentPlugins(s),
agentSelectors.currentAgentSystemRole(s),
]);
const [
isInbox,
description,
avatar,
backgroundColor,
model,
modelProvider,
plugins,
systemRole,
] = useSessionStore((s) => [
sessionSelectors.isInboxSession(s),
agentSelectors.currentAgentDescription(s),
agentSelectors.currentAgentAvatar(s),
agentSelectors.currentAgentBackgroundColor(s),
agentSelectors.currentAgentModel(s),
agentSelectors.currentAgentModelProvider(s),
agentSelectors.currentAgentPlugins(s),
agentSelectors.currentAgentSystemRole(s),
]);
const { t } = useTranslation('chat');
const { styles } = useStyles(withBackground);

Expand All @@ -42,7 +51,7 @@ const Preview = memo<FieldType & { title?: string }>(
desc={displayDesc}
tag={
<>
<ModelTag name={model} />
<ModelTag name={model} provider={modelProvider} />
{plugins?.length > 0 && <PluginTag plugins={plugins} />}
</>
}
Expand Down
47 changes: 29 additions & 18 deletions src/app/chat/features/SessionListContent/List/Item/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,35 @@ const SessionItem = memo<SessionItemProps>(({ id }) => {
const [active] = useSessionStore((s) => [s.activeId === id]);
const [loading] = useChatStore((s) => [!!s.chatLoadingId && id === s.activeId]);

const [pin, title, description, systemRole, avatar, avatarBackground, updateAt, model, group] =
useSessionStore((s) => {
const session = sessionSelectors.getSessionById(id)(s);
const meta = session.meta;
const systemRole = session.config.systemRole;
const [
pin,
title,
description,
systemRole,
avatar,
avatarBackground,
updateAt,
model,
provider,
group,
] = useSessionStore((s) => {
const session = sessionSelectors.getSessionById(id)(s);
const meta = session.meta;
const systemRole = session.config.systemRole;

return [
sessionHelpers.getSessionPinned(session),
agentSelectors.getTitle(meta),
agentSelectors.getDescription(meta),
systemRole,
agentSelectors.getAvatar(meta),
meta.backgroundColor,
session?.updatedAt,
session.config.model,
session?.group,
];
});
return [
sessionHelpers.getSessionPinned(session),
agentSelectors.getTitle(meta),
agentSelectors.getDescription(meta),
systemRole,
agentSelectors.getAvatar(meta),
meta.backgroundColor,
session?.updatedAt,
session.config.model,
session.config.provider,
session?.group,
];
});

const showModel = model !== defaultModel;

Expand All @@ -63,7 +74,7 @@ const SessionItem = memo<SessionItemProps>(({ id }) => {
() =>
!showModel ? undefined : (
<Flexbox gap={4} horizontal style={{ flexWrap: 'wrap' }}>
{showModel && <ModelTag name={model} />}
{showModel && <ModelTag name={model} provider={provider} />}
</Flexbox>
),
[showModel, model],
Expand Down
15 changes: 5 additions & 10 deletions src/app/settings/llm/Zhipu/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const LLM = memo(() => {
const [form] = AntForm.useForm();

const [enabledZhipu, setSettings] = useGlobalStore((s) => [
modelProviderSelectors.enabledZhipu(s),
modelProviderSelectors.enableZhipu(s),
s.setSettings,
]);

Expand All @@ -28,7 +28,7 @@ const LLM = memo(() => {
form.setFieldsValue(settings);
}, []);

const openAI: ItemGroup = {
const model: ItemGroup = {
children: [
{
children: (
Expand All @@ -51,8 +51,8 @@ const LLM = memo(() => {
defaultActive: enabledZhipu,
extra: (
<Switch
onChange={(e) => {
console.log(e);
onChange={(enabled) => {
setSettings({ languageModel: { zhipu: { enabled } } });
}}
value={enabledZhipu}
/>
Expand All @@ -62,12 +62,7 @@ const LLM = memo(() => {
};

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

Expand Down
5 changes: 0 additions & 5 deletions src/components/ModelProviderIcons/index.ts

This file was deleted.

47 changes: 47 additions & 0 deletions src/components/ModelProviderIcons/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { SiAmazonaws, SiOpenai } from '@icons-pack/react-simple-icons';
import { memo } from 'react';

import { ModelProvider } from '@/libs/agent-runtime';

import { Anthropic } from './Anthropic';
import { ChatGLM } from './ChatGLM';
import { GoogleDeepMind } from './GoogleDeepMind';
import { Mistral } from './Mistral';
import { Tongyi } from './Tongyi';

interface ModelProviderIconProps {
provider?: string;
}

const ModelProviderIcon = memo<ModelProviderIconProps>(({ provider }) => {
switch (provider) {
case ModelProvider.Anthropic: {
return <Anthropic size={12} />;
}
case ModelProvider.Tongyi: {
return <Tongyi size={12} />;
}
case ModelProvider.Mistral: {
return <Mistral size={11} />;
}
case ModelProvider.Bedrock: {
return <SiAmazonaws size={18} />;
}

case 'zhipu':
case ModelProvider.ChatGLM: {
return <ChatGLM size={18} />;
}

case ModelProvider.Google: {
return <GoogleDeepMind size={12} />;
}

default:
case ModelProvider.OpenAI: {
return <SiOpenai size={12} />;
}
}
});

export default ModelProviderIcon;
44 changes: 4 additions & 40 deletions src/components/ModelTag/index.tsx
Original file line number Diff line number Diff line change
@@ -1,50 +1,14 @@
import { SiAmazonaws, SiOpenai } from '@icons-pack/react-simple-icons';
import { Tag } from '@lobehub/ui';
import { memo, useMemo } from 'react';
import { memo } from 'react';

import {
Anthropic,
ChatGLM,
GoogleDeepMind,
Mistral,
Tongyi,
} from '@/components/ModelProviderIcons';
import { ModelProvider } from '@/libs/agent-runtime';
import ModelProviderIcon from '@/components/ModelProviderIcons';

interface ModelTagProps {
name: string;
provider?: ModelProvider;
provider?: string;
}
const ModelTag = memo<ModelTagProps>(({ provider, name }) => {
const icon = useMemo(() => {
switch (provider) {
case ModelProvider.Anthropic: {
return <Anthropic size={12} />;
}
case ModelProvider.Tongyi: {
return <Tongyi size={12} />;
}
case ModelProvider.Mistral: {
return <Mistral size={11} />;
}
case ModelProvider.Bedrock: {
return <SiAmazonaws size={18} />;
}
case ModelProvider.ChatGLM: {
return <ChatGLM size={18} />;
}
case ModelProvider.Google: {
return <GoogleDeepMind size={12} />;
}

default:
case ModelProvider.OpenAI: {
return <SiOpenai size={12} />;
}
}
}, [provider]);

return <Tag icon={icon}>{name}</Tag>;
return <Tag icon={<ModelProviderIcon provider={provider} />}>{name}</Tag>;
});

export default ModelTag;
28 changes: 5 additions & 23 deletions src/config/modelProviders.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,6 @@
interface ChatModelCard {
description?: string;
displayName?: string;
/**
* 是否支持 Function Call
*/
functionCall?: boolean;
id: string;
tokens?: number;
/**
* 是否支持视觉识别
*/
vision?: boolean;
}
import { ModelProviderCard } from '@/types/llm';

interface ModelProvider {
chatModels: ChatModelCard[];
name: string;
}

export const ZhiPuModelCard: ModelProvider = {
export const ZhiPuModelCard: ModelProviderCard = {
chatModels: [
{
description: '最新的 GLM-4 、最大支持 128k 上下文、支持 Function Call 、Retreival',
Expand All @@ -41,10 +23,10 @@ export const ZhiPuModelCard: ModelProvider = {
tokens: 128_000,
},
],
name: 'zhipu',
id: 'zhipu',
};

export const OpenAIModelCard: ModelProvider = {
export const OpenAIModelCard: ModelProviderCard = {
chatModels: [
{
description: 'GPT 3.5 Turbo,适用于各种文本生成和理解任务',
Expand Down Expand Up @@ -86,5 +68,5 @@ export const OpenAIModelCard: ModelProvider = {
vision: true, // 支持视觉任务
},
],
name: 'openai',
id: 'openai',
};
2 changes: 2 additions & 0 deletions src/config/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,7 @@ export const getServerConfig = () => {
: 'https://chat-plugins.lobehub.com',

PLUGIN_SETTINGS: process.env.PLUGIN_SETTINGS,

DEBUG_CHAT_COMPLETION: process.env.DEBUG_CHAT_COMPLETION === '1',
};
};
Loading

0 comments on commit 509b064

Please sign in to comment.