MCPcopy
hub / github.com/FlowiseAI/Flowise / prepareAgent

Function prepareAgent

packages/components/nodes/agents/ToolAgent/ToolAgent.ts:259–364  ·  view source on GitHub ↗
(
    nodeData: INodeData,
    options: ICommonObject,
    flowObj: { sessionId?: string; chatId?: string; input?: string }
)

Source from the content-addressed store, hash-verified

257}
258
259const prepareAgent = async (
260 nodeData: INodeData,
261 options: ICommonObject,
262 flowObj: { sessionId?: string; chatId?: string; input?: string }
263) => {
264 const model = nodeData.inputs?.model as BaseChatModel
265 const maxIterations = nodeData.inputs?.maxIterations as string
266 const memory = nodeData.inputs?.memory as FlowiseMemory
267 let systemMessage = nodeData.inputs?.systemMessage as string
268 let tools = nodeData.inputs?.tools
269 tools = flatten(tools)
270 const memoryKey = memory.memoryKey ? memory.memoryKey : 'chat_history'
271 const inputKey = memory.inputKey ? memory.inputKey : 'input'
272 const prependMessages = options?.prependMessages
273
274 systemMessage = transformBracesWithColon(systemMessage)
275
276 let prompt = ChatPromptTemplate.fromMessages([
277 ['system', systemMessage],
278 new MessagesPlaceholder(memoryKey),
279 ['human', `{${inputKey}}`],
280 new MessagesPlaceholder('agent_scratchpad')
281 ])
282
283 let promptVariables = {}
284 const chatPromptTemplate = nodeData.inputs?.chatPromptTemplate as ChatPromptTemplate
285 if (chatPromptTemplate && chatPromptTemplate.promptMessages.length) {
286 const humanPrompt = chatPromptTemplate.promptMessages[chatPromptTemplate.promptMessages.length - 1]
287 const messages = [
288 ...chatPromptTemplate.promptMessages.slice(0, -1),
289 new MessagesPlaceholder(memoryKey),
290 humanPrompt,
291 new MessagesPlaceholder('agent_scratchpad')
292 ]
293 prompt = ChatPromptTemplate.fromMessages(messages)
294 if ((chatPromptTemplate as any).promptValues) {
295 const promptValuesRaw = (chatPromptTemplate as any).promptValues
296 const promptValues = handleEscapeCharacters(promptValuesRaw, true)
297 for (const val in promptValues) {
298 promptVariables = {
299 ...promptVariables,
300 [val]: () => {
301 return promptValues[val]
302 }
303 }
304 }
305 }
306 }
307
308 if (llmSupportsVision(model)) {
309 const messageContent = await addImagesToMessages(nodeData, options, model.multiModalOption)
310
311 if (messageContent?.length) {
312 // Pop the `agent_scratchpad` MessagePlaceHolder
313 let messagePlaceholder = prompt.promptMessages.pop() as MessagesPlaceholder
314 if (prompt.promptMessages.at(-1) instanceof HumanMessagePromptTemplate) {
315 const lastMessage = prompt.promptMessages.pop() as HumanMessagePromptTemplate
316 const template = (lastMessage.prompt as PromptTemplate).template as string

Callers 2

initMethod · 0.70
runMethod · 0.70

Calls 6

transformBracesWithColonFunction · 0.90
handleEscapeCharactersFunction · 0.90
llmSupportsVisionFunction · 0.90
addImagesToMessagesFunction · 0.90
fromAgentAndToolsMethod · 0.80
getChatMessagesMethod · 0.65

Tested by

no test coverage detected