How to Chain Tool Outputs as Inputs in LangChain Tool-Calling Agent #28686
Replies: 1 comment 2 replies
-
To chain the outputs of two tools in LangChain's tool-calling agent, you can use the
Here's an example setup: from langchain_core.tools import tool
from langchain import hub
from langchain.agents import AgentExecutor, create_tool_calling_agent
from langchain_openai.chat_models import ChatOpenAI
# Define your tools
@tool
def tool_a(input_data: str) -> str:
"""Process input data and return a result."""
return f"Processed {input_data}"
@tool
def tool_b(processed_data: str) -> str:
"""Take processed data and perform another operation."""
return f"Further processed {processed_data}"
# Initialize the language model
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
# Create a tool-calling agent
tools = [tool_a, tool_b]
prompt = hub.pull("hwchase17/openai-tools-agent")
agent = create_tool_calling_agent(llm, tools, prompt)
# Create an agent executor
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
# Execute the agent with input that requires chaining
result = agent_executor.invoke(
{
"input": "Start with some data, process it with Tool A, and then pass the result to Tool B"
}
)
print(result) This setup allows the agent to effectively chain the output of Tool A as the input to Tool B [1]. To continue talking to Dosu, mention @dosu. Help Dosu learn! Give it feedback: Great Response | Irrelevant Answer | Incorrect Sources | Too Verbose | Hallucination | Other |
Beta Was this translation helpful? Give feedback.
-
I'm working with LangChain's tool-calling agent, and I need to chain the outputs of two tools together. Specifically, I want to call Tool A with input "X", which will return output "Y". Then, I want to pass that "Y" output as the input for Tool B and get its output "Z".
However, I'm not sure how to properly pass the output from Tool A as the input to Tool B within the LangChain framework.
Has anyone successfully implemented this or can provide guidance on how to achieve this chaining of tool outputs as inputs in LangChain's tool-calling agent? Any help would be greatly appreciated!
Beta Was this translation helpful? Give feedback.
All reactions