openai-sdk-helpers 0.4.3__py3-none-any.whl → 0.5.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.
- openai_sdk_helpers/__init__.py +41 -7
- openai_sdk_helpers/agent/__init__.py +1 -2
- openai_sdk_helpers/agent/base.py +89 -173
- openai_sdk_helpers/agent/configuration.py +12 -20
- openai_sdk_helpers/agent/coordinator.py +14 -17
- openai_sdk_helpers/agent/runner.py +3 -45
- openai_sdk_helpers/agent/search/base.py +49 -71
- openai_sdk_helpers/agent/search/vector.py +82 -110
- openai_sdk_helpers/agent/search/web.py +103 -81
- openai_sdk_helpers/agent/summarizer.py +20 -28
- openai_sdk_helpers/agent/translator.py +17 -23
- openai_sdk_helpers/agent/validator.py +17 -23
- openai_sdk_helpers/errors.py +9 -0
- openai_sdk_helpers/extract/__init__.py +23 -0
- openai_sdk_helpers/extract/extractor.py +157 -0
- openai_sdk_helpers/extract/generator.py +476 -0
- openai_sdk_helpers/prompt/extractor_config_agent_instructions.jinja +6 -0
- openai_sdk_helpers/prompt/extractor_config_generator.jinja +37 -0
- openai_sdk_helpers/prompt/extractor_config_generator_instructions.jinja +9 -0
- openai_sdk_helpers/prompt/extractor_prompt_optimizer_agent_instructions.jinja +4 -0
- openai_sdk_helpers/prompt/extractor_prompt_optimizer_request.jinja +11 -0
- openai_sdk_helpers/response/__init__.py +2 -6
- openai_sdk_helpers/response/base.py +85 -94
- openai_sdk_helpers/response/configuration.py +39 -14
- openai_sdk_helpers/response/files.py +2 -0
- openai_sdk_helpers/response/runner.py +1 -48
- openai_sdk_helpers/response/tool_call.py +0 -141
- openai_sdk_helpers/response/vector_store.py +8 -5
- openai_sdk_helpers/streamlit_app/app.py +1 -1
- openai_sdk_helpers/structure/__init__.py +16 -0
- openai_sdk_helpers/structure/base.py +239 -278
- openai_sdk_helpers/structure/extraction.py +1228 -0
- openai_sdk_helpers/structure/plan/plan.py +0 -20
- openai_sdk_helpers/structure/plan/task.py +0 -33
- openai_sdk_helpers/structure/prompt.py +16 -0
- openai_sdk_helpers/structure/responses.py +2 -2
- openai_sdk_helpers/structure/web_search.py +0 -10
- openai_sdk_helpers/tools.py +346 -99
- openai_sdk_helpers/utils/__init__.py +7 -0
- openai_sdk_helpers/utils/json/base_model.py +315 -32
- openai_sdk_helpers/utils/langextract.py +194 -0
- {openai_sdk_helpers-0.4.3.dist-info → openai_sdk_helpers-0.5.0.dist-info}/METADATA +18 -4
- {openai_sdk_helpers-0.4.3.dist-info → openai_sdk_helpers-0.5.0.dist-info}/RECORD +46 -37
- openai_sdk_helpers/streamlit_app/streamlit_web_search.py +0 -75
- {openai_sdk_helpers-0.4.3.dist-info → openai_sdk_helpers-0.5.0.dist-info}/WHEEL +0 -0
- {openai_sdk_helpers-0.4.3.dist-info → openai_sdk_helpers-0.5.0.dist-info}/entry_points.txt +0 -0
- {openai_sdk_helpers-0.4.3.dist-info → openai_sdk_helpers-0.5.0.dist-info}/licenses/LICENSE +0 -0
openai_sdk_helpers/__init__.py
CHANGED
|
@@ -16,6 +16,7 @@ from .errors import (
|
|
|
16
16
|
InputValidationError,
|
|
17
17
|
AsyncExecutionError,
|
|
18
18
|
ResourceCleanupError,
|
|
19
|
+
ExtractionError,
|
|
19
20
|
)
|
|
20
21
|
|
|
21
22
|
from .utils.validation import (
|
|
@@ -40,6 +41,11 @@ from .structure import (
|
|
|
40
41
|
ExtendedSummaryStructure,
|
|
41
42
|
ValidationResultStructure,
|
|
42
43
|
AgentBlueprint,
|
|
44
|
+
AnnotatedDocumentStructure,
|
|
45
|
+
AttributeStructure,
|
|
46
|
+
DocumentStructure,
|
|
47
|
+
ExampleDataStructure,
|
|
48
|
+
ExtractionStructure,
|
|
43
49
|
create_plan,
|
|
44
50
|
execute_task,
|
|
45
51
|
execute_plan,
|
|
@@ -67,15 +73,15 @@ from .response import (
|
|
|
67
73
|
ResponseConfiguration,
|
|
68
74
|
ResponseRegistry,
|
|
69
75
|
get_default_registry,
|
|
70
|
-
parse_tool_arguments,
|
|
71
76
|
attach_vector_store,
|
|
72
77
|
)
|
|
73
78
|
from .tools import (
|
|
74
|
-
serialize_tool_result,
|
|
75
79
|
tool_handler_factory,
|
|
76
80
|
StructureType,
|
|
81
|
+
ToolHandler,
|
|
82
|
+
ToolHandlerRegistration,
|
|
77
83
|
ToolSpec,
|
|
78
|
-
|
|
84
|
+
build_tool_definition_list,
|
|
79
85
|
)
|
|
80
86
|
from .settings import build_openai_settings
|
|
81
87
|
from .utils.output_validation import (
|
|
@@ -87,7 +93,17 @@ from .utils.output_validation import (
|
|
|
87
93
|
OutputValidator,
|
|
88
94
|
validate_output,
|
|
89
95
|
)
|
|
90
|
-
|
|
96
|
+
from .utils.langextract import LangExtractAdapter, build_langextract_adapter
|
|
97
|
+
from .extract import (
|
|
98
|
+
DocumentExtractor,
|
|
99
|
+
EXTRACTOR_CONFIG_AGENT_INSTRUCTIONS,
|
|
100
|
+
EXTRACTOR_CONFIG_GENERATOR,
|
|
101
|
+
PROMPT_OPTIMIZER_AGENT_INSTRUCTIONS,
|
|
102
|
+
generate_document_extractor_config,
|
|
103
|
+
generate_document_extractor_config_with_agent,
|
|
104
|
+
optimize_extractor_prompt,
|
|
105
|
+
optimize_extractor_prompt_with_agent,
|
|
106
|
+
)
|
|
91
107
|
|
|
92
108
|
__all__ = [
|
|
93
109
|
# Environment utilities
|
|
@@ -106,6 +122,7 @@ __all__ = [
|
|
|
106
122
|
"InputValidationError",
|
|
107
123
|
"AsyncExecutionError",
|
|
108
124
|
"ResourceCleanupError",
|
|
125
|
+
"ExtractionError",
|
|
109
126
|
# Validation
|
|
110
127
|
"validate_non_empty_string",
|
|
111
128
|
"validate_max_length",
|
|
@@ -143,6 +160,11 @@ __all__ = [
|
|
|
143
160
|
"WebSearchStructure",
|
|
144
161
|
"VectorSearchStructure",
|
|
145
162
|
"ValidationResultStructure",
|
|
163
|
+
"AnnotatedDocumentStructure",
|
|
164
|
+
"AttributeStructure",
|
|
165
|
+
"DocumentStructure",
|
|
166
|
+
"ExampleDataStructure",
|
|
167
|
+
"ExtractionStructure",
|
|
146
168
|
"ResponseBase",
|
|
147
169
|
"ResponseMessage",
|
|
148
170
|
"ResponseMessages",
|
|
@@ -150,13 +172,13 @@ __all__ = [
|
|
|
150
172
|
"ResponseConfiguration",
|
|
151
173
|
"ResponseRegistry",
|
|
152
174
|
"get_default_registry",
|
|
153
|
-
"parse_tool_arguments",
|
|
154
175
|
"attach_vector_store",
|
|
155
|
-
"serialize_tool_result",
|
|
156
176
|
"tool_handler_factory",
|
|
157
177
|
"StructureType",
|
|
178
|
+
"ToolHandler",
|
|
179
|
+
"ToolHandlerRegistration",
|
|
158
180
|
"ToolSpec",
|
|
159
|
-
"
|
|
181
|
+
"build_tool_definition_list",
|
|
160
182
|
"build_openai_settings",
|
|
161
183
|
"create_plan",
|
|
162
184
|
"execute_task",
|
|
@@ -169,4 +191,16 @@ __all__ = [
|
|
|
169
191
|
"LengthValidator",
|
|
170
192
|
"OutputValidator",
|
|
171
193
|
"validate_output",
|
|
194
|
+
# LangExtract
|
|
195
|
+
"LangExtractAdapter",
|
|
196
|
+
"build_langextract_adapter",
|
|
197
|
+
# Extraction helpers
|
|
198
|
+
"DocumentExtractor",
|
|
199
|
+
"EXTRACTOR_CONFIG_AGENT_INSTRUCTIONS",
|
|
200
|
+
"EXTRACTOR_CONFIG_GENERATOR",
|
|
201
|
+
"PROMPT_OPTIMIZER_AGENT_INSTRUCTIONS",
|
|
202
|
+
"generate_document_extractor_config",
|
|
203
|
+
"generate_document_extractor_config_with_agent",
|
|
204
|
+
"optimize_extractor_prompt",
|
|
205
|
+
"optimize_extractor_prompt_with_agent",
|
|
172
206
|
]
|
|
@@ -5,7 +5,7 @@ from .base import AgentBase
|
|
|
5
5
|
from .configuration import AgentConfiguration, AgentRegistry, get_default_registry
|
|
6
6
|
from ..structure.plan.enum import AgentEnum
|
|
7
7
|
from .coordinator import CoordinatorAgent
|
|
8
|
-
from .runner import run_sync, run_async
|
|
8
|
+
from .runner import run_sync, run_async
|
|
9
9
|
from .search.base import SearchPlanner, SearchToolAgent, SearchWriter
|
|
10
10
|
from .summarizer import SummarizerAgent
|
|
11
11
|
from .translator import TranslatorAgent
|
|
@@ -23,7 +23,6 @@ __all__ = [
|
|
|
23
23
|
"CoordinatorAgent",
|
|
24
24
|
"run_sync",
|
|
25
25
|
"run_async",
|
|
26
|
-
"run_streamed",
|
|
27
26
|
"run_coroutine_agent_sync",
|
|
28
27
|
"SearchPlanner",
|
|
29
28
|
"SearchToolAgent",
|
openai_sdk_helpers/agent/base.py
CHANGED
|
@@ -2,41 +2,36 @@
|
|
|
2
2
|
|
|
3
3
|
from __future__ import annotations
|
|
4
4
|
|
|
5
|
-
import json
|
|
6
5
|
from pathlib import Path
|
|
7
|
-
from typing import TYPE_CHECKING, Any,
|
|
6
|
+
from typing import TYPE_CHECKING, Any, Dict, Optional, Protocol, cast
|
|
8
7
|
import uuid
|
|
9
8
|
|
|
10
|
-
from agents import
|
|
11
|
-
Agent,
|
|
12
|
-
Handoff,
|
|
13
|
-
InputGuardrail,
|
|
14
|
-
OutputGuardrail,
|
|
15
|
-
RunResultStreaming,
|
|
16
|
-
Session,
|
|
17
|
-
)
|
|
9
|
+
from agents import Agent, Handoff, InputGuardrail, OutputGuardrail, Session
|
|
18
10
|
from agents.model_settings import ModelSettings
|
|
19
11
|
from agents.run_context import RunContextWrapper
|
|
20
12
|
from agents.tool import Tool
|
|
21
13
|
from jinja2 import Template
|
|
22
14
|
|
|
15
|
+
from ..environment import get_data_path
|
|
16
|
+
|
|
23
17
|
from ..utils.json.data_class import DataclassJSONSerializable
|
|
24
18
|
from ..structure.base import StructureBase
|
|
25
|
-
from ..
|
|
26
|
-
|
|
19
|
+
from ..tools import (
|
|
20
|
+
StructureType,
|
|
21
|
+
ToolHandlerRegistration,
|
|
22
|
+
ToolSpec,
|
|
23
|
+
)
|
|
27
24
|
|
|
28
25
|
from ..utils import (
|
|
29
26
|
check_filepath,
|
|
30
27
|
log,
|
|
31
28
|
)
|
|
32
29
|
|
|
33
|
-
from
|
|
34
|
-
|
|
35
|
-
from .runner import run_async, run_streamed, run_sync
|
|
30
|
+
from .runner import run_async, run_sync
|
|
36
31
|
|
|
37
32
|
if TYPE_CHECKING:
|
|
38
33
|
from ..settings import OpenAISettings
|
|
39
|
-
from ..response.base import ResponseBase
|
|
34
|
+
from ..response.base import ResponseBase
|
|
40
35
|
|
|
41
36
|
|
|
42
37
|
class AgentConfigurationProtocol(Protocol):
|
|
@@ -52,18 +47,14 @@ class AgentConfigurationProtocol(Protocol):
|
|
|
52
47
|
"""Agent description."""
|
|
53
48
|
...
|
|
54
49
|
|
|
55
|
-
@property
|
|
56
|
-
def model(self) -> Optional[str]:
|
|
57
|
-
"""Model identifier."""
|
|
58
|
-
...
|
|
59
|
-
|
|
60
50
|
@property
|
|
61
51
|
def template_path(self) -> Optional[str | Path]:
|
|
62
52
|
"""Template path."""
|
|
63
53
|
...
|
|
64
54
|
|
|
65
|
-
|
|
66
|
-
|
|
55
|
+
@property
|
|
56
|
+
def model(self) -> Optional[str]:
|
|
57
|
+
"""Model identifier."""
|
|
67
58
|
...
|
|
68
59
|
|
|
69
60
|
@property
|
|
@@ -136,7 +127,7 @@ class AgentBase(DataclassJSONSerializable):
|
|
|
136
127
|
... description="A custom agent",
|
|
137
128
|
... model="gpt-4o-mini"
|
|
138
129
|
... )
|
|
139
|
-
>>> agent = AgentBase(configuration=configuration
|
|
130
|
+
>>> agent = AgentBase(configuration=configuration)
|
|
140
131
|
>>> result = agent.run_sync("What is 2+2?")
|
|
141
132
|
|
|
142
133
|
Use absolute path to template:
|
|
@@ -146,7 +137,7 @@ class AgentBase(DataclassJSONSerializable):
|
|
|
146
137
|
... template_path="/absolute/path/to/template.jinja",
|
|
147
138
|
... model="gpt-4o-mini"
|
|
148
139
|
... )
|
|
149
|
-
>>> agent = AgentBase(configuration=configuration
|
|
140
|
+
>>> agent = AgentBase(configuration=configuration)
|
|
150
141
|
|
|
151
142
|
Use async execution:
|
|
152
143
|
|
|
@@ -186,8 +177,6 @@ class AgentBase(DataclassJSONSerializable):
|
|
|
186
177
|
Execute the agent asynchronously and optionally cast the result.
|
|
187
178
|
run_sync(input, context, output_structure, session)
|
|
188
179
|
Execute the agent synchronously.
|
|
189
|
-
run_streamed(input, context, output_structure, session)
|
|
190
|
-
Return a streaming result for the agent execution.
|
|
191
180
|
as_tool()
|
|
192
181
|
Return the agent as a callable tool.
|
|
193
182
|
as_response_tool()
|
|
@@ -204,8 +193,6 @@ class AgentBase(DataclassJSONSerializable):
|
|
|
204
193
|
configuration: AgentConfigurationProtocol,
|
|
205
194
|
run_context_wrapper: Optional[RunContextWrapper[Dict[str, Any]]] = None,
|
|
206
195
|
data_path: Path | str | None = None,
|
|
207
|
-
prompt_dir: Optional[Path] = None,
|
|
208
|
-
default_model: Optional[str] = None,
|
|
209
196
|
) -> None:
|
|
210
197
|
"""Initialize the AgentBase using a configuration object.
|
|
211
198
|
|
|
@@ -215,39 +202,31 @@ class AgentBase(DataclassJSONSerializable):
|
|
|
215
202
|
Configuration describing this agent.
|
|
216
203
|
run_context_wrapper : RunContextWrapper or None, default=None
|
|
217
204
|
Optional wrapper providing runtime context for prompt rendering.
|
|
218
|
-
|
|
219
|
-
Optional
|
|
220
|
-
``configuration.template_path`` is not provided or is relative. If
|
|
221
|
-
``configuration.template_path`` is an absolute path, this parameter is
|
|
222
|
-
ignored.
|
|
223
|
-
default_model : str or None, default=None
|
|
224
|
-
Optional fallback model identifier if the configuration does not supply one.
|
|
205
|
+
data_path : Path | str | None, default=None
|
|
206
|
+
Optional base path for storing agent data.
|
|
225
207
|
"""
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
if
|
|
230
|
-
raise ValueError(
|
|
231
|
-
|
|
232
|
-
|
|
208
|
+
self._configuration = configuration
|
|
209
|
+
self.uuid = uuid.uuid4()
|
|
210
|
+
self._model = configuration.model
|
|
211
|
+
if self._model is None:
|
|
212
|
+
raise ValueError(
|
|
213
|
+
f"Model must be specified in configuration for agent '{configuration.name}'."
|
|
214
|
+
)
|
|
233
215
|
|
|
234
216
|
# Build template from file or fall back to instructions
|
|
235
|
-
|
|
217
|
+
self._template_path = configuration.template_path
|
|
218
|
+
if self._template_path is None:
|
|
236
219
|
instructions_text = configuration.instructions_text
|
|
237
220
|
self._template = Template(instructions_text)
|
|
238
221
|
self._instructions = instructions_text
|
|
239
|
-
elif prompt_path.exists():
|
|
240
|
-
self._template = Template(prompt_path.read_text(encoding="utf-8"))
|
|
241
|
-
self._instructions = None
|
|
242
222
|
else:
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
self.model = model
|
|
223
|
+
self._template_path = Path(self._template_path)
|
|
224
|
+
if not self._template_path.exists():
|
|
225
|
+
raise FileNotFoundError(
|
|
226
|
+
f"Template for agent '{self._configuration.name}' not found at {self._template_path}."
|
|
227
|
+
)
|
|
228
|
+
self._template = Template(self._template_path.read_text(encoding="utf-8"))
|
|
229
|
+
self._instructions = None
|
|
251
230
|
|
|
252
231
|
# Resolve data_path with class name appended
|
|
253
232
|
class_name = self.__class__.__name__
|
|
@@ -337,7 +316,29 @@ class AgentBase(DataclassJSONSerializable):
|
|
|
337
316
|
str
|
|
338
317
|
Name used to identify the agent.
|
|
339
318
|
"""
|
|
340
|
-
return self.
|
|
319
|
+
return self._configuration.name
|
|
320
|
+
|
|
321
|
+
@property
|
|
322
|
+
def description(self) -> Optional[str]:
|
|
323
|
+
"""Return the description of this agent.
|
|
324
|
+
|
|
325
|
+
Returns
|
|
326
|
+
-------
|
|
327
|
+
str or None
|
|
328
|
+
Description of the agent's purpose.
|
|
329
|
+
"""
|
|
330
|
+
return self._configuration.description
|
|
331
|
+
|
|
332
|
+
@property
|
|
333
|
+
def model(self) -> str:
|
|
334
|
+
"""Return the model identifier for this agent.
|
|
335
|
+
|
|
336
|
+
Returns
|
|
337
|
+
-------
|
|
338
|
+
str
|
|
339
|
+
Model identifier used by the agent.
|
|
340
|
+
"""
|
|
341
|
+
return self._model # pyright: ignore[reportReturnType]
|
|
341
342
|
|
|
342
343
|
@property
|
|
343
344
|
def instructions_text(self) -> str:
|
|
@@ -348,9 +349,7 @@ class AgentBase(DataclassJSONSerializable):
|
|
|
348
349
|
str
|
|
349
350
|
Rendered instructions text using the current run context.
|
|
350
351
|
"""
|
|
351
|
-
|
|
352
|
-
return self._instructions
|
|
353
|
-
return self._build_prompt_from_jinja()
|
|
352
|
+
return self._configuration.instructions_text
|
|
354
353
|
|
|
355
354
|
@property
|
|
356
355
|
def tools(self) -> Optional[list]:
|
|
@@ -361,7 +360,7 @@ class AgentBase(DataclassJSONSerializable):
|
|
|
361
360
|
list or None
|
|
362
361
|
Tool definitions configured for the agent.
|
|
363
362
|
"""
|
|
364
|
-
return self.
|
|
363
|
+
return self._configuration.tools
|
|
365
364
|
|
|
366
365
|
@property
|
|
367
366
|
def output_structure(self) -> Optional[type[StructureBase]]:
|
|
@@ -372,7 +371,7 @@ class AgentBase(DataclassJSONSerializable):
|
|
|
372
371
|
type[StructureBase] or None
|
|
373
372
|
Output type used to cast responses.
|
|
374
373
|
"""
|
|
375
|
-
return self.
|
|
374
|
+
return self._configuration.output_structure
|
|
376
375
|
|
|
377
376
|
@property
|
|
378
377
|
def model_settings(self) -> Optional[ModelSettings]:
|
|
@@ -438,14 +437,14 @@ class AgentBase(DataclassJSONSerializable):
|
|
|
438
437
|
Initialized agent ready for execution.
|
|
439
438
|
"""
|
|
440
439
|
agent_config: Dict[str, Any] = {
|
|
441
|
-
"name": self.
|
|
442
|
-
"instructions": self.
|
|
443
|
-
"model": self.
|
|
440
|
+
"name": self._configuration.name,
|
|
441
|
+
"instructions": self._configuration.instructions_text or ".",
|
|
442
|
+
"model": self._model,
|
|
444
443
|
}
|
|
445
|
-
if self.
|
|
446
|
-
agent_config["output_type"] = self.
|
|
447
|
-
if self.
|
|
448
|
-
agent_config["tools"] = self.
|
|
444
|
+
if self._configuration.output_structure:
|
|
445
|
+
agent_config["output_type"] = self._configuration.output_structure
|
|
446
|
+
if self._configuration.tools:
|
|
447
|
+
agent_config["tools"] = self._configuration.tools
|
|
449
448
|
if self._model_settings:
|
|
450
449
|
agent_config["model_settings"] = self._model_settings
|
|
451
450
|
if self._handoffs:
|
|
@@ -535,47 +534,6 @@ class AgentBase(DataclassJSONSerializable):
|
|
|
535
534
|
session=session_to_use,
|
|
536
535
|
)
|
|
537
536
|
|
|
538
|
-
def run_streamed(
|
|
539
|
-
self,
|
|
540
|
-
input: str,
|
|
541
|
-
*,
|
|
542
|
-
context: Optional[Dict[str, Any]] = None,
|
|
543
|
-
output_structure: Optional[type[StructureBase]] = None,
|
|
544
|
-
session: Optional[Any] = None,
|
|
545
|
-
) -> RunResultStreaming | StructureBase:
|
|
546
|
-
"""Stream the agent execution results.
|
|
547
|
-
|
|
548
|
-
Parameters
|
|
549
|
-
----------
|
|
550
|
-
input : str
|
|
551
|
-
Prompt or query for the agent.
|
|
552
|
-
context : dict or None, default=None
|
|
553
|
-
Optional dictionary passed to the agent.
|
|
554
|
-
output_structure : type[StructureBase] or None, default=None
|
|
555
|
-
Optional type used to cast the final output.
|
|
556
|
-
session : Session or None, default=None
|
|
557
|
-
Optional session for maintaining conversation history across runs.
|
|
558
|
-
If not provided, uses the session from configuration if available.
|
|
559
|
-
|
|
560
|
-
Returns
|
|
561
|
-
-------
|
|
562
|
-
RunResultStreaming
|
|
563
|
-
Streaming output wrapper from the agent execution.
|
|
564
|
-
"""
|
|
565
|
-
# Use session from parameter, fall back to configuration session
|
|
566
|
-
session_to_use = session if session is not None else self._session
|
|
567
|
-
output_structure_to_use = output_structure or self._output_structure
|
|
568
|
-
result = run_streamed(
|
|
569
|
-
agent=self.get_agent(),
|
|
570
|
-
input=input,
|
|
571
|
-
context=context,
|
|
572
|
-
output_structure=output_structure_to_use,
|
|
573
|
-
session=session_to_use,
|
|
574
|
-
)
|
|
575
|
-
if output_structure_to_use and hasattr(result, "final_output_as"):
|
|
576
|
-
return cast(Any, result).final_output_as(output_structure_to_use)
|
|
577
|
-
return result
|
|
578
|
-
|
|
579
537
|
def as_tool(self) -> Tool:
|
|
580
538
|
"""Return the agent as a callable tool.
|
|
581
539
|
|
|
@@ -586,78 +544,35 @@ class AgentBase(DataclassJSONSerializable):
|
|
|
586
544
|
"""
|
|
587
545
|
agent = self.get_agent()
|
|
588
546
|
tool_obj: Tool = agent.as_tool(
|
|
589
|
-
tool_name=self.
|
|
547
|
+
tool_name=self._configuration.name,
|
|
548
|
+
tool_description=self._configuration.description,
|
|
590
549
|
)
|
|
591
550
|
return tool_obj
|
|
592
551
|
|
|
593
|
-
def
|
|
552
|
+
def as_tool_handler_registration(
|
|
594
553
|
self,
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
tool_description: str | None = None,
|
|
598
|
-
) -> tuple[dict[str, Callable[..., Any]], dict[str, Any]]:
|
|
599
|
-
"""Return response tool handler and definition for Responses API use.
|
|
600
|
-
|
|
601
|
-
The returned handler serializes tool output as JSON using
|
|
602
|
-
``tool_handler_factory`` so downstream response flows can rely on a
|
|
603
|
-
consistent payload format.
|
|
554
|
+
) -> ToolHandlerRegistration:
|
|
555
|
+
"""Return the agent as a ToolHandlerRegistration for Responses API use.
|
|
604
556
|
|
|
605
557
|
Parameters
|
|
606
558
|
----------
|
|
607
559
|
tool_name : str or None, default=None
|
|
608
560
|
Optional override for the tool name. When None, uses the agent name.
|
|
609
|
-
tool_description : str or None, default=None
|
|
610
|
-
Optional override for the tool description. When None, uses the
|
|
611
|
-
agent description.
|
|
612
|
-
|
|
613
|
-
Returns
|
|
614
|
-
-------
|
|
615
|
-
tuple[dict[str, Callable[..., Any]], dict[str, Any]]
|
|
616
|
-
Tool handler mapping and tool definition for Responses API usage.
|
|
617
|
-
|
|
618
|
-
Examples
|
|
619
|
-
--------
|
|
620
|
-
>>> tool_handler, tool_definition = agent.as_response_tool()
|
|
621
|
-
>>> response = ResponseBase(
|
|
622
|
-
... name="agent_tool",
|
|
623
|
-
... instructions="Use the agent tool when needed.",
|
|
624
|
-
... tools=[tool_definition],
|
|
625
|
-
... output_structure=None,
|
|
626
|
-
... tool_handlers=tool_handler,
|
|
627
|
-
... openai_settings=settings,
|
|
628
|
-
... )
|
|
629
|
-
>>> response.run_sync("Invoke the agent tool") # doctest: +SKIP
|
|
630
561
|
"""
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
prompt = json.dumps(kwargs)
|
|
639
|
-
return self.run_sync(str(prompt))
|
|
640
|
-
|
|
641
|
-
name = tool_name or self.name
|
|
642
|
-
description = tool_description or self.description
|
|
643
|
-
input_model = self._input_structure or PromptStructure
|
|
644
|
-
tool_handler = {name: tool_handler_factory(_run_agent, input_model=input_model)}
|
|
645
|
-
tool_definition = {
|
|
646
|
-
"type": "function",
|
|
647
|
-
"name": name,
|
|
648
|
-
"description": description,
|
|
649
|
-
"strict": True,
|
|
650
|
-
"additionalProperties": False,
|
|
651
|
-
"parameters": self._build_response_parameters(),
|
|
652
|
-
}
|
|
653
|
-
return tool_handler, tool_definition
|
|
562
|
+
tool_spec = ToolSpec(
|
|
563
|
+
tool_name=self.name,
|
|
564
|
+
tool_description=self.description,
|
|
565
|
+
input_structure=cast(StructureType, self._configuration.input_structure),
|
|
566
|
+
output_structure=cast(StructureType, self._configuration.output_structure),
|
|
567
|
+
)
|
|
568
|
+
return ToolHandlerRegistration(handler=self.run_sync, tool_spec=tool_spec)
|
|
654
569
|
|
|
655
570
|
def build_response(
|
|
656
571
|
self,
|
|
657
572
|
*,
|
|
658
573
|
openai_settings: OpenAISettings,
|
|
659
574
|
data_path: Path | str | None = None,
|
|
660
|
-
tool_handlers: dict[str,
|
|
575
|
+
tool_handlers: dict[str, ToolHandlerRegistration] | None = None,
|
|
661
576
|
system_vector_store: list[str] | None = None,
|
|
662
577
|
) -> ResponseBase[StructureBase]:
|
|
663
578
|
"""Build a ResponseBase instance from this agent configuration.
|
|
@@ -669,8 +584,9 @@ class AgentBase(DataclassJSONSerializable):
|
|
|
669
584
|
data_path : Path, str, or None, default None
|
|
670
585
|
Optional path for storing response artifacts. When None, the
|
|
671
586
|
response uses the default data directory.
|
|
672
|
-
tool_handlers : dict[str,
|
|
673
|
-
Optional mapping of tool names to handler
|
|
587
|
+
tool_handlers : dict[str, ToolHandlerRegistration] or None, default None
|
|
588
|
+
Optional mapping of tool names to handler registrations. Registrations
|
|
589
|
+
can include ToolSpec metadata to parse tool outputs by name.
|
|
674
590
|
system_vector_store : list[str] or None, default None
|
|
675
591
|
Optional list of vector store names to attach as system context.
|
|
676
592
|
|
|
@@ -684,7 +600,7 @@ class AgentBase(DataclassJSONSerializable):
|
|
|
684
600
|
>>> from openai_sdk_helpers import OpenAISettings
|
|
685
601
|
>>> response = agent.build_response(openai_settings=OpenAISettings.from_env())
|
|
686
602
|
"""
|
|
687
|
-
from ..response.base import ResponseBase
|
|
603
|
+
from ..response.base import ResponseBase
|
|
688
604
|
from ..settings import OpenAISettings
|
|
689
605
|
|
|
690
606
|
if not isinstance(openai_settings, OpenAISettings):
|
|
@@ -693,8 +609,8 @@ class AgentBase(DataclassJSONSerializable):
|
|
|
693
609
|
tools = self._normalize_response_tools(self.tools)
|
|
694
610
|
|
|
695
611
|
return ResponseBase(
|
|
696
|
-
name=self.name,
|
|
697
|
-
instructions=self.instructions_text,
|
|
612
|
+
name=self._configuration.name,
|
|
613
|
+
instructions=self._configuration.instructions_text,
|
|
698
614
|
tools=tools,
|
|
699
615
|
output_structure=self.output_structure,
|
|
700
616
|
system_vector_store=system_vector_store,
|
|
@@ -773,7 +689,7 @@ class AgentBase(DataclassJSONSerializable):
|
|
|
773
689
|
|
|
774
690
|
Examples
|
|
775
691
|
--------
|
|
776
|
-
>>> agent = AgentBase(configuration
|
|
692
|
+
>>> agent = AgentBase(configuration)
|
|
777
693
|
>>> try:
|
|
778
694
|
... result = agent.run_sync("query")
|
|
779
695
|
... finally:
|
|
@@ -790,7 +706,7 @@ class AgentBase(DataclassJSONSerializable):
|
|
|
790
706
|
str
|
|
791
707
|
String representation including agent name and model.
|
|
792
708
|
"""
|
|
793
|
-
return f"<AgentBase name={self.
|
|
709
|
+
return f"<AgentBase name={self.name!r} model={self.model!r}>"
|
|
794
710
|
|
|
795
711
|
def save(self, filepath: str | Path | None = None) -> None:
|
|
796
712
|
"""Serialize the message history to a JSON file.
|
|
@@ -809,7 +725,7 @@ class AgentBase(DataclassJSONSerializable):
|
|
|
809
725
|
target = Path(filepath)
|
|
810
726
|
else:
|
|
811
727
|
filename = f"{str(self.uuid).lower()}.json"
|
|
812
|
-
target = self._data_path / self.
|
|
728
|
+
target = self._data_path / self.name / filename
|
|
813
729
|
|
|
814
730
|
checked = check_filepath(filepath=target)
|
|
815
731
|
self.to_json_file(filepath=checked)
|
|
@@ -150,7 +150,7 @@ class AgentConfiguration(DataclassJSONSerializable):
|
|
|
150
150
|
Return the resolved instruction content as a string.
|
|
151
151
|
resolve_prompt_path(prompt_dir)
|
|
152
152
|
Resolve the prompt template path for this configuration.
|
|
153
|
-
gen_agent(run_context_wrapper
|
|
153
|
+
gen_agent(run_context_wrapper)
|
|
154
154
|
Create a AgentBase instance from this configuration.
|
|
155
155
|
replace(**changes)
|
|
156
156
|
Create a new AgentConfiguration with specified fields replaced.
|
|
@@ -176,17 +176,17 @@ class AgentConfiguration(DataclassJSONSerializable):
|
|
|
176
176
|
|
|
177
177
|
name: str
|
|
178
178
|
instructions: str | Path
|
|
179
|
-
description:
|
|
180
|
-
model:
|
|
181
|
-
template_path:
|
|
182
|
-
input_structure:
|
|
183
|
-
output_structure:
|
|
184
|
-
tools:
|
|
185
|
-
model_settings:
|
|
186
|
-
handoffs:
|
|
187
|
-
input_guardrails:
|
|
188
|
-
output_guardrails:
|
|
189
|
-
session:
|
|
179
|
+
description: str | None = None
|
|
180
|
+
model: str | None = None
|
|
181
|
+
template_path: str | Path | None = None
|
|
182
|
+
input_structure: type[StructureBase] | None = None
|
|
183
|
+
output_structure: type[StructureBase] | None = None
|
|
184
|
+
tools: list | None = None
|
|
185
|
+
model_settings: ModelSettings | None = None
|
|
186
|
+
handoffs: list[Agent | Handoff] | None = None
|
|
187
|
+
input_guardrails: list[InputGuardrail] | None = None
|
|
188
|
+
output_guardrails: list[OutputGuardrail] | None = None
|
|
189
|
+
session: Session | None = None
|
|
190
190
|
add_output_instructions: bool = False
|
|
191
191
|
add_web_search_tool: bool = False
|
|
192
192
|
|
|
@@ -294,8 +294,6 @@ class AgentConfiguration(DataclassJSONSerializable):
|
|
|
294
294
|
def gen_agent(
|
|
295
295
|
self,
|
|
296
296
|
run_context_wrapper: Any = None,
|
|
297
|
-
prompt_dir: Path | None = None,
|
|
298
|
-
default_model: str | None = None,
|
|
299
297
|
) -> Any:
|
|
300
298
|
"""Create a AgentBase instance from this configuration.
|
|
301
299
|
|
|
@@ -305,10 +303,6 @@ class AgentConfiguration(DataclassJSONSerializable):
|
|
|
305
303
|
----------
|
|
306
304
|
run_context_wrapper : RunContextWrapper or None, default=None
|
|
307
305
|
Optional wrapper providing runtime context for prompt rendering.
|
|
308
|
-
prompt_dir : Path or None, default=None
|
|
309
|
-
Optional directory holding prompt templates.
|
|
310
|
-
default_model : str or None, default=None
|
|
311
|
-
Optional fallback model identifier if configuration doesn't specify one.
|
|
312
306
|
|
|
313
307
|
Returns
|
|
314
308
|
-------
|
|
@@ -329,8 +323,6 @@ class AgentConfiguration(DataclassJSONSerializable):
|
|
|
329
323
|
return AgentBase(
|
|
330
324
|
configuration=self,
|
|
331
325
|
run_context_wrapper=run_context_wrapper,
|
|
332
|
-
prompt_dir=prompt_dir,
|
|
333
|
-
default_model=default_model,
|
|
334
326
|
)
|
|
335
327
|
|
|
336
328
|
def replace(self, **changes: Any) -> AgentConfiguration:
|