-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Expand file tree
/
Copy pathagent.py
More file actions
467 lines (402 loc) · 22 KB
/
Copy pathagent.py
File metadata and controls
467 lines (402 loc) · 22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
# SPDX-FileCopyrightText: 2022-present deepset GmbH <info@deepset.ai>
#
# SPDX-License-Identifier: Apache-2.0
import inspect
from typing import Any, Dict, List, Optional, Union
from haystack import component, default_from_dict, default_to_dict, logging, tracing
from haystack.components.generators.chat.types import ChatGenerator
from haystack.components.tools import ToolInvoker
from haystack.core.pipeline.async_pipeline import AsyncPipeline
from haystack.core.pipeline.pipeline import Pipeline
from haystack.core.pipeline.utils import _deepcopy_with_exceptions
from haystack.core.serialization import component_to_dict
from haystack.dataclasses import ChatMessage, ChatRole
from haystack.dataclasses.streaming_chunk import StreamingCallbackT, select_streaming_callback
from haystack.tools import Tool, Toolset, deserialize_tools_or_toolset_inplace, serialize_tools_or_toolset
from haystack.utils.callable_serialization import deserialize_callable, serialize_callable
from haystack.utils.deserialization import deserialize_chatgenerator_inplace
from .state.state import State, _schema_from_dict, _schema_to_dict, _validate_schema
from .state.state_utils import merge_lists
logger = logging.getLogger(__name__)
@component
class Agent:
"""
A Haystack component that implements a tool-using agent with provider-agnostic chat model support.
The component processes messages and executes tools until an exit_condition condition is met.
The exit_condition can be triggered either by a direct text response or by invoking a specific designated tool.
When you call an Agent without tools, it acts as a ChatGenerator, produces one response, then exits.
### Usage example
```python
from haystack.components.agents import Agent
from haystack.components.generators.chat import OpenAIChatGenerator
from haystack.dataclasses import ChatMessage
from haystack.tools.tool import Tool
tools = [Tool(name="calculator", description="..."), Tool(name="search", description="...")]
agent = Agent(
chat_generator=OpenAIChatGenerator(),
tools=tools,
exit_condition="search",
)
# Run the agent
result = agent.run(
messages=[ChatMessage.from_user("Find information about Haystack")]
)
assert "messages" in result # Contains conversation history
```
"""
def __init__(
self,
*,
chat_generator: ChatGenerator,
tools: Optional[Union[List[Tool], Toolset]] = None,
system_prompt: Optional[str] = None,
exit_conditions: Optional[List[str]] = None,
state_schema: Optional[Dict[str, Any]] = None,
max_agent_steps: int = 100,
streaming_callback: Optional[StreamingCallbackT] = None,
raise_on_tool_invocation_failure: bool = False,
tool_invoker_kwargs: Optional[Dict[str, Any]] = None,
) -> None:
"""
Initialize the agent component.
:param chat_generator: An instance of the chat generator that your agent should use. It must support tools.
:param tools: List of Tool objects or a Toolset that the agent can use.
:param system_prompt: System prompt for the agent.
:param exit_conditions: List of conditions that will cause the agent to return.
Can include "text" if the agent should return when it generates a message without tool calls,
or tool names that will cause the agent to return once the tool was executed. Defaults to ["text"].
:param state_schema: The schema for the runtime state used by the tools.
:param max_agent_steps: Maximum number of steps the agent will run before stopping. Defaults to 100.
If the agent exceeds this number of steps, it will stop and return the current state.
:param streaming_callback: A callback that will be invoked when a response is streamed from the LLM.
The same callback can be configured to emit tool results when a tool is called.
:param raise_on_tool_invocation_failure: Should the agent raise an exception when a tool invocation fails?
If set to False, the exception will be turned into a chat message and passed to the LLM.
:param tool_invoker_kwargs: Additional keyword arguments to pass to the ToolInvoker.
:raises TypeError: If the chat_generator does not support tools parameter in its run method.
:raises ValueError: If the exit_conditions are not valid.
"""
# Check if chat_generator supports tools parameter
chat_generator_run_method = inspect.signature(chat_generator.run)
if "tools" not in chat_generator_run_method.parameters:
raise TypeError(
f"{type(chat_generator).__name__} does not accept tools parameter in its run method. "
"The Agent component requires a chat generator that supports tools."
)
valid_exits = ["text"] + [tool.name for tool in tools or []]
if exit_conditions is None:
exit_conditions = ["text"]
if not all(condition in valid_exits for condition in exit_conditions):
raise ValueError(
f"Invalid exit conditions provided: {exit_conditions}. "
f"Valid exit conditions must be a subset of {valid_exits}. "
"Ensure that each exit condition corresponds to either 'text' or a valid tool name."
)
# Validate state schema if provided
if state_schema is not None:
_validate_schema(state_schema)
self._state_schema = state_schema or {}
# Initialize state schema
resolved_state_schema = _deepcopy_with_exceptions(self._state_schema)
if resolved_state_schema.get("messages") is None:
resolved_state_schema["messages"] = {"type": List[ChatMessage], "handler": merge_lists}
self.state_schema = resolved_state_schema
self.chat_generator = chat_generator
self.tools = tools or []
self.system_prompt = system_prompt
self.exit_conditions = exit_conditions
self.max_agent_steps = max_agent_steps
self.raise_on_tool_invocation_failure = raise_on_tool_invocation_failure
self.streaming_callback = streaming_callback
output_types = {"last_message": ChatMessage}
for param, config in self.state_schema.items():
output_types[param] = config["type"]
# Skip setting input types for parameters that are already in the run method
if param in ["messages", "streaming_callback"]:
continue
component.set_input_type(self, name=param, type=config["type"], default=None)
component.set_output_types(self, **output_types)
self.tool_invoker_kwargs = tool_invoker_kwargs
self._tool_invoker = None
if self.tools:
resolved_tool_invoker_kwargs = {
"tools": self.tools,
"raise_on_failure": self.raise_on_tool_invocation_failure,
**(tool_invoker_kwargs or {}),
}
self._tool_invoker = ToolInvoker(**resolved_tool_invoker_kwargs)
else:
logger.warning(
"No tools provided to the Agent. The Agent will behave like a ChatGenerator and only return text "
"responses. To enable tool usage, pass tools directly to the Agent, not to the chat_generator."
)
self._is_warmed_up = False
def warm_up(self) -> None:
"""
Warm up the Agent.
"""
if not self._is_warmed_up:
if hasattr(self.chat_generator, "warm_up"):
self.chat_generator.warm_up()
self._is_warmed_up = True
def to_dict(self) -> Dict[str, Any]:
"""
Serialize the component to a dictionary.
:return: Dictionary with serialized data
"""
if self.streaming_callback is not None:
streaming_callback = serialize_callable(self.streaming_callback)
else:
streaming_callback = None
return default_to_dict(
self,
chat_generator=component_to_dict(obj=self.chat_generator, name="chat_generator"),
tools=serialize_tools_or_toolset(self.tools),
system_prompt=self.system_prompt,
exit_conditions=self.exit_conditions,
# We serialize the original state schema, not the resolved one to reflect the original user input
state_schema=_schema_to_dict(self._state_schema),
max_agent_steps=self.max_agent_steps,
streaming_callback=streaming_callback,
raise_on_tool_invocation_failure=self.raise_on_tool_invocation_failure,
tool_invoker_kwargs=self.tool_invoker_kwargs,
)
@classmethod
def from_dict(cls, data: Dict[str, Any]) -> "Agent":
"""
Deserialize the agent from a dictionary.
:param data: Dictionary to deserialize from
:return: Deserialized agent
"""
init_params = data.get("init_parameters", {})
deserialize_chatgenerator_inplace(init_params, key="chat_generator")
if "state_schema" in init_params:
init_params["state_schema"] = _schema_from_dict(init_params["state_schema"])
if init_params.get("streaming_callback") is not None:
init_params["streaming_callback"] = deserialize_callable(init_params["streaming_callback"])
deserialize_tools_or_toolset_inplace(init_params, key="tools")
return default_from_dict(cls, data)
def _prepare_generator_inputs(self, streaming_callback: Optional[StreamingCallbackT] = None) -> Dict[str, Any]:
"""Prepare inputs for the chat generator."""
generator_inputs: Dict[str, Any] = {"tools": self.tools}
if streaming_callback is not None:
generator_inputs["streaming_callback"] = streaming_callback
return generator_inputs
def _create_agent_span(self) -> Any:
"""Create a span for the agent run."""
return tracing.tracer.trace(
"haystack.agent.run",
tags={
"haystack.agent.max_steps": self.max_agent_steps,
"haystack.agent.tools": self.tools,
"haystack.agent.exit_conditions": self.exit_conditions,
"haystack.agent.state_schema": _schema_to_dict(self.state_schema),
},
)
def run(
self, messages: List[ChatMessage], streaming_callback: Optional[StreamingCallbackT] = None, **kwargs: Any
) -> Dict[str, Any]:
"""
Process messages and execute tools until an exit condition is met.
:param messages: List of Haystack ChatMessage objects to process.
If a list of dictionaries is provided, each dictionary will be converted to a ChatMessage object.
:param streaming_callback: A callback that will be invoked when a response is streamed from the LLM.
The same callback can be configured to emit tool results when a tool is called.
:param kwargs: Additional data to pass to the State schema used by the Agent.
The keys must match the schema defined in the Agent's `state_schema`.
:returns:
A dictionary with the following keys:
- "messages": List of all messages exchanged during the agent's run.
- "last_message": The last message exchanged during the agent's run.
- Any additional keys defined in the `state_schema`.
:raises RuntimeError: If the Agent component wasn't warmed up before calling `run()`.
"""
if not self._is_warmed_up and hasattr(self.chat_generator, "warm_up"):
raise RuntimeError("The component Agent wasn't warmed up. Run 'warm_up()' before calling 'run()'.")
if self.system_prompt is not None:
messages = [ChatMessage.from_system(self.system_prompt)] + messages
if all(m.is_from(ChatRole.SYSTEM) for m in messages):
logger.warning(
"All messages provided to the Agent component are system messages. This is not recommended as the "
"Agent will not perform any actions specific to user input. Consider adding user messages to the input."
)
state = State(schema=self.state_schema, data=kwargs)
state.set("messages", messages)
component_visits = dict.fromkeys(["chat_generator", "tool_invoker"], 0)
streaming_callback = select_streaming_callback(
init_callback=self.streaming_callback, runtime_callback=streaming_callback, requires_async=False
)
generator_inputs = self._prepare_generator_inputs(streaming_callback=streaming_callback)
with self._create_agent_span() as span:
span.set_content_tag(
"haystack.agent.input",
_deepcopy_with_exceptions({"messages": messages, "streaming_callback": streaming_callback, **kwargs}),
)
counter = 0
while counter < self.max_agent_steps:
# 1. Call the ChatGenerator
result = Pipeline._run_component(
component_name="chat_generator",
component={"instance": self.chat_generator},
inputs={"messages": messages, **generator_inputs},
component_visits=component_visits,
parent_span=span,
)
llm_messages = result["replies"]
state.set("messages", llm_messages)
# 2. Check if any of the LLM responses contain a tool call or if the LLM is not using tools
if not any(msg.tool_call for msg in llm_messages) or self._tool_invoker is None:
counter += 1
break
# 3. Call the ToolInvoker
# We only send the messages from the LLM to the tool invoker
tool_invoker_result = Pipeline._run_component(
component_name="tool_invoker",
component={"instance": self._tool_invoker},
inputs={"messages": llm_messages, "state": state, "streaming_callback": streaming_callback},
component_visits=component_visits,
parent_span=span,
)
tool_messages = tool_invoker_result["tool_messages"]
state = tool_invoker_result["state"]
state.set("messages", tool_messages)
# 4. Check if any LLM message's tool call name matches an exit condition
if self.exit_conditions != ["text"] and self._check_exit_conditions(llm_messages, tool_messages):
counter += 1
break
# 5. Fetch the combined messages and send them back to the LLM
messages = state.get("messages")
counter += 1
if counter >= self.max_agent_steps:
logger.warning(
"Agent reached maximum agent steps of {max_agent_steps}, stopping.",
max_agent_steps=self.max_agent_steps,
)
span.set_content_tag("haystack.agent.output", state.data)
span.set_tag("haystack.agent.steps_taken", counter)
result = {**state.data}
all_messages = state.get("messages")
if all_messages:
result.update({"last_message": all_messages[-1]})
return result
async def run_async(
self, messages: List[ChatMessage], streaming_callback: Optional[StreamingCallbackT] = None, **kwargs: Any
) -> Dict[str, Any]:
"""
Asynchronously process messages and execute tools until the exit condition is met.
This is the asynchronous version of the `run` method. It follows the same logic but uses
asynchronous operations where possible, such as calling the `run_async` method of the ChatGenerator
if available.
:param messages: List of chat messages to process
:param streaming_callback: An asynchronous callback that will be invoked when a response
is streamed from the LLM. The same callback can be configured to emit tool results
when a tool is called.
:param kwargs: Additional data to pass to the State schema used by the Agent.
The keys must match the schema defined in the Agent's `state_schema`.
:returns:
A dictionary with the following keys:
- "messages": List of all messages exchanged during the agent's run.
- "last_message": The last message exchanged during the agent's run.
- Any additional keys defined in the `state_schema`.
:raises RuntimeError: If the Agent component wasn't warmed up before calling `run_async()`.
"""
if not self._is_warmed_up and hasattr(self.chat_generator, "warm_up"):
raise RuntimeError("The component Agent wasn't warmed up. Run 'warm_up()' before calling 'run_async()'.")
if self.system_prompt is not None:
messages = [ChatMessage.from_system(self.system_prompt)] + messages
if all(m.is_from(ChatRole.SYSTEM) for m in messages):
logger.warning(
"All messages provided to the Agent component are system messages. This is not recommended as the "
"Agent will not perform any actions specific to user input. Consider adding user messages to the input."
)
state = State(schema=self.state_schema, data=kwargs)
state.set("messages", messages)
component_visits = dict.fromkeys(["chat_generator", "tool_invoker"], 0)
streaming_callback = select_streaming_callback(
init_callback=self.streaming_callback, runtime_callback=streaming_callback, requires_async=True
)
generator_inputs = self._prepare_generator_inputs(streaming_callback=streaming_callback)
with self._create_agent_span() as span:
span.set_content_tag(
"haystack.agent.input",
_deepcopy_with_exceptions({"messages": messages, "streaming_callback": streaming_callback, **kwargs}),
)
counter = 0
while counter < self.max_agent_steps:
# 1. Call the ChatGenerator
result = await AsyncPipeline._run_component_async(
component_name="chat_generator",
component={"instance": self.chat_generator},
component_inputs={"messages": messages, **generator_inputs},
component_visits=component_visits,
max_runs_per_component=self.max_agent_steps,
parent_span=span,
)
llm_messages = result["replies"]
state.set("messages", llm_messages)
# 2. Check if any of the LLM responses contain a tool call or if the LLM is not using tools
if not any(msg.tool_call for msg in llm_messages) or self._tool_invoker is None:
counter += 1
break
# 3. Call the ToolInvoker
# We only send the messages from the LLM to the tool invoker
# Check if the ToolInvoker supports async execution. Currently, it doesn't.
tool_invoker_result = await AsyncPipeline._run_component_async(
component_name="tool_invoker",
component={"instance": self._tool_invoker},
component_inputs={
"messages": llm_messages,
"state": state,
"streaming_callback": streaming_callback,
},
component_visits=component_visits,
max_runs_per_component=self.max_agent_steps,
parent_span=span,
)
tool_messages = tool_invoker_result["tool_messages"]
state = tool_invoker_result["state"]
state.set("messages", tool_messages)
# 4. Check if any LLM message's tool call name matches an exit condition
if self.exit_conditions != ["text"] and self._check_exit_conditions(llm_messages, tool_messages):
counter += 1
break
# 5. Fetch the combined messages and send them back to the LLM
messages = state.get("messages")
counter += 1
if counter >= self.max_agent_steps:
logger.warning(
"Agent reached maximum agent steps of {max_agent_steps}, stopping.",
max_agent_steps=self.max_agent_steps,
)
span.set_content_tag("haystack.agent.output", state.data)
span.set_tag("haystack.agent.steps_taken", counter)
result = {**state.data}
all_messages = state.get("messages")
if all_messages:
result.update({"last_message": all_messages[-1]})
return result
def _check_exit_conditions(self, llm_messages: List[ChatMessage], tool_messages: List[ChatMessage]) -> bool:
"""
Check if any of the LLM messages' tool calls match an exit condition and if there are no errors.
:param llm_messages: List of messages from the LLM
:param tool_messages: List of messages from the tool invoker
:return: True if an exit condition is met and there are no errors, False otherwise
"""
matched_exit_conditions = set()
has_errors = False
for msg in llm_messages:
if msg.tool_call and msg.tool_call.tool_name in self.exit_conditions:
matched_exit_conditions.add(msg.tool_call.tool_name)
# Check if any error is specifically from the tool matching the exit condition
tool_errors = [
tool_msg.tool_call_result.error
for tool_msg in tool_messages
if tool_msg.tool_call_result is not None
and tool_msg.tool_call_result.origin.tool_name == msg.tool_call.tool_name
]
if any(tool_errors):
has_errors = True
# No need to check further if we found an error
break
# Only return True if at least one exit condition was matched AND none had errors
return bool(matched_exit_conditions) and not has_errors