# Sometimes, the LLM will hallucinate and call the wrong tool. We need to catch this and return an error message.
tool_messages=[]
ifmessage.tool_calls:
fortcinmessage.tool_calls:
iftc["name"]!="SubmitFinalAnswer":
tool_messages.append(
ToolMessage(
content=f"Error: The wrong tool was called: {tc['name']}. Please fix your mistakes. Remember to only call SubmitFinalAnswer to submit the final answer. Generated queries should be outputted WITHOUT a tool call.",
tool_call_id=tc["id"],
)
)
else:
tool_messages=[]
return{"messages":[message]+tool_messages}
workflow.add_node("query_gen",query_gen_node)
# Add a node for the model to check the query before executing it