Skip to content

Commit

Permalink
Bugfix/Avoid passing runnable assign when worker agent has no input v…
Browse files Browse the repository at this point in the history
…ariables (FlowiseAI#2550)

avoid passing runnable assign when worker agent has no input variables
  • Loading branch information
HenryHengZJ authored Jun 2, 2024
1 parent e15e6fa commit 8c66d2c
Showing 1 changed file with 39 additions and 17 deletions.
56 changes: 39 additions & 17 deletions packages/components/nodes/multiagents/Worker/Worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,19 +199,33 @@ async function createAgent(
}
const modelWithTools = llm.bindTools(tools)

const agent = RunnableSequence.from([
RunnablePassthrough.assign({
//@ts-ignore
agent_scratchpad: (input: { steps: ToolsAgentStep[] }) => formatToOpenAIToolMessages(input.steps)
}),
RunnablePassthrough.assign(transformObjectPropertyToFunction(workerInputVariablesValues)),
prompt,
modelWithTools,
new ToolCallingAgentOutputParser()
])
let agent

if (!workerInputVariablesValues || !Object.keys(workerInputVariablesValues).length) {
agent = RunnableSequence.from([
RunnablePassthrough.assign({
//@ts-ignore
agent_scratchpad: (input: { steps: ToolsAgentStep[] }) => formatToOpenAIToolMessages(input.steps)
}),
prompt,
modelWithTools,
new ToolCallingAgentOutputParser()
])
} else {
agent = RunnableSequence.from([
RunnablePassthrough.assign({
//@ts-ignore
agent_scratchpad: (input: { steps: ToolsAgentStep[] }) => formatToOpenAIToolMessages(input.steps)
}),
RunnablePassthrough.assign(transformObjectPropertyToFunction(workerInputVariablesValues)),
prompt,
modelWithTools,
new ToolCallingAgentOutputParser()
])
}

const executor = AgentExecutor.fromAgentAndTools({
agent: agent,
agent,
tools,
sessionId: flowObj?.sessionId,
chatId: flowObj?.chatId,
Expand All @@ -233,12 +247,19 @@ async function createAgent(
const msg = HumanMessagePromptTemplate.fromTemplate([...multiModalMessageContent])
prompt.promptMessages.splice(1, 0, msg)
}
const conversationChain = RunnableSequence.from([
RunnablePassthrough.assign(transformObjectPropertyToFunction(workerInputVariablesValues)),
prompt,
llm,
new StringOutputParser()
])

let conversationChain

if (!workerInputVariablesValues || !Object.keys(workerInputVariablesValues).length) {
conversationChain = RunnableSequence.from([prompt, llm, new StringOutputParser()])
} else {
conversationChain = RunnableSequence.from([
RunnablePassthrough.assign(transformObjectPropertyToFunction(workerInputVariablesValues)),
prompt,
llm,
new StringOutputParser()
])
}
return conversationChain
}
}
Expand All @@ -256,6 +277,7 @@ async function agentNode(
if (abortControllerSignal.signal.aborted) {
throw new Error('Aborted!')
}

const result = await agent.invoke({ ...state, signal: abortControllerSignal.signal }, config)
const additional_kwargs: ICommonObject = {}
if (result.usedTools) {
Expand Down

0 comments on commit 8c66d2c

Please sign in to comment.