unique_toolkit 0.7.7__py3-none-any.whl → 1.23.0__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.
Potentially problematic release.
This version of unique_toolkit might be problematic. Click here for more details.
- unique_toolkit/__init__.py +28 -1
- unique_toolkit/_common/api_calling/human_verification_manager.py +343 -0
- unique_toolkit/_common/base_model_type_attribute.py +303 -0
- unique_toolkit/_common/chunk_relevancy_sorter/config.py +49 -0
- unique_toolkit/_common/chunk_relevancy_sorter/exception.py +5 -0
- unique_toolkit/_common/chunk_relevancy_sorter/schemas.py +46 -0
- unique_toolkit/_common/chunk_relevancy_sorter/service.py +374 -0
- unique_toolkit/_common/chunk_relevancy_sorter/tests/test_service.py +275 -0
- unique_toolkit/_common/default_language_model.py +12 -0
- unique_toolkit/_common/docx_generator/__init__.py +7 -0
- unique_toolkit/_common/docx_generator/config.py +12 -0
- unique_toolkit/_common/docx_generator/schemas.py +80 -0
- unique_toolkit/_common/docx_generator/service.py +252 -0
- unique_toolkit/_common/docx_generator/template/Doc Template.docx +0 -0
- unique_toolkit/_common/endpoint_builder.py +305 -0
- unique_toolkit/_common/endpoint_requestor.py +430 -0
- unique_toolkit/_common/exception.py +24 -0
- unique_toolkit/_common/feature_flags/schema.py +9 -0
- unique_toolkit/_common/pydantic/rjsf_tags.py +936 -0
- unique_toolkit/_common/pydantic_helpers.py +154 -0
- unique_toolkit/_common/referencing.py +53 -0
- unique_toolkit/_common/string_utilities.py +140 -0
- unique_toolkit/_common/tests/test_referencing.py +521 -0
- unique_toolkit/_common/tests/test_string_utilities.py +506 -0
- unique_toolkit/_common/token/image_token_counting.py +67 -0
- unique_toolkit/_common/token/token_counting.py +204 -0
- unique_toolkit/_common/utils/__init__.py +1 -0
- unique_toolkit/_common/utils/files.py +43 -0
- unique_toolkit/_common/utils/structured_output/__init__.py +1 -0
- unique_toolkit/_common/utils/structured_output/schema.py +5 -0
- unique_toolkit/_common/utils/write_configuration.py +51 -0
- unique_toolkit/_common/validators.py +101 -4
- unique_toolkit/agentic/__init__.py +1 -0
- unique_toolkit/agentic/debug_info_manager/debug_info_manager.py +28 -0
- unique_toolkit/agentic/debug_info_manager/test/test_debug_info_manager.py +278 -0
- unique_toolkit/agentic/evaluation/config.py +36 -0
- unique_toolkit/{evaluators → agentic/evaluation}/context_relevancy/prompts.py +25 -0
- unique_toolkit/agentic/evaluation/context_relevancy/schema.py +80 -0
- unique_toolkit/agentic/evaluation/context_relevancy/service.py +273 -0
- unique_toolkit/agentic/evaluation/evaluation_manager.py +218 -0
- unique_toolkit/agentic/evaluation/hallucination/constants.py +61 -0
- unique_toolkit/agentic/evaluation/hallucination/hallucination_evaluation.py +111 -0
- unique_toolkit/{evaluators → agentic/evaluation}/hallucination/prompts.py +1 -1
- unique_toolkit/{evaluators → agentic/evaluation}/hallucination/service.py +16 -15
- unique_toolkit/{evaluators → agentic/evaluation}/hallucination/utils.py +30 -20
- unique_toolkit/{evaluators → agentic/evaluation}/output_parser.py +20 -2
- unique_toolkit/{evaluators → agentic/evaluation}/schemas.py +27 -7
- unique_toolkit/agentic/evaluation/tests/test_context_relevancy_service.py +253 -0
- unique_toolkit/agentic/evaluation/tests/test_output_parser.py +87 -0
- unique_toolkit/agentic/history_manager/history_construction_with_contents.py +297 -0
- unique_toolkit/agentic/history_manager/history_manager.py +242 -0
- unique_toolkit/agentic/history_manager/loop_token_reducer.py +484 -0
- unique_toolkit/agentic/history_manager/utils.py +96 -0
- unique_toolkit/agentic/postprocessor/postprocessor_manager.py +212 -0
- unique_toolkit/agentic/reference_manager/reference_manager.py +103 -0
- unique_toolkit/agentic/responses_api/__init__.py +19 -0
- unique_toolkit/agentic/responses_api/postprocessors/code_display.py +63 -0
- unique_toolkit/agentic/responses_api/postprocessors/generated_files.py +145 -0
- unique_toolkit/agentic/responses_api/stream_handler.py +15 -0
- unique_toolkit/agentic/short_term_memory_manager/persistent_short_term_memory_manager.py +141 -0
- unique_toolkit/agentic/thinking_manager/thinking_manager.py +103 -0
- unique_toolkit/agentic/tools/__init__.py +1 -0
- unique_toolkit/agentic/tools/a2a/__init__.py +36 -0
- unique_toolkit/agentic/tools/a2a/config.py +17 -0
- unique_toolkit/agentic/tools/a2a/evaluation/__init__.py +15 -0
- unique_toolkit/agentic/tools/a2a/evaluation/_utils.py +66 -0
- unique_toolkit/agentic/tools/a2a/evaluation/config.py +55 -0
- unique_toolkit/agentic/tools/a2a/evaluation/evaluator.py +260 -0
- unique_toolkit/agentic/tools/a2a/evaluation/summarization_user_message.j2 +9 -0
- unique_toolkit/agentic/tools/a2a/manager.py +55 -0
- unique_toolkit/agentic/tools/a2a/postprocessing/__init__.py +21 -0
- unique_toolkit/agentic/tools/a2a/postprocessing/_display_utils.py +185 -0
- unique_toolkit/agentic/tools/a2a/postprocessing/_ref_utils.py +73 -0
- unique_toolkit/agentic/tools/a2a/postprocessing/config.py +45 -0
- unique_toolkit/agentic/tools/a2a/postprocessing/display.py +180 -0
- unique_toolkit/agentic/tools/a2a/postprocessing/references.py +101 -0
- unique_toolkit/agentic/tools/a2a/postprocessing/test/test_display_utils.py +1335 -0
- unique_toolkit/agentic/tools/a2a/postprocessing/test/test_ref_utils.py +603 -0
- unique_toolkit/agentic/tools/a2a/prompts.py +46 -0
- unique_toolkit/agentic/tools/a2a/response_watcher/__init__.py +6 -0
- unique_toolkit/agentic/tools/a2a/response_watcher/service.py +91 -0
- unique_toolkit/agentic/tools/a2a/tool/__init__.py +4 -0
- unique_toolkit/agentic/tools/a2a/tool/_memory.py +26 -0
- unique_toolkit/agentic/tools/a2a/tool/_schema.py +9 -0
- unique_toolkit/agentic/tools/a2a/tool/config.py +73 -0
- unique_toolkit/agentic/tools/a2a/tool/service.py +306 -0
- unique_toolkit/agentic/tools/agent_chunks_hanlder.py +65 -0
- unique_toolkit/agentic/tools/config.py +167 -0
- unique_toolkit/agentic/tools/factory.py +44 -0
- unique_toolkit/agentic/tools/mcp/__init__.py +4 -0
- unique_toolkit/agentic/tools/mcp/manager.py +71 -0
- unique_toolkit/agentic/tools/mcp/models.py +28 -0
- unique_toolkit/agentic/tools/mcp/tool_wrapper.py +234 -0
- unique_toolkit/agentic/tools/openai_builtin/__init__.py +11 -0
- unique_toolkit/agentic/tools/openai_builtin/base.py +30 -0
- unique_toolkit/agentic/tools/openai_builtin/code_interpreter/__init__.py +8 -0
- unique_toolkit/agentic/tools/openai_builtin/code_interpreter/config.py +57 -0
- unique_toolkit/agentic/tools/openai_builtin/code_interpreter/service.py +230 -0
- unique_toolkit/agentic/tools/openai_builtin/manager.py +62 -0
- unique_toolkit/agentic/tools/schemas.py +141 -0
- unique_toolkit/agentic/tools/test/test_mcp_manager.py +536 -0
- unique_toolkit/agentic/tools/test/test_tool_progress_reporter.py +445 -0
- unique_toolkit/agentic/tools/tool.py +183 -0
- unique_toolkit/agentic/tools/tool_manager.py +523 -0
- unique_toolkit/agentic/tools/tool_progress_reporter.py +285 -0
- unique_toolkit/agentic/tools/utils/__init__.py +19 -0
- unique_toolkit/agentic/tools/utils/execution/__init__.py +1 -0
- unique_toolkit/agentic/tools/utils/execution/execution.py +286 -0
- unique_toolkit/agentic/tools/utils/source_handling/__init__.py +0 -0
- unique_toolkit/agentic/tools/utils/source_handling/schema.py +21 -0
- unique_toolkit/agentic/tools/utils/source_handling/source_formatting.py +207 -0
- unique_toolkit/agentic/tools/utils/source_handling/tests/test_source_formatting.py +216 -0
- unique_toolkit/app/__init__.py +6 -0
- unique_toolkit/app/dev_util.py +180 -0
- unique_toolkit/app/init_sdk.py +32 -1
- unique_toolkit/app/schemas.py +198 -31
- unique_toolkit/app/unique_settings.py +367 -0
- unique_toolkit/chat/__init__.py +8 -1
- unique_toolkit/chat/deprecated/service.py +232 -0
- unique_toolkit/chat/functions.py +642 -77
- unique_toolkit/chat/rendering.py +34 -0
- unique_toolkit/chat/responses_api.py +461 -0
- unique_toolkit/chat/schemas.py +133 -2
- unique_toolkit/chat/service.py +115 -767
- unique_toolkit/content/functions.py +153 -4
- unique_toolkit/content/schemas.py +122 -15
- unique_toolkit/content/service.py +278 -44
- unique_toolkit/content/smart_rules.py +301 -0
- unique_toolkit/content/utils.py +8 -3
- unique_toolkit/embedding/service.py +102 -11
- unique_toolkit/framework_utilities/__init__.py +1 -0
- unique_toolkit/framework_utilities/langchain/client.py +71 -0
- unique_toolkit/framework_utilities/langchain/history.py +19 -0
- unique_toolkit/framework_utilities/openai/__init__.py +6 -0
- unique_toolkit/framework_utilities/openai/client.py +83 -0
- unique_toolkit/framework_utilities/openai/message_builder.py +229 -0
- unique_toolkit/framework_utilities/utils.py +23 -0
- unique_toolkit/language_model/__init__.py +3 -0
- unique_toolkit/language_model/builder.py +27 -11
- unique_toolkit/language_model/default_language_model.py +3 -0
- unique_toolkit/language_model/functions.py +327 -43
- unique_toolkit/language_model/infos.py +992 -50
- unique_toolkit/language_model/reference.py +242 -0
- unique_toolkit/language_model/schemas.py +475 -48
- unique_toolkit/language_model/service.py +228 -27
- unique_toolkit/protocols/support.py +145 -0
- unique_toolkit/services/__init__.py +7 -0
- unique_toolkit/services/chat_service.py +1630 -0
- unique_toolkit/services/knowledge_base.py +861 -0
- unique_toolkit/short_term_memory/service.py +178 -41
- unique_toolkit/smart_rules/__init__.py +0 -0
- unique_toolkit/smart_rules/compile.py +56 -0
- unique_toolkit/test_utilities/events.py +197 -0
- {unique_toolkit-0.7.7.dist-info → unique_toolkit-1.23.0.dist-info}/METADATA +606 -7
- unique_toolkit-1.23.0.dist-info/RECORD +182 -0
- unique_toolkit/evaluators/__init__.py +0 -1
- unique_toolkit/evaluators/config.py +0 -35
- unique_toolkit/evaluators/constants.py +0 -1
- unique_toolkit/evaluators/context_relevancy/constants.py +0 -32
- unique_toolkit/evaluators/context_relevancy/service.py +0 -53
- unique_toolkit/evaluators/context_relevancy/utils.py +0 -142
- unique_toolkit/evaluators/hallucination/constants.py +0 -41
- unique_toolkit-0.7.7.dist-info/RECORD +0 -64
- /unique_toolkit/{evaluators → agentic/evaluation}/exception.py +0 -0
- {unique_toolkit-0.7.7.dist-info → unique_toolkit-1.23.0.dist-info}/LICENSE +0 -0
- {unique_toolkit-0.7.7.dist-info → unique_toolkit-1.23.0.dist-info}/WHEEL +0 -0
|
@@ -0,0 +1,523 @@
|
|
|
1
|
+
import asyncio
|
|
2
|
+
from abc import ABC, abstractmethod
|
|
3
|
+
from logging import Logger, getLogger
|
|
4
|
+
from typing import Literal, override
|
|
5
|
+
|
|
6
|
+
from openai.types.chat import (
|
|
7
|
+
ChatCompletionNamedToolChoiceParam,
|
|
8
|
+
)
|
|
9
|
+
from openai.types.responses import ToolParam, response_create_params
|
|
10
|
+
from pydantic import BaseModel, Field
|
|
11
|
+
|
|
12
|
+
from unique_toolkit.agentic.evaluation.schemas import EvaluationMetricName
|
|
13
|
+
from unique_toolkit.agentic.tools.a2a import A2AManager, SubAgentTool
|
|
14
|
+
from unique_toolkit.agentic.tools.config import ToolBuildConfig
|
|
15
|
+
from unique_toolkit.agentic.tools.factory import ToolFactory
|
|
16
|
+
from unique_toolkit.agentic.tools.mcp.manager import MCPManager
|
|
17
|
+
from unique_toolkit.agentic.tools.openai_builtin.base import (
|
|
18
|
+
OpenAIBuiltInTool,
|
|
19
|
+
)
|
|
20
|
+
from unique_toolkit.agentic.tools.openai_builtin.manager import OpenAIBuiltInToolManager
|
|
21
|
+
from unique_toolkit.agentic.tools.schemas import ToolCallResponse, ToolPrompts
|
|
22
|
+
from unique_toolkit.agentic.tools.tool import Tool
|
|
23
|
+
from unique_toolkit.agentic.tools.tool_progress_reporter import ToolProgressReporter
|
|
24
|
+
from unique_toolkit.agentic.tools.utils.execution.execution import (
|
|
25
|
+
Result,
|
|
26
|
+
SafeTaskExecutor,
|
|
27
|
+
)
|
|
28
|
+
from unique_toolkit.app.schemas import ChatEvent
|
|
29
|
+
from unique_toolkit.language_model.schemas import (
|
|
30
|
+
LanguageModelFunction,
|
|
31
|
+
LanguageModelToolDescription,
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
class ForcedToolOption:
|
|
36
|
+
type: str = "function"
|
|
37
|
+
|
|
38
|
+
def __init__(self, name: str):
|
|
39
|
+
self.name = name
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
class ToolManagerConfig(BaseModel):
|
|
43
|
+
tools: list[ToolBuildConfig] = Field(
|
|
44
|
+
default=[],
|
|
45
|
+
description="List of tools that the agent can use.",
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
max_tool_calls: int = Field(
|
|
49
|
+
default=10,
|
|
50
|
+
ge=1,
|
|
51
|
+
description="Maximum number of tool calls that can be executed in one iteration.",
|
|
52
|
+
)
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
class BaseToolManager(ABC):
|
|
56
|
+
def __init__(self, config: ToolManagerConfig):
|
|
57
|
+
self._config = config
|
|
58
|
+
# this needs to be a set of strings to avoid duplicates
|
|
59
|
+
self._tool_evaluation_check_list: set[EvaluationMetricName] = set()
|
|
60
|
+
|
|
61
|
+
@abstractmethod
|
|
62
|
+
def get_tool_by_name(self, name: str) -> Tool | None:
|
|
63
|
+
raise NotImplementedError()
|
|
64
|
+
|
|
65
|
+
@abstractmethod
|
|
66
|
+
def get_tool_choices(self) -> list[str]:
|
|
67
|
+
raise NotImplementedError()
|
|
68
|
+
|
|
69
|
+
@abstractmethod
|
|
70
|
+
def get_exclusive_tools(self) -> list[str]:
|
|
71
|
+
raise NotImplementedError()
|
|
72
|
+
|
|
73
|
+
@abstractmethod
|
|
74
|
+
def filter_tool_calls(
|
|
75
|
+
self,
|
|
76
|
+
tool_calls: list[LanguageModelFunction],
|
|
77
|
+
tool_types: list[Literal["mcp", "internal", "subagent"]],
|
|
78
|
+
) -> list[LanguageModelFunction]:
|
|
79
|
+
"""
|
|
80
|
+
Filter tool calls by their types.
|
|
81
|
+
|
|
82
|
+
Args:
|
|
83
|
+
tool_calls: List of tool calls to filter
|
|
84
|
+
tool_types: List of tool types to include (e.g., ["mcp", "internal", "subagent"])
|
|
85
|
+
|
|
86
|
+
Returns:
|
|
87
|
+
Filtered list of tool calls matching the specified types
|
|
88
|
+
"""
|
|
89
|
+
raise NotImplementedError()
|
|
90
|
+
|
|
91
|
+
def does_a_tool_take_control(self, tool_calls: list[LanguageModelFunction]) -> bool:
|
|
92
|
+
for tool_call in tool_calls:
|
|
93
|
+
tool_instance = self.get_tool_by_name(tool_call.name)
|
|
94
|
+
if tool_instance and tool_instance.takes_control():
|
|
95
|
+
return True
|
|
96
|
+
return False
|
|
97
|
+
|
|
98
|
+
async def execute_selected_tools(
|
|
99
|
+
self,
|
|
100
|
+
tool_calls: list[LanguageModelFunction],
|
|
101
|
+
) -> list[ToolCallResponse]:
|
|
102
|
+
tool_calls = tool_calls
|
|
103
|
+
|
|
104
|
+
tool_calls = self.filter_duplicate_tool_calls(
|
|
105
|
+
tool_calls=tool_calls,
|
|
106
|
+
)
|
|
107
|
+
num_tool_calls = len(tool_calls)
|
|
108
|
+
|
|
109
|
+
if num_tool_calls > self._config.max_tool_calls:
|
|
110
|
+
self._logger.warning(
|
|
111
|
+
(
|
|
112
|
+
"Number of tool calls %s exceeds the allowed maximum of %s."
|
|
113
|
+
"The tool calls will be reduced to the first %s."
|
|
114
|
+
),
|
|
115
|
+
num_tool_calls,
|
|
116
|
+
self._config.max_tool_calls,
|
|
117
|
+
self._config.max_tool_calls,
|
|
118
|
+
)
|
|
119
|
+
tool_calls = tool_calls[: self._config.max_tool_calls]
|
|
120
|
+
|
|
121
|
+
tool_call_responses = await self._execute_parallelized(tool_calls)
|
|
122
|
+
return tool_call_responses
|
|
123
|
+
|
|
124
|
+
async def _execute_parallelized(
|
|
125
|
+
self,
|
|
126
|
+
tool_calls: list[LanguageModelFunction],
|
|
127
|
+
) -> list[ToolCallResponse]:
|
|
128
|
+
self._logger.info("Execute tool calls")
|
|
129
|
+
|
|
130
|
+
task_executor = SafeTaskExecutor(
|
|
131
|
+
logger=self._logger,
|
|
132
|
+
)
|
|
133
|
+
|
|
134
|
+
# Create tasks for each tool call
|
|
135
|
+
tasks = [
|
|
136
|
+
task_executor.execute_async(
|
|
137
|
+
self.execute_tool_call,
|
|
138
|
+
tool_call=tool_call,
|
|
139
|
+
)
|
|
140
|
+
for tool_call in tool_calls
|
|
141
|
+
]
|
|
142
|
+
|
|
143
|
+
# Wait until all tasks are finished
|
|
144
|
+
tool_call_results = await asyncio.gather(*tasks)
|
|
145
|
+
tool_call_results_unpacked: list[ToolCallResponse] = []
|
|
146
|
+
for i, result in enumerate(tool_call_results):
|
|
147
|
+
unpacked_tool_call_result = self._create_tool_call_response(
|
|
148
|
+
result, tool_calls[i]
|
|
149
|
+
)
|
|
150
|
+
if unpacked_tool_call_result.debug_info is None:
|
|
151
|
+
unpacked_tool_call_result.debug_info = {}
|
|
152
|
+
unpacked_tool_call_result.debug_info["is_exclusive"] = (
|
|
153
|
+
tool_calls[i].name in self.get_exclusive_tools()
|
|
154
|
+
)
|
|
155
|
+
unpacked_tool_call_result.debug_info["is_forced"] = (
|
|
156
|
+
tool_calls[i].name in self.get_tool_choices()
|
|
157
|
+
)
|
|
158
|
+
tool_call_results_unpacked.append(unpacked_tool_call_result)
|
|
159
|
+
|
|
160
|
+
return tool_call_results_unpacked
|
|
161
|
+
|
|
162
|
+
async def execute_tool_call(
|
|
163
|
+
self, tool_call: LanguageModelFunction
|
|
164
|
+
) -> ToolCallResponse:
|
|
165
|
+
self._logger.info(f"Processing tool call: {tool_call.name}")
|
|
166
|
+
|
|
167
|
+
tool_instance = self.get_tool_by_name(
|
|
168
|
+
tool_call.name
|
|
169
|
+
) # we need to copy this as it will have problematic interference on multi calls.
|
|
170
|
+
|
|
171
|
+
if tool_instance:
|
|
172
|
+
# Execute the tool
|
|
173
|
+
tool_response: ToolCallResponse = await tool_instance.run(
|
|
174
|
+
tool_call=tool_call
|
|
175
|
+
)
|
|
176
|
+
evaluation_checks = tool_instance.evaluation_check_list()
|
|
177
|
+
self._tool_evaluation_check_list.update(evaluation_checks)
|
|
178
|
+
|
|
179
|
+
return tool_response
|
|
180
|
+
|
|
181
|
+
return ToolCallResponse(
|
|
182
|
+
id=tool_call.id, # type: ignore
|
|
183
|
+
name=tool_call.name,
|
|
184
|
+
error_message=f"Tool of name {tool_call.name} not found",
|
|
185
|
+
)
|
|
186
|
+
|
|
187
|
+
def _create_tool_call_response(
|
|
188
|
+
self, result: Result[ToolCallResponse], tool_call: LanguageModelFunction
|
|
189
|
+
) -> ToolCallResponse:
|
|
190
|
+
if not result.success:
|
|
191
|
+
return ToolCallResponse(
|
|
192
|
+
id=tool_call.id or "unknown_id",
|
|
193
|
+
name=tool_call.name,
|
|
194
|
+
error_message=str(result.exception),
|
|
195
|
+
)
|
|
196
|
+
unpacked = result.unpack()
|
|
197
|
+
if not isinstance(unpacked, ToolCallResponse):
|
|
198
|
+
return ToolCallResponse(
|
|
199
|
+
id=tool_call.id or "unknown_id",
|
|
200
|
+
name=tool_call.name,
|
|
201
|
+
error_message="Tool call response is not of type ToolCallResponse",
|
|
202
|
+
)
|
|
203
|
+
return unpacked
|
|
204
|
+
|
|
205
|
+
def filter_duplicate_tool_calls(
|
|
206
|
+
self,
|
|
207
|
+
tool_calls: list[LanguageModelFunction],
|
|
208
|
+
) -> list[LanguageModelFunction]:
|
|
209
|
+
"""
|
|
210
|
+
Filter out duplicate tool calls based on name and arguments.
|
|
211
|
+
"""
|
|
212
|
+
|
|
213
|
+
unique_tool_calls = []
|
|
214
|
+
|
|
215
|
+
for call in tool_calls:
|
|
216
|
+
if all(not call == other_call for other_call in unique_tool_calls):
|
|
217
|
+
unique_tool_calls.append(call)
|
|
218
|
+
|
|
219
|
+
if len(tool_calls) != len(unique_tool_calls):
|
|
220
|
+
self._logger = getLogger(__name__)
|
|
221
|
+
self._logger.warning(
|
|
222
|
+
f"Filtered out {len(tool_calls) - len(unique_tool_calls)} duplicate tool calls."
|
|
223
|
+
)
|
|
224
|
+
return unique_tool_calls
|
|
225
|
+
|
|
226
|
+
def get_evaluation_check_list(self) -> list[EvaluationMetricName]:
|
|
227
|
+
return list(self._tool_evaluation_check_list)
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
class ToolManager(BaseToolManager):
|
|
231
|
+
"""
|
|
232
|
+
Manages the tools available to the agent and executes tool calls.
|
|
233
|
+
|
|
234
|
+
This class is responsible for:
|
|
235
|
+
- Initializing tools based on the provided configuration and runtime events.
|
|
236
|
+
- Filtering tools based on availability, exclusivity, and user-defined constraints.
|
|
237
|
+
- Managing the lifecycle of tools, including retrieval, execution, and logging.
|
|
238
|
+
- Executing tool calls in parallel when possible to optimize performance.
|
|
239
|
+
- Enforcing limits on the number of tool calls and handling duplicate requests.
|
|
240
|
+
|
|
241
|
+
Key Features:
|
|
242
|
+
- Dynamic Tool Initialization: Tools are dynamically selected and initialized
|
|
243
|
+
based on runtime events and user preferences.
|
|
244
|
+
- Parallel Execution: Supports asynchronous execution of tools for efficiency.
|
|
245
|
+
- Error Handling: Provides detailed error messages and logs for failed tool calls.
|
|
246
|
+
- Scalability: Designed to handle a large number of tools and tool calls efficiently.
|
|
247
|
+
|
|
248
|
+
Only the ToolManager is allowed to interact with the tools directly.
|
|
249
|
+
"""
|
|
250
|
+
|
|
251
|
+
def __init__(
|
|
252
|
+
self,
|
|
253
|
+
logger: Logger,
|
|
254
|
+
config: ToolManagerConfig,
|
|
255
|
+
event: ChatEvent,
|
|
256
|
+
tool_progress_reporter: ToolProgressReporter,
|
|
257
|
+
mcp_manager: MCPManager,
|
|
258
|
+
a2a_manager: A2AManager,
|
|
259
|
+
):
|
|
260
|
+
super().__init__(config)
|
|
261
|
+
self._logger = logger
|
|
262
|
+
self._config = config
|
|
263
|
+
self._tool_progress_reporter = tool_progress_reporter
|
|
264
|
+
self._tools = []
|
|
265
|
+
self._tool_choices = event.payload.tool_choices
|
|
266
|
+
self._disabled_tools = event.payload.disabled_tools
|
|
267
|
+
self._exclusive_tools = [
|
|
268
|
+
tool.name for tool in self._config.tools if tool.is_exclusive
|
|
269
|
+
]
|
|
270
|
+
# this needs to be a set of strings to avoid duplicates
|
|
271
|
+
self._tool_evaluation_check_list: set[EvaluationMetricName] = set()
|
|
272
|
+
self._mcp_manager = mcp_manager
|
|
273
|
+
self._a2a_manager = a2a_manager
|
|
274
|
+
self._init__tools(event)
|
|
275
|
+
|
|
276
|
+
def _init__tools(self, event: ChatEvent) -> None:
|
|
277
|
+
tool_choices = self._tool_choices
|
|
278
|
+
tool_configs = self._config.tools
|
|
279
|
+
self._logger.info("Initializing tool definitions...")
|
|
280
|
+
self._logger.info(f"Tool choices: {tool_choices}")
|
|
281
|
+
|
|
282
|
+
tool_configs, sub_agents = self._a2a_manager.get_all_sub_agents(
|
|
283
|
+
tool_configs, event
|
|
284
|
+
)
|
|
285
|
+
|
|
286
|
+
# Build internal tools from configurations
|
|
287
|
+
self._internal_tools = [
|
|
288
|
+
ToolFactory.build_tool_with_settings(
|
|
289
|
+
t.name,
|
|
290
|
+
t,
|
|
291
|
+
t.configuration,
|
|
292
|
+
event,
|
|
293
|
+
tool_progress_reporter=self._tool_progress_reporter,
|
|
294
|
+
)
|
|
295
|
+
for t in tool_configs
|
|
296
|
+
]
|
|
297
|
+
|
|
298
|
+
# Get MCP tools (these are already properly instantiated)
|
|
299
|
+
self._mcp_tools = self._mcp_manager.get_all_mcp_tools()
|
|
300
|
+
# Combine both types of tools
|
|
301
|
+
self.available_tools = self._internal_tools + self._mcp_tools + sub_agents
|
|
302
|
+
self._sub_agents = sub_agents
|
|
303
|
+
|
|
304
|
+
for t in self.available_tools:
|
|
305
|
+
if not t.is_enabled():
|
|
306
|
+
continue
|
|
307
|
+
if t.name in self._disabled_tools:
|
|
308
|
+
continue
|
|
309
|
+
# if tool choices are given, only include those tools
|
|
310
|
+
if len(self._tool_choices) > 0 and t.name not in self._tool_choices:
|
|
311
|
+
continue
|
|
312
|
+
# is the tool exclusive and has been choosen by the user?
|
|
313
|
+
if t.is_exclusive() and len(tool_choices) > 0 and t.name in tool_choices:
|
|
314
|
+
self._tools = [t] # override all other tools
|
|
315
|
+
break
|
|
316
|
+
# if the tool is exclusive but no tool choices are given, skip it
|
|
317
|
+
if t.is_exclusive():
|
|
318
|
+
continue
|
|
319
|
+
|
|
320
|
+
self._tools.append(t)
|
|
321
|
+
|
|
322
|
+
@override
|
|
323
|
+
def filter_tool_calls(
|
|
324
|
+
self,
|
|
325
|
+
tool_calls: list[LanguageModelFunction],
|
|
326
|
+
tool_types: list[Literal["mcp", "internal", "subagent"]],
|
|
327
|
+
) -> list[LanguageModelFunction]:
|
|
328
|
+
filtered_calls = []
|
|
329
|
+
|
|
330
|
+
# Build sets for efficient lookup
|
|
331
|
+
internal_tool_names = {tool.name for tool in self._internal_tools}
|
|
332
|
+
mcp_tool_names = {tool.name for tool in self._mcp_tools}
|
|
333
|
+
sub_agent_names = {tool.name for tool in self._sub_agents}
|
|
334
|
+
|
|
335
|
+
for tool_call in tool_calls:
|
|
336
|
+
if "internal" in tool_types and tool_call.name in internal_tool_names:
|
|
337
|
+
filtered_calls.append(tool_call)
|
|
338
|
+
elif "mcp" in tool_types and tool_call.name in mcp_tool_names:
|
|
339
|
+
filtered_calls.append(tool_call)
|
|
340
|
+
elif "subagent" in tool_types and tool_call.name in sub_agent_names:
|
|
341
|
+
filtered_calls.append(tool_call)
|
|
342
|
+
|
|
343
|
+
return filtered_calls
|
|
344
|
+
|
|
345
|
+
@property
|
|
346
|
+
def sub_agents(self) -> list[SubAgentTool]:
|
|
347
|
+
return self._sub_agents
|
|
348
|
+
|
|
349
|
+
def get_evaluation_check_list(self) -> list[EvaluationMetricName]:
|
|
350
|
+
return list(self._tool_evaluation_check_list)
|
|
351
|
+
|
|
352
|
+
def log_loaded_tools(self):
|
|
353
|
+
self._logger.info(f"Loaded tools: {[tool.name for tool in self._tools]}")
|
|
354
|
+
|
|
355
|
+
@override
|
|
356
|
+
def get_tool_by_name(self, name: str) -> Tool | None:
|
|
357
|
+
for tool in self._tools:
|
|
358
|
+
if tool.name == name:
|
|
359
|
+
return tool
|
|
360
|
+
return None
|
|
361
|
+
|
|
362
|
+
@override
|
|
363
|
+
def get_tool_choices(self) -> list[str]:
|
|
364
|
+
return self._tool_choices
|
|
365
|
+
|
|
366
|
+
@override
|
|
367
|
+
def get_exclusive_tools(self) -> list[str]:
|
|
368
|
+
return self._exclusive_tools
|
|
369
|
+
|
|
370
|
+
def get_tools(self) -> list[Tool]:
|
|
371
|
+
return self._tools # type: ignore
|
|
372
|
+
|
|
373
|
+
def get_forced_tools(
|
|
374
|
+
self,
|
|
375
|
+
) -> list[ChatCompletionNamedToolChoiceParam]:
|
|
376
|
+
return [
|
|
377
|
+
self._convert_to_forced_tool(t.name)
|
|
378
|
+
for t in self._tools
|
|
379
|
+
if t.name in self._tool_choices
|
|
380
|
+
]
|
|
381
|
+
|
|
382
|
+
def get_tool_definitions(
|
|
383
|
+
self,
|
|
384
|
+
) -> list[LanguageModelToolDescription]:
|
|
385
|
+
return [tool.tool_description() for tool in self._tools]
|
|
386
|
+
|
|
387
|
+
def get_tool_prompts(self) -> list[ToolPrompts]:
|
|
388
|
+
return [tool.get_tool_prompts() for tool in self._tools]
|
|
389
|
+
|
|
390
|
+
def add_forced_tool(self, name):
|
|
391
|
+
tool = self.get_tool_by_name(name)
|
|
392
|
+
if not tool:
|
|
393
|
+
raise ValueError(f"Tool {name} not found")
|
|
394
|
+
|
|
395
|
+
if tool.name not in self._tool_choices:
|
|
396
|
+
self._tool_choices.append(tool.name)
|
|
397
|
+
|
|
398
|
+
def _convert_to_forced_tool(
|
|
399
|
+
self, tool_name: str
|
|
400
|
+
) -> ChatCompletionNamedToolChoiceParam:
|
|
401
|
+
return {
|
|
402
|
+
"type": "function",
|
|
403
|
+
"function": {"name": tool_name},
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
def tool_choices(self) -> list[str]:
|
|
407
|
+
return self._tool_choices.copy()
|
|
408
|
+
|
|
409
|
+
|
|
410
|
+
class ResponsesApiToolManager(BaseToolManager):
|
|
411
|
+
def __init__(
|
|
412
|
+
self,
|
|
413
|
+
logger: Logger,
|
|
414
|
+
config: ToolManagerConfig,
|
|
415
|
+
tool_manager: ToolManager,
|
|
416
|
+
builtin_tools: list[OpenAIBuiltInTool],
|
|
417
|
+
) -> None:
|
|
418
|
+
super().__init__(config)
|
|
419
|
+
self._logger = logger
|
|
420
|
+
self._config = config
|
|
421
|
+
self._tool_manager = tool_manager
|
|
422
|
+
self._builtin_tools = builtin_tools
|
|
423
|
+
self._tools = self._tool_manager.get_tools()
|
|
424
|
+
|
|
425
|
+
@classmethod
|
|
426
|
+
async def build_manager(
|
|
427
|
+
cls,
|
|
428
|
+
logger: Logger,
|
|
429
|
+
config: ToolManagerConfig,
|
|
430
|
+
event: ChatEvent,
|
|
431
|
+
tool_progress_reporter: ToolProgressReporter,
|
|
432
|
+
mcp_manager: MCPManager,
|
|
433
|
+
a2a_manager: A2AManager,
|
|
434
|
+
builtin_tool_manager: OpenAIBuiltInToolManager,
|
|
435
|
+
) -> "ResponsesApiToolManager":
|
|
436
|
+
(
|
|
437
|
+
tool_configs,
|
|
438
|
+
builtin_tools,
|
|
439
|
+
) = await builtin_tool_manager.get_all_openai_builtin_tools(config.tools)
|
|
440
|
+
|
|
441
|
+
completions_tool_manager_config = ToolManagerConfig(
|
|
442
|
+
tools=tool_configs, max_tool_calls=config.max_tool_calls
|
|
443
|
+
)
|
|
444
|
+
completions_tool_manager = ToolManager(
|
|
445
|
+
logger=logger,
|
|
446
|
+
config=completions_tool_manager_config,
|
|
447
|
+
event=event,
|
|
448
|
+
tool_progress_reporter=tool_progress_reporter,
|
|
449
|
+
mcp_manager=mcp_manager,
|
|
450
|
+
a2a_manager=a2a_manager,
|
|
451
|
+
)
|
|
452
|
+
|
|
453
|
+
return cls(
|
|
454
|
+
logger=logger,
|
|
455
|
+
config=config,
|
|
456
|
+
tool_manager=completions_tool_manager,
|
|
457
|
+
builtin_tools=builtin_tools,
|
|
458
|
+
)
|
|
459
|
+
|
|
460
|
+
@override
|
|
461
|
+
def filter_tool_calls(
|
|
462
|
+
self,
|
|
463
|
+
tool_calls: list[LanguageModelFunction],
|
|
464
|
+
tool_types: list[Literal["mcp", "internal", "subagent"]],
|
|
465
|
+
) -> list[LanguageModelFunction]:
|
|
466
|
+
"""Delegate filtering to the underlying tool manager."""
|
|
467
|
+
return self._tool_manager.filter_tool_calls(tool_calls, tool_types)
|
|
468
|
+
|
|
469
|
+
@override
|
|
470
|
+
def get_tool_by_name(self, name: str) -> Tool | None:
|
|
471
|
+
return self._tool_manager.get_tool_by_name(name)
|
|
472
|
+
|
|
473
|
+
@override
|
|
474
|
+
def get_tool_choices(self) -> list[str]:
|
|
475
|
+
return self._tool_manager._tool_choices
|
|
476
|
+
|
|
477
|
+
@override
|
|
478
|
+
def get_exclusive_tools(self) -> list[str]:
|
|
479
|
+
return self._tool_manager._exclusive_tools
|
|
480
|
+
|
|
481
|
+
@property
|
|
482
|
+
def sub_agents(self) -> list[SubAgentTool]:
|
|
483
|
+
return self._tool_manager.sub_agents
|
|
484
|
+
|
|
485
|
+
def log_loaded_tools(self):
|
|
486
|
+
self._logger.info(
|
|
487
|
+
f"Loaded tools: {[tool.name for tool in self._tools + self._builtin_tools]}"
|
|
488
|
+
)
|
|
489
|
+
|
|
490
|
+
def get_tools(self) -> list[Tool]:
|
|
491
|
+
return self._tool_manager.get_tools()
|
|
492
|
+
|
|
493
|
+
def get_forced_tools(
|
|
494
|
+
self,
|
|
495
|
+
) -> list[response_create_params.ToolChoice]:
|
|
496
|
+
"""
|
|
497
|
+
Note that built-in tools cannot be forced at the moment
|
|
498
|
+
"""
|
|
499
|
+
return [
|
|
500
|
+
{
|
|
501
|
+
"name": t.name,
|
|
502
|
+
"type": "function",
|
|
503
|
+
}
|
|
504
|
+
for t in self._tools
|
|
505
|
+
if t.name in self._tool_manager.tool_choices()
|
|
506
|
+
]
|
|
507
|
+
|
|
508
|
+
def get_tool_definitions(
|
|
509
|
+
self,
|
|
510
|
+
) -> list[LanguageModelToolDescription | ToolParam]:
|
|
511
|
+
if len(self._tool_manager.tool_choices()) > 0:
|
|
512
|
+
# We cannot send a builtin tool in this case (api error)
|
|
513
|
+
return [tool.tool_description() for tool in self._tools]
|
|
514
|
+
else:
|
|
515
|
+
return [
|
|
516
|
+
tool.tool_description() for tool in self._tools + self._builtin_tools
|
|
517
|
+
]
|
|
518
|
+
|
|
519
|
+
def get_tool_prompts(self) -> list[ToolPrompts]:
|
|
520
|
+
return [tool.get_tool_prompts() for tool in self._tools + self._builtin_tools]
|
|
521
|
+
|
|
522
|
+
def add_forced_tool(self, name: str) -> None:
|
|
523
|
+
self._tool_manager.add_forced_tool(name)
|