(
nodeData: INodeData,
options: ICommonObject,
flowObj: { sessionId?: string; chatId?: string; input?: string }
)
| 216 | } |
| 217 | |
| 218 | const prepareAgent = async ( |
| 219 | nodeData: INodeData, |
| 220 | options: ICommonObject, |
| 221 | flowObj: { sessionId?: string; chatId?: string; input?: string } |
| 222 | ) => { |
| 223 | const model = nodeData.inputs?.model as BaseChatModel |
| 224 | const maxIterations = nodeData.inputs?.maxIterations as string |
| 225 | const memory = nodeData.inputs?.memory as FlowiseMemory |
| 226 | let systemMessage = nodeData.inputs?.systemMessage as string |
| 227 | let tools = nodeData.inputs?.tools |
| 228 | tools = flatten(tools) |
| 229 | const inputKey = memory.inputKey ? memory.inputKey : 'input' |
| 230 | const memoryKey = memory.memoryKey ? memory.memoryKey : 'chat_history' |
| 231 | const prependMessages = options?.prependMessages |
| 232 | |
| 233 | systemMessage = transformBracesWithColon(systemMessage) |
| 234 | |
| 235 | let promptMessage = systemMessage ? systemMessage : defaultSystemMessage |
| 236 | if (memory.memoryKey) promptMessage = promptMessage.replaceAll('{chat_history}', `{${memory.memoryKey}}`) |
| 237 | if (memory.inputKey) promptMessage = promptMessage.replaceAll('{input}', `{${memory.inputKey}}`) |
| 238 | |
| 239 | const prompt = ChatPromptTemplate.fromMessages([ |
| 240 | HumanMessagePromptTemplate.fromTemplate(promptMessage), |
| 241 | new MessagesPlaceholder('agent_scratchpad') |
| 242 | ]) |
| 243 | |
| 244 | const missingVariables = ['tools', 'agent_scratchpad'].filter((v) => !prompt.inputVariables.includes(v)) |
| 245 | |
| 246 | if (missingVariables.length > 0) { |
| 247 | throw new Error(`Provided prompt is missing required input variables: ${JSON.stringify(missingVariables)}`) |
| 248 | } |
| 249 | |
| 250 | const llmWithStop = (model as BaseLanguageModel).withConfig({ |
| 251 | stop: ['</tool_input>', '</final_answer>'] |
| 252 | }) |
| 253 | |
| 254 | const messages = (await memory.getChatMessages(flowObj.sessionId, false, prependMessages)) as IMessage[] |
| 255 | let chatHistoryMsgTxt = '' |
| 256 | for (const message of messages) { |
| 257 | if (message.type === 'apiMessage') { |
| 258 | chatHistoryMsgTxt += `\\nAI:${message.message}` |
| 259 | } else if (message.type === 'userMessage') { |
| 260 | chatHistoryMsgTxt += `\\nHuman:${message.message}` |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | const runnableAgent = RunnableSequence.from([ |
| 265 | { |
| 266 | [inputKey]: (i: { input: string; tools: Tool[]; steps: AgentStep[] }) => i.input, |
| 267 | agent_scratchpad: (i: { input: string; tools: Tool[]; steps: AgentStep[] }) => formatLogToMessage(i.steps), |
| 268 | tools: (_: { input: string; tools: Tool[]; steps: AgentStep[] }) => |
| 269 | tools.map((tool: Tool) => `${tool.name}: ${tool.description}`), |
| 270 | [memoryKey]: (_: { input: string; tools: Tool[]; steps: AgentStep[] }) => chatHistoryMsgTxt |
| 271 | }, |
| 272 | prompt, |
| 273 | llmWithStop, |
| 274 | new XMLAgentOutputParser() |
| 275 | ]) |
no test coverage detected