MCPcopy Index your code
hub / github.com/FlowiseAI/Flowise / createAgent

Function createAgent

packages/components/nodes/multiagents/Worker/Worker.ts:161–267  ·  view source on GitHub ↗
(
    llm: BaseChatModel,
    tools: any[],
    systemPrompt: string,
    multiModalMessageContent: MessageContentImageUrl[],
    workerInputVariablesValues: ICommonObject,
    maxIterations?: string,
    flowObj?: { sessionId?: string; chatId?: string; input?: string }
)

Source from the content-addressed store, hash-verified

159}
160
161async function createAgent(
162 llm: BaseChatModel,
163 tools: any[],
164 systemPrompt: string,
165 multiModalMessageContent: MessageContentImageUrl[],
166 workerInputVariablesValues: ICommonObject,
167 maxIterations?: string,
168 flowObj?: { sessionId?: string; chatId?: string; input?: string }
169): Promise<AgentExecutor | RunnableSequence> {
170 if (tools.length) {
171 const combinedPrompt =
172 systemPrompt +
173 '\nWork autonomously according to your specialty, using the tools available to you.' +
174 ' Do not ask for clarification.' +
175 ' Your other team members (and other teams) will collaborate with you with their own specialties.' +
176 ' You are chosen for a reason! You are one of the following team members: {team_members}.'
177
178 //const toolNames = tools.length ? tools.map((t) => t.name).join(', ') : ''
179 const prompt = ChatPromptTemplate.fromMessages([
180 ['system', combinedPrompt],
181 new MessagesPlaceholder('messages'),
182 new MessagesPlaceholder('agent_scratchpad')
183 /* Gettind rid of this for now because other LLMs dont support system message at later stage
184 [
185 'system',
186 [
187 'Supervisor instructions: {instructions}\n' + tools.length
188 ? `Remember, you individually can only use these tools: ${toolNames}`
189 : '' + '\n\nEnd if you have already completed the requested task. Communicate the work completed.'
190 ].join('\n')
191 ]*/
192 ])
193
194 if (multiModalMessageContent.length) {
195 const msg = HumanMessagePromptTemplate.fromTemplate([...multiModalMessageContent])
196 prompt.promptMessages.splice(1, 0, msg)
197 }
198
199 if (llm.bindTools === undefined) {
200 throw new Error(`This agent only compatible with function calling models.`)
201 }
202 const modelWithTools = llm.bindTools(tools)
203
204 let agent
205
206 if (!workerInputVariablesValues || !Object.keys(workerInputVariablesValues).length) {
207 agent = RunnableSequence.from([
208 RunnablePassthrough.assign({
209 //@ts-ignore
210 agent_scratchpad: (input: { steps: ToolsAgentStep[] }) => formatToOpenAIToolMessages(input.steps)
211 }),
212 prompt,
213 modelWithTools,
214 new ToolCallingAgentOutputParser()
215 ])
216 } else {
217 agent = RunnableSequence.from([
218 RunnablePassthrough.assign({

Callers 1

initMethod · 0.70

Calls 3

fromAgentAndToolsMethod · 0.80

Tested by

no test coverage detected