unique_toolkit 1.15.0__py3-none-any.whl → 1.16.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.
- unique_toolkit/agentic/debug_info_manager/debug_info_manager.py +16 -6
- unique_toolkit/agentic/debug_info_manager/test/test_debug_info_manager.py +278 -0
- unique_toolkit/agentic/postprocessor/postprocessor_manager.py +50 -11
- 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/tools/factory.py +4 -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/tool_manager.py +290 -125
- unique_toolkit/chat/functions.py +15 -6
- unique_toolkit/chat/responses_api.py +461 -0
- unique_toolkit/language_model/functions.py +25 -9
- unique_toolkit/language_model/schemas.py +222 -27
- unique_toolkit/protocols/support.py +91 -9
- unique_toolkit/services/__init__.py +7 -0
- unique_toolkit/services/chat_service.py +139 -7
- {unique_toolkit-1.15.0.dist-info → unique_toolkit-1.16.1.dist-info}/METADATA +8 -1
- {unique_toolkit-1.15.0.dist-info → unique_toolkit-1.16.1.dist-info}/RECORD +26 -13
- {unique_toolkit-1.15.0.dist-info → unique_toolkit-1.16.1.dist-info}/LICENSE +0 -0
- {unique_toolkit-1.15.0.dist-info → unique_toolkit-1.16.1.dist-info}/WHEEL +0 -0
@@ -1,3 +1,5 @@
|
|
1
|
+
from typing import Any
|
2
|
+
|
1
3
|
from unique_toolkit.agentic.tools.schemas import ToolCallResponse
|
2
4
|
|
3
5
|
|
@@ -5,14 +7,22 @@ class DebugInfoManager:
|
|
5
7
|
def __init__(self):
|
6
8
|
self.debug_info = {"tools": []}
|
7
9
|
|
8
|
-
def extract_tool_debug_info(
|
10
|
+
def extract_tool_debug_info(
|
11
|
+
self,
|
12
|
+
tool_call_responses: list[ToolCallResponse],
|
13
|
+
loop_iteration_index: int | None = None,
|
14
|
+
):
|
9
15
|
for tool_call_response in tool_call_responses:
|
10
|
-
|
11
|
-
|
12
|
-
|
16
|
+
tool_info = {
|
17
|
+
"name": tool_call_response.name,
|
18
|
+
"info": tool_call_response.debug_info,
|
19
|
+
}
|
20
|
+
if loop_iteration_index is not None:
|
21
|
+
tool_info["info"]["loop_iteration"] = loop_iteration_index
|
22
|
+
self.debug_info["tools"].append(tool_info)
|
13
23
|
|
14
|
-
def add(self, key, value):
|
24
|
+
def add(self, key: str, value: Any) -> None:
|
15
25
|
self.debug_info = self.debug_info | {key: value}
|
16
26
|
|
17
|
-
def get(self):
|
27
|
+
def get(self) -> dict[str, Any]:
|
18
28
|
return self.debug_info
|
@@ -0,0 +1,278 @@
|
|
1
|
+
"""
|
2
|
+
Test suite for DebugInfoManager class.
|
3
|
+
|
4
|
+
This test suite validates the DebugInfoManager's ability to:
|
5
|
+
1. Initialize with empty debug info
|
6
|
+
2. Extract tool debug info from ToolCallResponse objects
|
7
|
+
3. Handle loop iteration indices
|
8
|
+
4. Add arbitrary key-value pairs to debug info
|
9
|
+
5. Retrieve the complete debug info dictionary
|
10
|
+
"""
|
11
|
+
|
12
|
+
from unique_toolkit.agentic.debug_info_manager.debug_info_manager import (
|
13
|
+
DebugInfoManager,
|
14
|
+
)
|
15
|
+
from unique_toolkit.agentic.tools.schemas import ToolCallResponse
|
16
|
+
|
17
|
+
|
18
|
+
class TestDebugInfoManager:
|
19
|
+
"""Test suite for DebugInfoManager functionality."""
|
20
|
+
|
21
|
+
def test_init__initializes_empty_debug_info__on_creation(self):
|
22
|
+
"""Test that DebugInfoManager initializes with empty tools list."""
|
23
|
+
manager = DebugInfoManager()
|
24
|
+
|
25
|
+
assert manager.debug_info == {"tools": []}
|
26
|
+
assert manager.get() == {"tools": []}
|
27
|
+
|
28
|
+
def test_extract_tool_debug_info__adds_single_tool__with_valid_response(self):
|
29
|
+
"""Test extracting debug info from a single ToolCallResponse."""
|
30
|
+
manager = DebugInfoManager()
|
31
|
+
tool_call_response = ToolCallResponse(
|
32
|
+
id="tool_1",
|
33
|
+
name="TestTool",
|
34
|
+
debug_info={"execution_time": "100ms", "status": "success"},
|
35
|
+
)
|
36
|
+
|
37
|
+
manager.extract_tool_debug_info([tool_call_response])
|
38
|
+
|
39
|
+
debug_info = manager.get()
|
40
|
+
assert len(debug_info["tools"]) == 1
|
41
|
+
assert debug_info["tools"][0]["name"] == "TestTool"
|
42
|
+
assert debug_info["tools"][0]["info"]["execution_time"] == "100ms"
|
43
|
+
assert debug_info["tools"][0]["info"]["status"] == "success"
|
44
|
+
|
45
|
+
def test_extract_tool_debug_info__adds_multiple_tools__with_multiple_responses(
|
46
|
+
self,
|
47
|
+
):
|
48
|
+
"""Test extracting debug info from multiple ToolCallResponse objects."""
|
49
|
+
manager = DebugInfoManager()
|
50
|
+
tool_call_responses = [
|
51
|
+
ToolCallResponse(
|
52
|
+
id="tool_1",
|
53
|
+
name="SearchTool",
|
54
|
+
debug_info={"query": "test query", "results": 5},
|
55
|
+
),
|
56
|
+
ToolCallResponse(
|
57
|
+
id="tool_2",
|
58
|
+
name="CalculatorTool",
|
59
|
+
debug_info={"operation": "add", "result": 42},
|
60
|
+
),
|
61
|
+
ToolCallResponse(
|
62
|
+
id="tool_3",
|
63
|
+
name="WeatherTool",
|
64
|
+
debug_info={"location": "New York", "temperature": "72F"},
|
65
|
+
),
|
66
|
+
]
|
67
|
+
|
68
|
+
manager.extract_tool_debug_info(tool_call_responses)
|
69
|
+
|
70
|
+
debug_info = manager.get()
|
71
|
+
assert len(debug_info["tools"]) == 3
|
72
|
+
assert debug_info["tools"][0]["name"] == "SearchTool"
|
73
|
+
assert debug_info["tools"][1]["name"] == "CalculatorTool"
|
74
|
+
assert debug_info["tools"][2]["name"] == "WeatherTool"
|
75
|
+
|
76
|
+
def test_extract_tool_debug_info__preserves_order__with_sequential_calls(self):
|
77
|
+
"""Test that multiple calls to extract_tool_debug_info preserve order."""
|
78
|
+
manager = DebugInfoManager()
|
79
|
+
|
80
|
+
# First call
|
81
|
+
manager.extract_tool_debug_info(
|
82
|
+
[ToolCallResponse(id="tool_1", name="Tool1", debug_info={"step": 1})]
|
83
|
+
)
|
84
|
+
|
85
|
+
# Second call
|
86
|
+
manager.extract_tool_debug_info(
|
87
|
+
[ToolCallResponse(id="tool_2", name="Tool2", debug_info={"step": 2})]
|
88
|
+
)
|
89
|
+
|
90
|
+
# Third call
|
91
|
+
manager.extract_tool_debug_info(
|
92
|
+
[ToolCallResponse(id="tool_3", name="Tool3", debug_info={"step": 3})]
|
93
|
+
)
|
94
|
+
|
95
|
+
debug_info = manager.get()
|
96
|
+
assert len(debug_info["tools"]) == 3
|
97
|
+
assert debug_info["tools"][0]["info"]["step"] == 1
|
98
|
+
assert debug_info["tools"][1]["info"]["step"] == 2
|
99
|
+
assert debug_info["tools"][2]["info"]["step"] == 3
|
100
|
+
|
101
|
+
def test_extract_tool_debug_info__adds_loop_iteration__when_index_provided(self):
|
102
|
+
"""Test that loop_iteration_index is added to debug info when provided."""
|
103
|
+
manager = DebugInfoManager()
|
104
|
+
tool_call_response = ToolCallResponse(
|
105
|
+
id="tool_1", name="IterativeTool", debug_info={"status": "processing"}
|
106
|
+
)
|
107
|
+
|
108
|
+
manager.extract_tool_debug_info([tool_call_response], loop_iteration_index=3)
|
109
|
+
|
110
|
+
debug_info = manager.get()
|
111
|
+
assert debug_info["tools"][0]["info"]["loop_iteration"] == 3
|
112
|
+
assert debug_info["tools"][0]["info"]["status"] == "processing"
|
113
|
+
|
114
|
+
def test_extract_tool_debug_info__omits_loop_iteration__when_index_is_none(self):
|
115
|
+
"""Test that loop_iteration is not added when index is None."""
|
116
|
+
manager = DebugInfoManager()
|
117
|
+
tool_call_response = ToolCallResponse(
|
118
|
+
id="tool_1", name="SingleRunTool", debug_info={"status": "complete"}
|
119
|
+
)
|
120
|
+
|
121
|
+
manager.extract_tool_debug_info([tool_call_response], loop_iteration_index=None)
|
122
|
+
|
123
|
+
debug_info = manager.get()
|
124
|
+
assert "loop_iteration" not in debug_info["tools"][0]["info"]
|
125
|
+
assert debug_info["tools"][0]["info"]["status"] == "complete"
|
126
|
+
|
127
|
+
def test_extract_tool_debug_info__handles_empty_debug_info__gracefully(self):
|
128
|
+
"""Test extracting from ToolCallResponse with empty debug_info dict."""
|
129
|
+
manager = DebugInfoManager()
|
130
|
+
tool_call_response = ToolCallResponse(
|
131
|
+
id="tool_1", name="MinimalTool", debug_info={}
|
132
|
+
)
|
133
|
+
|
134
|
+
manager.extract_tool_debug_info([tool_call_response])
|
135
|
+
|
136
|
+
debug_info = manager.get()
|
137
|
+
assert len(debug_info["tools"]) == 1
|
138
|
+
assert debug_info["tools"][0]["name"] == "MinimalTool"
|
139
|
+
assert debug_info["tools"][0]["info"] == {}
|
140
|
+
|
141
|
+
def test_extract_tool_debug_info__handles_empty_list__without_error(self):
|
142
|
+
"""Test that passing an empty list doesn't cause errors."""
|
143
|
+
manager = DebugInfoManager()
|
144
|
+
|
145
|
+
manager.extract_tool_debug_info([])
|
146
|
+
|
147
|
+
debug_info = manager.get()
|
148
|
+
assert debug_info["tools"] == []
|
149
|
+
|
150
|
+
def test_add__adds_new_key_value_pair__to_debug_info(self):
|
151
|
+
"""Test adding a new key-value pair to debug_info."""
|
152
|
+
manager = DebugInfoManager()
|
153
|
+
|
154
|
+
manager.add("execution_summary", {"total_time": "500ms", "total_calls": 5})
|
155
|
+
|
156
|
+
debug_info = manager.get()
|
157
|
+
assert "execution_summary" in debug_info
|
158
|
+
assert debug_info["execution_summary"]["total_time"] == "500ms"
|
159
|
+
assert debug_info["execution_summary"]["total_calls"] == 5
|
160
|
+
|
161
|
+
def test_add__preserves_tools_list__when_adding_new_keys(self):
|
162
|
+
"""Test that add() preserves the tools list."""
|
163
|
+
manager = DebugInfoManager()
|
164
|
+
manager.extract_tool_debug_info(
|
165
|
+
[
|
166
|
+
ToolCallResponse(
|
167
|
+
id="tool_1", name="TestTool", debug_info={"test": "data"}
|
168
|
+
)
|
169
|
+
]
|
170
|
+
)
|
171
|
+
|
172
|
+
manager.add("metadata", {"version": "1.0"})
|
173
|
+
|
174
|
+
debug_info = manager.get()
|
175
|
+
assert len(debug_info["tools"]) == 1
|
176
|
+
assert debug_info["tools"][0]["name"] == "TestTool"
|
177
|
+
assert debug_info["metadata"]["version"] == "1.0"
|
178
|
+
|
179
|
+
def test_add__overwrites_existing_key__when_key_exists(self):
|
180
|
+
"""Test that add() overwrites an existing key."""
|
181
|
+
manager = DebugInfoManager()
|
182
|
+
manager.add("status", "in_progress")
|
183
|
+
manager.add("status", "completed")
|
184
|
+
|
185
|
+
debug_info = manager.get()
|
186
|
+
assert debug_info["status"] == "completed"
|
187
|
+
|
188
|
+
def test_add__adds_multiple_keys__with_sequential_calls(self):
|
189
|
+
"""Test adding multiple key-value pairs with sequential calls."""
|
190
|
+
manager = DebugInfoManager()
|
191
|
+
|
192
|
+
manager.add("key1", "value1")
|
193
|
+
manager.add("key2", {"nested": "value2"})
|
194
|
+
manager.add("key3", [1, 2, 3])
|
195
|
+
|
196
|
+
debug_info = manager.get()
|
197
|
+
assert debug_info["key1"] == "value1"
|
198
|
+
assert debug_info["key2"]["nested"] == "value2"
|
199
|
+
assert debug_info["key3"] == [1, 2, 3]
|
200
|
+
|
201
|
+
def test_get__returns_complete_debug_info__with_mixed_data(self):
|
202
|
+
"""Test get() returns complete debug info with tools and custom keys."""
|
203
|
+
manager = DebugInfoManager()
|
204
|
+
|
205
|
+
# Add tool debug info
|
206
|
+
manager.extract_tool_debug_info(
|
207
|
+
[ToolCallResponse(id="tool_1", name="Tool1", debug_info={"data": "test"})],
|
208
|
+
loop_iteration_index=0,
|
209
|
+
)
|
210
|
+
|
211
|
+
# Add custom keys
|
212
|
+
manager.add("start_time", "2025-10-16T10:00:00")
|
213
|
+
manager.add("end_time", "2025-10-16T10:01:00")
|
214
|
+
|
215
|
+
debug_info = manager.get()
|
216
|
+
|
217
|
+
assert "tools" in debug_info
|
218
|
+
assert "start_time" in debug_info
|
219
|
+
assert "end_time" in debug_info
|
220
|
+
assert len(debug_info["tools"]) == 1
|
221
|
+
assert debug_info["start_time"] == "2025-10-16T10:00:00"
|
222
|
+
|
223
|
+
def test_integration__complete_workflow__with_all_operations(self):
|
224
|
+
"""Integration test: complete workflow using all DebugInfoManager methods."""
|
225
|
+
manager = DebugInfoManager()
|
226
|
+
|
227
|
+
# Initial state
|
228
|
+
assert manager.get() == {"tools": []}
|
229
|
+
|
230
|
+
# Add some metadata
|
231
|
+
manager.add("session_id", "abc-123")
|
232
|
+
manager.add("user_id", "user-456")
|
233
|
+
|
234
|
+
# First tool call (loop iteration 0)
|
235
|
+
manager.extract_tool_debug_info(
|
236
|
+
[
|
237
|
+
ToolCallResponse(
|
238
|
+
id="tool_1",
|
239
|
+
name="SearchTool",
|
240
|
+
debug_info={"query": "AI research", "hits": 100},
|
241
|
+
)
|
242
|
+
],
|
243
|
+
loop_iteration_index=0,
|
244
|
+
)
|
245
|
+
|
246
|
+
# Second tool call (loop iteration 1)
|
247
|
+
manager.extract_tool_debug_info(
|
248
|
+
[
|
249
|
+
ToolCallResponse(
|
250
|
+
id="tool_2",
|
251
|
+
name="AnalysisTool",
|
252
|
+
debug_info={"processed": 50, "relevant": 10},
|
253
|
+
),
|
254
|
+
ToolCallResponse(
|
255
|
+
id="tool_3",
|
256
|
+
name="SummaryTool",
|
257
|
+
debug_info={"paragraphs": 3, "words": 250},
|
258
|
+
),
|
259
|
+
],
|
260
|
+
loop_iteration_index=1,
|
261
|
+
)
|
262
|
+
|
263
|
+
# Add final summary
|
264
|
+
manager.add("summary", {"total_tools": 3, "total_iterations": 2})
|
265
|
+
|
266
|
+
# Verify complete debug info
|
267
|
+
debug_info = manager.get()
|
268
|
+
|
269
|
+
assert debug_info["session_id"] == "abc-123"
|
270
|
+
assert debug_info["user_id"] == "user-456"
|
271
|
+
assert len(debug_info["tools"]) == 3
|
272
|
+
assert debug_info["tools"][0]["name"] == "SearchTool"
|
273
|
+
assert debug_info["tools"][0]["info"]["loop_iteration"] == 0
|
274
|
+
assert debug_info["tools"][1]["name"] == "AnalysisTool"
|
275
|
+
assert debug_info["tools"][1]["info"]["loop_iteration"] == 1
|
276
|
+
assert debug_info["tools"][2]["name"] == "SummaryTool"
|
277
|
+
assert debug_info["tools"][2]["info"]["loop_iteration"] == 1
|
278
|
+
assert debug_info["summary"]["total_tools"] == 3
|
@@ -6,6 +6,7 @@ from unique_toolkit.agentic.tools.utils.execution.execution import SafeTaskExecu
|
|
6
6
|
from unique_toolkit.chat.service import ChatService
|
7
7
|
from unique_toolkit.language_model.schemas import (
|
8
8
|
LanguageModelStreamResponse,
|
9
|
+
ResponsesLanguageModelStreamResponse,
|
9
10
|
)
|
10
11
|
|
11
12
|
|
@@ -26,7 +27,30 @@ class Postprocessor(ABC):
|
|
26
27
|
"Subclasses must implement this method to apply post-processing to the response."
|
27
28
|
)
|
28
29
|
|
29
|
-
async def remove_from_text(self, text) -> str:
|
30
|
+
async def remove_from_text(self, text: str) -> str:
|
31
|
+
raise NotImplementedError(
|
32
|
+
"Subclasses must implement this method to remove post-processing from the message."
|
33
|
+
)
|
34
|
+
|
35
|
+
|
36
|
+
class ResponsesApiPostprocessor(ABC):
|
37
|
+
def __init__(self, name: str):
|
38
|
+
self.name = name
|
39
|
+
|
40
|
+
def get_name(self) -> str:
|
41
|
+
return self.name
|
42
|
+
|
43
|
+
async def run(self, loop_response: ResponsesLanguageModelStreamResponse) -> None:
|
44
|
+
raise NotImplementedError("Subclasses must implement this method.")
|
45
|
+
|
46
|
+
def apply_postprocessing_to_response(
|
47
|
+
self, loop_response: ResponsesLanguageModelStreamResponse
|
48
|
+
) -> bool:
|
49
|
+
raise NotImplementedError(
|
50
|
+
"Subclasses must implement this method to apply post-processing to the response."
|
51
|
+
)
|
52
|
+
|
53
|
+
async def remove_from_text(self, text: str) -> str:
|
30
54
|
raise NotImplementedError(
|
31
55
|
"Subclasses must implement this method to remove post-processing from the message."
|
32
56
|
)
|
@@ -59,12 +83,16 @@ class PostprocessorManager:
|
|
59
83
|
):
|
60
84
|
self._logger = logger
|
61
85
|
self._chat_service = chat_service
|
62
|
-
self._postprocessors: list[Postprocessor] = []
|
86
|
+
self._postprocessors: list[Postprocessor | ResponsesApiPostprocessor] = []
|
63
87
|
|
64
|
-
def add_postprocessor(
|
88
|
+
def add_postprocessor(
|
89
|
+
self, postprocessor: Postprocessor | ResponsesApiPostprocessor
|
90
|
+
):
|
65
91
|
self._postprocessors.append(postprocessor)
|
66
92
|
|
67
|
-
def get_postprocessors(
|
93
|
+
def get_postprocessors(
|
94
|
+
self, name: str
|
95
|
+
) -> list[Postprocessor | ResponsesApiPostprocessor]:
|
68
96
|
return self._postprocessors
|
69
97
|
|
70
98
|
async def run_postprocessors(
|
@@ -75,25 +103,36 @@ class PostprocessorManager:
|
|
75
103
|
logger=self._logger,
|
76
104
|
)
|
77
105
|
|
106
|
+
if isinstance(loop_response, ResponsesLanguageModelStreamResponse):
|
107
|
+
postprocessors = self._postprocessors
|
108
|
+
else:
|
109
|
+
postprocessors = [
|
110
|
+
postprocessor
|
111
|
+
for postprocessor in self._postprocessors
|
112
|
+
if isinstance(postprocessor, Postprocessor)
|
113
|
+
]
|
114
|
+
|
78
115
|
tasks = [
|
79
116
|
task_executor.execute_async(
|
80
117
|
self.execute_postprocessors,
|
81
118
|
loop_response=loop_response,
|
82
119
|
postprocessor_instance=postprocessor,
|
83
120
|
)
|
84
|
-
for postprocessor in
|
121
|
+
for postprocessor in postprocessors
|
85
122
|
]
|
86
123
|
postprocessor_results = await asyncio.gather(*tasks)
|
87
124
|
|
88
|
-
for
|
125
|
+
for postprocessor, result in zip(postprocessors, postprocessor_results):
|
89
126
|
if not result.success:
|
90
127
|
self._logger.warning(
|
91
|
-
|
128
|
+
"Postprocessor %s failed to run.",
|
129
|
+
postprocessor.get_name(),
|
130
|
+
exc_info=result.exception,
|
92
131
|
)
|
93
132
|
|
94
133
|
modification_results = [
|
95
|
-
postprocessor.apply_postprocessing_to_response(loop_response)
|
96
|
-
for postprocessor in
|
134
|
+
postprocessor.apply_postprocessing_to_response(loop_response) # type: ignore
|
135
|
+
for postprocessor in postprocessors
|
97
136
|
]
|
98
137
|
|
99
138
|
has_been_modified = any(modification_results)
|
@@ -108,9 +147,9 @@ class PostprocessorManager:
|
|
108
147
|
async def execute_postprocessors(
|
109
148
|
self,
|
110
149
|
loop_response: LanguageModelStreamResponse,
|
111
|
-
postprocessor_instance: Postprocessor,
|
150
|
+
postprocessor_instance: Postprocessor | ResponsesApiPostprocessor,
|
112
151
|
) -> None:
|
113
|
-
await postprocessor_instance.run(loop_response)
|
152
|
+
await postprocessor_instance.run(loop_response) # type: ignore
|
114
153
|
|
115
154
|
async def remove_from_text(
|
116
155
|
self,
|
@@ -0,0 +1,19 @@
|
|
1
|
+
from unique_toolkit.agentic.responses_api.postprocessors.code_display import (
|
2
|
+
ShowExecutedCodePostprocessor,
|
3
|
+
ShowExecutedCodePostprocessorConfig,
|
4
|
+
)
|
5
|
+
from unique_toolkit.agentic.responses_api.postprocessors.generated_files import (
|
6
|
+
DisplayCodeInterpreterFilesPostProcessor,
|
7
|
+
DisplayCodeInterpreterFilesPostProcessorConfig,
|
8
|
+
)
|
9
|
+
from unique_toolkit.agentic.responses_api.stream_handler import (
|
10
|
+
ResponsesStreamingHandler,
|
11
|
+
)
|
12
|
+
|
13
|
+
__all__ = [
|
14
|
+
"ShowExecutedCodePostprocessor",
|
15
|
+
"ShowExecutedCodePostprocessorConfig",
|
16
|
+
"DisplayCodeInterpreterFilesPostProcessorConfig",
|
17
|
+
"DisplayCodeInterpreterFilesPostProcessor",
|
18
|
+
"ResponsesStreamingHandler",
|
19
|
+
]
|
@@ -0,0 +1,63 @@
|
|
1
|
+
import logging
|
2
|
+
import re
|
3
|
+
from typing import override
|
4
|
+
|
5
|
+
from pydantic import BaseModel, Field
|
6
|
+
|
7
|
+
from unique_toolkit.agentic.postprocessor.postprocessor_manager import (
|
8
|
+
ResponsesApiPostprocessor,
|
9
|
+
)
|
10
|
+
from unique_toolkit.agentic.tools.config import get_configuration_dict
|
11
|
+
from unique_toolkit.language_model.schemas import ResponsesLanguageModelStreamResponse
|
12
|
+
|
13
|
+
_TEMPLATE = """
|
14
|
+
<details><summary>Code Interpreter Call</summary>
|
15
|
+
|
16
|
+
```python
|
17
|
+
{code}
|
18
|
+
```
|
19
|
+
|
20
|
+
</details>
|
21
|
+
</br>
|
22
|
+
|
23
|
+
""".lstrip()
|
24
|
+
|
25
|
+
logger = logging.getLogger(__name__)
|
26
|
+
|
27
|
+
|
28
|
+
class ShowExecutedCodePostprocessorConfig(BaseModel):
|
29
|
+
model_config = get_configuration_dict()
|
30
|
+
remove_from_history: bool = Field(
|
31
|
+
default=False,
|
32
|
+
description="If set, the code interpreter call will be removed from the history on subsequent calls to the assistant.",
|
33
|
+
)
|
34
|
+
|
35
|
+
|
36
|
+
class ShowExecutedCodePostprocessor(ResponsesApiPostprocessor):
|
37
|
+
def __init__(self, config: ShowExecutedCodePostprocessorConfig):
|
38
|
+
super().__init__(self.__class__.__name__)
|
39
|
+
self._config = config
|
40
|
+
|
41
|
+
@override
|
42
|
+
async def run(self, loop_response: ResponsesLanguageModelStreamResponse) -> None:
|
43
|
+
return None
|
44
|
+
|
45
|
+
@override
|
46
|
+
def apply_postprocessing_to_response(
|
47
|
+
self, loop_response: ResponsesLanguageModelStreamResponse
|
48
|
+
) -> bool:
|
49
|
+
prepended_text = ""
|
50
|
+
for output in loop_response.code_interpreter_calls:
|
51
|
+
prepended_text += _TEMPLATE.format(code=output.code)
|
52
|
+
|
53
|
+
loop_response.message.text = prepended_text + loop_response.message.text
|
54
|
+
|
55
|
+
return prepended_text != ""
|
56
|
+
|
57
|
+
@override
|
58
|
+
async def remove_from_text(self, text) -> str:
|
59
|
+
if not self._config.remove_from_history:
|
60
|
+
return text
|
61
|
+
# Remove code interpreter blocks using regex
|
62
|
+
pattern = r"<details><summary>Code Interpreter Call</summary>.*?</details>"
|
63
|
+
return re.sub(pattern, "", text, flags=re.DOTALL)
|
@@ -0,0 +1,145 @@
|
|
1
|
+
import logging
|
2
|
+
import re
|
3
|
+
from mimetypes import guess_type
|
4
|
+
from typing import override
|
5
|
+
|
6
|
+
from openai import AsyncOpenAI
|
7
|
+
from pydantic import BaseModel
|
8
|
+
from unique_sdk import Content
|
9
|
+
|
10
|
+
from unique_toolkit.agentic.postprocessor.postprocessor_manager import (
|
11
|
+
ResponsesApiPostprocessor,
|
12
|
+
)
|
13
|
+
from unique_toolkit.agentic.tools.config import get_configuration_dict
|
14
|
+
from unique_toolkit.content.schemas import ContentReference
|
15
|
+
from unique_toolkit.content.service import ContentService
|
16
|
+
from unique_toolkit.language_model.schemas import ResponsesLanguageModelStreamResponse
|
17
|
+
|
18
|
+
logger = logging.getLogger(__name__)
|
19
|
+
|
20
|
+
|
21
|
+
class DisplayCodeInterpreterFilesPostProcessorConfig(BaseModel):
|
22
|
+
model_config = get_configuration_dict()
|
23
|
+
upload_scope_id: str
|
24
|
+
|
25
|
+
|
26
|
+
class DisplayCodeInterpreterFilesPostProcessor(
|
27
|
+
ResponsesApiPostprocessor,
|
28
|
+
):
|
29
|
+
def __init__(
|
30
|
+
self,
|
31
|
+
client: AsyncOpenAI,
|
32
|
+
content_service: ContentService,
|
33
|
+
config: DisplayCodeInterpreterFilesPostProcessorConfig,
|
34
|
+
) -> None:
|
35
|
+
super().__init__(self.__class__.__name__)
|
36
|
+
self._content_service = content_service
|
37
|
+
self._config = config
|
38
|
+
self._client = client
|
39
|
+
self._content_map = {}
|
40
|
+
|
41
|
+
@override
|
42
|
+
async def run(self, loop_response: ResponsesLanguageModelStreamResponse) -> None:
|
43
|
+
logger.info("Fetching and adding code interpreter files to the response")
|
44
|
+
|
45
|
+
container_files = loop_response.container_files
|
46
|
+
logger.info("Found %s container files", len(container_files))
|
47
|
+
|
48
|
+
self._content_map = {}
|
49
|
+
for container_file in container_files:
|
50
|
+
logger.info("Fetching file content for %s", container_file.filename)
|
51
|
+
file_content = await self._client.containers.files.content.retrieve(
|
52
|
+
container_id=container_file.container_id, file_id=container_file.file_id
|
53
|
+
)
|
54
|
+
|
55
|
+
logger.info(
|
56
|
+
"Uploading file content for %s to knowledge base",
|
57
|
+
container_file.filename,
|
58
|
+
)
|
59
|
+
content = self._content_service.upload_content_from_bytes(
|
60
|
+
content=file_content.content,
|
61
|
+
content_name=container_file.filename,
|
62
|
+
skip_ingestion=True,
|
63
|
+
mime_type=guess_type(container_file.filename)[0] or "text/plain",
|
64
|
+
scope_id=self._config.upload_scope_id,
|
65
|
+
)
|
66
|
+
self._content_map[container_file.filename] = content
|
67
|
+
|
68
|
+
@override
|
69
|
+
def apply_postprocessing_to_response(
|
70
|
+
self, loop_response: ResponsesLanguageModelStreamResponse
|
71
|
+
) -> bool:
|
72
|
+
ref_number = _get_next_ref_number(loop_response.message.references)
|
73
|
+
changed = False
|
74
|
+
# images
|
75
|
+
for filename, content in self._content_map.items():
|
76
|
+
# Images
|
77
|
+
loop_response.message.text, replaced = _replace_container_image_citation(
|
78
|
+
text=loop_response.message.text, filename=filename, content=content
|
79
|
+
)
|
80
|
+
changed |= replaced
|
81
|
+
|
82
|
+
# Files
|
83
|
+
loop_response.message.text, replaced = _replace_container_file_citation(
|
84
|
+
text=loop_response.message.text,
|
85
|
+
filename=filename,
|
86
|
+
ref_number=ref_number,
|
87
|
+
)
|
88
|
+
changed |= replaced
|
89
|
+
if replaced:
|
90
|
+
loop_response.message.references.append(
|
91
|
+
ContentReference(
|
92
|
+
sequence_number=ref_number,
|
93
|
+
source_id=content.id,
|
94
|
+
source="node-ingestion-chunks",
|
95
|
+
url=f"unique://content/{content.id}",
|
96
|
+
name=filename,
|
97
|
+
)
|
98
|
+
)
|
99
|
+
ref_number += 1
|
100
|
+
return changed
|
101
|
+
|
102
|
+
@override
|
103
|
+
async def remove_from_text(self, text) -> str:
|
104
|
+
return text
|
105
|
+
|
106
|
+
|
107
|
+
def _get_next_ref_number(references: list[ContentReference]) -> int:
|
108
|
+
max_ref_number = 0
|
109
|
+
for ref in references:
|
110
|
+
max_ref_number = max(max_ref_number, ref.sequence_number)
|
111
|
+
return max_ref_number + 1
|
112
|
+
|
113
|
+
|
114
|
+
def _replace_container_image_citation(
|
115
|
+
text: str, filename: str, content: Content
|
116
|
+
) -> tuple[str, bool]:
|
117
|
+
image_markdown = rf"!\[.*?\]\(sandbox:/mnt/data/{re.escape(filename)}\)"
|
118
|
+
|
119
|
+
if not re.search(image_markdown, text):
|
120
|
+
logger.info("No image markdown found for %s", filename)
|
121
|
+
return text, False
|
122
|
+
|
123
|
+
logger.info("Displaying image %s", filename)
|
124
|
+
return re.sub(
|
125
|
+
image_markdown,
|
126
|
+
f"",
|
127
|
+
text,
|
128
|
+
), True
|
129
|
+
|
130
|
+
|
131
|
+
def _replace_container_file_citation(
|
132
|
+
text: str, filename: str, ref_number: int
|
133
|
+
) -> tuple[str, bool]:
|
134
|
+
file_markdown = rf"\[.*?\]\(sandbox:/mnt/data/{re.escape(filename)}\)"
|
135
|
+
|
136
|
+
if not re.search(file_markdown, text):
|
137
|
+
logger.info("No file markdown found for %s", filename)
|
138
|
+
return text, False
|
139
|
+
|
140
|
+
logger.info("Displaying file %s", filename)
|
141
|
+
return re.sub(
|
142
|
+
file_markdown,
|
143
|
+
f"<sup>{ref_number}</sup>",
|
144
|
+
text,
|
145
|
+
), True
|
@@ -0,0 +1,15 @@
|
|
1
|
+
from unique_toolkit.protocols.support import ResponsesSupportCompleteWithReferences
|
2
|
+
from unique_toolkit.services.chat_service import ChatService
|
3
|
+
|
4
|
+
|
5
|
+
class ResponsesStreamingHandler(ResponsesSupportCompleteWithReferences):
|
6
|
+
def __init__(self, chat_service: ChatService):
|
7
|
+
self._chat_service = chat_service
|
8
|
+
|
9
|
+
def complete_with_references(self, *args, **kwargs):
|
10
|
+
return self._chat_service.complete_responses_with_references(*args, **kwargs)
|
11
|
+
|
12
|
+
async def complete_with_references_async(self, *args, **kwargs):
|
13
|
+
return await self._chat_service.complete_responses_with_references_async(
|
14
|
+
*args, **kwargs
|
15
|
+
)
|
@@ -11,6 +11,10 @@ class ToolFactory:
|
|
11
11
|
tool_map: dict[str, type[Tool]] = {}
|
12
12
|
tool_config_map: dict[str, Callable] = {}
|
13
13
|
|
14
|
+
@classmethod
|
15
|
+
def register_tool_config(cls, tool_name: str, tool_config: type[BaseToolConfig]):
|
16
|
+
cls.tool_config_map[tool_name] = tool_config
|
17
|
+
|
14
18
|
@classmethod
|
15
19
|
def register_tool(
|
16
20
|
cls,
|