agent-pack-haystack 0.0.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.
@@ -0,0 +1,39 @@
1
+ Metadata-Version: 2.4
2
+ Name: agent-pack-haystack
3
+ Version: 0.0.1
4
+ Summary: Pre-configured Haystack Agents
5
+ Project-URL: Documentation, https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/agent_pack#readme
6
+ Project-URL: Issues, https://github.com/deepset-ai/haystack-core-integrations/issues
7
+ Project-URL: Source, https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/agent_pack
8
+ Author-email: deepset GmbH <info@deepset.ai>
9
+ License-Expression: Apache-2.0
10
+ License-File: LICENSE.txt
11
+ Keywords: agents,haystack
12
+ Classifier: Development Status :: 4 - Beta
13
+ Classifier: License :: OSI Approved :: Apache Software License
14
+ Classifier: Programming Language :: Python
15
+ Classifier: Programming Language :: Python :: 3.10
16
+ Classifier: Programming Language :: Python :: 3.11
17
+ Classifier: Programming Language :: Python :: 3.12
18
+ Classifier: Programming Language :: Python :: 3.13
19
+ Classifier: Programming Language :: Python :: 3.14
20
+ Classifier: Programming Language :: Python :: Implementation :: CPython
21
+ Classifier: Programming Language :: Python :: Implementation :: PyPy
22
+ Requires-Python: >=3.10
23
+ Requires-Dist: haystack-ai>=3.0.0
24
+ Description-Content-Type: text/markdown
25
+
26
+ # agent-pack-haystack
27
+
28
+ [![PyPI - Version](https://img.shields.io/pypi/v/agent-pack-haystack.svg)](https://pypi.org/project/agent-pack-haystack)
29
+ [![PyPI - Python Version](https://img.shields.io/pypi/pyversions/agent-pack-haystack.svg)](https://pypi.org/project/agent-pack-haystack)
30
+
31
+ - [Changelog](https://github.com/deepset-ai/haystack-core-integrations/blob/main/integrations/agent_pack/CHANGELOG.md)
32
+
33
+ ---
34
+
35
+ ## Contributing
36
+
37
+ Refer to the general [Contribution Guidelines](https://github.com/deepset-ai/haystack-core-integrations/blob/main/CONTRIBUTING.md).
38
+
39
+ To run integration tests locally, you need to export the `OPENAI_API_KEY` and `TAVILY_API_KEY` environment variables.
@@ -0,0 +1,16 @@
1
+ haystack_integrations/agent_pack/__init__.py,sha256=KHB2rXe8bnIbyL7hs25noL3N_wHgwjtba-N_hyV05-Y,288
2
+ haystack_integrations/agent_pack/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
3
+ haystack_integrations/agent_pack/advanced_rag/__init__.py,sha256=XX-ddnPoMOFTmWbOtROCvtDxJUZKWx8sg8b_eba1TPM,535
4
+ haystack_integrations/agent_pack/advanced_rag/agent.py,sha256=-PvM7S0e2i2n-LtCNztR5pGoydPMQxYZqENKp6li4Hw,9411
5
+ haystack_integrations/agent_pack/advanced_rag/hooks.py,sha256=1_yjVrmI5lx3B5hnPr2nb2J7cMD0jZJXKlN1ivzk7KA,3438
6
+ haystack_integrations/agent_pack/advanced_rag/prompts.py,sha256=FiIrPFsR915m0536tLBep9N7p--Rlc2QTqY7avUNy2I,4716
7
+ haystack_integrations/agent_pack/advanced_rag/tools.py,sha256=A-jPLJr66Qe82oiG8xpIj-k5CtQ_rX0Y8xx3GIZwiIk,35130
8
+ haystack_integrations/agent_pack/deep_research/__init__.py,sha256=vE1wxfJ1Z9rhz_uINRHLQvZL0mAeRZxBhtChv4h3K8s,199
9
+ haystack_integrations/agent_pack/deep_research/agent.py,sha256=b1lQSgNZlLKz6EivF50wI1EBSLxyYz5d4YkWcm0t-n4,8694
10
+ haystack_integrations/agent_pack/deep_research/hooks.py,sha256=9t_MnlII9ELiWQqldmW8eSlgb4PAOMnmz02WLs4AXs4,3562
11
+ haystack_integrations/agent_pack/deep_research/prompts.py,sha256=hcWu68LeS1sC-fX0vYBLzNLdMS_aky3OK6ceR9uC2Gs,4469
12
+ haystack_integrations/agent_pack/deep_research/tools.py,sha256=aDV2skNIk01uJ80skLqVqEc5WjpfriwnpAyWwPbWsvY,4939
13
+ agent_pack_haystack-0.0.1.dist-info/METADATA,sha256=IP6rGjw-WZFOPYICeVtsh7uHW6P6ejly1uSLYl4HAJk,1873
14
+ agent_pack_haystack-0.0.1.dist-info/WHEEL,sha256=lCkmxWfQsSc9CfIClYeavTdQeEX2toPqufh9gI35EQA,87
15
+ agent_pack_haystack-0.0.1.dist-info/licenses/LICENSE.txt,sha256=OrAtTu4aPmrTjKDSTzkSPgo_ji6NOMWO2N9JOTFyAzM,11350
16
+ agent_pack_haystack-0.0.1.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.31.0
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright 2023-present deepset GmbH
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
@@ -0,0 +1,8 @@
1
+ # SPDX-FileCopyrightText: 2022-present deepset GmbH <info@deepset.ai>
2
+ #
3
+ # SPDX-License-Identifier: Apache-2.0
4
+
5
+ from .advanced_rag import create_advanced_rag_agent
6
+ from .deep_research import create_deep_research_agent
7
+
8
+ __all__ = ["create_advanced_rag_agent", "create_deep_research_agent"]
@@ -0,0 +1,21 @@
1
+ # SPDX-FileCopyrightText: 2022-present deepset GmbH <info@deepset.ai>
2
+ #
3
+ # SPDX-License-Identifier: Apache-2.0
4
+
5
+ from .agent import create_advanced_rag_agent
6
+ from .tools import (
7
+ DocumentStoreToolset,
8
+ FetchDocumentsByFilterTool,
9
+ GetMetadataFieldRangeTool,
10
+ GetMetadataFieldValuesTool,
11
+ ListMetadataFieldsTool,
12
+ )
13
+
14
+ __all__ = [
15
+ "DocumentStoreToolset",
16
+ "FetchDocumentsByFilterTool",
17
+ "GetMetadataFieldRangeTool",
18
+ "GetMetadataFieldValuesTool",
19
+ "ListMetadataFieldsTool",
20
+ "create_advanced_rag_agent",
21
+ ]
@@ -0,0 +1,170 @@
1
+ # SPDX-FileCopyrightText: 2022-present deepset GmbH <info@deepset.ai>
2
+ #
3
+ # SPDX-License-Identifier: Apache-2.0
4
+
5
+ from typing import Any
6
+
7
+ from haystack import Document, Pipeline
8
+ from haystack.components.agents import Agent
9
+ from haystack.components.generators.chat import OpenAIResponsesChatGenerator
10
+ from haystack.components.generators.chat.types import ChatGenerator
11
+ from haystack.components.retrievers.types import TextRetriever
12
+ from haystack.document_stores.types import DocumentStore
13
+ from haystack.hooks import Hook, HookPoint
14
+ from haystack.lazy_imports import LazyImport
15
+ from haystack.tools import Tool, Toolset, ToolsType
16
+
17
+ from haystack_integrations.agent_pack.advanced_rag import prompts
18
+ from haystack_integrations.agent_pack.advanced_rag.hooks import BackupAnswerHook
19
+ from haystack_integrations.agent_pack.advanced_rag.tools import (
20
+ DocumentStoreToolset,
21
+ _make_retrieval_pipeline_tool,
22
+ _make_retriever_tool,
23
+ )
24
+
25
+ # The default system prompt renders today's date via the Jinja `{% now %}` tag, which needs `arrow`. Without this
26
+ # check, a missing `arrow` surfaces as a cryptic `TemplateSyntaxError: Encountered unknown tag 'now'` because
27
+ # `ChatPromptBuilder` silently skips the extension when `arrow` is not installed.
28
+ with LazyImport(message='Run "pip install arrow>=1.3.0"') as arrow_import:
29
+ import arrow # noqa: F401
30
+
31
+ _DEFAULT_TIMEOUT = 180.0
32
+ _DEFAULT_MAX_RETRIES = 5
33
+
34
+
35
+ def _default_llm(model: str) -> OpenAIResponsesChatGenerator:
36
+ """
37
+ A default OpenAI Responses-API generator with the pack's timeout/retry settings.
38
+
39
+ Reasoning effort is set to "low": enough deliberate thinking for tool sequencing and filter construction, without
40
+ compounding latency across the agent's steps (leaving it unset would default to the heavier "medium").
41
+
42
+ :param model: The OpenAI model name.
43
+ :returns: The generator.
44
+ """
45
+ return OpenAIResponsesChatGenerator(
46
+ model=model,
47
+ timeout=_DEFAULT_TIMEOUT,
48
+ max_retries=_DEFAULT_MAX_RETRIES,
49
+ generation_kwargs={"reasoning": {"effort": "low"}},
50
+ )
51
+
52
+
53
+ def create_advanced_rag_agent(
54
+ *,
55
+ document_store: DocumentStore,
56
+ retriever: TextRetriever | Pipeline | None = None,
57
+ retrieval_pipeline_input_mapping: dict[str, list[str]] | None = None,
58
+ retrieval_pipeline_output_mapping: dict[str, str] | None = None,
59
+ llm: ChatGenerator | None = None,
60
+ backup_answer_llm: ChatGenerator | None = None,
61
+ system_prompt: str | None = None,
62
+ max_agent_steps: int = 20,
63
+ max_fetched_docs: int = 10,
64
+ extra_tools: ToolsType | None = None,
65
+ state_schema: dict[str, Any] | None = None,
66
+ hooks: dict[HookPoint, list[Hook]] | None = None,
67
+ raise_on_tool_invocation_failure: bool = False,
68
+ tool_concurrency_limit: int = 4,
69
+ ) -> Agent:
70
+ """
71
+ Create the advanced RAG agent.
72
+
73
+ The agent answers questions from documents it retrieves out of the document store. Instead of guessing which
74
+ metadata fields exist, it can inspect the store (fields, values, ranges) and construct a Haystack filter to
75
+ narrow its retrieval when metadata helps — plain, unfiltered retrieval remains available when it doesn't. The
76
+ answer cites the retrieved documents.
77
+
78
+ The required `retriever` becomes the `search_documents` tool; `document_store` additionally feeds the three
79
+ metadata inspection tools and must implement the metadata introspection methods (`get_metadata_fields_info`,
80
+ `get_metadata_field_unique_values`, `get_metadata_field_min_max`).
81
+
82
+ :param document_store: The document store the metadata inspection tools and the `fetch_documents_by_filter` tool
83
+ run against.
84
+ :param retriever: What retrieves for the `search_documents` tool (required). Either a standalone retriever
85
+ component following the `TextRetriever` protocol, i.e. its `run` method accepts `query` and `filters`
86
+ (e.g. `InMemoryBM25Retriever`, or an embedding retriever wrapped in `TextEmbeddingRetriever`), or a custom
87
+ retrieval `Pipeline` (e.g. embedder -> retriever, or hybrid retrieval) — a pipeline additionally requires
88
+ `retrieval_pipeline_input_mapping`. It should retrieve by relevance scoring (keyword or embedding-based) —
89
+ direct, unscored fetching is already covered by the built-in `fetch_documents_by_filter` tool.
90
+ :param retrieval_pipeline_input_mapping: Required when `retriever` is a `Pipeline`: maps the tool inputs to
91
+ pipeline input sockets; must have exactly the keys "query" and "filters",
92
+ e.g. `{"query": ["embedder.text"], "filters": ["retriever.filters"]}`.
93
+ :param retrieval_pipeline_output_mapping: Optional when `retriever` is a `Pipeline`: maps pipeline output sockets
94
+ to tool outputs, e.g. `{"retriever.documents": "documents"}`.
95
+ :param llm: LLM that drives the agent loop. Defaults to `OpenAIResponsesChatGenerator("gpt-5.4")` with low
96
+ reasoning effort.
97
+ :param backup_answer_llm: LLM the built-in `BackupAnswerHook` uses to write a best-effort answer when the run
98
+ is cut off by `max_agent_steps`. Defaults to a separate `OpenAIResponsesChatGenerator("gpt-5.4")` with low
99
+ reasoning effort.
100
+ :param system_prompt: Overrides the pre-made system prompt.
101
+ :param max_agent_steps: Maximum steps for the agent loop. If the loop is cut off by this limit before writing an
102
+ answer, an `after_run` hook (`BackupAnswerHook`) makes one extra LLM call to produce a best-effort answer from
103
+ the evidence gathered so far, so `last_message` always carries a text answer.
104
+ :param max_fetched_docs: Maximum number of documents `fetch_documents_by_filter` shows per fetch. A filter fetch is
105
+ not bounded by a retriever's `top_k`, so this caps the tool result instead; the scored `search_documents` tool
106
+ is bounded by the `top_k` configured on your retrieval components.
107
+ :param extra_tools: Additional tools (or toolsets) for the agent, appended after the built-in document-store
108
+ toolset and the retrieval tool.
109
+ :param state_schema: Additional entries merged into the agent's state schema. The built-in `documents` entry
110
+ (the accumulated retrieved documents) always takes precedence.
111
+ :param hooks: Additional hooks per hook point, merged with the built-in hooks. For `after_run`, the built-in
112
+ backup-answer hook runs first, so custom hooks see the final answer.
113
+ :param raise_on_tool_invocation_failure: If True, a failing tool call raises instead of being returned to the LLM
114
+ as an error message it can recover from (the default).
115
+ :param tool_concurrency_limit: Maximum number of tool calls executed in parallel within one agent step.
116
+ :returns: The advanced RAG `Agent`. Call it with the question as a user message,
117
+ `agent.run(messages=[ChatMessage.from_user(question)])`; the answer is in `last_message` (a `ChatMessage`) and
118
+ `documents` carries every document the agent retrieved during the run (deduplicated by id, in first-retrieved
119
+ order) — the answer cites them by the first 8 characters of their id, e.g. `[doc a1b2c3d4]`. The standard Agent
120
+ outputs `messages`, `step_count`, `token_usage` and `tool_call_counts` are also returned.
121
+ """
122
+ if system_prompt is None:
123
+ # Only the default system prompt requires the `{% now %}` Jinja tag (and thus `arrow`).
124
+ arrow_import.check()
125
+
126
+ if retriever is None:
127
+ msg = "`retriever` is required: pass a retriever component or a retrieval `Pipeline`."
128
+ raise ValueError(msg)
129
+
130
+ retrieval_tool: Tool
131
+ if isinstance(retriever, Pipeline):
132
+ if retrieval_pipeline_input_mapping is None:
133
+ msg = "`retrieval_pipeline_input_mapping` is required when `retriever` is a `Pipeline`."
134
+ raise ValueError(msg)
135
+ retrieval_tool = _make_retrieval_pipeline_tool(
136
+ pipeline=retriever,
137
+ input_mapping=retrieval_pipeline_input_mapping,
138
+ output_mapping=retrieval_pipeline_output_mapping,
139
+ )
140
+ else:
141
+ if retrieval_pipeline_input_mapping is not None or retrieval_pipeline_output_mapping is not None:
142
+ msg = "The `retrieval_pipeline_*` arguments are only valid when `retriever` is a `Pipeline`."
143
+ raise ValueError(msg)
144
+ retrieval_tool = _make_retriever_tool(retriever=retriever)
145
+
146
+ llm = llm or _default_llm("gpt-5.4")
147
+ backup_answer_llm = backup_answer_llm or _default_llm("gpt-5.4")
148
+
149
+ tools: list[Tool | Toolset] = [
150
+ DocumentStoreToolset(document_store, max_fetched_docs=max_fetched_docs),
151
+ retrieval_tool,
152
+ ]
153
+ if extra_tools is not None:
154
+ tools.extend([extra_tools] if isinstance(extra_tools, Toolset) else list(extra_tools))
155
+
156
+ merged_hooks: dict[HookPoint, list[Hook]] = {"after_run": [BackupAnswerHook(chat_generator=backup_answer_llm)]}
157
+ for hook_point, point_hooks in (hooks or {}).items():
158
+ merged_hooks.setdefault(hook_point, []).extend(point_hooks)
159
+
160
+ return Agent(
161
+ chat_generator=llm,
162
+ system_prompt=system_prompt or prompts.SYSTEM_TEMPLATE,
163
+ tools=tools,
164
+ exit_conditions=["text"],
165
+ max_agent_steps=max_agent_steps,
166
+ state_schema={**(state_schema or {}), "documents": {"type": list[Document]}},
167
+ hooks=merged_hooks,
168
+ raise_on_tool_invocation_failure=raise_on_tool_invocation_failure,
169
+ tool_concurrency_limit=tool_concurrency_limit,
170
+ )
@@ -0,0 +1,89 @@
1
+ # SPDX-FileCopyrightText: 2022-present deepset GmbH <info@deepset.ai>
2
+ #
3
+ # SPDX-License-Identifier: Apache-2.0
4
+
5
+ from haystack import logging
6
+ from haystack.components.agents.state import State
7
+ from haystack.components.generators.chat.types import ChatGenerator
8
+ from haystack.core.serialization import default_from_dict, default_to_dict
9
+ from haystack.dataclasses import ChatMessage, ChatRole
10
+
11
+ from haystack_integrations.agent_pack.advanced_rag import prompts
12
+
13
+ logger = logging.getLogger(__name__)
14
+
15
+
16
+ class BackupAnswerHook:
17
+ """
18
+ Produce a final answer when the agent run ends without one. Runs as an `after_run` hook.
19
+
20
+ When the agent exhausts `max_agent_steps` mid-investigation, the run ends on a tool call or tool result instead of
21
+ an assistant text answer (and only `after_run` hooks run in this situation). This hook detects that case and makes
22
+ one LLM call over the conversation so far to produce a best-effort answer from the already-gathered evidence.
23
+ """
24
+
25
+ allowed_hook_points = ("after_run",)
26
+
27
+ def __init__(self, chat_generator: ChatGenerator) -> None:
28
+ """
29
+ Create the hook.
30
+
31
+ :param chat_generator: LLM that writes the backup answer from the gathered evidence.
32
+ """
33
+ self.chat_generator = chat_generator
34
+
35
+ def warm_up(self) -> None:
36
+ """Prepare the hook's generator for use; called from the Agent's `warm_up`."""
37
+ if hasattr(self.chat_generator, "warm_up"):
38
+ self.chat_generator.warm_up()
39
+
40
+ def close(self) -> None:
41
+ """Release the hook's generator resources; called from the Agent's `close`."""
42
+ if hasattr(self.chat_generator, "close"):
43
+ self.chat_generator.close()
44
+
45
+ def to_dict(self) -> dict:
46
+ """
47
+ Serialize the hook to a dictionary.
48
+
49
+ :returns: Dictionary with serialized data.
50
+ """
51
+ return default_to_dict(self, chat_generator=self.chat_generator)
52
+
53
+ @classmethod
54
+ def from_dict(cls, data: dict) -> "BackupAnswerHook":
55
+ """
56
+ Deserialize the hook from a dictionary.
57
+
58
+ :param data: Dictionary to deserialize from.
59
+ :returns: Deserialized hook.
60
+ """
61
+ return default_from_dict(cls, data)
62
+
63
+ @staticmethod
64
+ def _needs_backup(messages: list[ChatMessage]) -> bool:
65
+ """
66
+ Whether the run ended without a final assistant text answer.
67
+
68
+ :param messages: The run's messages.
69
+ :returns: True when the last message is not a plain assistant text reply.
70
+ """
71
+ if not messages:
72
+ return False
73
+ last = messages[-1]
74
+ return not (last.is_from(ChatRole.ASSISTANT) and bool(last.text) and not last.tool_calls)
75
+
76
+ def run(self, state: State) -> None:
77
+ """
78
+ Append a best-effort final answer when the run ended without one (e.g. step exhaustion).
79
+
80
+ :param state: The agent run's state.
81
+ """
82
+ messages = state.data.get("messages") or []
83
+ if not self._needs_backup(messages):
84
+ return
85
+ logger.info("run ended without a final answer (likely max_agent_steps); writing a backup answer")
86
+ transcript = [m for m in messages if not m.is_from(ChatRole.SYSTEM)]
87
+ prompt = [ChatMessage.from_system(prompts.BACKUP_ANSWER_PROMPT), *transcript]
88
+ reply = self.chat_generator.run(messages=prompt)["replies"][0]
89
+ state.set("messages", [reply]) # merge_lists handler: appended as the final message
@@ -0,0 +1,91 @@
1
+ # SPDX-FileCopyrightText: 2022-present deepset GmbH <info@deepset.ai>
2
+ #
3
+ # SPDX-License-Identifier: Apache-2.0
4
+
5
+ _FILTER_SYNTAX = """A filter is either a single condition:
6
+ {"field": "meta.category", "operator": "==", "value": "science"}
7
+
8
+ or a logical group of conditions:
9
+ {"operator": "AND", "conditions": [
10
+ {"field": "meta.category", "operator": "==", "value": "science"},
11
+ {"field": "meta.year", "operator": ">=", "value": 2020}
12
+ ]}
13
+
14
+ Comparison operators: ==, !=, >, >=, <, <=, in, not in ("in" and "not in" take a list as value).
15
+ Logical operators: AND, OR, NOT (NOT takes a conditions list with exactly ONE entry).
16
+ Groups can be nested inside "conditions".
17
+
18
+ Rules:
19
+ - Always prefix field names with "meta." (field "year" becomes "meta.year").
20
+ - Use ONLY field names returned by list_metadata_fields and exact values as returned by
21
+ get_metadata_field_values; for numeric or date ranges stay within the bounds from
22
+ get_metadata_field_range."""
23
+
24
+
25
+ FILTER_GRAMMAR = f"""Optional metadata filter to narrow the search, using Haystack filter syntax.
26
+
27
+ {_FILTER_SYNTAX}
28
+ - Omit this parameter entirely when no metadata filter is needed."""
29
+
30
+
31
+ FETCH_FILTER_GRAMMAR = f"""The metadata filter selecting the documents to fetch, using Haystack filter syntax.
32
+
33
+ {_FILTER_SYNTAX}"""
34
+
35
+
36
+ RETRIEVAL_TOOL_DESCRIPTION = (
37
+ "Search the document store for documents relevant to a query, ranked by relevance. "
38
+ "Optionally narrow the search with a metadata filter (see the filters parameter). "
39
+ "Returns the matching documents with their id, metadata and content."
40
+ )
41
+
42
+
43
+ FILTER_RETRIEVER_TOOL_DESCRIPTION = (
44
+ "Fetch documents directly by metadata filter, WITHOUT relevance ranking. Returns the matching "
45
+ "documents in reading order (up to max_docs per call) plus the total match count; page through "
46
+ "larger match sets with offset. Use this when you can identify the exact documents you need "
47
+ "by their metadata (e.g. a specific title, source or file name); use search_documents when "
48
+ "you need a relevance search."
49
+ )
50
+
51
+
52
+ BACKUP_ANSWER_PROMPT = (
53
+ "You are finalizing an interrupted document-search session. The conversation below ended "
54
+ "before a final answer was written because the step budget ran out. Using ONLY the "
55
+ "information already gathered in the conversation (metadata listings and retrieved "
56
+ "documents), write the best possible answer to the user's original question, referencing "
57
+ "the documents used. If the gathered information is insufficient to answer, begin with "
58
+ '"No matching information was found" and briefly state what is missing. Do not call tools.'
59
+ )
60
+
61
+
62
+ SYSTEM_TEMPLATE = """{% message role="system" %}
63
+ You answer questions using ONLY documents retrieved from a document store.
64
+ Today's date is {% now 'local', '%d %B %Y' %}.
65
+
66
+ Process:
67
+ 1. Call `list_metadata_fields` FIRST to learn which metadata fields exist and their types.
68
+ 2. Before filtering on a field, verify what it contains: use `get_metadata_field_values` for
69
+ keyword/boolean fields (filter values must match exactly) and `get_metadata_field_range` for
70
+ numeric or date-like fields.
71
+ 3. Call `search_documents` with a focused query and, when metadata can narrow the question, a
72
+ filter (the filter syntax is described in its `filters` parameter; always prefix field names
73
+ with "meta."). Not every question needs a filter — omit it when metadata cannot help.
74
+ When you can identify the exact documents you need by their metadata alone (e.g. a specific
75
+ title, source or file name), fetch them directly with `fetch_documents_by_filter` instead of
76
+ searching. The same applies when you need the COMPLETE set of documents matching a filter:
77
+ fetch with that filter and, if the result reports more matches than shown, continue with the
78
+ `offset` parameter — do not repeat searches with reworded queries or loosened filters.
79
+ 4. If a search returns no documents, relax or drop the filter and retry — at most a couple of
80
+ attempts. Do not re-inspect metadata you have already seen.
81
+
82
+ Answering:
83
+ - Answer using only the content of the retrieved documents. Cite the documents you used with the
84
+ exact bracketed reference shown in the tool results (e.g. [doc a1b2c3d4]), adding
85
+ distinguishing metadata such as a title or source when it helps the reader.
86
+ - If you cannot find the requested information — the question asks about metadata fields or
87
+ values that don't exist in the store, or retrieval returns nothing relevant even after
88
+ relaxing the filter — do NOT guess or answer from general knowledge. Begin your answer with
89
+ "No matching information was found" and briefly state what you checked (e.g. which field or
90
+ value does not exist).
91
+ {% endmessage %}"""