openai-sdk-helpers 0.4.2__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 +45 -41
- openai_sdk_helpers/agent/__init__.py +4 -6
- openai_sdk_helpers/agent/base.py +110 -191
- openai_sdk_helpers/agent/{config.py → configuration.py} +24 -32
- openai_sdk_helpers/agent/{coordination.py → coordinator.py} +22 -23
- openai_sdk_helpers/agent/runner.py +3 -45
- openai_sdk_helpers/agent/search/base.py +54 -76
- openai_sdk_helpers/agent/search/vector.py +92 -108
- openai_sdk_helpers/agent/search/web.py +104 -82
- openai_sdk_helpers/agent/summarizer.py +22 -28
- openai_sdk_helpers/agent/translator.py +22 -24
- openai_sdk_helpers/agent/{validation.py → validator.py} +19 -23
- openai_sdk_helpers/cli.py +8 -22
- openai_sdk_helpers/environment.py +8 -13
- 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/prompt/vector_planner.jinja +7 -0
- openai_sdk_helpers/prompt/vector_search.jinja +6 -0
- openai_sdk_helpers/prompt/vector_writer.jinja +7 -0
- openai_sdk_helpers/response/__init__.py +3 -7
- openai_sdk_helpers/response/base.py +89 -98
- openai_sdk_helpers/response/{config.py → configuration.py} +45 -20
- openai_sdk_helpers/response/files.py +2 -0
- openai_sdk_helpers/response/planner.py +1 -1
- openai_sdk_helpers/response/prompter.py +1 -1
- 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/__init__.py +1 -1
- openai_sdk_helpers/streamlit_app/app.py +17 -18
- openai_sdk_helpers/streamlit_app/{config.py → configuration.py} +13 -13
- 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/types.py +3 -3
- openai_sdk_helpers/utils/__init__.py +9 -6
- openai_sdk_helpers/utils/json/base_model.py +316 -33
- openai_sdk_helpers/utils/json/data_class.py +1 -1
- openai_sdk_helpers/utils/langextract.py +194 -0
- openai_sdk_helpers/utils/registry.py +19 -15
- openai_sdk_helpers/vector_storage/storage.py +1 -1
- {openai_sdk_helpers-0.4.2.dist-info → openai_sdk_helpers-0.5.0.dist-info}/METADATA +25 -11
- openai_sdk_helpers-0.5.0.dist-info/RECORD +95 -0
- openai_sdk_helpers/agent/prompt_utils.py +0 -15
- openai_sdk_helpers/context_manager.py +0 -241
- openai_sdk_helpers/deprecation.py +0 -167
- openai_sdk_helpers/retry.py +0 -175
- openai_sdk_helpers/streamlit_app/streamlit_web_search.py +0 -75
- openai_sdk_helpers/utils/deprecation.py +0 -167
- openai_sdk_helpers-0.4.2.dist-info/RECORD +0 -88
- /openai_sdk_helpers/{logging_config.py → logging.py} +0 -0
- /openai_sdk_helpers/{config.py → settings.py} +0 -0
- {openai_sdk_helpers-0.4.2.dist-info → openai_sdk_helpers-0.5.0.dist-info}/WHEEL +0 -0
- {openai_sdk_helpers-0.4.2.dist-info → openai_sdk_helpers-0.5.0.dist-info}/entry_points.txt +0 -0
- {openai_sdk_helpers-0.4.2.dist-info → openai_sdk_helpers-0.5.0.dist-info}/licenses/LICENSE +0 -0
openai_sdk_helpers/agent/base.py
CHANGED
|
@@ -2,40 +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 ..
|
|
19
|
+
from ..tools import (
|
|
20
|
+
StructureType,
|
|
21
|
+
ToolHandlerRegistration,
|
|
22
|
+
ToolSpec,
|
|
23
|
+
)
|
|
26
24
|
|
|
27
25
|
from ..utils import (
|
|
28
26
|
check_filepath,
|
|
29
27
|
log,
|
|
30
28
|
)
|
|
31
29
|
|
|
32
|
-
from
|
|
33
|
-
|
|
34
|
-
from .runner import run_async, run_streamed, run_sync
|
|
30
|
+
from .runner import run_async, run_sync
|
|
35
31
|
|
|
36
32
|
if TYPE_CHECKING:
|
|
37
|
-
from ..
|
|
38
|
-
from ..response.base import ResponseBase
|
|
33
|
+
from ..settings import OpenAISettings
|
|
34
|
+
from ..response.base import ResponseBase
|
|
39
35
|
|
|
40
36
|
|
|
41
37
|
class AgentConfigurationProtocol(Protocol):
|
|
@@ -51,18 +47,14 @@ class AgentConfigurationProtocol(Protocol):
|
|
|
51
47
|
"""Agent description."""
|
|
52
48
|
...
|
|
53
49
|
|
|
54
|
-
@property
|
|
55
|
-
def model(self) -> Optional[str]:
|
|
56
|
-
"""Model identifier."""
|
|
57
|
-
...
|
|
58
|
-
|
|
59
50
|
@property
|
|
60
51
|
def template_path(self) -> Optional[str | Path]:
|
|
61
52
|
"""Template path."""
|
|
62
53
|
...
|
|
63
54
|
|
|
64
|
-
|
|
65
|
-
|
|
55
|
+
@property
|
|
56
|
+
def model(self) -> Optional[str]:
|
|
57
|
+
"""Model identifier."""
|
|
66
58
|
...
|
|
67
59
|
|
|
68
60
|
@property
|
|
@@ -130,22 +122,22 @@ class AgentBase(DataclassJSONSerializable):
|
|
|
130
122
|
Create a basic agent from configuration:
|
|
131
123
|
|
|
132
124
|
>>> from openai_sdk_helpers.agent import AgentBase, AgentConfiguration
|
|
133
|
-
>>>
|
|
125
|
+
>>> configuration = AgentConfiguration(
|
|
134
126
|
... name="my_agent",
|
|
135
127
|
... description="A custom agent",
|
|
136
128
|
... model="gpt-4o-mini"
|
|
137
129
|
... )
|
|
138
|
-
>>> agent = AgentBase(
|
|
130
|
+
>>> agent = AgentBase(configuration=configuration)
|
|
139
131
|
>>> result = agent.run_sync("What is 2+2?")
|
|
140
132
|
|
|
141
133
|
Use absolute path to template:
|
|
142
134
|
|
|
143
|
-
>>>
|
|
135
|
+
>>> configuration = AgentConfiguration(
|
|
144
136
|
... name="my_agent",
|
|
145
137
|
... template_path="/absolute/path/to/template.jinja",
|
|
146
138
|
... model="gpt-4o-mini"
|
|
147
139
|
... )
|
|
148
|
-
>>> agent = AgentBase(
|
|
140
|
+
>>> agent = AgentBase(configuration=configuration)
|
|
149
141
|
|
|
150
142
|
Use async execution:
|
|
151
143
|
|
|
@@ -185,8 +177,6 @@ class AgentBase(DataclassJSONSerializable):
|
|
|
185
177
|
Execute the agent asynchronously and optionally cast the result.
|
|
186
178
|
run_sync(input, context, output_structure, session)
|
|
187
179
|
Execute the agent synchronously.
|
|
188
|
-
run_streamed(input, context, output_structure, session)
|
|
189
|
-
Return a streaming result for the agent execution.
|
|
190
180
|
as_tool()
|
|
191
181
|
Return the agent as a callable tool.
|
|
192
182
|
as_response_tool()
|
|
@@ -200,53 +190,43 @@ class AgentBase(DataclassJSONSerializable):
|
|
|
200
190
|
def __init__(
|
|
201
191
|
self,
|
|
202
192
|
*,
|
|
203
|
-
|
|
193
|
+
configuration: AgentConfigurationProtocol,
|
|
204
194
|
run_context_wrapper: Optional[RunContextWrapper[Dict[str, Any]]] = None,
|
|
205
195
|
data_path: Path | str | None = None,
|
|
206
|
-
prompt_dir: Optional[Path] = None,
|
|
207
|
-
default_model: Optional[str] = None,
|
|
208
196
|
) -> None:
|
|
209
197
|
"""Initialize the AgentBase using a configuration object.
|
|
210
198
|
|
|
211
199
|
Parameters
|
|
212
200
|
----------
|
|
213
|
-
|
|
201
|
+
configuration : AgentConfigurationProtocol
|
|
214
202
|
Configuration describing this agent.
|
|
215
203
|
run_context_wrapper : RunContextWrapper or None, default=None
|
|
216
204
|
Optional wrapper providing runtime context for prompt rendering.
|
|
217
|
-
|
|
218
|
-
Optional
|
|
219
|
-
``config.template_path`` is not provided or is relative. If
|
|
220
|
-
``config.template_path`` is an absolute path, this parameter is
|
|
221
|
-
ignored.
|
|
222
|
-
default_model : str or None, default=None
|
|
223
|
-
Optional fallback model identifier if the config does not supply one.
|
|
205
|
+
data_path : Path | str | None, default=None
|
|
206
|
+
Optional base path for storing agent data.
|
|
224
207
|
"""
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
if
|
|
229
|
-
raise ValueError(
|
|
230
|
-
|
|
231
|
-
|
|
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
|
+
)
|
|
232
215
|
|
|
233
216
|
# Build template from file or fall back to instructions
|
|
234
|
-
|
|
235
|
-
|
|
217
|
+
self._template_path = configuration.template_path
|
|
218
|
+
if self._template_path is None:
|
|
219
|
+
instructions_text = configuration.instructions_text
|
|
236
220
|
self._template = Template(instructions_text)
|
|
237
221
|
self._instructions = instructions_text
|
|
238
|
-
elif prompt_path.exists():
|
|
239
|
-
self._template = Template(prompt_path.read_text(encoding="utf-8"))
|
|
240
|
-
self._instructions = None
|
|
241
222
|
else:
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
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
|
|
250
230
|
|
|
251
231
|
# Resolve data_path with class name appended
|
|
252
232
|
class_name = self.__class__.__name__
|
|
@@ -261,14 +241,16 @@ class AgentBase(DataclassJSONSerializable):
|
|
|
261
241
|
|
|
262
242
|
self._data_path = get_data_path(self.__class__.__name__)
|
|
263
243
|
|
|
264
|
-
self._input_structure =
|
|
265
|
-
self._output_structure =
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
self.
|
|
269
|
-
self.
|
|
270
|
-
self.
|
|
271
|
-
self.
|
|
244
|
+
self._input_structure = configuration.input_structure
|
|
245
|
+
self._output_structure = (
|
|
246
|
+
configuration.output_structure or configuration.input_structure
|
|
247
|
+
)
|
|
248
|
+
self._tools = configuration.tools
|
|
249
|
+
self._model_settings = configuration.model_settings
|
|
250
|
+
self._handoffs = configuration.handoffs
|
|
251
|
+
self._input_guardrails = configuration.input_guardrails
|
|
252
|
+
self._output_guardrails = configuration.output_guardrails
|
|
253
|
+
self._session = configuration.session
|
|
272
254
|
self._run_context_wrapper = run_context_wrapper
|
|
273
255
|
|
|
274
256
|
def _build_prompt_from_jinja(self) -> str:
|
|
@@ -334,7 +316,29 @@ class AgentBase(DataclassJSONSerializable):
|
|
|
334
316
|
str
|
|
335
317
|
Name used to identify the agent.
|
|
336
318
|
"""
|
|
337
|
-
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]
|
|
338
342
|
|
|
339
343
|
@property
|
|
340
344
|
def instructions_text(self) -> str:
|
|
@@ -345,9 +349,7 @@ class AgentBase(DataclassJSONSerializable):
|
|
|
345
349
|
str
|
|
346
350
|
Rendered instructions text using the current run context.
|
|
347
351
|
"""
|
|
348
|
-
|
|
349
|
-
return self._instructions
|
|
350
|
-
return self._build_prompt_from_jinja()
|
|
352
|
+
return self._configuration.instructions_text
|
|
351
353
|
|
|
352
354
|
@property
|
|
353
355
|
def tools(self) -> Optional[list]:
|
|
@@ -358,7 +360,7 @@ class AgentBase(DataclassJSONSerializable):
|
|
|
358
360
|
list or None
|
|
359
361
|
Tool definitions configured for the agent.
|
|
360
362
|
"""
|
|
361
|
-
return self.
|
|
363
|
+
return self._configuration.tools
|
|
362
364
|
|
|
363
365
|
@property
|
|
364
366
|
def output_structure(self) -> Optional[type[StructureBase]]:
|
|
@@ -369,7 +371,7 @@ class AgentBase(DataclassJSONSerializable):
|
|
|
369
371
|
type[StructureBase] or None
|
|
370
372
|
Output type used to cast responses.
|
|
371
373
|
"""
|
|
372
|
-
return self.
|
|
374
|
+
return self._configuration.output_structure
|
|
373
375
|
|
|
374
376
|
@property
|
|
375
377
|
def model_settings(self) -> Optional[ModelSettings]:
|
|
@@ -435,14 +437,14 @@ class AgentBase(DataclassJSONSerializable):
|
|
|
435
437
|
Initialized agent ready for execution.
|
|
436
438
|
"""
|
|
437
439
|
agent_config: Dict[str, Any] = {
|
|
438
|
-
"name": self.
|
|
439
|
-
"instructions": self.
|
|
440
|
-
"model": self.
|
|
440
|
+
"name": self._configuration.name,
|
|
441
|
+
"instructions": self._configuration.instructions_text or ".",
|
|
442
|
+
"model": self._model,
|
|
441
443
|
}
|
|
442
|
-
if self.
|
|
443
|
-
agent_config["output_type"] = self.
|
|
444
|
-
if self.
|
|
445
|
-
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
|
|
446
448
|
if self._model_settings:
|
|
447
449
|
agent_config["model_settings"] = self._model_settings
|
|
448
450
|
if self._handoffs:
|
|
@@ -474,7 +476,7 @@ class AgentBase(DataclassJSONSerializable):
|
|
|
474
476
|
Optional type used to cast the final output.
|
|
475
477
|
session : Session or None, default=None
|
|
476
478
|
Optional session for maintaining conversation history across runs.
|
|
477
|
-
If not provided, uses the session from
|
|
479
|
+
If not provided, uses the session from configuration if available.
|
|
478
480
|
|
|
479
481
|
Returns
|
|
480
482
|
-------
|
|
@@ -483,7 +485,7 @@ class AgentBase(DataclassJSONSerializable):
|
|
|
483
485
|
"""
|
|
484
486
|
if self._output_structure is not None and output_structure is None:
|
|
485
487
|
output_structure = self._output_structure
|
|
486
|
-
# Use session from parameter, fall back to
|
|
488
|
+
# Use session from parameter, fall back to configuration session
|
|
487
489
|
session_to_use = session if session is not None else self._session
|
|
488
490
|
return await run_async(
|
|
489
491
|
agent=self.get_agent(),
|
|
@@ -513,7 +515,7 @@ class AgentBase(DataclassJSONSerializable):
|
|
|
513
515
|
Optional type used to cast the final output.
|
|
514
516
|
session : Session or None, default=None
|
|
515
517
|
Optional session for maintaining conversation history across runs.
|
|
516
|
-
If not provided, uses the session from
|
|
518
|
+
If not provided, uses the session from configuration if available.
|
|
517
519
|
|
|
518
520
|
Returns
|
|
519
521
|
-------
|
|
@@ -522,7 +524,7 @@ class AgentBase(DataclassJSONSerializable):
|
|
|
522
524
|
"""
|
|
523
525
|
if self._output_structure is not None and output_structure is None:
|
|
524
526
|
output_structure = self._output_structure
|
|
525
|
-
# Use session from parameter, fall back to
|
|
527
|
+
# Use session from parameter, fall back to configuration session
|
|
526
528
|
session_to_use = session if session is not None else self._session
|
|
527
529
|
return run_sync(
|
|
528
530
|
agent=self.get_agent(),
|
|
@@ -532,47 +534,6 @@ class AgentBase(DataclassJSONSerializable):
|
|
|
532
534
|
session=session_to_use,
|
|
533
535
|
)
|
|
534
536
|
|
|
535
|
-
def run_streamed(
|
|
536
|
-
self,
|
|
537
|
-
input: str,
|
|
538
|
-
*,
|
|
539
|
-
context: Optional[Dict[str, Any]] = None,
|
|
540
|
-
output_structure: Optional[type[StructureBase]] = None,
|
|
541
|
-
session: Optional[Any] = None,
|
|
542
|
-
) -> RunResultStreaming | StructureBase:
|
|
543
|
-
"""Stream the agent execution results.
|
|
544
|
-
|
|
545
|
-
Parameters
|
|
546
|
-
----------
|
|
547
|
-
input : str
|
|
548
|
-
Prompt or query for the agent.
|
|
549
|
-
context : dict or None, default=None
|
|
550
|
-
Optional dictionary passed to the agent.
|
|
551
|
-
output_structure : type[StructureBase] or None, default=None
|
|
552
|
-
Optional type used to cast the final output.
|
|
553
|
-
session : Session or None, default=None
|
|
554
|
-
Optional session for maintaining conversation history across runs.
|
|
555
|
-
If not provided, uses the session from config if available.
|
|
556
|
-
|
|
557
|
-
Returns
|
|
558
|
-
-------
|
|
559
|
-
RunResultStreaming
|
|
560
|
-
Streaming output wrapper from the agent execution.
|
|
561
|
-
"""
|
|
562
|
-
# Use session from parameter, fall back to config session
|
|
563
|
-
session_to_use = session if session is not None else self._session
|
|
564
|
-
output_structure_to_use = output_structure or self._output_structure
|
|
565
|
-
result = run_streamed(
|
|
566
|
-
agent=self.get_agent(),
|
|
567
|
-
input=input,
|
|
568
|
-
context=context,
|
|
569
|
-
output_structure=output_structure_to_use,
|
|
570
|
-
session=session_to_use,
|
|
571
|
-
)
|
|
572
|
-
if output_structure_to_use and hasattr(result, "final_output_as"):
|
|
573
|
-
return cast(Any, result).final_output_as(output_structure_to_use)
|
|
574
|
-
return result
|
|
575
|
-
|
|
576
537
|
def as_tool(self) -> Tool:
|
|
577
538
|
"""Return the agent as a callable tool.
|
|
578
539
|
|
|
@@ -583,78 +544,35 @@ class AgentBase(DataclassJSONSerializable):
|
|
|
583
544
|
"""
|
|
584
545
|
agent = self.get_agent()
|
|
585
546
|
tool_obj: Tool = agent.as_tool(
|
|
586
|
-
tool_name=self.
|
|
547
|
+
tool_name=self._configuration.name,
|
|
548
|
+
tool_description=self._configuration.description,
|
|
587
549
|
)
|
|
588
550
|
return tool_obj
|
|
589
551
|
|
|
590
|
-
def
|
|
552
|
+
def as_tool_handler_registration(
|
|
591
553
|
self,
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
tool_description: str | None = None,
|
|
595
|
-
) -> tuple[dict[str, Callable[..., Any]], dict[str, Any]]:
|
|
596
|
-
"""Return response tool handler and definition for Responses API use.
|
|
597
|
-
|
|
598
|
-
The returned handler serializes tool output as JSON using
|
|
599
|
-
``tool_handler_factory`` so downstream response flows can rely on a
|
|
600
|
-
consistent payload format.
|
|
554
|
+
) -> ToolHandlerRegistration:
|
|
555
|
+
"""Return the agent as a ToolHandlerRegistration for Responses API use.
|
|
601
556
|
|
|
602
557
|
Parameters
|
|
603
558
|
----------
|
|
604
559
|
tool_name : str or None, default=None
|
|
605
560
|
Optional override for the tool name. When None, uses the agent name.
|
|
606
|
-
tool_description : str or None, default=None
|
|
607
|
-
Optional override for the tool description. When None, uses the
|
|
608
|
-
agent description.
|
|
609
|
-
|
|
610
|
-
Returns
|
|
611
|
-
-------
|
|
612
|
-
tuple[dict[str, Callable[..., Any]], dict[str, Any]]
|
|
613
|
-
Tool handler mapping and tool definition for Responses API usage.
|
|
614
|
-
|
|
615
|
-
Examples
|
|
616
|
-
--------
|
|
617
|
-
>>> tool_handler, tool_definition = agent.as_response_tool()
|
|
618
|
-
>>> response = ResponseBase(
|
|
619
|
-
... name="agent_tool",
|
|
620
|
-
... instructions="Use the agent tool when needed.",
|
|
621
|
-
... tools=[tool_definition],
|
|
622
|
-
... output_structure=None,
|
|
623
|
-
... tool_handlers=tool_handler,
|
|
624
|
-
... openai_settings=settings,
|
|
625
|
-
... )
|
|
626
|
-
>>> response.run_sync("Invoke the agent tool") # doctest: +SKIP
|
|
627
561
|
"""
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
prompt = json.dumps(kwargs)
|
|
636
|
-
return self.run_sync(str(prompt))
|
|
637
|
-
|
|
638
|
-
name = tool_name or self.name
|
|
639
|
-
description = tool_description or self.description
|
|
640
|
-
input_model = self._input_structure or PromptStructure
|
|
641
|
-
tool_handler = {name: tool_handler_factory(_run_agent, input_model=input_model)}
|
|
642
|
-
tool_definition = {
|
|
643
|
-
"type": "function",
|
|
644
|
-
"name": name,
|
|
645
|
-
"description": description,
|
|
646
|
-
"strict": True,
|
|
647
|
-
"additionalProperties": False,
|
|
648
|
-
"parameters": self._build_response_parameters(),
|
|
649
|
-
}
|
|
650
|
-
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)
|
|
651
569
|
|
|
652
570
|
def build_response(
|
|
653
571
|
self,
|
|
654
572
|
*,
|
|
655
573
|
openai_settings: OpenAISettings,
|
|
656
574
|
data_path: Path | str | None = None,
|
|
657
|
-
tool_handlers: dict[str,
|
|
575
|
+
tool_handlers: dict[str, ToolHandlerRegistration] | None = None,
|
|
658
576
|
system_vector_store: list[str] | None = None,
|
|
659
577
|
) -> ResponseBase[StructureBase]:
|
|
660
578
|
"""Build a ResponseBase instance from this agent configuration.
|
|
@@ -666,8 +584,9 @@ class AgentBase(DataclassJSONSerializable):
|
|
|
666
584
|
data_path : Path, str, or None, default None
|
|
667
585
|
Optional path for storing response artifacts. When None, the
|
|
668
586
|
response uses the default data directory.
|
|
669
|
-
tool_handlers : dict[str,
|
|
670
|
-
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.
|
|
671
590
|
system_vector_store : list[str] or None, default None
|
|
672
591
|
Optional list of vector store names to attach as system context.
|
|
673
592
|
|
|
@@ -681,8 +600,8 @@ class AgentBase(DataclassJSONSerializable):
|
|
|
681
600
|
>>> from openai_sdk_helpers import OpenAISettings
|
|
682
601
|
>>> response = agent.build_response(openai_settings=OpenAISettings.from_env())
|
|
683
602
|
"""
|
|
684
|
-
from ..response.base import ResponseBase
|
|
685
|
-
from ..
|
|
603
|
+
from ..response.base import ResponseBase
|
|
604
|
+
from ..settings import OpenAISettings
|
|
686
605
|
|
|
687
606
|
if not isinstance(openai_settings, OpenAISettings):
|
|
688
607
|
raise TypeError("openai_settings must be an OpenAISettings instance")
|
|
@@ -690,8 +609,8 @@ class AgentBase(DataclassJSONSerializable):
|
|
|
690
609
|
tools = self._normalize_response_tools(self.tools)
|
|
691
610
|
|
|
692
611
|
return ResponseBase(
|
|
693
|
-
name=self.name,
|
|
694
|
-
instructions=self.instructions_text,
|
|
612
|
+
name=self._configuration.name,
|
|
613
|
+
instructions=self._configuration.instructions_text,
|
|
695
614
|
tools=tools,
|
|
696
615
|
output_structure=self.output_structure,
|
|
697
616
|
system_vector_store=system_vector_store,
|
|
@@ -770,7 +689,7 @@ class AgentBase(DataclassJSONSerializable):
|
|
|
770
689
|
|
|
771
690
|
Examples
|
|
772
691
|
--------
|
|
773
|
-
>>> agent = AgentBase(
|
|
692
|
+
>>> agent = AgentBase(configuration)
|
|
774
693
|
>>> try:
|
|
775
694
|
... result = agent.run_sync("query")
|
|
776
695
|
... finally:
|
|
@@ -787,7 +706,7 @@ class AgentBase(DataclassJSONSerializable):
|
|
|
787
706
|
str
|
|
788
707
|
String representation including agent name and model.
|
|
789
708
|
"""
|
|
790
|
-
return f"<AgentBase name={self.
|
|
709
|
+
return f"<AgentBase name={self.name!r} model={self.model!r}>"
|
|
791
710
|
|
|
792
711
|
def save(self, filepath: str | Path | None = None) -> None:
|
|
793
712
|
"""Serialize the message history to a JSON file.
|
|
@@ -806,7 +725,7 @@ class AgentBase(DataclassJSONSerializable):
|
|
|
806
725
|
target = Path(filepath)
|
|
807
726
|
else:
|
|
808
727
|
filename = f"{str(self.uuid).lower()}.json"
|
|
809
|
-
target = self._data_path / self.
|
|
728
|
+
target = self._data_path / self.name / filename
|
|
810
729
|
|
|
811
730
|
checked = check_filepath(filepath=target)
|
|
812
731
|
self.to_json_file(filepath=checked)
|
|
@@ -24,12 +24,12 @@ class AgentRegistry(RegistryBase["AgentConfiguration"]):
|
|
|
24
24
|
Examples
|
|
25
25
|
--------
|
|
26
26
|
>>> registry = AgentRegistry()
|
|
27
|
-
>>>
|
|
27
|
+
>>> configuration = AgentConfiguration(
|
|
28
28
|
... name="test_agent",
|
|
29
29
|
... model="gpt-4o-mini",
|
|
30
30
|
... instructions="Test instructions"
|
|
31
31
|
... )
|
|
32
|
-
>>> registry.register(
|
|
32
|
+
>>> registry.register(configuration)
|
|
33
33
|
>>> retrieved = registry.get("test_agent")
|
|
34
34
|
>>> retrieved.name
|
|
35
35
|
'test_agent'
|
|
@@ -85,10 +85,10 @@ def get_default_registry() -> AgentRegistry:
|
|
|
85
85
|
Examples
|
|
86
86
|
--------
|
|
87
87
|
>>> registry = get_default_registry()
|
|
88
|
-
>>>
|
|
88
|
+
>>> configuration = AgentConfiguration(
|
|
89
89
|
... name="test", model="gpt-4o-mini", instructions="Test instructions"
|
|
90
90
|
... )
|
|
91
|
-
>>> registry.register(
|
|
91
|
+
>>> registry.register(configuration)
|
|
92
92
|
"""
|
|
93
93
|
return _default_registry
|
|
94
94
|
|
|
@@ -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.
|
|
@@ -165,28 +165,28 @@ class AgentConfiguration(DataclassJSONSerializable):
|
|
|
165
165
|
|
|
166
166
|
Examples
|
|
167
167
|
--------
|
|
168
|
-
>>>
|
|
168
|
+
>>> configuration = AgentConfiguration(
|
|
169
169
|
... name="summarizer",
|
|
170
170
|
... description="Summarizes text",
|
|
171
171
|
... model="gpt-4o-mini"
|
|
172
172
|
... )
|
|
173
|
-
>>>
|
|
173
|
+
>>> configuration.name
|
|
174
174
|
'summarizer'
|
|
175
175
|
"""
|
|
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 config doesn't specify one.
|
|
312
306
|
|
|
313
307
|
Returns
|
|
314
308
|
-------
|
|
@@ -317,20 +311,18 @@ class AgentConfiguration(DataclassJSONSerializable):
|
|
|
317
311
|
|
|
318
312
|
Examples
|
|
319
313
|
--------
|
|
320
|
-
>>>
|
|
314
|
+
>>> configuration = AgentConfiguration(
|
|
321
315
|
... name="helper", model="gpt-4o-mini", instructions="Help the user"
|
|
322
316
|
... )
|
|
323
|
-
>>> agent =
|
|
317
|
+
>>> agent = configuration.gen_agent()
|
|
324
318
|
>>> result = agent.run_sync("Hello!")
|
|
325
319
|
"""
|
|
326
320
|
# Import here to avoid circular dependency
|
|
327
321
|
from .base import AgentBase
|
|
328
322
|
|
|
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:
|
|
@@ -352,10 +344,10 @@ class AgentConfiguration(DataclassJSONSerializable):
|
|
|
352
344
|
|
|
353
345
|
Examples
|
|
354
346
|
--------
|
|
355
|
-
>>>
|
|
347
|
+
>>> configuration = AgentConfiguration(
|
|
356
348
|
... name="agent1", model="gpt-4o-mini", instructions="Agent instructions"
|
|
357
349
|
... )
|
|
358
|
-
>>> config2 =
|
|
350
|
+
>>> config2 = configuration.replace(name="agent2", description="Modified")
|
|
359
351
|
>>> config2.name
|
|
360
352
|
'agent2'
|
|
361
353
|
>>> config2.model
|
|
@@ -387,7 +379,7 @@ class AgentConfiguration(DataclassJSONSerializable):
|
|
|
387
379
|
>>> response_config.name
|
|
388
380
|
'responder'
|
|
389
381
|
"""
|
|
390
|
-
from ..response.
|
|
382
|
+
from ..response.configuration import ResponseConfiguration
|
|
391
383
|
|
|
392
384
|
return ResponseConfiguration(
|
|
393
385
|
name=self.name,
|