From 6b28bcd9b56130a00655e38068dda9e0b8f7d043 Mon Sep 17 00:00:00 2001 From: Hk-Gosuto Date: Fri, 11 Aug 2023 17:55:50 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81session=E7=BA=A7?= =?UTF-8?q?=E5=88=AB=E6=8F=92=E4=BB=B6=E5=BC=80=E5=85=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/langchain/tool/agent/route.ts | 13 +- app/components/chat.module.scss | 1 + app/components/chat.tsx | 191 +++++++++--------- ...lugin_disenable.svg => plugin_disable.svg} | 0 app/locales/cn.ts | 4 +- app/locales/en.ts | 4 +- app/store/chat.ts | 6 +- 7 files changed, 117 insertions(+), 102 deletions(-) rename app/icons/{plugin_disenable.svg => plugin_disable.svg} (100%) diff --git a/app/api/langchain/tool/agent/route.ts b/app/api/langchain/tool/agent/route.ts index f67b9e0a3..356395b8f 100644 --- a/app/api/langchain/tool/agent/route.ts +++ b/app/api/langchain/tool/agent/route.ts @@ -68,6 +68,7 @@ async function handle(req: NextRequest) { const handler = BaseCallbackHandler.fromMethods({ async handleLLMNewToken(token: string) { if (token) { + console.log("[Token]", token); var response = new ResponseBody(); response.message = token; await writer.ready; @@ -200,12 +201,20 @@ async function handle(req: NextRequest) { frequencyPenalty: reqBody.frequency_penalty, }); - const executor = await initializeAgentExecutorWithOptions(tools, llm, { - agentType: "openai-functions", + let executor = await initializeAgentExecutorWithOptions(tools, llm, { + agentType: "chat-conversational-react-description", returnIntermediateSteps: true, maxIterations: 3, memory: memory, }); + if (reqBody.model.endsWith("0613")) + executor = await initializeAgentExecutorWithOptions(tools, llm, { + agentType: "openai-functions", + returnIntermediateSteps: true, + maxIterations: 3, + memory: memory, + }); + executor.call( { input: reqBody.messages.slice(-1)[0].content, diff --git a/app/components/chat.module.scss b/app/components/chat.module.scss index 2be0e795d..150477510 100644 --- a/app/components/chat.module.scss +++ b/app/components/chat.module.scss @@ -3,6 +3,7 @@ .chat-input-actions { display: flex; flex-wrap: wrap; + justify-content: space-between; .chat-input-action { display: inline-flex; diff --git a/app/components/chat.tsx b/app/components/chat.tsx index 50c7f13cc..a4cc1ee86 100644 --- a/app/components/chat.tsx +++ b/app/components/chat.tsx @@ -27,6 +27,8 @@ import PinIcon from "../icons/pin.svg"; import EditIcon from "../icons/rename.svg"; import ConfirmIcon from "../icons/confirm.svg"; import CancelIcon from "../icons/cancel.svg"; +import EnablePluginIcon from "../icons/plugin_enable.svg"; +import DisablePluginIcon from "../icons/plugin_disable.svg"; import LightIcon from "../icons/light.svg"; import DarkIcon from "../icons/dark.svg"; @@ -414,11 +416,11 @@ export function ChatActions(props: { const navigate = useNavigate(); const chatStore = useChatStore(); - // switch tools - const useTools = chatStore.currentSession().useTools; - function switchUseTools() { + // switch Plugins + const usePlugins = chatStore.currentSession().usePlugins; + function switchUsePlugins() { chatStore.updateCurrentSession((session) => { - session.useTools = !session.useTools; + session.usePlugins = !session.usePlugins; }); } @@ -450,107 +452,110 @@ export function ChatActions(props: { return (
- {couldStop && ( +
+ {couldStop && ( + } + /> + )} + {!props.hitBottom && ( + } + /> + )} + {props.hitBottom && ( + } + /> + )} + } + onClick={nextTheme} + text={Locale.Chat.InputActions.Theme[theme]} + icon={ + <> + {theme === Theme.Auto ? ( + + ) : theme === Theme.Light ? ( + + ) : theme === Theme.Dark ? ( + + ) : null} + + } /> - )} - {!props.hitBottom && ( + } + onClick={props.showPromptHints} + text={Locale.Chat.InputActions.Prompt} + icon={} /> - )} - {props.hitBottom && ( + } + onClick={() => { + navigate(Path.Masks); + }} + text={Locale.Chat.InputActions.Masks} + icon={} /> - )} - - {theme === Theme.Auto ? ( - - ) : theme === Theme.Light ? ( - - ) : theme === Theme.Dark ? ( - - ) : null} - - } - /> + setShowModelSelector(true)} + text={currentModel} + icon={} + /> - } - /> + : } + /> - { - navigate(Path.Masks); - }} - text={Locale.Chat.InputActions.Masks} - icon={} - /> - - } - onClick={() => { - chatStore.updateCurrentSession((session) => { - if (session.clearContextIndex === session.messages.length) { - session.clearContextIndex = undefined; - } else { - session.clearContextIndex = session.messages.length; - session.memoryPrompt = ""; // will clear memory - } - }); - }} - /> - - setShowModelSelector(true)} - text={currentModel} - icon={} - /> - - {/* : } - /> */} - - {showModelSelector && ( - ({ - title: m, - value: m, - }))} - onClose={() => setShowModelSelector(false)} - onSelection={(s) => { - if (s.length === 0) return; + {showModelSelector && ( + ({ + title: m, + value: m, + }))} + onClose={() => setShowModelSelector(false)} + onSelection={(s) => { + if (s.length === 0) return; + chatStore.updateCurrentSession((session) => { + session.mask.modelConfig.model = s[0] as ModelType; + session.mask.syncGlobalConfig = false; + }); + showToast(s[0]); + }} + /> + )} +
+
+ } + onClick={() => { chatStore.updateCurrentSession((session) => { - session.mask.modelConfig.model = s[0] as ModelType; - session.mask.syncGlobalConfig = false; + if (session.clearContextIndex === session.messages.length) { + session.clearContextIndex = undefined; + } else { + session.clearContextIndex = session.messages.length; + session.memoryPrompt = ""; // will clear memory + } }); - showToast(s[0]); }} /> - )} +
); } @@ -1198,7 +1203,7 @@ function _Chat() { )} - {session.useTools && + {session.usePlugins && !isUser && message.toolMessages && message.toolMessages.map((tool, index) => ( diff --git a/app/icons/plugin_disenable.svg b/app/icons/plugin_disable.svg similarity index 100% rename from app/icons/plugin_disenable.svg rename to app/icons/plugin_disable.svg diff --git a/app/locales/cn.ts b/app/locales/cn.ts index 8fbc0421e..00ffc15eb 100644 --- a/app/locales/cn.ts +++ b/app/locales/cn.ts @@ -58,8 +58,8 @@ const cn = { Masks: "所有面具", Clear: "清除聊天", Settings: "对话设置", - OpenTools: "开启插件", - CloseTools: "关闭插件", + EnablePlugins: "开启插件", + DisablePlugins: "关闭插件", }, Rename: "重命名对话", Typing: "正在输入…", diff --git a/app/locales/en.ts b/app/locales/en.ts index f8d9c8e4b..058e37574 100644 --- a/app/locales/en.ts +++ b/app/locales/en.ts @@ -60,8 +60,8 @@ const en: LocaleType = { Masks: "Masks", Clear: "Clear Context", Settings: "Settings", - OpenTools: "Enable Plugins", - CloseTools: "Disable Plugins", + EnablePlugins: "Enable Plugins", + DisablePlugins: "Disable Plugins", }, Rename: "Rename Chat", Typing: "Typing…", diff --git a/app/store/chat.ts b/app/store/chat.ts index 166151fcb..a70699956 100644 --- a/app/store/chat.ts +++ b/app/store/chat.ts @@ -61,7 +61,7 @@ export interface ChatSession { clearContextIndex?: number; mask: Mask; - useTools: boolean; + usePlugins: boolean; } export const DEFAULT_TOPIC = Locale.Store.DefaultTopic; @@ -85,7 +85,7 @@ function createEmptySession(): ChatSession { lastSummarizeIndex: 0, mask: createEmptyMask(), - useTools: true, + usePlugins: true, }; } @@ -317,7 +317,7 @@ export const useChatStore = create()( session.messages.push(botMessage); }); - if (session.useTools && modelConfig.model.endsWith("0613")) { + if (session.usePlugins) { console.log("[ToolAgent] start"); api.llm.toolAgentChat({ messages: sendMessages,