Skip to content

Commit 41c8192

Browse files
sjrlbilgeyucel
andauthored
Update 43_Building_a_Tool_Calling_Agent.ipynb (#397)
* Update 43_Building_a_Tool_Calling_Agent.ipynb * Update 43_Building_a_Tool_Calling_Agent.ipynb Add forgotten import * Update 43_Building_a_Tool_Calling_Agent.ipynb Use outputs_to_string * Update 43_Building_a_Tool_Calling_Agent.ipynb * add explanation around input output mapping --------- Co-authored-by: bilgeyucel <[email protected]>
1 parent 7c7e439 commit 41c8192

File tree

1 file changed

+27
-20
lines changed

1 file changed

+27
-20
lines changed

tutorials/43_Building_a_Tool_Calling_Agent.ipynb

+27-20
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@
188188
"source": [
189189
"## Using Agent with a Pipeline as Tool\n",
190190
"\n",
191-
"Now, for a more sophisticated example, let's build a research assistant that can search the web, fetch content from links, and generate comprehensive answers. In contrast to our previous Agent, we now want to follow the links on the search engine results page and access their content. We'll start with a Haystack Pipeline that the Agent can use as a tool:"
191+
"Now, for a more sophisticated example, let's build a research assistant that can search the web, fetch content from links, and generate comprehensive answers. In contrast to our previous Agent, we now want to follow the links on the search engine results page, access their content and parse their content through [OutputAdapter](https://docs.haystack.deepset.ai/docs/outputadapter). We'll start with a Haystack Pipeline that the Agent can use as a tool:"
192192
]
193193
},
194194
{
@@ -204,8 +204,8 @@
204204
"outputs": [],
205205
"source": [
206206
"from haystack.components.builders.answer_builder import AnswerBuilder\n",
207-
"from haystack.components.builders.chat_prompt_builder import ChatPromptBuilder\n",
208207
"from haystack.components.converters.html import HTMLToDocument\n",
208+
"from haystack.components.converters.output_adapter import OutputAdapter\n",
209209
"from haystack.components.fetchers.link_content import LinkContentFetcher\n",
210210
"from haystack.components.websearch.serper_dev import SerperDevWebSearch\n",
211211
"from haystack.dataclasses import ChatMessage\n",
@@ -217,29 +217,24 @@
217217
"search_pipeline.add_component(\"fetcher\", LinkContentFetcher(timeout=3, raise_on_failure=False, retry_attempts=2))\n",
218218
"search_pipeline.add_component(\"converter\", HTMLToDocument())\n",
219219
"search_pipeline.add_component(\n",
220-
" \"builder\",\n",
221-
" ChatPromptBuilder(\n",
222-
" template=[\n",
223-
" ChatMessage.from_user(\n",
224-
" \"\"\"\n",
225-
"{% for doc in docs %}\n",
226-
" {% if doc.content %}\n",
220+
" \"output_adapter\",\n",
221+
" OutputAdapter(\n",
222+
" template=\"\"\"\n",
223+
"{%- for doc in docs -%}\n",
224+
" {%- if doc.content -%}\n",
227225
" <search-result url=\"{{ doc.meta.url }}\">\n",
228226
" {{ doc.content|truncate(25000) }}\n",
229227
" </search-result>\n",
230-
" {% endif %}\n",
231-
"{% endfor %}\n",
232-
"\"\"\"\n",
233-
" )\n",
234-
" ],\n",
235-
" variables=[\"docs\"],\n",
236-
" required_variables=[\"docs\"],\n",
228+
" {%- endif -%}\n",
229+
"{%- endfor -%}\n",
230+
"\"\"\",\n",
231+
" output_type=str,\n",
237232
" ),\n",
238233
")\n",
239234
"\n",
240235
"search_pipeline.connect(\"search.links\", \"fetcher.urls\")\n",
241236
"search_pipeline.connect(\"fetcher.streams\", \"converter.sources\")\n",
242-
"search_pipeline.connect(\"converter.documents\", \"builder.docs\")"
237+
"search_pipeline.connect(\"converter.documents\", \"output_adapter.docs\")"
243238
]
244239
},
245240
{
@@ -250,7 +245,11 @@
250245
"source": [
251246
"### Creating a Tool from a Pipeline\n",
252247
"\n",
253-
"As the next step, create a `SuperComponent` with the `search_pipeline` and convert it into a tool with `ComponentTool`. Then, you can initialize the Agent with the created `search_tool`\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",
254253
"\n",
255254
"> 💡 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."
256255
]
@@ -268,9 +267,17 @@
268267
"from haystack.components.agents import Agent\n",
269268
"from haystack.components.generators.chat import OpenAIChatGenerator\n",
270269
"\n",
271-
"search_component = SuperComponent(pipeline=search_pipeline)\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",
272276
"search_tool = ComponentTool(\n",
273-
" name=\"search\", description=\"Use this tool to search for information on the internet.\", component=search_component\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",
274281
")\n",
275282
"\n",
276283
"agent = Agent(\n",

0 commit comments

Comments
 (0)