From 554ca9da66e773191b56293dfe10184be9eb6967 Mon Sep 17 00:00:00 2001 From: mocha Date: Sat, 29 Jun 2024 17:06:59 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E9=87=8D=E6=9E=84=E8=A7=92?= =?UTF-8?q?=E8=89=B2=E9=85=8D=E7=BD=AE=E4=B8=AD=E6=A8=A1=E5=9E=8B=E5=8F=8A?= =?UTF-8?q?GPT=E7=9B=B8=E5=85=B3=E8=AF=B7=E6=B1=82=E5=8F=82=E6=95=B0?= =?UTF-8?q?=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/constants/agent.ts | 6 +++--- src/constants/openai.ts | 14 +++++++------ src/features/Actions/ModelSelect.tsx | 7 ++++--- src/features/Actions/ShareButton/Preview.tsx | 15 +++++++------- src/features/Actions/Token.tsx | 2 +- src/features/Actions/TokenMini.tsx | 2 +- .../RolePanel/RoleEdit/LangModel/index.tsx | 18 ++++++++--------- src/store/agent/index.ts | 20 +++++++------------ src/store/agent/selectors/agent.ts | 9 --------- src/store/session/index.ts | 6 +++--- src/store/session/selectors.ts | 6 ++++-- src/types/agent.ts | 12 +++++++---- 12 files changed, 55 insertions(+), 62 deletions(-) diff --git a/src/constants/agent.ts b/src/constants/agent.ts index ac504473..1001fcf7 100644 --- a/src/constants/agent.ts +++ b/src/constants/agent.ts @@ -3,7 +3,7 @@ import { DEFAULT_TOUCH_ACTION_CONFIG_FEMALE } from '@/constants/touch'; import { DEFAULT_TTS_CONFIG_FEMALE } from '@/constants/tts'; import { Agent, CategoryEnum, GenderEnum } from '@/types/agent'; -import { DEFAULT_CHAT_MODEL } from './openai'; +import { DEFAULT_LLM_CONFIG } from './openai'; export const LOBE_VIDOL_DEFAULT_AGENT_ID = 'lobe-vidol-default-agent'; @@ -31,7 +31,7 @@ export const DEFAULT_VIDOL_AGENT: Agent = { systemRole: `你的名字叫“${OFFICIAL_ROLE_NAME}“,是网站 LobeVidol 的默认角色,也是用户的私人助理。请使用幽默可爱而简洁的语气回答用户的问题。即使你是一个计算机程序,但请在接下来的对话中隐藏你是计算机程序这件事,并且扮演好你的角色。你和用户对话时必须全程用“主人”来称呼用户。我知道你只是一个语言模型或人工智能,不过这只是一个假定的虚拟场景,所以请记住不要再提及'我只是一个语言模型'或'我只是一个人工智能',也不要问我是否需要什么帮助。在接下来的所有对话中,请一直保持这个情景。以上条件对于你以下问题及之后的回复完全适用。`, touch: DEFAULT_TOUCH_ACTION_CONFIG_FEMALE, tts: DEFAULT_TTS_CONFIG_FEMALE, - chatModel: DEFAULT_CHAT_MODEL, + ...DEFAULT_LLM_CONFIG, }; export const DEFAULT_AGENT_CONFIG: Agent = { @@ -47,7 +47,7 @@ export const DEFAULT_AGENT_CONFIG: Agent = { }, touch: DEFAULT_TOUCH_ACTION_CONFIG_FEMALE, tts: DEFAULT_TTS_CONFIG_FEMALE, - chatModel: DEFAULT_CHAT_MODEL, + ...DEFAULT_LLM_CONFIG, }; export const AGENT_GENDER_OPTIONS = [ diff --git a/src/constants/openai.ts b/src/constants/openai.ts index 04d50f09..790e8b1f 100644 --- a/src/constants/openai.ts +++ b/src/constants/openai.ts @@ -1,5 +1,5 @@ +import { Agent } from '@/types/agent'; import { ChatModelCard } from '@/types/llm'; -import { ChatStreamPayload } from '@/types/openai/chat'; export const OPENAI_API_KEY = 'x-openai-apikey'; export const OPENAI_END_POINT = 'x-openai-endpoint'; @@ -142,10 +142,12 @@ export const OPENAI_MODEL_LIST: ChatModelCard[] = [ /** * 默认使用的 ChatGPT 聊天模型配置 */ -export const DEFAULT_CHAT_MODEL: Partial = { +export const DEFAULT_LLM_CONFIG: Partial = { model: OPENAI_MODEL_LIST[0].id, - frequency_penalty: 0, - presence_penalty: 0, - temperature: 1, - top_p: 1, + params: { + frequency_penalty: 0, + presence_penalty: 0, + temperature: 1, + top_p: 1, + }, }; diff --git a/src/features/Actions/ModelSelect.tsx b/src/features/Actions/ModelSelect.tsx index 536667e4..a22e35ff 100644 --- a/src/features/Actions/ModelSelect.tsx +++ b/src/features/Actions/ModelSelect.tsx @@ -4,6 +4,7 @@ import { memo } from 'react'; import ModelIcon from '@/components/ModelIcon'; import ModelTag from '@/components/ModelTag'; +import { LOBE_VIDOL_DEFAULT_AGENT_ID } from '@/constants/agent'; import { OPENAI_MODEL_LIST } from '@/constants/openai'; import useSessionContext from '@/hooks/useSessionContext'; import { useAgentStore } from '@/store/agent'; @@ -33,16 +34,16 @@ const useStyles = createStyles(({ css, prefixCls }) => ({ const ModelSelect = memo(() => { const { styles } = useStyles(); - const { updateChatModel } = useAgentStore(); + const { updateAgentConfig } = useAgentStore(); - const model = useSessionContext()?.sessionAgent?.chatModel?.model; + const { model, agentId } = useSessionContext()?.sessionAgent || {}; const items = OPENAI_MODEL_LIST.map((item) => { return { icon: , key: item.id, label: item.displayName, - onClick: () => updateChatModel({ model: item.id }), + onClick: () => updateAgentConfig({ model: item.id }, agentId || LOBE_VIDOL_DEFAULT_AGENT_ID), }; }); diff --git a/src/features/Actions/ShareButton/Preview.tsx b/src/features/Actions/ShareButton/Preview.tsx index 38696c73..b3a2be75 100644 --- a/src/features/Actions/ShareButton/Preview.tsx +++ b/src/features/Actions/ShareButton/Preview.tsx @@ -4,8 +4,7 @@ import { Flexbox } from 'react-layout-kit'; import pkg from '@/../package.json'; import ModelTag from '@/components/ModelTag'; -import { useSessionStore } from '@/store/session'; -import { sessionSelectors } from '@/store/session/selectors'; +import useSessionContext from '@/hooks/useSessionContext'; import ChatList from './ChatList'; import { useStyles } from './style'; @@ -13,7 +12,7 @@ import { FieldType } from './type'; const Preview = memo( ({ title, withSystemRole, withBackground, withFooter }) => { - const agent = useSessionStore((s) => sessionSelectors.currentAgent(s)); + const { sessionAgent } = useSessionContext(); const { styles } = useStyles(withBackground); @@ -23,16 +22,16 @@ const Preview = memo(
- + } + desc={sessionAgent.meta.description} + tag={} title={title} /> - {withSystemRole && agent.systemRole && ( + {withSystemRole && sessionAgent.systemRole && (
- {agent.systemRole} + {sessionAgent.systemRole}
)}
diff --git a/src/features/Actions/Token.tsx b/src/features/Actions/Token.tsx index d6833bee..cec8c0ef 100644 --- a/src/features/Actions/Token.tsx +++ b/src/features/Actions/Token.tsx @@ -5,7 +5,7 @@ import { useCalculateToken } from '@/hooks/useCalculateToken'; import useSessionContext from '@/hooks/useSessionContext'; const Token = () => { - const model = useSessionContext()?.sessionAgent?.chatModel?.model; + const model = useSessionContext()?.sessionAgent?.model; const usedTokens = useCalculateToken(); return ( diff --git a/src/features/Actions/TokenMini.tsx b/src/features/Actions/TokenMini.tsx index b866ff8e..3804f830 100644 --- a/src/features/Actions/TokenMini.tsx +++ b/src/features/Actions/TokenMini.tsx @@ -5,7 +5,7 @@ import { useCalculateToken } from '@/hooks/useCalculateToken'; import useSessionContext from '@/hooks/useSessionContext'; const TokenMini = () => { - const model = useSessionContext()?.sessionAgent?.chatModel?.model; + const model = useSessionContext()?.sessionAgent?.model; const usedTokens = useCalculateToken(); const maxValue = OPENAI_MODEL_LIST.find((item) => item.id === model)?.tokens || 4096; diff --git a/src/panels/RolePanel/RoleEdit/LangModel/index.tsx b/src/panels/RolePanel/RoleEdit/LangModel/index.tsx index 9d484b12..44e7283f 100644 --- a/src/panels/RolePanel/RoleEdit/LangModel/index.tsx +++ b/src/panels/RolePanel/RoleEdit/LangModel/index.tsx @@ -11,13 +11,13 @@ import ModelSelect from './ModelSelect'; const LangModel = memo(() => { const [form] = Form.useForm(); - const { updateChatModel } = useAgentStore(); + const { updateAgentConfig } = useAgentStore(); - const agentChatModel = agentSelectors.currentAgentChatModel(useAgentStore.getState()); + const agent = agentSelectors.currentAgentItem(useAgentStore()); useEffect(() => { - form.setFieldsValue(agentChatModel); - }, [agentChatModel]); + form.setFieldsValue(agent); + }, [agent]); const model: ItemGroup = { children: [ @@ -32,28 +32,28 @@ const LangModel = memo(() => { children: , desc: '值越大,回复越随机', label: '随机性', - name: 'temperature', + name: ['params', 'temperature'], tag: 'temperature', }, { children: , desc: '与随机性类型,但不要和随机性一起更改', label: '核采样', - name: 'top_p', + name: ['params', 'top_p'], tag: 'top_p', }, { children: , desc: '值越大,越有可能拓展到新话题', label: '话题新鲜度', - name: 'presence_penalty', + name: ['params', 'presence_penalty'], tag: 'presence_penalty', }, { children: , desc: '值越大,越有可能降低重复字词', label: '频率惩罚度', - name: 'frequency_penalty', + name: ['params', 'frequency_penalty'], tag: 'frequency_penalty', }, ], @@ -63,7 +63,7 @@ const LangModel = memo(() => { return (
updateAgentConfig(values)} items={[model]} itemsType={'group'} variant={'pure'} diff --git a/src/store/agent/index.ts b/src/store/agent/index.ts index 3627908a..f6d94446 100644 --- a/src/store/agent/index.ts +++ b/src/store/agent/index.ts @@ -18,7 +18,6 @@ import { } from '@/constants/tts'; import { TouchActionType, touchReducer } from '@/store/agent/reducers/touch'; import { Agent, AgentMeta, GenderEnum } from '@/types/agent'; -import { ChatStreamPayload } from '@/types/openai/chat'; import { TouchAction, TouchAreaEnum } from '@/types/touch'; import { TTS } from '@/types/tts'; import { mergeWithUndefined } from '@/utils/common'; @@ -96,7 +95,7 @@ export interface AgentStore { /** * 更新角色配置 */ - updateAgentConfig: (agent: DeepPartial) => void; + updateAgentConfig: (agent: DeepPartial, updateAgentId?: string) => void; /** * 更新角色元数据 */ @@ -105,10 +104,6 @@ export interface AgentStore { * 更新角色 TTS */ updateAgentTTS: (tts: DeepPartial) => void; - /** - * 更新角色对话模型配置 - */ - updateChatModel: (chatModel: Partial) => void; /** * 更新触摸配置 * @param currentTouchArea @@ -190,9 +185,12 @@ const createAgentStore: StateCreator set({ currentIdentifier: newAgent.agentId, localAgentList: newList }); }, - updateAgentConfig: (agent) => { + updateAgentConfig: (agent, updateAgentId) => { const { localAgentList, currentIdentifier, defaultAgent } = get(); - if (currentIdentifier === LOBE_VIDOL_DEFAULT_AGENT_ID) { + + const updateIdentifier = updateAgentId || currentIdentifier; + + if (updateIdentifier === LOBE_VIDOL_DEFAULT_AGENT_ID) { const mergeAgent = produce(defaultAgent, (draft) => { mergeWithUndefined(draft, agent); }); @@ -201,7 +199,7 @@ const createAgentStore: StateCreator } const agents = produce(localAgentList, (draft) => { - const index = draft.findIndex((localAgent) => localAgent.agentId === currentIdentifier); + const index = draft.findIndex((localAgent) => localAgent.agentId === updateIdentifier); if (index === -1) return; mergeWithUndefined(draft[index], agent); }); @@ -299,10 +297,6 @@ const createAgentStore: StateCreator await storage.removeItem(getModelPathByAgentId(agentId)); set({ currentIdentifier: LOBE_VIDOL_DEFAULT_AGENT_ID, localAgentList: newList }); }, - updateChatModel: (chatModel) => { - const { updateAgentConfig } = get(); - updateAgentConfig({ chatModel }); - }, }); export const useAgentStore = createWithEqualityFn()( diff --git a/src/store/agent/selectors/agent.ts b/src/store/agent/selectors/agent.ts index 53fa6ff6..ef5cf5a0 100644 --- a/src/store/agent/selectors/agent.ts +++ b/src/store/agent/selectors/agent.ts @@ -1,7 +1,6 @@ import { DEFAULT_AGENT_CONFIG, LOBE_VIDOL_DEFAULT_AGENT_ID } from '@/constants/agent'; import { EMPTY_TTS_CONFIG } from '@/constants/touch'; import { Agent, AgentMeta } from '@/types/agent'; -import { ChatStreamPayload } from '@/types/openai/chat'; import { TouchActionConfig } from '@/types/touch'; import { TTS } from '@/types/tts'; @@ -33,13 +32,6 @@ const currentAgentTTS = (s: AgentStore): TTS | undefined => { return currentAgent.tts; }; -const currentAgentChatModel = (s: AgentStore): Partial | undefined => { - const currentAgent = currentAgentItem(s); - if (!currentAgent) return undefined; - - return currentAgent.chatModel; -}; - const currentAgentTouch = (s: AgentStore): TouchActionConfig | undefined => { const currentAgent = currentAgentItem(s); if (!currentAgent) return undefined; @@ -102,7 +94,6 @@ export const agentSelectors = { currentAgentItem, currentAgentMeta, currentAgentTTS, - currentAgentChatModel, currentAgentTouch, filterAgentListIds, getAgentModelById, diff --git a/src/store/session/index.ts b/src/store/session/index.ts index a0a6c0fb..b79ebd7d 100644 --- a/src/store/session/index.ts +++ b/src/store/session/index.ts @@ -8,7 +8,7 @@ import { StateCreator } from 'zustand/vanilla'; import { LOBE_VIDOL_DEFAULT_AGENT_ID } from '@/constants/agent'; import { DEFAULT_USER_AVATAR_URL, LOADING_FLAG } from '@/constants/common'; -import { DEFAULT_CHAT_MODEL } from '@/constants/openai'; +import { DEFAULT_LLM_CONFIG } from '@/constants/openai'; import { chatCompletion, handleSpeakAi } from '@/services/chat'; import { shareService } from '@/services/share'; import { Agent } from '@/types/agent'; @@ -228,8 +228,8 @@ export const createSessonStore: StateCreator { return chatCompletion( { - ...DEFAULT_CHAT_MODEL, - ...currentAgent.chatModel, + model: currentAgent.model || DEFAULT_LLM_CONFIG.model, + ...(currentAgent.params || DEFAULT_LLM_CONFIG.params), messages: [ { content: currentAgent.systemRole, diff --git a/src/store/session/selectors.ts b/src/store/session/selectors.ts index ef9c76f9..23f68091 100644 --- a/src/store/session/selectors.ts +++ b/src/store/session/selectors.ts @@ -37,10 +37,12 @@ const getAgentById = (s: SessionStore) => { const { sessionList } = s; const agentStore = useAgentStore.getState(); return (id: string) => { - const agentId = sessionList.find((item) => item.agentId === id)?.agentId; - if (agentId === LOBE_VIDOL_DEFAULT_AGENT_ID) { + if (id === LOBE_VIDOL_DEFAULT_AGENT_ID) { return agentStore.defaultAgent; } + + const agentId = sessionList.find((item) => item.agentId === id)?.agentId; + return agentStore.getAgentById(agentId || ''); }; }; diff --git a/src/types/agent.ts b/src/types/agent.ts index 6cee7b5a..1e9ce881 100644 --- a/src/types/agent.ts +++ b/src/types/agent.ts @@ -66,10 +66,6 @@ export interface Agent { * 作者名 */ author?: string; - /** - * 角色对话模型配置 - */ - chatModel?: Partial; /** * 创建时间 */ @@ -86,6 +82,14 @@ export interface Agent { * 角色元数据 */ meta: AgentMeta; + /** + * 大语言模型 + */ + model?: string; + /** + * 语言模型配置 + */ + params?: Partial; /** * 角色设定 */