openai-sdk-helpers 0.3.0__py3-none-any.whl → 0.4.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.
- openai_sdk_helpers/__init__.py +6 -6
- openai_sdk_helpers/agent/__init__.py +4 -4
- openai_sdk_helpers/agent/base.py +254 -113
- openai_sdk_helpers/agent/config.py +91 -37
- 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 +63 -58
- 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/__init__.py +4 -4
- openai_sdk_helpers/streamlit_app/app.py +16 -16
- openai_sdk_helpers/streamlit_app/config.py +82 -70
- 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 +46 -8
- openai_sdk_helpers/vector_storage/storage.py +10 -0
- {openai_sdk_helpers-0.3.0.dist-info → openai_sdk_helpers-0.4.1.dist-info}/METADATA +7 -7
- openai_sdk_helpers-0.4.1.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.1.dist-info}/WHEEL +0 -0
- {openai_sdk_helpers-0.3.0.dist-info → openai_sdk_helpers-0.4.1.dist-info}/entry_points.txt +0 -0
- {openai_sdk_helpers-0.3.0.dist-info → openai_sdk_helpers-0.4.1.dist-info}/licenses/LICENSE +0 -0
|
@@ -26,24 +26,32 @@ MAX_CONCURRENT_SEARCHES = 10
|
|
|
26
26
|
class WebAgentPlanner(SearchPlanner[WebSearchPlanStructure]):
|
|
27
27
|
"""Plan web searches to satisfy a user query.
|
|
28
28
|
|
|
29
|
+
Parameters
|
|
30
|
+
----------
|
|
31
|
+
prompt_dir : Path or None, default=None
|
|
32
|
+
Directory containing prompt templates.
|
|
33
|
+
default_model : str or None, default=None
|
|
34
|
+
Default model identifier to use when not defined in config.
|
|
35
|
+
|
|
29
36
|
Methods
|
|
30
37
|
-------
|
|
31
38
|
run_agent(query)
|
|
32
39
|
Generate a search plan for the provided query.
|
|
40
|
+
|
|
41
|
+
Raises
|
|
42
|
+
------
|
|
43
|
+
ValueError
|
|
44
|
+
If the default model is not provided.
|
|
45
|
+
|
|
46
|
+
Examples
|
|
47
|
+
--------
|
|
48
|
+
>>> planner = WebAgentPlanner(default_model="gpt-4o-mini")
|
|
33
49
|
"""
|
|
34
50
|
|
|
35
51
|
def __init__(
|
|
36
52
|
self, prompt_dir: Optional[Path] = None, default_model: Optional[str] = None
|
|
37
53
|
) -> None:
|
|
38
|
-
"""Initialize the planner agent.
|
|
39
|
-
|
|
40
|
-
Parameters
|
|
41
|
-
----------
|
|
42
|
-
prompt_dir : Path or None, default=None
|
|
43
|
-
Directory containing prompt templates.
|
|
44
|
-
default_model : str or None, default=None
|
|
45
|
-
Default model identifier to use when not defined in config.
|
|
46
|
-
"""
|
|
54
|
+
"""Initialize the planner agent."""
|
|
47
55
|
super().__init__(prompt_dir=prompt_dir, default_model=default_model)
|
|
48
56
|
|
|
49
57
|
def _configure_agent(self) -> AgentConfiguration:
|
|
@@ -58,7 +66,7 @@ class WebAgentPlanner(SearchPlanner[WebSearchPlanStructure]):
|
|
|
58
66
|
name="web_planner",
|
|
59
67
|
instructions="Agent instructions",
|
|
60
68
|
description="Agent that plans web searches based on a user query.",
|
|
61
|
-
|
|
69
|
+
output_structure=WebSearchPlanStructure,
|
|
62
70
|
)
|
|
63
71
|
|
|
64
72
|
|
|
@@ -69,26 +77,34 @@ class WebSearchToolAgent(
|
|
|
69
77
|
):
|
|
70
78
|
"""Execute web searches defined in a plan.
|
|
71
79
|
|
|
80
|
+
Parameters
|
|
81
|
+
----------
|
|
82
|
+
prompt_dir : Path or None, default=None
|
|
83
|
+
Directory containing prompt templates.
|
|
84
|
+
default_model : str or None, default=None
|
|
85
|
+
Default model identifier to use when not defined in config.
|
|
86
|
+
|
|
72
87
|
Methods
|
|
73
88
|
-------
|
|
74
89
|
run_agent(search_plan)
|
|
75
90
|
Execute searches described by the plan.
|
|
76
91
|
run_search(item)
|
|
77
92
|
Perform a single web search and summarise the result.
|
|
93
|
+
|
|
94
|
+
Raises
|
|
95
|
+
------
|
|
96
|
+
ValueError
|
|
97
|
+
If the default model is not provided.
|
|
98
|
+
|
|
99
|
+
Examples
|
|
100
|
+
--------
|
|
101
|
+
>>> tool = WebSearchToolAgent(default_model="gpt-4o-mini")
|
|
78
102
|
"""
|
|
79
103
|
|
|
80
104
|
def __init__(
|
|
81
105
|
self, prompt_dir: Optional[Path] = None, default_model: Optional[str] = None
|
|
82
106
|
) -> None:
|
|
83
|
-
"""Initialize the search tool agent.
|
|
84
|
-
|
|
85
|
-
Parameters
|
|
86
|
-
----------
|
|
87
|
-
prompt_dir : Path or None, default=None
|
|
88
|
-
Directory containing prompt templates.
|
|
89
|
-
default_model : str or None, default=None
|
|
90
|
-
Default model identifier to use when not defined in config.
|
|
91
|
-
"""
|
|
107
|
+
"""Initialize the search tool agent."""
|
|
92
108
|
super().__init__(
|
|
93
109
|
prompt_dir=prompt_dir,
|
|
94
110
|
default_model=default_model,
|
|
@@ -107,7 +123,7 @@ class WebSearchToolAgent(
|
|
|
107
123
|
name="web_search",
|
|
108
124
|
instructions="Agent instructions",
|
|
109
125
|
description="Agent that performs web searches and summarizes results.",
|
|
110
|
-
|
|
126
|
+
input_structure=WebSearchPlanStructure,
|
|
111
127
|
tools=[WebSearchTool()],
|
|
112
128
|
model_settings=ModelSettings(tool_choice="required"),
|
|
113
129
|
)
|
|
@@ -136,7 +152,6 @@ class WebSearchToolAgent(
|
|
|
136
152
|
result = await super(SearchToolAgent, self).run_async(
|
|
137
153
|
input=item.query,
|
|
138
154
|
context=template_context,
|
|
139
|
-
output_type=str,
|
|
140
155
|
)
|
|
141
156
|
return self._coerce_item_result(result)
|
|
142
157
|
|
|
@@ -167,24 +182,32 @@ class WebSearchToolAgent(
|
|
|
167
182
|
class WebAgentWriter(SearchWriter[WebSearchReportStructure]):
|
|
168
183
|
"""Summarize search results into a human-readable report.
|
|
169
184
|
|
|
185
|
+
Parameters
|
|
186
|
+
----------
|
|
187
|
+
prompt_dir : Path or None, default=None
|
|
188
|
+
Directory containing prompt templates.
|
|
189
|
+
default_model : str or None, default=None
|
|
190
|
+
Default model identifier to use when not defined in config.
|
|
191
|
+
|
|
170
192
|
Methods
|
|
171
193
|
-------
|
|
172
194
|
run_agent(query, search_results)
|
|
173
195
|
Compile a report from search results.
|
|
196
|
+
|
|
197
|
+
Raises
|
|
198
|
+
------
|
|
199
|
+
ValueError
|
|
200
|
+
If the default model is not provided.
|
|
201
|
+
|
|
202
|
+
Examples
|
|
203
|
+
--------
|
|
204
|
+
>>> writer = WebAgentWriter(default_model="gpt-4o-mini")
|
|
174
205
|
"""
|
|
175
206
|
|
|
176
207
|
def __init__(
|
|
177
208
|
self, prompt_dir: Optional[Path] = None, default_model: Optional[str] = None
|
|
178
209
|
) -> None:
|
|
179
|
-
"""Initialize the writer agent.
|
|
180
|
-
|
|
181
|
-
Parameters
|
|
182
|
-
----------
|
|
183
|
-
prompt_dir : Path or None, default=None
|
|
184
|
-
Directory containing prompt templates.
|
|
185
|
-
default_model : str or None, default=None
|
|
186
|
-
Default model identifier to use when not defined in config.
|
|
187
|
-
"""
|
|
210
|
+
"""Initialize the writer agent."""
|
|
188
211
|
super().__init__(prompt_dir=prompt_dir, default_model=default_model)
|
|
189
212
|
|
|
190
213
|
def _configure_agent(self) -> AgentConfiguration:
|
|
@@ -199,13 +222,20 @@ class WebAgentWriter(SearchWriter[WebSearchReportStructure]):
|
|
|
199
222
|
name="web_writer",
|
|
200
223
|
instructions="Agent instructions",
|
|
201
224
|
description="Agent that writes a report based on web search results.",
|
|
202
|
-
|
|
225
|
+
output_structure=WebSearchReportStructure,
|
|
203
226
|
)
|
|
204
227
|
|
|
205
228
|
|
|
206
229
|
class WebAgentSearch:
|
|
207
230
|
"""Manage the complete web search workflow.
|
|
208
231
|
|
|
232
|
+
Parameters
|
|
233
|
+
----------
|
|
234
|
+
prompt_dir : Path or None, default=None
|
|
235
|
+
Directory containing prompt templates.
|
|
236
|
+
default_model : str or None, default=None
|
|
237
|
+
Default model identifier to use when not defined in config.
|
|
238
|
+
|
|
209
239
|
Methods
|
|
210
240
|
-------
|
|
211
241
|
run_agent_async(search_query)
|
|
@@ -216,6 +246,15 @@ class WebAgentSearch:
|
|
|
216
246
|
Convenience asynchronous entry point for the workflow.
|
|
217
247
|
run_web_agent_sync(search_query)
|
|
218
248
|
Convenience synchronous entry point for the workflow.
|
|
249
|
+
|
|
250
|
+
Raises
|
|
251
|
+
------
|
|
252
|
+
ValueError
|
|
253
|
+
If the default model is not provided.
|
|
254
|
+
|
|
255
|
+
Examples
|
|
256
|
+
--------
|
|
257
|
+
>>> search = WebAgentSearch(default_model="gpt-4o-mini")
|
|
219
258
|
"""
|
|
220
259
|
|
|
221
260
|
def __init__(
|
|
@@ -223,15 +262,7 @@ class WebAgentSearch:
|
|
|
223
262
|
prompt_dir: Optional[Path] = None,
|
|
224
263
|
default_model: Optional[str] = None,
|
|
225
264
|
) -> None:
|
|
226
|
-
"""Create the main web search agent.
|
|
227
|
-
|
|
228
|
-
Parameters
|
|
229
|
-
----------
|
|
230
|
-
prompt_dir : Path or None, default=None
|
|
231
|
-
Directory containing prompt templates.
|
|
232
|
-
default_model : str or None, default=None
|
|
233
|
-
Default model identifier to use when not defined in config.
|
|
234
|
-
"""
|
|
265
|
+
"""Create the main web search agent."""
|
|
235
266
|
self._prompt_dir = prompt_dir
|
|
236
267
|
self._default_model = default_model
|
|
237
268
|
|
|
@@ -3,21 +3,32 @@
|
|
|
3
3
|
from __future__ import annotations
|
|
4
4
|
|
|
5
5
|
from pathlib import Path
|
|
6
|
-
from typing import Any, Dict, Optional
|
|
6
|
+
from typing import Any, Dict, Optional, Type
|
|
7
7
|
|
|
8
8
|
from ..structure import SummaryStructure
|
|
9
|
-
from .base import
|
|
9
|
+
from ..structure.base import StructureBase
|
|
10
|
+
from .base import AgentBase
|
|
10
11
|
from .config import AgentConfiguration
|
|
11
12
|
from .prompt_utils import DEFAULT_PROMPT_DIR
|
|
12
13
|
|
|
13
14
|
|
|
14
|
-
class SummarizerAgent(
|
|
15
|
+
class SummarizerAgent(AgentBase):
|
|
15
16
|
"""Generate concise summaries from provided text.
|
|
16
17
|
|
|
17
18
|
This agent uses OpenAI models to create structured summaries from longer-form
|
|
18
19
|
content. The output follows the ``SummaryStructure`` format by default but
|
|
19
20
|
can be customized with a different output type.
|
|
20
21
|
|
|
22
|
+
Parameters
|
|
23
|
+
----------
|
|
24
|
+
prompt_dir : Path or None, default=None
|
|
25
|
+
Optional directory containing Jinja prompt templates. Defaults to the
|
|
26
|
+
packaged ``prompt`` directory when not provided.
|
|
27
|
+
default_model : str or None, default=None
|
|
28
|
+
Fallback model identifier when not specified elsewhere.
|
|
29
|
+
output_structure : type[StructureBase], default=SummaryStructure
|
|
30
|
+
Type describing the expected summary output.
|
|
31
|
+
|
|
21
32
|
Examples
|
|
22
33
|
--------
|
|
23
34
|
Basic usage with default settings:
|
|
@@ -50,7 +61,7 @@ class SummarizerAgent(BaseAgent):
|
|
|
50
61
|
*,
|
|
51
62
|
prompt_dir: Optional[Path] = None,
|
|
52
63
|
default_model: Optional[str] = None,
|
|
53
|
-
|
|
64
|
+
output_structure: Type[StructureBase] = SummaryStructure,
|
|
54
65
|
) -> None:
|
|
55
66
|
"""Initialize the summarizer agent configuration.
|
|
56
67
|
|
|
@@ -61,14 +72,23 @@ class SummarizerAgent(BaseAgent):
|
|
|
61
72
|
packaged ``prompt`` directory when not provided.
|
|
62
73
|
default_model : str or None, default=None
|
|
63
74
|
Fallback model identifier when not specified elsewhere.
|
|
64
|
-
|
|
75
|
+
output_structure : type[StructureBase], default=SummaryStructure
|
|
65
76
|
Type describing the expected summary output.
|
|
77
|
+
|
|
78
|
+
Raises
|
|
79
|
+
------
|
|
80
|
+
ValueError
|
|
81
|
+
If the default model is not provided.
|
|
82
|
+
|
|
83
|
+
Examples
|
|
84
|
+
--------
|
|
85
|
+
>>> summarizer = SummarizerAgent(default_model="gpt-4o-mini")
|
|
66
86
|
"""
|
|
67
87
|
config = AgentConfiguration(
|
|
68
88
|
name="summarizer",
|
|
69
89
|
instructions="Agent instructions",
|
|
70
90
|
description="Summarize passages into concise findings.",
|
|
71
|
-
|
|
91
|
+
output_structure=output_structure,
|
|
72
92
|
)
|
|
73
93
|
prompt_directory = prompt_dir or DEFAULT_PROMPT_DIR
|
|
74
94
|
super().__init__(
|
|
@@ -91,6 +111,11 @@ class SummarizerAgent(BaseAgent):
|
|
|
91
111
|
-------
|
|
92
112
|
Any
|
|
93
113
|
Structured summary produced by the agent.
|
|
114
|
+
|
|
115
|
+
Raises
|
|
116
|
+
------
|
|
117
|
+
APIError
|
|
118
|
+
If the OpenAI API call fails.
|
|
94
119
|
"""
|
|
95
120
|
context: Optional[Dict[str, Any]] = None
|
|
96
121
|
if metadata:
|
|
@@ -99,7 +124,7 @@ class SummarizerAgent(BaseAgent):
|
|
|
99
124
|
result = await self.run_async(
|
|
100
125
|
input=text,
|
|
101
126
|
context=context,
|
|
102
|
-
|
|
127
|
+
output_structure=self._output_structure,
|
|
103
128
|
)
|
|
104
129
|
return result
|
|
105
130
|
|
|
@@ -5,17 +5,27 @@ from __future__ import annotations
|
|
|
5
5
|
from pathlib import Path
|
|
6
6
|
from typing import Any, Dict, Optional
|
|
7
7
|
|
|
8
|
-
from .base import
|
|
8
|
+
from .base import AgentBase
|
|
9
9
|
from .config import AgentConfiguration
|
|
10
10
|
from .prompt_utils import DEFAULT_PROMPT_DIR
|
|
11
|
+
from ..structure import TranslationStructure
|
|
12
|
+
from ..structure.base import StructureBase
|
|
11
13
|
|
|
12
14
|
|
|
13
|
-
class TranslatorAgent(
|
|
15
|
+
class TranslatorAgent(AgentBase):
|
|
14
16
|
"""Translate text into a target language.
|
|
15
17
|
|
|
16
18
|
This agent provides language translation services using OpenAI models,
|
|
17
19
|
supporting both synchronous and asynchronous execution modes.
|
|
18
20
|
|
|
21
|
+
Parameters
|
|
22
|
+
----------
|
|
23
|
+
prompt_dir : Path or None, default=None
|
|
24
|
+
Optional directory containing Jinja prompt templates. Defaults to the
|
|
25
|
+
packaged ``prompt`` directory when not provided.
|
|
26
|
+
default_model : str or None, default=None
|
|
27
|
+
Fallback model identifier when not specified elsewhere.
|
|
28
|
+
|
|
19
29
|
Examples
|
|
20
30
|
--------
|
|
21
31
|
Basic translation:
|
|
@@ -23,7 +33,7 @@ class TranslatorAgent(BaseAgent):
|
|
|
23
33
|
>>> from openai_sdk_helpers.agent import TranslatorAgent
|
|
24
34
|
>>> translator = TranslatorAgent(default_model="gpt-4o-mini")
|
|
25
35
|
>>> result = translator.run_sync("Hello world", target_language="Spanish")
|
|
26
|
-
>>> print(result)
|
|
36
|
+
>>> print(result.text)
|
|
27
37
|
'Hola mundo'
|
|
28
38
|
|
|
29
39
|
Async translation with context:
|
|
@@ -62,12 +72,21 @@ class TranslatorAgent(BaseAgent):
|
|
|
62
72
|
packaged ``prompt`` directory when not provided.
|
|
63
73
|
default_model : str or None, default=None
|
|
64
74
|
Fallback model identifier when not specified elsewhere.
|
|
75
|
+
|
|
76
|
+
Raises
|
|
77
|
+
------
|
|
78
|
+
ValueError
|
|
79
|
+
If the default model is not provided.
|
|
80
|
+
|
|
81
|
+
Examples
|
|
82
|
+
--------
|
|
83
|
+
>>> translator = TranslatorAgent(default_model="gpt-4o-mini")
|
|
65
84
|
"""
|
|
66
85
|
config = AgentConfiguration(
|
|
67
86
|
name="translator",
|
|
68
87
|
instructions="Agent instructions",
|
|
69
88
|
description="Translate text into the requested language.",
|
|
70
|
-
|
|
89
|
+
output_structure=TranslationStructure,
|
|
71
90
|
)
|
|
72
91
|
prompt_directory = prompt_dir or DEFAULT_PROMPT_DIR
|
|
73
92
|
super().__init__(
|
|
@@ -79,7 +98,7 @@ class TranslatorAgent(BaseAgent):
|
|
|
79
98
|
text: str,
|
|
80
99
|
target_language: str,
|
|
81
100
|
context: Optional[Dict[str, Any]] = None,
|
|
82
|
-
) ->
|
|
101
|
+
) -> TranslationStructure:
|
|
83
102
|
"""Translate ``text`` to ``target_language``.
|
|
84
103
|
|
|
85
104
|
Parameters
|
|
@@ -93,29 +112,41 @@ class TranslatorAgent(BaseAgent):
|
|
|
93
112
|
|
|
94
113
|
Returns
|
|
95
114
|
-------
|
|
96
|
-
|
|
97
|
-
|
|
115
|
+
TranslationStructure
|
|
116
|
+
Structured translation output from the agent.
|
|
117
|
+
|
|
118
|
+
Raises
|
|
119
|
+
------
|
|
120
|
+
APIError
|
|
121
|
+
If the OpenAI API call fails.
|
|
122
|
+
|
|
123
|
+
Examples
|
|
124
|
+
--------
|
|
125
|
+
>>> import asyncio
|
|
126
|
+
>>> async def main():
|
|
127
|
+
... result = await translator.run_agent("Hello", "Spanish")
|
|
128
|
+
... return result
|
|
129
|
+
>>> asyncio.run(main())
|
|
98
130
|
"""
|
|
99
131
|
template_context: Dict[str, Any] = {"target_language": target_language}
|
|
100
132
|
if context:
|
|
101
133
|
template_context.update(context)
|
|
102
134
|
|
|
103
|
-
result:
|
|
135
|
+
result: TranslationStructure = await self.run_async(
|
|
104
136
|
input=text,
|
|
105
137
|
context=template_context,
|
|
106
|
-
output_type=str,
|
|
107
138
|
)
|
|
108
139
|
return result
|
|
109
140
|
|
|
110
141
|
def run_sync(
|
|
111
142
|
self,
|
|
112
143
|
input: str,
|
|
113
|
-
context: Optional[Dict[str, Any]] = None,
|
|
114
|
-
output_type: Optional[Any] = None,
|
|
115
144
|
*,
|
|
145
|
+
context: Optional[Dict[str, Any]] = None,
|
|
146
|
+
output_structure: Optional[type[StructureBase]] = None,
|
|
147
|
+
session: Optional[Any] = None,
|
|
116
148
|
target_language: Optional[str] = None,
|
|
117
|
-
|
|
118
|
-
) -> str:
|
|
149
|
+
) -> TranslationStructure:
|
|
119
150
|
"""Translate ``input`` to ``target_language`` synchronously.
|
|
120
151
|
|
|
121
152
|
Parameters
|
|
@@ -124,31 +155,32 @@ class TranslatorAgent(BaseAgent):
|
|
|
124
155
|
Source content to translate.
|
|
125
156
|
context : dict or None, default=None
|
|
126
157
|
Additional context values to merge into the prompt.
|
|
127
|
-
|
|
158
|
+
output_structure : type[StructureBase] or None, default=None
|
|
128
159
|
Optional output type cast for the response.
|
|
129
160
|
target_language : str or None, optional
|
|
130
161
|
Target language to translate the content into. Required unless supplied
|
|
131
|
-
within ``context
|
|
132
|
-
|
|
133
|
-
Optional
|
|
134
|
-
backward compatibility.
|
|
162
|
+
within ``context``.
|
|
163
|
+
session : Session or None, default=None
|
|
164
|
+
Optional session for maintaining conversation history across runs.
|
|
135
165
|
|
|
136
166
|
Returns
|
|
137
167
|
-------
|
|
138
|
-
|
|
139
|
-
|
|
168
|
+
TranslationStructure
|
|
169
|
+
Structured translation output from the agent.
|
|
140
170
|
|
|
141
171
|
Raises
|
|
142
172
|
------
|
|
143
173
|
ValueError
|
|
144
174
|
If ``target_language`` is not provided.
|
|
175
|
+
|
|
176
|
+
Examples
|
|
177
|
+
--------
|
|
178
|
+
>>> result = translator.run_sync("Hello", target_language="Spanish")
|
|
145
179
|
"""
|
|
146
180
|
merged_context: Dict[str, Any] = {}
|
|
147
181
|
|
|
148
182
|
if context:
|
|
149
183
|
merged_context.update(context)
|
|
150
|
-
if "context" in kwargs and kwargs["context"]:
|
|
151
|
-
merged_context.update(kwargs["context"])
|
|
152
184
|
if target_language:
|
|
153
185
|
merged_context["target_language"] = target_language
|
|
154
186
|
|
|
@@ -156,10 +188,11 @@ class TranslatorAgent(BaseAgent):
|
|
|
156
188
|
msg = "target_language is required for translation"
|
|
157
189
|
raise ValueError(msg)
|
|
158
190
|
|
|
159
|
-
result:
|
|
191
|
+
result: TranslationStructure = super().run_sync(
|
|
160
192
|
input=input,
|
|
161
193
|
context=merged_context,
|
|
162
|
-
|
|
194
|
+
output_structure=output_structure or self._output_structure,
|
|
195
|
+
session=session,
|
|
163
196
|
)
|
|
164
197
|
return result
|
|
165
198
|
|
|
@@ -6,18 +6,26 @@ from pathlib import Path
|
|
|
6
6
|
from typing import Any, Dict, Optional
|
|
7
7
|
|
|
8
8
|
from ..structure.validation import ValidationResultStructure
|
|
9
|
-
from .base import
|
|
9
|
+
from .base import AgentBase
|
|
10
10
|
from .config import AgentConfiguration
|
|
11
11
|
from .prompt_utils import DEFAULT_PROMPT_DIR
|
|
12
12
|
|
|
13
13
|
|
|
14
|
-
class ValidatorAgent(
|
|
14
|
+
class ValidatorAgent(AgentBase):
|
|
15
15
|
"""Check user prompts and agent responses against safety guardrails.
|
|
16
16
|
|
|
17
17
|
This agent validates inputs and outputs to ensure they comply with safety
|
|
18
18
|
policies and usage guidelines, returning structured validation results with
|
|
19
19
|
recommended actions.
|
|
20
20
|
|
|
21
|
+
Parameters
|
|
22
|
+
----------
|
|
23
|
+
prompt_dir : Path or None, default=None
|
|
24
|
+
Optional directory containing Jinja prompt templates. Defaults to the
|
|
25
|
+
packaged ``prompt`` directory when not provided.
|
|
26
|
+
default_model : str or None, default=None
|
|
27
|
+
Fallback model identifier when not specified elsewhere.
|
|
28
|
+
|
|
21
29
|
Examples
|
|
22
30
|
--------
|
|
23
31
|
Validate user input:
|
|
@@ -63,12 +71,21 @@ class ValidatorAgent(BaseAgent):
|
|
|
63
71
|
packaged ``prompt`` directory when not provided.
|
|
64
72
|
default_model : str or None, default=None
|
|
65
73
|
Fallback model identifier when not specified elsewhere.
|
|
74
|
+
|
|
75
|
+
Raises
|
|
76
|
+
------
|
|
77
|
+
ValueError
|
|
78
|
+
If the default model is not provided.
|
|
79
|
+
|
|
80
|
+
Examples
|
|
81
|
+
--------
|
|
82
|
+
>>> validator = ValidatorAgent(default_model="gpt-4o-mini")
|
|
66
83
|
"""
|
|
67
84
|
config = AgentConfiguration(
|
|
68
85
|
name="validator",
|
|
69
86
|
instructions="Agent instructions",
|
|
70
87
|
description="Validate user input and agent output against guardrails.",
|
|
71
|
-
|
|
88
|
+
output_structure=ValidationResultStructure,
|
|
72
89
|
)
|
|
73
90
|
prompt_directory = prompt_dir or DEFAULT_PROMPT_DIR
|
|
74
91
|
super().__init__(
|
|
@@ -102,6 +119,19 @@ class ValidatorAgent(BaseAgent):
|
|
|
102
119
|
-------
|
|
103
120
|
ValidationResultStructure
|
|
104
121
|
Structured validation result describing any violations and actions.
|
|
122
|
+
|
|
123
|
+
Raises
|
|
124
|
+
------
|
|
125
|
+
APIError
|
|
126
|
+
If the OpenAI API call fails.
|
|
127
|
+
|
|
128
|
+
Examples
|
|
129
|
+
--------
|
|
130
|
+
>>> import asyncio
|
|
131
|
+
>>> async def main():
|
|
132
|
+
... result = await validator.run_agent("Safe input")
|
|
133
|
+
... return result
|
|
134
|
+
>>> asyncio.run(main())
|
|
105
135
|
"""
|
|
106
136
|
context: Dict[str, Any] = {"user_input": user_input}
|
|
107
137
|
if agent_output is not None:
|
|
@@ -114,7 +144,7 @@ class ValidatorAgent(BaseAgent):
|
|
|
114
144
|
result: ValidationResultStructure = await self.run_async(
|
|
115
145
|
input=user_input,
|
|
116
146
|
context=context,
|
|
117
|
-
|
|
147
|
+
output_structure=ValidationResultStructure,
|
|
118
148
|
)
|
|
119
149
|
return result
|
|
120
150
|
|
openai_sdk_helpers/cli.py
CHANGED
|
@@ -43,6 +43,15 @@ def cmd_agent_test(args: argparse.Namespace) -> int:
|
|
|
43
43
|
-------
|
|
44
44
|
int
|
|
45
45
|
Exit code (0 for success).
|
|
46
|
+
|
|
47
|
+
Raises
|
|
48
|
+
------
|
|
49
|
+
NotImplementedError
|
|
50
|
+
As the function is not yet implemented.
|
|
51
|
+
|
|
52
|
+
Examples
|
|
53
|
+
--------
|
|
54
|
+
>>> cmd_agent_test(argparse.Namespace(agent_name="test", input="hello"))
|
|
46
55
|
"""
|
|
47
56
|
print(f"Testing agent: {args.agent_name}")
|
|
48
57
|
print(f"Input: {args.input}")
|
|
@@ -62,6 +71,15 @@ def cmd_template_validate(args: argparse.Namespace) -> int:
|
|
|
62
71
|
-------
|
|
63
72
|
int
|
|
64
73
|
Exit code (0 for success, 1 for validation errors).
|
|
74
|
+
|
|
75
|
+
Raises
|
|
76
|
+
------
|
|
77
|
+
FileNotFoundError
|
|
78
|
+
If the template path does not exist.
|
|
79
|
+
|
|
80
|
+
Examples
|
|
81
|
+
--------
|
|
82
|
+
>>> cmd_template_validate(argparse.Namespace(template_path="."))
|
|
65
83
|
"""
|
|
66
84
|
from jinja2 import Environment, FileSystemLoader, TemplateSyntaxError
|
|
67
85
|
|
|
@@ -116,6 +134,15 @@ def cmd_registry_list(args: argparse.Namespace) -> int:
|
|
|
116
134
|
-------
|
|
117
135
|
int
|
|
118
136
|
Exit code (0 for success).
|
|
137
|
+
|
|
138
|
+
Raises
|
|
139
|
+
------
|
|
140
|
+
ImportError
|
|
141
|
+
If openai_sdk_helpers is not installed.
|
|
142
|
+
|
|
143
|
+
Examples
|
|
144
|
+
--------
|
|
145
|
+
>>> cmd_registry_list(argparse.Namespace())
|
|
119
146
|
"""
|
|
120
147
|
try:
|
|
121
148
|
from openai_sdk_helpers import get_default_registry
|
|
@@ -151,6 +178,17 @@ def cmd_registry_inspect(args: argparse.Namespace) -> int:
|
|
|
151
178
|
-------
|
|
152
179
|
int
|
|
153
180
|
Exit code (0 for success, 1 for not found).
|
|
181
|
+
|
|
182
|
+
Raises
|
|
183
|
+
------
|
|
184
|
+
ImportError
|
|
185
|
+
If openai_sdk_helpers is not installed.
|
|
186
|
+
KeyError
|
|
187
|
+
If the configuration is not found in the registry.
|
|
188
|
+
|
|
189
|
+
Examples
|
|
190
|
+
--------
|
|
191
|
+
>>> cmd_registry_inspect(argparse.Namespace(config_name="my_config"))
|
|
154
192
|
"""
|
|
155
193
|
try:
|
|
156
194
|
from openai_sdk_helpers import get_default_registry
|
|
@@ -198,6 +236,10 @@ def main(argv: list[str] | None = None) -> int:
|
|
|
198
236
|
-------
|
|
199
237
|
int
|
|
200
238
|
Exit code.
|
|
239
|
+
|
|
240
|
+
Examples
|
|
241
|
+
--------
|
|
242
|
+
>>> main(["agent", "test", "my_agent"])
|
|
201
243
|
"""
|
|
202
244
|
parser = argparse.ArgumentParser(
|
|
203
245
|
prog="openai-helpers",
|
openai_sdk_helpers/config.py
CHANGED
|
@@ -40,7 +40,7 @@ def get_data_path(name: str) -> Path:
|
|
|
40
40
|
Returns
|
|
41
41
|
-------
|
|
42
42
|
Path
|
|
43
|
-
Directory path under
|
|
43
|
+
Directory path under ~/openai-sdk-helpers specific to name.
|
|
44
44
|
The directory is created if it does not exist.
|
|
45
45
|
|
|
46
46
|
Examples
|
|
@@ -50,6 +50,7 @@ def get_data_path(name: str) -> Path:
|
|
|
50
50
|
>>> path.exists()
|
|
51
51
|
True
|
|
52
52
|
"""
|
|
53
|
-
|
|
53
|
+
# Use the workspace 'data' directory, with a subdirectory for the module name
|
|
54
|
+
base = Path(__file__).parent.parent.parent / "data"
|
|
54
55
|
path = base / name
|
|
55
56
|
return ensure_directory(path)
|