openai-sdk-helpers 0.3.0__py3-none-any.whl → 0.4.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 +6 -6
- openai_sdk_helpers/agent/__init__.py +2 -2
- openai_sdk_helpers/agent/base.py +231 -110
- openai_sdk_helpers/agent/config.py +83 -29
- openai_sdk_helpers/agent/coordination.py +64 -28
- openai_sdk_helpers/agent/runner.py +16 -15
- openai_sdk_helpers/agent/search/base.py +94 -45
- openai_sdk_helpers/agent/search/vector.py +86 -58
- openai_sdk_helpers/agent/search/web.py +71 -40
- openai_sdk_helpers/agent/summarizer.py +32 -7
- openai_sdk_helpers/agent/translator.py +57 -24
- openai_sdk_helpers/agent/validation.py +34 -4
- openai_sdk_helpers/cli.py +42 -0
- openai_sdk_helpers/config.py +0 -1
- openai_sdk_helpers/environment.py +3 -2
- openai_sdk_helpers/files_api.py +35 -3
- openai_sdk_helpers/prompt/base.py +6 -0
- openai_sdk_helpers/response/__init__.py +3 -3
- openai_sdk_helpers/response/base.py +142 -73
- openai_sdk_helpers/response/config.py +43 -51
- openai_sdk_helpers/response/files.py +5 -5
- openai_sdk_helpers/response/messages.py +3 -3
- openai_sdk_helpers/response/runner.py +7 -7
- openai_sdk_helpers/response/tool_call.py +94 -4
- openai_sdk_helpers/response/vector_store.py +3 -3
- openai_sdk_helpers/streamlit_app/app.py +16 -16
- openai_sdk_helpers/streamlit_app/config.py +38 -37
- openai_sdk_helpers/streamlit_app/streamlit_web_search.py +2 -2
- openai_sdk_helpers/structure/__init__.py +6 -2
- openai_sdk_helpers/structure/agent_blueprint.py +2 -2
- openai_sdk_helpers/structure/base.py +8 -99
- openai_sdk_helpers/structure/plan/plan.py +2 -2
- openai_sdk_helpers/structure/plan/task.py +9 -9
- openai_sdk_helpers/structure/prompt.py +2 -2
- openai_sdk_helpers/structure/responses.py +15 -15
- openai_sdk_helpers/structure/summary.py +3 -3
- openai_sdk_helpers/structure/translation.py +32 -0
- openai_sdk_helpers/structure/validation.py +2 -2
- openai_sdk_helpers/structure/vector_search.py +7 -7
- openai_sdk_helpers/structure/web_search.py +6 -6
- openai_sdk_helpers/tools.py +41 -15
- openai_sdk_helpers/utils/__init__.py +19 -5
- openai_sdk_helpers/utils/json/__init__.py +55 -0
- openai_sdk_helpers/utils/json/base_model.py +181 -0
- openai_sdk_helpers/utils/{json_utils.py → json/data_class.py} +33 -68
- openai_sdk_helpers/utils/json/ref.py +113 -0
- openai_sdk_helpers/utils/json/utils.py +203 -0
- openai_sdk_helpers/utils/output_validation.py +21 -1
- openai_sdk_helpers/utils/path_utils.py +34 -1
- openai_sdk_helpers/utils/registry.py +17 -6
- openai_sdk_helpers/vector_storage/storage.py +10 -0
- {openai_sdk_helpers-0.3.0.dist-info → openai_sdk_helpers-0.4.0.dist-info}/METADATA +7 -7
- openai_sdk_helpers-0.4.0.dist-info/RECORD +86 -0
- openai_sdk_helpers-0.3.0.dist-info/RECORD +0 -81
- {openai_sdk_helpers-0.3.0.dist-info → openai_sdk_helpers-0.4.0.dist-info}/WHEEL +0 -0
- {openai_sdk_helpers-0.3.0.dist-info → openai_sdk_helpers-0.4.0.dist-info}/entry_points.txt +0 -0
- {openai_sdk_helpers-0.3.0.dist-info → openai_sdk_helpers-0.4.0.dist-info}/licenses/LICENSE +0 -0
|
@@ -1,17 +1,18 @@
|
|
|
1
|
-
"""Configuration helpers for ``
|
|
1
|
+
"""Configuration helpers for ``AgentBase``."""
|
|
2
2
|
|
|
3
3
|
from __future__ import annotations
|
|
4
4
|
|
|
5
|
-
from dataclasses import dataclass
|
|
5
|
+
from dataclasses import dataclass, field
|
|
6
6
|
from pathlib import Path
|
|
7
|
-
from typing import Any, Optional
|
|
7
|
+
from typing import Any, Optional, Type
|
|
8
8
|
|
|
9
9
|
from agents import Agent, Handoff, InputGuardrail, OutputGuardrail, Session
|
|
10
10
|
from agents.model_settings import ModelSettings
|
|
11
11
|
|
|
12
|
-
from ..utils import
|
|
12
|
+
from ..utils.json.data_class import DataclassJSONSerializable
|
|
13
13
|
from ..utils.registry import BaseRegistry
|
|
14
14
|
from ..utils.instructions import resolve_instructions_from_path
|
|
15
|
+
from ..structure.base import StructureBase
|
|
15
16
|
|
|
16
17
|
|
|
17
18
|
class AgentConfigurationRegistry(BaseRegistry["AgentConfiguration"]):
|
|
@@ -73,10 +74,6 @@ class AgentConfigurationRegistry(BaseRegistry["AgentConfiguration"]):
|
|
|
73
74
|
return super().load_from_directory(path, config_class=config_class)
|
|
74
75
|
|
|
75
76
|
|
|
76
|
-
# Global default registry instance
|
|
77
|
-
_default_registry = AgentConfigurationRegistry()
|
|
78
|
-
|
|
79
|
-
|
|
80
77
|
def get_default_registry() -> AgentConfigurationRegistry:
|
|
81
78
|
"""Return the global default registry instance.
|
|
82
79
|
|
|
@@ -97,12 +94,12 @@ def get_default_registry() -> AgentConfigurationRegistry:
|
|
|
97
94
|
|
|
98
95
|
|
|
99
96
|
@dataclass(frozen=True, slots=True)
|
|
100
|
-
class AgentConfiguration(
|
|
101
|
-
"""Immutable configuration for building a
|
|
97
|
+
class AgentConfiguration(DataclassJSONSerializable):
|
|
98
|
+
"""Immutable configuration for building a AgentBase.
|
|
102
99
|
|
|
103
100
|
Encapsulates all metadata required to define an agent including its
|
|
104
101
|
instructions, tools, model settings, handoffs, guardrails, and session
|
|
105
|
-
management. Inherits from
|
|
102
|
+
management. Inherits from DataclassJSONSerializable to support serialization.
|
|
106
103
|
|
|
107
104
|
This dataclass is frozen (immutable) to ensure thread-safety and
|
|
108
105
|
enable use as dictionary keys. All list-type fields use None as the
|
|
@@ -124,12 +121,10 @@ class AgentConfiguration(JSONSerializable):
|
|
|
124
121
|
Path to the Jinja template (absolute or relative to prompt_dir).
|
|
125
122
|
This takes precedence over instructions if both are provided.
|
|
126
123
|
Default is None.
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
Type describing the agent output, commonly a Pydantic model or
|
|
132
|
-
builtin like str. Default is None.
|
|
124
|
+
input_structure : type[StructureBase], optional
|
|
125
|
+
Structure class describing the agent input. Default is None.
|
|
126
|
+
output_structure : type[StructureBase], optional
|
|
127
|
+
Structure class describing the agent output. Default is None.
|
|
133
128
|
tools : list, optional
|
|
134
129
|
Tool definitions available to the agent. Default is None.
|
|
135
130
|
model_settings : ModelSettings, optional
|
|
@@ -153,8 +148,10 @@ class AgentConfiguration(JSONSerializable):
|
|
|
153
148
|
Validate configuration invariants after initialization.
|
|
154
149
|
instructions_text
|
|
155
150
|
Return the resolved instruction content as a string.
|
|
156
|
-
|
|
157
|
-
|
|
151
|
+
resolve_prompt_path(prompt_dir)
|
|
152
|
+
Resolve the prompt template path for this configuration.
|
|
153
|
+
gen_agent(run_context_wrapper, prompt_dir, default_model)
|
|
154
|
+
Create a AgentBase instance from this configuration.
|
|
158
155
|
replace(**changes)
|
|
159
156
|
Create a new AgentConfiguration with specified fields replaced.
|
|
160
157
|
to_json()
|
|
@@ -182,14 +179,17 @@ class AgentConfiguration(JSONSerializable):
|
|
|
182
179
|
description: Optional[str] = None
|
|
183
180
|
model: Optional[str] = None
|
|
184
181
|
template_path: Optional[str | Path] = None
|
|
185
|
-
|
|
186
|
-
|
|
182
|
+
input_structure: Optional[Type[StructureBase]] = None
|
|
183
|
+
output_structure: Optional[Type[StructureBase]] = None
|
|
187
184
|
tools: Optional[list] = None
|
|
188
185
|
model_settings: Optional[ModelSettings] = None
|
|
189
186
|
handoffs: Optional[list[Agent | Handoff]] = None
|
|
190
187
|
input_guardrails: Optional[list[InputGuardrail]] = None
|
|
191
188
|
output_guardrails: Optional[list[OutputGuardrail]] = None
|
|
192
189
|
session: Optional[Session] = None
|
|
190
|
+
_instructions_cache: Optional[str] = field(
|
|
191
|
+
default=None, init=False, repr=False, compare=False
|
|
192
|
+
)
|
|
193
193
|
|
|
194
194
|
def __post_init__(self) -> None:
|
|
195
195
|
"""Validate configuration invariants after initialization.
|
|
@@ -202,6 +202,8 @@ class AgentConfiguration(JSONSerializable):
|
|
|
202
202
|
TypeError
|
|
203
203
|
If name is not a non-empty string.
|
|
204
204
|
If instructions is not a string or Path.
|
|
205
|
+
If input_structure or output_structure is not a class.
|
|
206
|
+
If input_structure or output_structure does not subclass StructureBase.
|
|
205
207
|
ValueError
|
|
206
208
|
If instructions is an empty string.
|
|
207
209
|
FileNotFoundError
|
|
@@ -226,6 +228,19 @@ class AgentConfiguration(JSONSerializable):
|
|
|
226
228
|
else:
|
|
227
229
|
raise TypeError("AgentConfiguration.instructions must be a str or Path")
|
|
228
230
|
|
|
231
|
+
for attr in ("input_structure", "output_structure"):
|
|
232
|
+
cls = getattr(self, attr)
|
|
233
|
+
if cls is None:
|
|
234
|
+
continue
|
|
235
|
+
if not isinstance(cls, type):
|
|
236
|
+
raise TypeError(
|
|
237
|
+
f"AgentConfiguration.{attr} must be a class (Type[StructureBase]) or None"
|
|
238
|
+
)
|
|
239
|
+
if not issubclass(cls, StructureBase):
|
|
240
|
+
raise TypeError(
|
|
241
|
+
f"AgentConfiguration.{attr} must subclass StructureBase"
|
|
242
|
+
)
|
|
243
|
+
|
|
229
244
|
if self.template_path is not None and isinstance(self.template_path, Path):
|
|
230
245
|
# Validate template_path if it's a Path object
|
|
231
246
|
template = self.template_path.expanduser()
|
|
@@ -243,21 +258,44 @@ class AgentConfiguration(JSONSerializable):
|
|
|
243
258
|
str
|
|
244
259
|
Plain-text instructions, loading template files when necessary.
|
|
245
260
|
"""
|
|
246
|
-
|
|
261
|
+
cached = self._instructions_cache
|
|
262
|
+
if cached is None:
|
|
263
|
+
cached = self._resolve_instructions()
|
|
264
|
+
object.__setattr__(self, "_instructions_cache", cached)
|
|
265
|
+
return cached
|
|
247
266
|
|
|
248
267
|
def _resolve_instructions(self) -> str:
|
|
249
268
|
"""Resolve instructions from string or file path."""
|
|
250
269
|
return resolve_instructions_from_path(self.instructions)
|
|
251
270
|
|
|
252
|
-
def
|
|
271
|
+
def resolve_prompt_path(self, prompt_dir: Path | None = None) -> Path | None:
|
|
272
|
+
"""Resolve the prompt template path for this configuration.
|
|
273
|
+
|
|
274
|
+
Parameters
|
|
275
|
+
----------
|
|
276
|
+
prompt_dir : Path or None, default=None
|
|
277
|
+
Directory holding prompt templates when a relative path is needed.
|
|
278
|
+
|
|
279
|
+
Returns
|
|
280
|
+
-------
|
|
281
|
+
Path or None
|
|
282
|
+
Resolved prompt path if a template is configured or discovered.
|
|
283
|
+
"""
|
|
284
|
+
if self.template_path:
|
|
285
|
+
return Path(self.template_path)
|
|
286
|
+
if prompt_dir is not None:
|
|
287
|
+
return prompt_dir / f"{self.name}.jinja"
|
|
288
|
+
return None
|
|
289
|
+
|
|
290
|
+
def gen_agent(
|
|
253
291
|
self,
|
|
254
292
|
run_context_wrapper: Any = None,
|
|
255
293
|
prompt_dir: Path | None = None,
|
|
256
294
|
default_model: str | None = None,
|
|
257
295
|
) -> Any:
|
|
258
|
-
"""Create a
|
|
296
|
+
"""Create a AgentBase instance from this configuration.
|
|
259
297
|
|
|
260
|
-
This is a convenience method that
|
|
298
|
+
This is a convenience method that instantiates ``AgentBase`` directly.
|
|
261
299
|
|
|
262
300
|
Parameters
|
|
263
301
|
----------
|
|
@@ -270,7 +308,7 @@ class AgentConfiguration(JSONSerializable):
|
|
|
270
308
|
|
|
271
309
|
Returns
|
|
272
310
|
-------
|
|
273
|
-
|
|
311
|
+
AgentBase
|
|
274
312
|
Configured agent instance ready for execution.
|
|
275
313
|
|
|
276
314
|
Examples
|
|
@@ -278,13 +316,13 @@ class AgentConfiguration(JSONSerializable):
|
|
|
278
316
|
>>> config = AgentConfiguration(
|
|
279
317
|
... name="helper", model="gpt-4o-mini", instructions="Help the user"
|
|
280
318
|
... )
|
|
281
|
-
>>> agent = config.
|
|
319
|
+
>>> agent = config.gen_agent()
|
|
282
320
|
>>> result = agent.run_sync("Hello!")
|
|
283
321
|
"""
|
|
284
322
|
# Import here to avoid circular dependency
|
|
285
|
-
from .base import
|
|
323
|
+
from .base import AgentBase
|
|
286
324
|
|
|
287
|
-
return
|
|
325
|
+
return AgentBase(
|
|
288
326
|
config=self,
|
|
289
327
|
run_context_wrapper=run_context_wrapper,
|
|
290
328
|
prompt_dir=prompt_dir,
|
|
@@ -323,6 +361,18 @@ class AgentConfiguration(JSONSerializable):
|
|
|
323
361
|
|
|
324
362
|
return replace(self, **changes)
|
|
325
363
|
|
|
364
|
+
def to_json(self) -> dict[str, Any]:
|
|
365
|
+
"""Return a JSON-compatible dict representation.
|
|
366
|
+
|
|
367
|
+
Returns
|
|
368
|
+
-------
|
|
369
|
+
dict[str, Any]
|
|
370
|
+
Serialized configuration data without cached fields.
|
|
371
|
+
"""
|
|
372
|
+
data = DataclassJSONSerializable.to_json(self)
|
|
373
|
+
data.pop("_instructions_cache", None)
|
|
374
|
+
return data
|
|
375
|
+
|
|
326
376
|
@classmethod
|
|
327
377
|
def from_json(cls, data: dict[str, Any]) -> AgentConfiguration:
|
|
328
378
|
"""Create an AgentConfiguration from JSON data.
|
|
@@ -350,6 +400,7 @@ class AgentConfiguration(JSONSerializable):
|
|
|
350
400
|
"""
|
|
351
401
|
# Make a copy to avoid modifying the input
|
|
352
402
|
data = data.copy()
|
|
403
|
+
data.pop("_instructions_cache", None)
|
|
353
404
|
|
|
354
405
|
# Handle instructions field: if it's a string path to an existing file,
|
|
355
406
|
# convert it back to Path for proper file loading
|
|
@@ -372,4 +423,7 @@ class AgentConfiguration(JSONSerializable):
|
|
|
372
423
|
return super(AgentConfiguration, cls).from_json(data)
|
|
373
424
|
|
|
374
425
|
|
|
426
|
+
# Global default registry instance
|
|
427
|
+
_default_registry = AgentConfigurationRegistry()
|
|
428
|
+
|
|
375
429
|
__all__ = ["AgentConfiguration", "AgentConfigurationRegistry", "get_default_registry"]
|
|
@@ -12,8 +12,8 @@ from typing import Any, Callable, Dict, List, Optional
|
|
|
12
12
|
|
|
13
13
|
|
|
14
14
|
from ..structure import TaskStructure, PlanStructure, PromptStructure
|
|
15
|
-
from ..utils import
|
|
16
|
-
from .base import
|
|
15
|
+
from ..utils import DataclassJSONSerializable, ensure_directory, log
|
|
16
|
+
from .base import AgentBase
|
|
17
17
|
from .config import AgentConfiguration
|
|
18
18
|
from ..structure.plan.enum import AgentEnum
|
|
19
19
|
|
|
@@ -23,9 +23,30 @@ ExecutePlanFn = Callable[[PlanStructure], List[str]]
|
|
|
23
23
|
SummarizeFn = Callable[[List[str]], str]
|
|
24
24
|
|
|
25
25
|
|
|
26
|
-
class CoordinatorAgent(
|
|
26
|
+
class CoordinatorAgent(AgentBase):
|
|
27
27
|
"""Coordinate agent plans while persisting project state and outputs.
|
|
28
28
|
|
|
29
|
+
Parameters
|
|
30
|
+
----------
|
|
31
|
+
prompt_fn : PromptFn
|
|
32
|
+
Callable that generates a prompt brief from the input string.
|
|
33
|
+
build_plan_fn : BuildPlanFn
|
|
34
|
+
Callable that generates a plan from the prompt brief.
|
|
35
|
+
execute_plan_fn : ExecutePlanFn
|
|
36
|
+
Callable that executes a plan and returns results.
|
|
37
|
+
summarize_fn : SummarizeFn
|
|
38
|
+
Callable that summarizes a list of result strings.
|
|
39
|
+
module_data_path : Path
|
|
40
|
+
Base path for persisting project artifacts.
|
|
41
|
+
name : str
|
|
42
|
+
Name of the parent module for data organization.
|
|
43
|
+
config : AgentConfiguration or None, default=None
|
|
44
|
+
Optional agent configuration describing prompts and metadata.
|
|
45
|
+
prompt_dir : Path or None, default=None
|
|
46
|
+
Optional directory holding prompt templates.
|
|
47
|
+
default_model : str or None, default=None
|
|
48
|
+
Optional fallback model identifier.
|
|
49
|
+
|
|
29
50
|
Methods
|
|
30
51
|
-------
|
|
31
52
|
build_prompt(prompt)
|
|
@@ -89,6 +110,22 @@ class CoordinatorAgent(BaseAgent, JSONSerializable):
|
|
|
89
110
|
Optional directory holding prompt templates.
|
|
90
111
|
default_model : str or None, default=None
|
|
91
112
|
Optional fallback model identifier.
|
|
113
|
+
|
|
114
|
+
Raises
|
|
115
|
+
------
|
|
116
|
+
ValueError
|
|
117
|
+
If the provided configuration is invalid.
|
|
118
|
+
|
|
119
|
+
Examples
|
|
120
|
+
--------
|
|
121
|
+
>>> coordinator = CoordinatorAgent(
|
|
122
|
+
... prompt_fn=lambda p: PromptStructure(prompt=p),
|
|
123
|
+
... build_plan_fn=lambda p: PlanStructure(),
|
|
124
|
+
... execute_plan_fn=lambda p: [],
|
|
125
|
+
... summarize_fn=lambda r: "summary",
|
|
126
|
+
... module_data_path=Path("."),
|
|
127
|
+
... name="test",
|
|
128
|
+
... )
|
|
92
129
|
"""
|
|
93
130
|
if config is None:
|
|
94
131
|
config = AgentConfiguration(
|
|
@@ -120,6 +157,10 @@ class CoordinatorAgent(BaseAgent, JSONSerializable):
|
|
|
120
157
|
----------
|
|
121
158
|
prompt : str
|
|
122
159
|
The core request or goal for the project.
|
|
160
|
+
|
|
161
|
+
Examples
|
|
162
|
+
--------
|
|
163
|
+
>>> coordinator.build_prompt("Analyze the impact of AI on healthcare.")
|
|
123
164
|
"""
|
|
124
165
|
log("build_prompt", level=logging.INFO)
|
|
125
166
|
self.start_date = datetime.now(timezone.utc)
|
|
@@ -134,6 +175,11 @@ class CoordinatorAgent(BaseAgent, JSONSerializable):
|
|
|
134
175
|
------
|
|
135
176
|
ValueError
|
|
136
177
|
If called before :meth:`build_prompt`.
|
|
178
|
+
|
|
179
|
+
Examples
|
|
180
|
+
--------
|
|
181
|
+
>>> coordinator.build_prompt("Analyze AI in healthcare.")
|
|
182
|
+
>>> coordinator.build_plan()
|
|
137
183
|
"""
|
|
138
184
|
log("build_plan", level=logging.INFO)
|
|
139
185
|
if not self.brief:
|
|
@@ -151,6 +197,12 @@ class CoordinatorAgent(BaseAgent, JSONSerializable):
|
|
|
151
197
|
-------
|
|
152
198
|
list[str]
|
|
153
199
|
Flattened list of results from all executed tasks.
|
|
200
|
+
|
|
201
|
+
Examples
|
|
202
|
+
--------
|
|
203
|
+
>>> coordinator.build_prompt("Analyze AI.")
|
|
204
|
+
>>> coordinator.build_plan()
|
|
205
|
+
>>> results = coordinator.execute_plan()
|
|
154
206
|
"""
|
|
155
207
|
log("execute_plan", level=logging.INFO)
|
|
156
208
|
if not self.plan:
|
|
@@ -174,6 +226,11 @@ class CoordinatorAgent(BaseAgent, JSONSerializable):
|
|
|
174
226
|
-------
|
|
175
227
|
str
|
|
176
228
|
Concise summary derived from the provided results.
|
|
229
|
+
|
|
230
|
+
Examples
|
|
231
|
+
--------
|
|
232
|
+
>>> results = ["AI is impacting healthcare.", "New models are faster."]
|
|
233
|
+
>>> summary = coordinator.summarize_plan(results)
|
|
177
234
|
"""
|
|
178
235
|
log("summarize_plan", level=logging.INFO)
|
|
179
236
|
|
|
@@ -199,37 +256,16 @@ class CoordinatorAgent(BaseAgent, JSONSerializable):
|
|
|
199
256
|
----------
|
|
200
257
|
prompt : str
|
|
201
258
|
The request or question to analyze and summarize.
|
|
259
|
+
|
|
260
|
+
Examples
|
|
261
|
+
--------
|
|
262
|
+
>>> coordinator.run_plan("Analyze the future of AI.")
|
|
202
263
|
"""
|
|
203
264
|
self.build_prompt(prompt)
|
|
204
265
|
self.build_plan()
|
|
205
266
|
results = self.execute_plan()
|
|
206
267
|
self.summarize_plan(results)
|
|
207
268
|
|
|
208
|
-
@property
|
|
209
|
-
def file_path(self) -> Path:
|
|
210
|
-
"""Return the path where the project snapshot will be stored.
|
|
211
|
-
|
|
212
|
-
Returns
|
|
213
|
-
-------
|
|
214
|
-
Path
|
|
215
|
-
Location of the JSON artifact for the current run.
|
|
216
|
-
"""
|
|
217
|
-
if not self.start_date:
|
|
218
|
-
self.start_date = datetime.now(timezone.utc)
|
|
219
|
-
start_date_str = self.start_date.strftime("%Y%m%d_%H%M%S")
|
|
220
|
-
return self._module_data_path / self._name / f"{start_date_str}.json"
|
|
221
|
-
|
|
222
|
-
def save(self) -> Path:
|
|
223
|
-
"""Persist the current project state to disk.
|
|
224
|
-
|
|
225
|
-
Returns
|
|
226
|
-
-------
|
|
227
|
-
Path
|
|
228
|
-
Path to the saved JSON artifact.
|
|
229
|
-
"""
|
|
230
|
-
self.to_json_file(self.file_path)
|
|
231
|
-
return self.file_path
|
|
232
|
-
|
|
233
269
|
@staticmethod
|
|
234
270
|
def _run_task(
|
|
235
271
|
task: TaskStructure,
|
|
@@ -12,6 +12,7 @@ from typing import Any, Dict, Optional
|
|
|
12
12
|
from agents import Agent, RunResult, RunResultStreaming, Runner, Session
|
|
13
13
|
|
|
14
14
|
from openai_sdk_helpers.utils.async_utils import run_coroutine_with_fallback
|
|
15
|
+
from ..structure.base import StructureBase
|
|
15
16
|
|
|
16
17
|
|
|
17
18
|
async def run_async(
|
|
@@ -19,7 +20,7 @@ async def run_async(
|
|
|
19
20
|
input: str,
|
|
20
21
|
*,
|
|
21
22
|
context: Optional[Dict[str, Any]] = None,
|
|
22
|
-
|
|
23
|
+
output_structure: Optional[type[StructureBase]] = None,
|
|
23
24
|
session: Optional[Session] = None,
|
|
24
25
|
) -> Any:
|
|
25
26
|
"""Run an Agent asynchronously.
|
|
@@ -32,7 +33,7 @@ async def run_async(
|
|
|
32
33
|
Prompt or query string for the agent.
|
|
33
34
|
context : dict or None, default=None
|
|
34
35
|
Optional context dictionary passed to the agent.
|
|
35
|
-
|
|
36
|
+
output_structure : type[StructureBase] or None, default=None
|
|
36
37
|
Optional type used to cast the final output.
|
|
37
38
|
session : Session or None, default=None
|
|
38
39
|
Optional session for maintaining conversation history.
|
|
@@ -40,7 +41,7 @@ async def run_async(
|
|
|
40
41
|
Returns
|
|
41
42
|
-------
|
|
42
43
|
Any
|
|
43
|
-
Agent response, optionally converted to ``
|
|
44
|
+
Agent response, optionally converted to ``output_structure``.
|
|
44
45
|
|
|
45
46
|
Examples
|
|
46
47
|
--------
|
|
@@ -53,8 +54,8 @@ async def run_async(
|
|
|
53
54
|
>>> asyncio.run(example()) # doctest: +SKIP
|
|
54
55
|
"""
|
|
55
56
|
result = await Runner.run(agent, input, context=context, session=session)
|
|
56
|
-
if
|
|
57
|
-
return result.final_output_as(
|
|
57
|
+
if output_structure is not None:
|
|
58
|
+
return result.final_output_as(output_structure)
|
|
58
59
|
return result
|
|
59
60
|
|
|
60
61
|
|
|
@@ -63,7 +64,7 @@ def run_sync(
|
|
|
63
64
|
input: str,
|
|
64
65
|
*,
|
|
65
66
|
context: Optional[Dict[str, Any]] = None,
|
|
66
|
-
|
|
67
|
+
output_structure: Optional[type[StructureBase]] = None,
|
|
67
68
|
session: Optional[Session] = None,
|
|
68
69
|
) -> Any:
|
|
69
70
|
"""Run an Agent synchronously.
|
|
@@ -80,7 +81,7 @@ def run_sync(
|
|
|
80
81
|
Prompt or query string for the agent.
|
|
81
82
|
context : dict or None, default=None
|
|
82
83
|
Optional context dictionary passed to the agent.
|
|
83
|
-
|
|
84
|
+
output_structure : type[StructureBase] or None, default=None
|
|
84
85
|
Optional type used to cast the final output.
|
|
85
86
|
session : Session or None, default=None
|
|
86
87
|
Optional session for maintaining conversation history.
|
|
@@ -88,7 +89,7 @@ def run_sync(
|
|
|
88
89
|
Returns
|
|
89
90
|
-------
|
|
90
91
|
Any
|
|
91
|
-
Agent response, optionally converted to ``
|
|
92
|
+
Agent response, optionally converted to ``output_structure``.
|
|
92
93
|
|
|
93
94
|
Raises
|
|
94
95
|
------
|
|
@@ -103,8 +104,8 @@ def run_sync(
|
|
|
103
104
|
"""
|
|
104
105
|
coro = Runner.run(agent, input, context=context, session=session)
|
|
105
106
|
result: RunResult = run_coroutine_with_fallback(coro)
|
|
106
|
-
if
|
|
107
|
-
return result.final_output_as(
|
|
107
|
+
if output_structure is not None:
|
|
108
|
+
return result.final_output_as(output_structure)
|
|
108
109
|
return result
|
|
109
110
|
|
|
110
111
|
|
|
@@ -113,9 +114,9 @@ def run_streamed(
|
|
|
113
114
|
input: str,
|
|
114
115
|
*,
|
|
115
116
|
context: Optional[Dict[str, Any]] = None,
|
|
116
|
-
|
|
117
|
+
output_structure: Optional[type[StructureBase]] = None,
|
|
117
118
|
session: Optional[Session] = None,
|
|
118
|
-
) -> RunResultStreaming:
|
|
119
|
+
) -> RunResultStreaming | StructureBase:
|
|
119
120
|
"""Stream agent execution results.
|
|
120
121
|
|
|
121
122
|
Parameters
|
|
@@ -126,7 +127,7 @@ def run_streamed(
|
|
|
126
127
|
Prompt or query string for the agent.
|
|
127
128
|
context : dict or None, default=None
|
|
128
129
|
Optional context dictionary passed to the agent.
|
|
129
|
-
|
|
130
|
+
output_structure : type[StructureBase] or None, default=None
|
|
130
131
|
Optional type used to cast the final output.
|
|
131
132
|
session : Session or None, default=None
|
|
132
133
|
Optional session for maintaining conversation history.
|
|
@@ -145,8 +146,8 @@ def run_streamed(
|
|
|
145
146
|
... print(chunk, end="")
|
|
146
147
|
"""
|
|
147
148
|
result = Runner.run_streamed(agent, input, context=context, session=session)
|
|
148
|
-
if
|
|
149
|
-
return result.final_output_as(
|
|
149
|
+
if output_structure is not None:
|
|
150
|
+
return result.final_output_as(output_structure)
|
|
150
151
|
return result
|
|
151
152
|
|
|
152
153
|
|