feat: 支持session级别插件开关

This commit is contained in:
Hk-Gosuto 2023-08-11 17:55:50 +08:00
parent 987598f30d
commit 6b28bcd9b5
7 changed files with 117 additions and 102 deletions

View File

@ -68,6 +68,7 @@ async function handle(req: NextRequest) {
const handler = BaseCallbackHandler.fromMethods({ const handler = BaseCallbackHandler.fromMethods({
async handleLLMNewToken(token: string) { async handleLLMNewToken(token: string) {
if (token) { if (token) {
console.log("[Token]", token);
var response = new ResponseBody(); var response = new ResponseBody();
response.message = token; response.message = token;
await writer.ready; await writer.ready;
@ -200,12 +201,20 @@ async function handle(req: NextRequest) {
frequencyPenalty: reqBody.frequency_penalty, frequencyPenalty: reqBody.frequency_penalty,
}); });
const executor = await initializeAgentExecutorWithOptions(tools, llm, { let executor = await initializeAgentExecutorWithOptions(tools, llm, {
agentType: "openai-functions", agentType: "chat-conversational-react-description",
returnIntermediateSteps: true, returnIntermediateSteps: true,
maxIterations: 3, maxIterations: 3,
memory: memory, memory: memory,
}); });
if (reqBody.model.endsWith("0613"))
executor = await initializeAgentExecutorWithOptions(tools, llm, {
agentType: "openai-functions",
returnIntermediateSteps: true,
maxIterations: 3,
memory: memory,
});
executor.call( executor.call(
{ {
input: reqBody.messages.slice(-1)[0].content, input: reqBody.messages.slice(-1)[0].content,

View File

@ -3,6 +3,7 @@
.chat-input-actions { .chat-input-actions {
display: flex; display: flex;
flex-wrap: wrap; flex-wrap: wrap;
justify-content: space-between;
.chat-input-action { .chat-input-action {
display: inline-flex; display: inline-flex;

View File

@ -27,6 +27,8 @@ import PinIcon from "../icons/pin.svg";
import EditIcon from "../icons/rename.svg"; import EditIcon from "../icons/rename.svg";
import ConfirmIcon from "../icons/confirm.svg"; import ConfirmIcon from "../icons/confirm.svg";
import CancelIcon from "../icons/cancel.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 LightIcon from "../icons/light.svg";
import DarkIcon from "../icons/dark.svg"; import DarkIcon from "../icons/dark.svg";
@ -414,11 +416,11 @@ export function ChatActions(props: {
const navigate = useNavigate(); const navigate = useNavigate();
const chatStore = useChatStore(); const chatStore = useChatStore();
// switch tools // switch Plugins
const useTools = chatStore.currentSession().useTools; const usePlugins = chatStore.currentSession().usePlugins;
function switchUseTools() { function switchUsePlugins() {
chatStore.updateCurrentSession((session) => { chatStore.updateCurrentSession((session) => {
session.useTools = !session.useTools; session.usePlugins = !session.usePlugins;
}); });
} }
@ -450,107 +452,110 @@ export function ChatActions(props: {
return ( return (
<div className={styles["chat-input-actions"]}> <div className={styles["chat-input-actions"]}>
{couldStop && ( <div>
{couldStop && (
<ChatAction
onClick={stopAll}
text={Locale.Chat.InputActions.Stop}
icon={<StopIcon />}
/>
)}
{!props.hitBottom && (
<ChatAction
onClick={props.scrollToBottom}
text={Locale.Chat.InputActions.ToBottom}
icon={<BottomIcon />}
/>
)}
{props.hitBottom && (
<ChatAction
onClick={props.showPromptModal}
text={Locale.Chat.InputActions.Settings}
icon={<SettingsIcon />}
/>
)}
<ChatAction <ChatAction
onClick={stopAll} onClick={nextTheme}
text={Locale.Chat.InputActions.Stop} text={Locale.Chat.InputActions.Theme[theme]}
icon={<StopIcon />} icon={
<>
{theme === Theme.Auto ? (
<AutoIcon />
) : theme === Theme.Light ? (
<LightIcon />
) : theme === Theme.Dark ? (
<DarkIcon />
) : null}
</>
}
/> />
)}
{!props.hitBottom && (
<ChatAction <ChatAction
onClick={props.scrollToBottom} onClick={props.showPromptHints}
text={Locale.Chat.InputActions.ToBottom} text={Locale.Chat.InputActions.Prompt}
icon={<BottomIcon />} icon={<PromptIcon />}
/> />
)}
{props.hitBottom && (
<ChatAction <ChatAction
onClick={props.showPromptModal} onClick={() => {
text={Locale.Chat.InputActions.Settings} navigate(Path.Masks);
icon={<SettingsIcon />} }}
text={Locale.Chat.InputActions.Masks}
icon={<MaskIcon />}
/> />
)}
<ChatAction <ChatAction
onClick={nextTheme} onClick={() => setShowModelSelector(true)}
text={Locale.Chat.InputActions.Theme[theme]} text={currentModel}
icon={ icon={<RobotIcon />}
<> />
{theme === Theme.Auto ? (
<AutoIcon />
) : theme === Theme.Light ? (
<LightIcon />
) : theme === Theme.Dark ? (
<DarkIcon />
) : null}
</>
}
/>
<ChatAction <ChatAction
onClick={props.showPromptHints} onClick={switchUsePlugins}
text={Locale.Chat.InputActions.Prompt} text={
icon={<PromptIcon />} usePlugins
/> ? Locale.Chat.InputActions.DisablePlugins
: Locale.Chat.InputActions.EnablePlugins
}
icon={usePlugins ? <EnablePluginIcon /> : <DisablePluginIcon />}
/>
<ChatAction {showModelSelector && (
onClick={() => { <Selector
navigate(Path.Masks); defaultSelectedValue={currentModel}
}} items={models.map((m) => ({
text={Locale.Chat.InputActions.Masks} title: m,
icon={<MaskIcon />} value: m,
/> }))}
onClose={() => setShowModelSelector(false)}
<ChatAction onSelection={(s) => {
text={Locale.Chat.InputActions.Clear} if (s.length === 0) return;
icon={<BreakIcon />} chatStore.updateCurrentSession((session) => {
onClick={() => { session.mask.modelConfig.model = s[0] as ModelType;
chatStore.updateCurrentSession((session) => { session.mask.syncGlobalConfig = false;
if (session.clearContextIndex === session.messages.length) { });
session.clearContextIndex = undefined; showToast(s[0]);
} else { }}
session.clearContextIndex = session.messages.length; />
session.memoryPrompt = ""; // will clear memory )}
} </div>
}); <div>
}} <ChatAction
/> text={Locale.Chat.InputActions.Clear}
icon={<BreakIcon />}
<ChatAction onClick={() => {
onClick={() => setShowModelSelector(true)}
text={currentModel}
icon={<RobotIcon />}
/>
{/* <ChatAction
onClick={switchUseTools}
text={
useTools
? Locale.Chat.InputActions.CloseTools
: Locale.Chat.InputActions.OpenTools
}
icon={useTools ? <SearchOpenIcon /> : <SearchCloseIcon />}
/> */}
{showModelSelector && (
<Selector
defaultSelectedValue={currentModel}
items={models.map((m) => ({
title: m,
value: m,
}))}
onClose={() => setShowModelSelector(false)}
onSelection={(s) => {
if (s.length === 0) return;
chatStore.updateCurrentSession((session) => { chatStore.updateCurrentSession((session) => {
session.mask.modelConfig.model = s[0] as ModelType; if (session.clearContextIndex === session.messages.length) {
session.mask.syncGlobalConfig = false; session.clearContextIndex = undefined;
} else {
session.clearContextIndex = session.messages.length;
session.memoryPrompt = ""; // will clear memory
}
}); });
showToast(s[0]);
}} }}
/> />
)} </div>
</div> </div>
); );
} }
@ -1198,7 +1203,7 @@ function _Chat() {
</div> </div>
)} )}
</div> </div>
{session.useTools && {session.usePlugins &&
!isUser && !isUser &&
message.toolMessages && message.toolMessages &&
message.toolMessages.map((tool, index) => ( message.toolMessages.map((tool, index) => (

View File

Before

Width:  |  Height:  |  Size: 963 B

After

Width:  |  Height:  |  Size: 963 B

View File

@ -58,8 +58,8 @@ const cn = {
Masks: "所有面具", Masks: "所有面具",
Clear: "清除聊天", Clear: "清除聊天",
Settings: "对话设置", Settings: "对话设置",
OpenTools: "开启插件", EnablePlugins: "开启插件",
CloseTools: "关闭插件", DisablePlugins: "关闭插件",
}, },
Rename: "重命名对话", Rename: "重命名对话",
Typing: "正在输入…", Typing: "正在输入…",

View File

@ -60,8 +60,8 @@ const en: LocaleType = {
Masks: "Masks", Masks: "Masks",
Clear: "Clear Context", Clear: "Clear Context",
Settings: "Settings", Settings: "Settings",
OpenTools: "Enable Plugins", EnablePlugins: "Enable Plugins",
CloseTools: "Disable Plugins", DisablePlugins: "Disable Plugins",
}, },
Rename: "Rename Chat", Rename: "Rename Chat",
Typing: "Typing…", Typing: "Typing…",

View File

@ -61,7 +61,7 @@ export interface ChatSession {
clearContextIndex?: number; clearContextIndex?: number;
mask: Mask; mask: Mask;
useTools: boolean; usePlugins: boolean;
} }
export const DEFAULT_TOPIC = Locale.Store.DefaultTopic; export const DEFAULT_TOPIC = Locale.Store.DefaultTopic;
@ -85,7 +85,7 @@ function createEmptySession(): ChatSession {
lastSummarizeIndex: 0, lastSummarizeIndex: 0,
mask: createEmptyMask(), mask: createEmptyMask(),
useTools: true, usePlugins: true,
}; };
} }
@ -317,7 +317,7 @@ export const useChatStore = create<ChatStore>()(
session.messages.push(botMessage); session.messages.push(botMessage);
}); });
if (session.useTools && modelConfig.model.endsWith("0613")) { if (session.usePlugins) {
console.log("[ToolAgent] start"); console.log("[ToolAgent] start");
api.llm.toolAgentChat({ api.llm.toolAgentChat({
messages: sendMessages, messages: sendMessages,