haystack-experimental 0.13.0__py3-none-any.whl → 0.14.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (25) hide show
  1. haystack_experimental/components/agents/__init__.py +16 -0
  2. haystack_experimental/components/agents/agent.py +633 -0
  3. haystack_experimental/components/agents/human_in_the_loop/__init__.py +35 -0
  4. haystack_experimental/components/agents/human_in_the_loop/breakpoint.py +63 -0
  5. haystack_experimental/components/agents/human_in_the_loop/dataclasses.py +72 -0
  6. haystack_experimental/components/agents/human_in_the_loop/errors.py +28 -0
  7. haystack_experimental/components/agents/human_in_the_loop/policies.py +78 -0
  8. haystack_experimental/components/agents/human_in_the_loop/strategies.py +455 -0
  9. haystack_experimental/components/agents/human_in_the_loop/types.py +89 -0
  10. haystack_experimental/components/agents/human_in_the_loop/user_interfaces.py +209 -0
  11. haystack_experimental/components/generators/chat/openai.py +8 -10
  12. haystack_experimental/components/preprocessors/embedding_based_document_splitter.py +18 -6
  13. haystack_experimental/components/preprocessors/md_header_level_inferrer.py +146 -0
  14. haystack_experimental/components/summarizers/__init__.py +7 -0
  15. haystack_experimental/components/summarizers/llm_summarizer.py +317 -0
  16. haystack_experimental/core/__init__.py +3 -0
  17. haystack_experimental/core/pipeline/__init__.py +3 -0
  18. haystack_experimental/core/pipeline/breakpoint.py +119 -0
  19. haystack_experimental/dataclasses/__init__.py +3 -0
  20. haystack_experimental/dataclasses/breakpoints.py +53 -0
  21. {haystack_experimental-0.13.0.dist-info → haystack_experimental-0.14.1.dist-info}/METADATA +29 -14
  22. {haystack_experimental-0.13.0.dist-info → haystack_experimental-0.14.1.dist-info}/RECORD +25 -7
  23. {haystack_experimental-0.13.0.dist-info → haystack_experimental-0.14.1.dist-info}/WHEEL +0 -0
  24. {haystack_experimental-0.13.0.dist-info → haystack_experimental-0.14.1.dist-info}/licenses/LICENSE +0 -0
  25. {haystack_experimental-0.13.0.dist-info → haystack_experimental-0.14.1.dist-info}/licenses/LICENSE-MIT.txt +0 -0
@@ -0,0 +1,16 @@
1
+ # SPDX-FileCopyrightText: 2022-present deepset GmbH <info@deepset.ai>
2
+ #
3
+ # SPDX-License-Identifier: Apache-2.0
4
+
5
+ import sys
6
+ from typing import TYPE_CHECKING
7
+
8
+ from lazy_imports import LazyImporter
9
+
10
+ _import_structure = {"agent": ["Agent"]}
11
+
12
+ if TYPE_CHECKING:
13
+ from .agent import Agent as Agent
14
+
15
+ else:
16
+ sys.modules[__name__] = LazyImporter(name=__name__, module_file=__file__, import_structure=_import_structure)
@@ -0,0 +1,633 @@
1
+ # SPDX-FileCopyrightText: 2022-present deepset GmbH <info@deepset.ai>
2
+ #
3
+ # SPDX-License-Identifier: Apache-2.0
4
+
5
+ # pylint: disable=wrong-import-order,wrong-import-position,ungrouped-imports
6
+ # ruff: noqa: I001
7
+
8
+ from dataclasses import dataclass
9
+ from typing import Any, Optional, Union
10
+
11
+ # Monkey patch Haystack's AgentSnapshot with our extended version
12
+ import haystack.dataclasses.breakpoints as hdb
13
+ from haystack_experimental.dataclasses.breakpoints import AgentSnapshot
14
+
15
+ hdb.AgentSnapshot = AgentSnapshot # type: ignore[misc]
16
+
17
+ # Monkey patch Haystack's breakpoint functions with our extended versions
18
+ import haystack.core.pipeline.breakpoint as hs_breakpoint
19
+ import haystack_experimental.core.pipeline.breakpoint as exp_breakpoint
20
+
21
+ hs_breakpoint._create_agent_snapshot = exp_breakpoint._create_agent_snapshot
22
+ hs_breakpoint._create_pipeline_snapshot_from_tool_invoker = exp_breakpoint._create_pipeline_snapshot_from_tool_invoker # type: ignore[assignment]
23
+
24
+ from haystack import logging
25
+ from haystack.components.agents.agent import Agent as HaystackAgent
26
+ from haystack.components.agents.agent import _ExecutionContext as Haystack_ExecutionContext
27
+ from haystack.components.agents.agent import _schema_from_dict
28
+ from haystack.components.agents.state import replace_values
29
+ from haystack.components.generators.chat.types import ChatGenerator
30
+ from haystack.core.errors import PipelineRuntimeError
31
+ from haystack.core.pipeline import AsyncPipeline, Pipeline
32
+ from haystack.core.pipeline.breakpoint import (
33
+ _create_pipeline_snapshot_from_chat_generator,
34
+ _create_pipeline_snapshot_from_tool_invoker,
35
+ )
36
+ from haystack.core.pipeline.utils import _deepcopy_with_exceptions
37
+ from haystack.core.serialization import default_from_dict, import_class_by_name
38
+ from haystack.dataclasses import ChatMessage
39
+ from haystack.dataclasses.breakpoints import AgentBreakpoint, ToolBreakpoint
40
+ from haystack.dataclasses.streaming_chunk import StreamingCallbackT
41
+ from haystack.tools import ToolsType, deserialize_tools_or_toolset_inplace
42
+ from haystack.utils.callable_serialization import deserialize_callable
43
+ from haystack.utils.deserialization import deserialize_chatgenerator_inplace
44
+
45
+ from haystack_experimental.components.agents.human_in_the_loop import (
46
+ ConfirmationStrategy,
47
+ ToolExecutionDecision,
48
+ HITLBreakpointException,
49
+ )
50
+ from haystack_experimental.components.agents.human_in_the_loop.strategies import _process_confirmation_strategies
51
+
52
+ logger = logging.getLogger(__name__)
53
+
54
+
55
+ @dataclass
56
+ class _ExecutionContext(Haystack_ExecutionContext):
57
+ """
58
+ Execution context for the Agent component
59
+
60
+ Extends Haystack's _ExecutionContext to include tool execution decisions for human-in-the-loop strategies.
61
+
62
+ :param tool_execution_decisions: Optional list of ToolExecutionDecision objects to use instead of prompting
63
+ the user. This is useful when restarting from a snapshot where tool execution decisions were already made.
64
+ """
65
+
66
+ tool_execution_decisions: Optional[list[ToolExecutionDecision]] = None
67
+
68
+
69
+ class Agent(HaystackAgent):
70
+ """
71
+ A Haystack component that implements a tool-using agent with provider-agnostic chat model support.
72
+
73
+ NOTE: This class extends Haystack's Agent component to add support for human-in-the-loop confirmation strategies.
74
+
75
+ The component processes messages and executes tools until an exit condition is met.
76
+ The exit condition can be triggered either by a direct text response or by invoking a specific designated tool.
77
+ Multiple exit conditions can be specified.
78
+
79
+ When you call an Agent without tools, it acts as a ChatGenerator, produces one response, then exits.
80
+
81
+ ### Usage example
82
+ ```python
83
+ from haystack.components.generators.chat import OpenAIChatGenerator
84
+ from haystack.dataclasses import ChatMessage
85
+ from haystack.tools.tool import Tool
86
+
87
+ from haystack_experimental.components.agents import Agent
88
+ from haystack_experimental.components.agents.human_in_the_loop import (
89
+ HumanInTheLoopStrategy,
90
+ AlwaysAskPolicy,
91
+ NeverAskPolicy,
92
+ SimpleConsoleUI,
93
+ )
94
+
95
+ calculator_tool = Tool(name="calculator", description="A tool for performing mathematical calculations.", ...)
96
+ search_tool = Tool(name="search", description="A tool for searching the web.", ...)
97
+
98
+ agent = Agent(
99
+ chat_generator=OpenAIChatGenerator(),
100
+ tools=[calculator_tool, search_tool],
101
+ confirmation_strategies={
102
+ calculator_tool.name: HumanInTheLoopStrategy(
103
+ confirmation_policy=NeverAskPolicy(), confirmation_ui=SimpleConsoleUI()
104
+ ),
105
+ search_tool.name: HumanInTheLoopStrategy(
106
+ confirmation_policy=AlwaysAskPolicy(), confirmation_ui=SimpleConsoleUI()
107
+ ),
108
+ },
109
+ )
110
+
111
+ # Run the agent
112
+ result = agent.run(
113
+ messages=[ChatMessage.from_user("Find information about Haystack")]
114
+ )
115
+
116
+ assert "messages" in result # Contains conversation history
117
+ ```
118
+ """
119
+
120
+ def __init__(
121
+ self,
122
+ *,
123
+ chat_generator: ChatGenerator,
124
+ tools: Optional[ToolsType] = None,
125
+ system_prompt: Optional[str] = None,
126
+ exit_conditions: Optional[list[str]] = None,
127
+ state_schema: Optional[dict[str, Any]] = None,
128
+ max_agent_steps: int = 100,
129
+ streaming_callback: Optional[StreamingCallbackT] = None,
130
+ raise_on_tool_invocation_failure: bool = False,
131
+ confirmation_strategies: Optional[dict[str, ConfirmationStrategy]] = None,
132
+ tool_invoker_kwargs: Optional[dict[str, Any]] = None,
133
+ ) -> None:
134
+ """
135
+ Initialize the agent component.
136
+
137
+ :param chat_generator: An instance of the chat generator that your agent should use. It must support tools.
138
+ :param tools: List of Tool objects or a Toolset that the agent can use.
139
+ :param system_prompt: System prompt for the agent.
140
+ :param exit_conditions: List of conditions that will cause the agent to return.
141
+ Can include "text" if the agent should return when it generates a message without tool calls,
142
+ or tool names that will cause the agent to return once the tool was executed. Defaults to ["text"].
143
+ :param state_schema: The schema for the runtime state used by the tools.
144
+ :param max_agent_steps: Maximum number of steps the agent will run before stopping. Defaults to 100.
145
+ If the agent exceeds this number of steps, it will stop and return the current state.
146
+ :param streaming_callback: A callback that will be invoked when a response is streamed from the LLM.
147
+ The same callback can be configured to emit tool results when a tool is called.
148
+ :param raise_on_tool_invocation_failure: Should the agent raise an exception when a tool invocation fails?
149
+ If set to False, the exception will be turned into a chat message and passed to the LLM.
150
+ :param tool_invoker_kwargs: Additional keyword arguments to pass to the ToolInvoker.
151
+ :raises TypeError: If the chat_generator does not support tools parameter in its run method.
152
+ :raises ValueError: If the exit_conditions are not valid.
153
+ """
154
+ super(Agent, self).__init__(
155
+ chat_generator=chat_generator,
156
+ tools=tools,
157
+ system_prompt=system_prompt,
158
+ exit_conditions=exit_conditions,
159
+ state_schema=state_schema,
160
+ max_agent_steps=max_agent_steps,
161
+ streaming_callback=streaming_callback,
162
+ raise_on_tool_invocation_failure=raise_on_tool_invocation_failure,
163
+ tool_invoker_kwargs=tool_invoker_kwargs,
164
+ )
165
+ self._confirmation_strategies = confirmation_strategies or {}
166
+
167
+ def _initialize_fresh_execution(
168
+ self,
169
+ messages: list[ChatMessage],
170
+ streaming_callback: Optional[StreamingCallbackT],
171
+ requires_async: bool,
172
+ *,
173
+ system_prompt: Optional[str] = None,
174
+ tools: Optional[Union[ToolsType, list[str]]] = None,
175
+ **kwargs: dict[str, Any],
176
+ ) -> _ExecutionContext:
177
+ """
178
+ Initialize execution context for a fresh run of the agent.
179
+
180
+ :param messages: List of ChatMessage objects to start the agent with.
181
+ :param streaming_callback: Optional callback for streaming responses.
182
+ :param requires_async: Whether the agent run requires asynchronous execution.
183
+ :param system_prompt: System prompt for the agent. If provided, it overrides the default system prompt.
184
+ :param tools: Optional list of Tool objects, a Toolset, or list of tool names to use for this run.
185
+ When passing tool names, tools are selected from the Agent's originally configured tools.
186
+ :param kwargs: Additional data to pass to the State used by the Agent.
187
+ """
188
+ exe_context = super(Agent, self)._initialize_fresh_execution(
189
+ messages=messages,
190
+ streaming_callback=streaming_callback,
191
+ requires_async=requires_async,
192
+ system_prompt=system_prompt,
193
+ tools=tools,
194
+ **kwargs,
195
+ )
196
+ # NOTE: 1st difference with parent method to add this to tool_invoker_inputs
197
+ if self._tool_invoker:
198
+ exe_context.tool_invoker_inputs["enable_streaming_callback_passthrough"] = (
199
+ self._tool_invoker.enable_streaming_callback_passthrough
200
+ )
201
+ # NOTE: 2nd difference is to use the extended _ExecutionContext
202
+ return _ExecutionContext(
203
+ state=exe_context.state,
204
+ component_visits=exe_context.component_visits,
205
+ chat_generator_inputs=exe_context.chat_generator_inputs,
206
+ tool_invoker_inputs=exe_context.tool_invoker_inputs,
207
+ )
208
+
209
+ def _initialize_from_snapshot( # type: ignore[override]
210
+ self,
211
+ snapshot: AgentSnapshot,
212
+ streaming_callback: Optional[StreamingCallbackT],
213
+ requires_async: bool,
214
+ *,
215
+ tools: Optional[Union[ToolsType, list[str]]] = None,
216
+ ) -> _ExecutionContext:
217
+ """
218
+ Initialize execution context from an AgentSnapshot.
219
+
220
+ :param snapshot: An AgentSnapshot containing the state of a previously saved agent execution.
221
+ :param streaming_callback: Optional callback for streaming responses.
222
+ :param requires_async: Whether the agent run requires asynchronous execution.
223
+ :param tools: Optional list of Tool objects, a Toolset, or list of tool names to use for this run.
224
+ When passing tool names, tools are selected from the Agent's originally configured tools.
225
+ """
226
+ exe_context = super(Agent, self)._initialize_from_snapshot(
227
+ snapshot=snapshot, streaming_callback=streaming_callback, requires_async=requires_async, tools=tools
228
+ )
229
+ # NOTE: 1st difference with parent method to add this to tool_invoker_inputs
230
+ if self._tool_invoker:
231
+ exe_context.tool_invoker_inputs["enable_streaming_callback_passthrough"] = (
232
+ self._tool_invoker.enable_streaming_callback_passthrough
233
+ )
234
+ # NOTE: 2nd difference is to use the extended _ExecutionContext and add tool_execution_decisions
235
+ return _ExecutionContext(
236
+ state=exe_context.state,
237
+ component_visits=exe_context.component_visits,
238
+ chat_generator_inputs=exe_context.chat_generator_inputs,
239
+ tool_invoker_inputs=exe_context.tool_invoker_inputs,
240
+ counter=exe_context.counter,
241
+ skip_chat_generator=exe_context.skip_chat_generator,
242
+ tool_execution_decisions=snapshot.tool_execution_decisions,
243
+ )
244
+
245
+ def run( # noqa: PLR0915
246
+ self,
247
+ messages: list[ChatMessage],
248
+ streaming_callback: Optional[StreamingCallbackT] = None,
249
+ *,
250
+ break_point: Optional[AgentBreakpoint] = None,
251
+ snapshot: Optional[AgentSnapshot] = None, # type: ignore[override]
252
+ system_prompt: Optional[str] = None,
253
+ tools: Optional[Union[ToolsType, list[str]]] = None,
254
+ **kwargs: Any,
255
+ ) -> dict[str, Any]:
256
+ """
257
+ Process messages and execute tools until an exit condition is met.
258
+
259
+ :param messages: List of Haystack ChatMessage objects to process.
260
+ :param streaming_callback: A callback that will be invoked when a response is streamed from the LLM.
261
+ The same callback can be configured to emit tool results when a tool is called.
262
+ :param break_point: An AgentBreakpoint, can be a Breakpoint for the "chat_generator" or a ToolBreakpoint
263
+ for "tool_invoker".
264
+ :param snapshot: A dictionary containing a snapshot of a previously saved agent execution. The snapshot contains
265
+ the relevant information to restart the Agent execution from where it left off.
266
+ :param system_prompt: System prompt for the agent. If provided, it overrides the default system prompt.
267
+ :param tools: Optional list of Tool objects, a Toolset, or list of tool names to use for this run.
268
+ When passing tool names, tools are selected from the Agent's originally configured tools.
269
+ :param kwargs: Additional data to pass to the State schema used by the Agent.
270
+ The keys must match the schema defined in the Agent's `state_schema`.
271
+ :returns:
272
+ A dictionary with the following keys:
273
+ - "messages": List of all messages exchanged during the agent's run.
274
+ - "last_message": The last message exchanged during the agent's run.
275
+ - Any additional keys defined in the `state_schema`.
276
+ :raises RuntimeError: If the Agent component wasn't warmed up before calling `run()`.
277
+ :raises BreakpointException: If an agent breakpoint is triggered.
278
+ """
279
+ # We pop parent_snapshot from kwargs to avoid passing it into State.
280
+ parent_snapshot = kwargs.pop("parent_snapshot", None)
281
+ agent_inputs = {
282
+ "messages": messages,
283
+ "streaming_callback": streaming_callback,
284
+ "break_point": break_point,
285
+ "snapshot": snapshot,
286
+ **kwargs,
287
+ }
288
+ self._runtime_checks(break_point=break_point, snapshot=snapshot)
289
+
290
+ if snapshot:
291
+ exe_context = self._initialize_from_snapshot(
292
+ snapshot=snapshot, streaming_callback=streaming_callback, requires_async=False, tools=tools
293
+ )
294
+ else:
295
+ exe_context = self._initialize_fresh_execution(
296
+ messages=messages,
297
+ streaming_callback=streaming_callback,
298
+ requires_async=False,
299
+ system_prompt=system_prompt,
300
+ tools=tools,
301
+ **kwargs,
302
+ )
303
+
304
+ with self._create_agent_span() as span:
305
+ span.set_content_tag("haystack.agent.input", _deepcopy_with_exceptions(agent_inputs))
306
+
307
+ while exe_context.counter < self.max_agent_steps:
308
+ # Handle breakpoint and ChatGenerator call
309
+ Agent._check_chat_generator_breakpoint(
310
+ execution_context=exe_context, break_point=break_point, parent_snapshot=parent_snapshot
311
+ )
312
+ # We skip the chat generator when restarting from a snapshot from a ToolBreakpoint
313
+ if exe_context.skip_chat_generator:
314
+ llm_messages = exe_context.state.get("messages", [])[-1:]
315
+ # Set to False so the next iteration will call the chat generator
316
+ exe_context.skip_chat_generator = False
317
+ else:
318
+ try:
319
+ result = Pipeline._run_component(
320
+ component_name="chat_generator",
321
+ component={"instance": self.chat_generator},
322
+ inputs={
323
+ "messages": exe_context.state.data["messages"],
324
+ **exe_context.chat_generator_inputs,
325
+ },
326
+ component_visits=exe_context.component_visits,
327
+ parent_span=span,
328
+ )
329
+ except PipelineRuntimeError as e:
330
+ pipeline_snapshot = _create_pipeline_snapshot_from_chat_generator(
331
+ agent_name=getattr(self, "__component_name__", None),
332
+ execution_context=exe_context,
333
+ parent_snapshot=parent_snapshot,
334
+ )
335
+ e.pipeline_snapshot = pipeline_snapshot
336
+ raise e
337
+
338
+ llm_messages = result["replies"]
339
+ exe_context.state.set("messages", llm_messages)
340
+
341
+ # Check if any of the LLM responses contain a tool call or if the LLM is not using tools
342
+ if not any(msg.tool_call for msg in llm_messages) or self._tool_invoker is None:
343
+ exe_context.counter += 1
344
+ break
345
+
346
+ # Apply confirmation strategies and update State and messages sent to ToolInvoker
347
+ try:
348
+ # Run confirmation strategies to get updated tool call messages and modified chat history
349
+ modified_tool_call_messages, new_chat_history = _process_confirmation_strategies(
350
+ confirmation_strategies=self._confirmation_strategies,
351
+ messages_with_tool_calls=llm_messages,
352
+ execution_context=exe_context,
353
+ )
354
+ # Replace the chat history in state with the modified one
355
+ exe_context.state.set(key="messages", value=new_chat_history, handler_override=replace_values)
356
+ except HITLBreakpointException as tbp_error:
357
+ # We create a break_point to pass into _check_tool_invoker_breakpoint
358
+ break_point = AgentBreakpoint(
359
+ agent_name=getattr(self, "__component_name__", ""),
360
+ break_point=ToolBreakpoint(
361
+ component_name="tool_invoker",
362
+ tool_name=tbp_error.tool_name,
363
+ visit_count=exe_context.component_visits["tool_invoker"],
364
+ snapshot_file_path=tbp_error.snapshot_file_path,
365
+ ),
366
+ )
367
+
368
+ # Handle breakpoint
369
+ Agent._check_tool_invoker_breakpoint(
370
+ execution_context=exe_context, break_point=break_point, parent_snapshot=parent_snapshot
371
+ )
372
+
373
+ # Run ToolInvoker
374
+ try:
375
+ # We only send the messages from the LLM to the tool invoker
376
+ tool_invoker_result = Pipeline._run_component(
377
+ component_name="tool_invoker",
378
+ component={"instance": self._tool_invoker},
379
+ inputs={
380
+ "messages": modified_tool_call_messages,
381
+ "state": exe_context.state,
382
+ **exe_context.tool_invoker_inputs,
383
+ },
384
+ component_visits=exe_context.component_visits,
385
+ parent_span=span,
386
+ )
387
+ except PipelineRuntimeError as e:
388
+ # Access the original Tool Invoker exception
389
+ original_error = e.__cause__
390
+ tool_name = getattr(original_error, "tool_name", None)
391
+
392
+ pipeline_snapshot = _create_pipeline_snapshot_from_tool_invoker(
393
+ tool_name=tool_name,
394
+ agent_name=getattr(self, "__component_name__", None),
395
+ execution_context=exe_context,
396
+ parent_snapshot=parent_snapshot,
397
+ )
398
+ e.pipeline_snapshot = pipeline_snapshot
399
+ raise e
400
+
401
+ # Set execution context tool execution decisions to empty after applying them b/c they should only
402
+ # be used once for the current tool calls
403
+ exe_context.tool_execution_decisions = None
404
+ tool_messages = tool_invoker_result["tool_messages"]
405
+ exe_context.state = tool_invoker_result["state"]
406
+ exe_context.state.set("messages", tool_messages)
407
+
408
+ # Check if any LLM message's tool call name matches an exit condition
409
+ if self.exit_conditions != ["text"] and self._check_exit_conditions(llm_messages, tool_messages):
410
+ exe_context.counter += 1
411
+ break
412
+
413
+ # Increment the step counter
414
+ exe_context.counter += 1
415
+
416
+ if exe_context.counter >= self.max_agent_steps:
417
+ logger.warning(
418
+ "Agent reached maximum agent steps of {max_agent_steps}, stopping.",
419
+ max_agent_steps=self.max_agent_steps,
420
+ )
421
+ span.set_content_tag("haystack.agent.output", exe_context.state.data)
422
+ span.set_tag("haystack.agent.steps_taken", exe_context.counter)
423
+
424
+ result = {**exe_context.state.data}
425
+ if msgs := result.get("messages"):
426
+ result["last_message"] = msgs[-1]
427
+ return result
428
+
429
+ async def run_async(
430
+ self,
431
+ messages: list[ChatMessage],
432
+ streaming_callback: Optional[StreamingCallbackT] = None,
433
+ *,
434
+ break_point: Optional[AgentBreakpoint] = None,
435
+ snapshot: Optional[AgentSnapshot] = None, # type: ignore[override]
436
+ system_prompt: Optional[str] = None,
437
+ tools: Optional[Union[ToolsType, list[str]]] = None,
438
+ **kwargs: Any,
439
+ ) -> dict[str, Any]:
440
+ """
441
+ Asynchronously process messages and execute tools until the exit condition is met.
442
+
443
+ This is the asynchronous version of the `run` method. It follows the same logic but uses
444
+ asynchronous operations where possible, such as calling the `run_async` method of the ChatGenerator
445
+ if available.
446
+
447
+ :param messages: List of Haystack ChatMessage objects to process.
448
+ :param streaming_callback: An asynchronous callback that will be invoked when a response is streamed from the
449
+ LLM. The same callback can be configured to emit tool results when a tool is called.
450
+ :param break_point: An AgentBreakpoint, can be a Breakpoint for the "chat_generator" or a ToolBreakpoint
451
+ for "tool_invoker".
452
+ :param snapshot: A dictionary containing a snapshot of a previously saved agent execution. The snapshot contains
453
+ the relevant information to restart the Agent execution from where it left off.
454
+ :param system_prompt: System prompt for the agent. If provided, it overrides the default system prompt.
455
+ :param tools: Optional list of Tool objects, a Toolset, or list of tool names to use for this run.
456
+ :param kwargs: Additional data to pass to the State schema used by the Agent.
457
+ The keys must match the schema defined in the Agent's `state_schema`.
458
+ :returns:
459
+ A dictionary with the following keys:
460
+ - "messages": List of all messages exchanged during the agent's run.
461
+ - "last_message": The last message exchanged during the agent's run.
462
+ - Any additional keys defined in the `state_schema`.
463
+ :raises RuntimeError: If the Agent component wasn't warmed up before calling `run_async()`.
464
+ :raises BreakpointException: If an agent breakpoint is triggered.
465
+ """
466
+ # We pop parent_snapshot from kwargs to avoid passing it into State.
467
+ parent_snapshot = kwargs.pop("parent_snapshot", None)
468
+ agent_inputs = {
469
+ "messages": messages,
470
+ "streaming_callback": streaming_callback,
471
+ "break_point": break_point,
472
+ "snapshot": snapshot,
473
+ **kwargs,
474
+ }
475
+ self._runtime_checks(break_point=break_point, snapshot=snapshot)
476
+
477
+ if snapshot:
478
+ exe_context = self._initialize_from_snapshot(
479
+ snapshot=snapshot, streaming_callback=streaming_callback, requires_async=True, tools=tools
480
+ )
481
+ else:
482
+ exe_context = self._initialize_fresh_execution(
483
+ messages=messages,
484
+ streaming_callback=streaming_callback,
485
+ requires_async=True,
486
+ system_prompt=system_prompt,
487
+ tools=tools,
488
+ **kwargs,
489
+ )
490
+
491
+ with self._create_agent_span() as span:
492
+ span.set_content_tag("haystack.agent.input", _deepcopy_with_exceptions(agent_inputs))
493
+
494
+ while exe_context.counter < self.max_agent_steps:
495
+ # Handle breakpoint and ChatGenerator call
496
+ self._check_chat_generator_breakpoint(
497
+ execution_context=exe_context, break_point=break_point, parent_snapshot=parent_snapshot
498
+ )
499
+ # We skip the chat generator when restarting from a snapshot from a ToolBreakpoint
500
+ if exe_context.skip_chat_generator:
501
+ llm_messages = exe_context.state.get("messages", [])[-1:]
502
+ # Set to False so the next iteration will call the chat generator
503
+ exe_context.skip_chat_generator = False
504
+ else:
505
+ result = await AsyncPipeline._run_component_async(
506
+ component_name="chat_generator",
507
+ component={"instance": self.chat_generator},
508
+ component_inputs={
509
+ "messages": exe_context.state.data["messages"],
510
+ **exe_context.chat_generator_inputs,
511
+ },
512
+ component_visits=exe_context.component_visits,
513
+ parent_span=span,
514
+ )
515
+ llm_messages = result["replies"]
516
+ exe_context.state.set("messages", llm_messages)
517
+
518
+ # Check if any of the LLM responses contain a tool call or if the LLM is not using tools
519
+ if not any(msg.tool_call for msg in llm_messages) or self._tool_invoker is None:
520
+ exe_context.counter += 1
521
+ break
522
+
523
+ # Apply confirmation strategies and update State and messages sent to ToolInvoker
524
+ try:
525
+ # Run confirmation strategies to get updated tool call messages and modified chat history
526
+ modified_tool_call_messages, new_chat_history = _process_confirmation_strategies(
527
+ confirmation_strategies=self._confirmation_strategies,
528
+ messages_with_tool_calls=llm_messages,
529
+ execution_context=exe_context,
530
+ )
531
+ # Replace the chat history in state with the modified one
532
+ exe_context.state.set(key="messages", value=new_chat_history, handler_override=replace_values)
533
+ except HITLBreakpointException as tbp_error:
534
+ # We create a break_point to pass into _check_tool_invoker_breakpoint
535
+ break_point = AgentBreakpoint(
536
+ agent_name=getattr(self, "__component_name__", ""),
537
+ break_point=ToolBreakpoint(
538
+ component_name="tool_invoker",
539
+ tool_name=tbp_error.tool_name,
540
+ visit_count=exe_context.component_visits["tool_invoker"],
541
+ snapshot_file_path=tbp_error.snapshot_file_path,
542
+ ),
543
+ )
544
+
545
+ # Handle breakpoint
546
+ Agent._check_tool_invoker_breakpoint(
547
+ execution_context=exe_context, break_point=break_point, parent_snapshot=parent_snapshot
548
+ )
549
+
550
+ # Run ToolInvoker
551
+ # We only send the messages from the LLM to the tool invoker
552
+ tool_invoker_result = await AsyncPipeline._run_component_async(
553
+ component_name="tool_invoker",
554
+ component={"instance": self._tool_invoker},
555
+ component_inputs={
556
+ "messages": modified_tool_call_messages,
557
+ "state": exe_context.state,
558
+ **exe_context.tool_invoker_inputs,
559
+ },
560
+ component_visits=exe_context.component_visits,
561
+ parent_span=span,
562
+ )
563
+
564
+ # Set execution context tool execution decisions to empty after applying them b/c they should only
565
+ # be used once for the current tool calls
566
+ exe_context.tool_execution_decisions = None
567
+ tool_messages = tool_invoker_result["tool_messages"]
568
+ exe_context.state = tool_invoker_result["state"]
569
+ exe_context.state.set("messages", tool_messages)
570
+
571
+ # Check if any LLM message's tool call name matches an exit condition
572
+ if self.exit_conditions != ["text"] and self._check_exit_conditions(llm_messages, tool_messages):
573
+ exe_context.counter += 1
574
+ break
575
+
576
+ # Increment the step counter
577
+ exe_context.counter += 1
578
+
579
+ if exe_context.counter >= self.max_agent_steps:
580
+ logger.warning(
581
+ "Agent reached maximum agent steps of {max_agent_steps}, stopping.",
582
+ max_agent_steps=self.max_agent_steps,
583
+ )
584
+ span.set_content_tag("haystack.agent.output", exe_context.state.data)
585
+ span.set_tag("haystack.agent.steps_taken", exe_context.counter)
586
+
587
+ result = {**exe_context.state.data}
588
+ if msgs := result.get("messages"):
589
+ result["last_message"] = msgs[-1]
590
+ return result
591
+
592
+ def to_dict(self) -> dict[str, Any]:
593
+ """
594
+ Serialize the component to a dictionary.
595
+
596
+ :return: Dictionary with serialized data
597
+ """
598
+ data = super(Agent, self).to_dict()
599
+ data["init_parameters"]["confirmation_strategies"] = (
600
+ {name: strategy.to_dict() for name, strategy in self._confirmation_strategies.items()}
601
+ if self._confirmation_strategies
602
+ else None
603
+ )
604
+ return data
605
+
606
+ @classmethod
607
+ def from_dict(cls, data: dict[str, Any]) -> "Agent":
608
+ """
609
+ Deserialize the agent from a dictionary.
610
+
611
+ :param data: Dictionary to deserialize from
612
+ :return: Deserialized agent
613
+ """
614
+ init_params = data.get("init_parameters", {})
615
+
616
+ deserialize_chatgenerator_inplace(init_params, key="chat_generator")
617
+
618
+ if "state_schema" in init_params:
619
+ init_params["state_schema"] = _schema_from_dict(init_params["state_schema"])
620
+
621
+ if init_params.get("streaming_callback") is not None:
622
+ init_params["streaming_callback"] = deserialize_callable(init_params["streaming_callback"])
623
+
624
+ deserialize_tools_or_toolset_inplace(init_params, key="tools")
625
+
626
+ if "confirmation_strategies" in init_params and init_params["confirmation_strategies"] is not None:
627
+ for name, strategy_dict in init_params["confirmation_strategies"].items():
628
+ strategy_class = import_class_by_name(strategy_dict["type"])
629
+ if not hasattr(strategy_class, "from_dict"):
630
+ raise TypeError(f"{strategy_class} does not have from_dict method implemented.")
631
+ init_params["confirmation_strategies"][name] = strategy_class.from_dict(strategy_dict)
632
+
633
+ return default_from_dict(cls, data)