Skip to content

Commit a19ca78

Browse files
committed
update tutorial to use PipelineTool
1 parent 6283cda commit a19ca78

1 file changed

Lines changed: 4 additions & 74 deletions

File tree

tutorials/43_Building_a_Tool_Calling_Agent.ipynb

Lines changed: 4 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,7 @@
55
"metadata": {
66
"id": "2OvkPji9O-qX"
77
},
8-
"source": [
9-
"# Tutorial: Building a Tool-Calling Agent\n",
10-
"\n",
11-
"- **Level**: Beginner\n",
12-
"- **Time to complete**: 15 minutes\n",
13-
"- **Components Used**: [`Agent`](https://docs.haystack.deepset.ai/docs/agent), [`OpenAIChatGenerator`](https://docs.haystack.deepset.ai/docs/openaichatgenerator), [`SerperDevWebSearch`](https://docs.haystack.deepset.ai/docs/serperdevwebsearch), [`ComponentTool`](https://docs.haystack.deepset.ai/docs/componenttool), [`SuperComponent`](https://docs.haystack.deepset.ai/docs/supercomponents)\n",
14-
"- **Prerequisites**: You must have an [OpenAI API Key](https://platform.openai.com/api-keys) and a [SerperDev API Key](https://serper.dev/api-key)\n",
15-
"- **Goal**: After completing this tutorial, you'll have learned how to create an Agent that can use tools both components and pipelines to answer questions and perform tasks."
16-
]
8+
"source": "# Tutorial: Building a Tool-Calling Agent\n\n- **Level**: Beginner\n- **Time to complete**: 15 minutes\n- **Components Used**: [`Agent`](https://docs.haystack.deepset.ai/docs/agent), [`OpenAIChatGenerator`](https://docs.haystack.deepset.ai/docs/openaichatgenerator), [`SerperDevWebSearch`](https://docs.haystack.deepset.ai/docs/serperdevwebsearch), [`ComponentTool`](https://docs.haystack.deepset.ai/docs/componenttool), [`PipelineTool`](https://docs.haystack.deepset.ai/docs/pipelinetool)\n- **Prerequisites**: You must have an [OpenAI API Key](https://platform.openai.com/api-keys) and a [SerperDev API Key](https://serper.dev/api-key)\n- **Goal**: After completing this tutorial, you'll have learned how to create an Agent that can use tools both components and pipelines to answer questions and perform tasks."
179
},
1810
{
1911
"cell_type": "markdown",
@@ -203,7 +195,6 @@
203195
},
204196
"outputs": [],
205197
"source": [
206-
"from haystack.components.builders.answer_builder import AnswerBuilder\n",
207198
"from haystack.components.converters.html import HTMLToDocument\n",
208199
"from haystack.components.converters.output_adapter import OutputAdapter\n",
209200
"from haystack.components.fetchers.link_content import LinkContentFetcher\n",
@@ -242,17 +233,7 @@
242233
"metadata": {
243234
"id": "wLIcnWl-66QA"
244235
},
245-
"source": [
246-
"### Creating a Tool from a Pipeline\n",
247-
"\n",
248-
"Next, wrap the `search_pipeline` inside a [`SuperComponent`](https://docs.haystack.deepset.ai/docs/supercomponents) and turn it into a tool using `ComponentTool`. The `ComponentTool` automatically creates LLM-compatible tool schemas based on the component’s input sockets. \n",
249-
"\n",
250-
"To control what data the `ComponentTool` should receive and returns, you can optionally define `input_mapping` and `output_mapping`. For example, this lets you ensure that only the `\"query\"` input of the `search_pipeline` is mentioned in LLM-compatible tool schema, and only `\"search_result\"` is returned from the `SuperComponent`.\n",
251-
"\n",
252-
"Finally, you can initialize the Agent with the resulting `search_tool`.\n",
253-
"\n",
254-
"> 💡 Learn alternative ways of creating tools in [`Tool`](https://docs.haystack.deepset.ai/docs/tool) and [`MCPTool`](https://docs.haystack.deepset.ai/docs/mcptool) documentation pages."
255-
]
236+
"source": "### Creating a Tool from a Pipeline\n\nNext, wrap the `search_pipeline` in a [`PipelineTool`](https://docs.haystack.deepset.ai/docs/pipelinetool). `PipelineTool` directly exposes a pipeline as an LLM-callable tool, replacing the older pattern of wrapping a pipeline in a `SuperComponent` and then passing it to `ComponentTool`.\n\nUse `input_mapping` and `output_mapping` to control which pipeline inputs and outputs are exposed. Here, `input_mapping` ensures only `\"query\"` is surfaced in the tool schema, and `output_mapping` extracts the formatted string produced by `output_adapter`.\n\nFinally, you can initialize the Agent with the resulting `search_tool`.\n\n> 💡 Learn alternative ways of creating tools in [`Tool`](https://docs.haystack.deepset.ai/docs/tool) and [`MCPTool`](https://docs.haystack.deepset.ai/docs/mcptool) documentation pages."
256237
},
257238
{
258239
"cell_type": "code",
@@ -261,42 +242,7 @@
261242
"id": "yxaN3KBo65pv"
262243
},
263244
"outputs": [],
264-
"source": [
265-
"from haystack.core.super_component import SuperComponent\n",
266-
"from haystack.tools import ComponentTool\n",
267-
"from haystack.components.agents import Agent\n",
268-
"from haystack.components.generators.chat import OpenAIChatGenerator\n",
269-
"\n",
270-
"search_component = SuperComponent(\n",
271-
" pipeline=search_pipeline,\n",
272-
" input_mapping={\"query\": [\"search.query\"]},\n",
273-
" output_mapping={\"output_adapter.output\": \"search_result\"},\n",
274-
")\n",
275-
"\n",
276-
"search_tool = ComponentTool(\n",
277-
" name=\"search\",\n",
278-
" description=\"Use this tool to search for information on the internet.\",\n",
279-
" component=search_component,\n",
280-
" outputs_to_string={\"source\": \"search_result\"},\n",
281-
")\n",
282-
"\n",
283-
"agent = Agent(\n",
284-
" chat_generator=OpenAIChatGenerator(model=\"gpt-4o-mini\"),\n",
285-
" tools=[search_tool],\n",
286-
" system_prompt=\"\"\"\n",
287-
" You are a deep research assistant.\n",
288-
" You create comprehensive research reports to answer the user's questions.\n",
289-
" You use the 'search'-tool to answer any questions.\n",
290-
" You perform multiple searches until you have the information you need to answer the question.\n",
291-
" Make sure you research different aspects of the question.\n",
292-
" Use markdown to format your response.\n",
293-
" When you use information from the websearch results, cite your sources using markdown links.\n",
294-
" It is important that you cite accurately.\n",
295-
" \"\"\",\n",
296-
" exit_conditions=[\"text\"],\n",
297-
" max_agent_steps=20,\n",
298-
")"
299-
]
245+
"source": "from haystack.tools import PipelineTool\nfrom haystack.components.agents import Agent\nfrom haystack.components.generators.chat import OpenAIChatGenerator\n\nsearch_tool = PipelineTool(\n name=\"search\",\n description=\"Use this tool to search for information on the internet.\",\n pipeline=search_pipeline,\n input_mapping={\"query\": [\"search.query\"]},\n output_mapping={\"output_adapter.output\": \"search_result\"},\n outputs_to_string={\"source\": \"search_result\"},\n)\n\nagent = Agent(\n chat_generator=OpenAIChatGenerator(model=\"gpt-4o-mini\"),\n tools=[search_tool],\n system_prompt=\"\"\"\n You are a deep research assistant.\n You create comprehensive research reports to answer the user's questions.\n You use the 'search'-tool to answer any questions.\n You perform multiple searches until you have the information you need to answer the question.\n Make sure you research different aspects of the question.\n Use markdown to format your response.\n When you use information from the websearch results, cite your sources using markdown links.\n It is important that you cite accurately.\n \"\"\",\n exit_conditions=[\"text\"],\n max_agent_steps=20,\n)"
300246
},
301247
{
302248
"cell_type": "markdown",
@@ -320,7 +266,6 @@
320266
"query = \"What are the latest updates on the Artemis moon mission?\"\n",
321267
"messages = [ChatMessage.from_user(query)]\n",
322268
"\n",
323-
"agent.warm_up()\n",
324269
"agent_output = agent.run(messages=messages)\n",
325270
"\n",
326271
"print(agent_output[\"messages\"][-1].text)"
@@ -353,22 +298,7 @@
353298
"metadata": {
354299
"id": "czMjWwnxPA-3"
355300
},
356-
"source": [
357-
"Let's break down this last example in the tutorial.\n",
358-
"The **Agent** is the main component that orchestrates the interaction between the LLM and tools.\n",
359-
"We use **ComponentTool** as a wrapper that allows Haystack components to be used as tools by the agent.\n",
360-
"The **SuperComponent** wraps entire pipelines so that they can be used as components and thus also as tools.\n",
361-
"\n",
362-
"We created a sophisticated search pipeline that:\n",
363-
"1. Searches the web using SerperDevWebSearch\n",
364-
"2. Fetches content from the found links\n",
365-
"3. Converts HTML content to Documents\n",
366-
"4. Formats the results for the Agent\n",
367-
"\n",
368-
"The Agent then uses this pipeline as a tool to gather information and generate comprehensive answers.\n",
369-
"\n",
370-
"By the way, did you know that the Agent is a Haystack component itself? That means you can use and combine an Agent in your pipelines just like any other component!"
371-
]
301+
"source": "Let's break down this last example in the tutorial.\nThe **Agent** is the main component that orchestrates the interaction between the LLM and tools.\nWe use **ComponentTool** as a wrapper that allows individual Haystack components to be used as tools by the agent.\nThe **PipelineTool** wraps entire pipelines so that they can be used as tools directly, without needing an intermediate `SuperComponent`.\n\nWe created a sophisticated search pipeline that:\n1. Searches the web using SerperDevWebSearch\n2. Fetches content from the found links\n3. Converts HTML content to Documents\n4. Formats the results for the Agent\n\nThe Agent then uses this pipeline as a tool to gather information and generate comprehensive answers.\n\nBy the way, did you know that the Agent is a Haystack component itself? That means you can use and combine an Agent in your pipelines just like any other component!"
372302
},
373303
{
374304
"cell_type": "markdown",

0 commit comments

Comments
 (0)