@@ -26,22 +26,67 @@ When you call an Agent without tools, it acts as a ChatGenerator, produces one r
2626from haystack.components.agents import Agent
2727from haystack.components.generators.chat import OpenAIChatGenerator
2828from haystack.dataclasses import ChatMessage
29- from haystack.tools.tool import Tool
30-
31- tools = [Tool(name = " calculator" , description = " ..." ), Tool(name = " search" , description = " ..." )]
32-
29+ from haystack.tools import Tool
30+
31+ # Tool functions - in practice, these would have real implementations
32+ def search (query : str ) -> str :
33+ ''' Search for information on the web.'''
34+ # Placeholder: would call actual search API
35+ return " In France, a 15% s ervice charge is typically included, but leaving 5-10% e xtra is appreciated."
36+
37+ def calculator (operation : str , a : float , b : float ) -> float :
38+ ''' Perform mathematical calculations.'''
39+ if operation == " multiply" :
40+ return a * b
41+ elif operation == " percentage" :
42+ return (a / 100 ) * b
43+ return 0
44+
45+ # Define tools with JSON Schema
46+ tools = [
47+ Tool(
48+ name = " search" ,
49+ description = " Searches for information on the web" ,
50+ parameters = {
51+ " type" : " object" ,
52+ " properties" : {
53+ " query" : {" type" : " string" , " description" : " The search query" }
54+ },
55+ " required" : [" query" ]
56+ },
57+ function = search
58+ ),
59+ Tool(
60+ name = " calculator" ,
61+ description = " Performs mathematical calculations" ,
62+ parameters = {
63+ " type" : " object" ,
64+ " properties" : {
65+ " operation" : {" type" : " string" , " description" : " Operation: multiply, percentage" },
66+ " a" : {" type" : " number" , " description" : " First number" },
67+ " b" : {" type" : " number" , " description" : " Second number" }
68+ },
69+ " required" : [" operation" , " a" , " b" ]
70+ },
71+ function = calculator
72+ )
73+ ]
74+
75+ # Create and run the agent
3376agent = Agent(
3477 chat_generator = OpenAIChatGenerator(),
35- tools = tools,
36- exit_conditions = [" search" ],
78+ tools = tools
3779)
3880
39- # Run the agent
4081result = agent.run(
41- messages = [ChatMessage.from_user(" Find information about Haystack " )]
82+ messages = [ChatMessage.from_user(" Calculate the appropriate tip for an €85 meal in France " )]
4283)
4384
44- assert " messages" in result # Contains conversation history
85+ # The agent will:
86+ # 1. Search for tipping customs in France
87+ # 2. Use calculator to compute tip based on findings
88+ # 3. Return the final answer with context
89+ print (result[" messages" ][- 1 ].text)
4590```
4691
4792<a id =" agent.Agent.__init__ " ></a >
0 commit comments