@@ -110,17 +110,29 @@ paths, construct a `FunctionHook` directly with both `function` and `async_funct
110110
111111``` python
112112from haystack.components.agents import Agent
113+ from haystack.components.generators.chat import OpenAIChatGenerator
113114from haystack.hooks import hook
114115from haystack.components.agents.state import State
115116from haystack.dataclasses import ChatMessage
117+ from haystack.tools import tool
118+
119+ @tool
120+ def weather_tool (city : str ) -> str :
121+ ''' Get the current weather for a given city.'''
122+ return f " The weather in { city} is sunny. "
123+
124+ @tool
125+ def save (content : str ) -> str :
126+ ''' Save content to durable storage.'''
127+ return " Saved."
116128
117129@hook
118130def require_save (state : State) -> None :
119131 if state.get(" tool_call_counts" , {}).get(" save" , 0 ) == 0 :
120132 state.set(" messages" , [ChatMessage.from_system(" You must call `save` before finishing." )])
121133 state.set(" continue_run" , True )
122134
123- agent = Agent(chat_generator = ... , tools = [... ], hooks = {" on_exit" : [require_save]})
135+ agent = Agent(chat_generator = OpenAIChatGenerator() , tools = [weather_tool, save ], hooks = {" on_exit" : [require_save]})
124136```
125137
126138** Parameters:**
@@ -199,6 +211,8 @@ Register it on an `Agent` to confirm, modify, or reject tool calls before they r
199211
200212``` python
201213from haystack.components.agents import Agent
214+ from haystack.components.generators.chat import OpenAIChatGenerator
215+ from haystack.tools import tool
202216from haystack.hooks.human_in_the_loop import (
203217 AlwaysAskPolicy,
204218 BlockingConfirmationStrategy,
@@ -208,14 +222,19 @@ from haystack.hooks.human_in_the_loop import (
208222 SimpleConsoleUI,
209223)
210224
225+ @tool
226+ def delete_file (path : str ) -> str :
227+ ''' Delete the file at the given path.'''
228+ return f " Deleted { path} . "
229+
211230hook = ConfirmationHook(
212231 confirmation_strategies = {
213- " my_tool " : BlockingConfirmationStrategy(
232+ " delete_file " : BlockingConfirmationStrategy(
214233 confirmation_policy = NeverAskPolicy(), confirmation_ui = SimpleConsoleUI()
215234 )
216235 }
217236)
218- agent = Agent(chat_generator = ... , tools = [... ], hooks = {" before_tool" : [hook]})
237+ agent = Agent(chat_generator = OpenAIChatGenerator() , tools = [delete_file ], hooks = {" before_tool" : [hook]})
219238```
220239
221240A key may be a single tool name, a tuple of tool names sharing one strategy, or the wildcard ` "*" ` which applies
@@ -652,6 +671,8 @@ This `after_tool` Agent hook writes the full result to the store so the next LLM
652671the full result. Register it on an ` Agent ` under the ` after_tool ` hook point. Which tools offload, and under what
653672condition, is controlled per tool by ` offload_strategies ` :
654673
674+ <!-- test-concept -->
675+
655676``` python
656677from haystack.components.agents import Agent
657678from haystack.components.generators.chat import OpenAIChatGenerator
0 commit comments