openai-sdk-helpers 0.6.0__py3-none-any.whl → 0.6.2__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/agent/__init__.py +2 -0
- openai_sdk_helpers/agent/base.py +88 -12
- openai_sdk_helpers/agent/classifier.py +905 -94
- openai_sdk_helpers/agent/configuration.py +42 -0
- openai_sdk_helpers/agent/files.py +120 -0
- openai_sdk_helpers/agent/runner.py +9 -9
- openai_sdk_helpers/agent/translator.py +2 -2
- openai_sdk_helpers/files_api.py +46 -1
- openai_sdk_helpers/prompt/classifier.jinja +28 -7
- openai_sdk_helpers/settings.py +65 -0
- openai_sdk_helpers/structure/__init__.py +4 -0
- openai_sdk_helpers/structure/base.py +79 -55
- openai_sdk_helpers/structure/classification.py +265 -43
- openai_sdk_helpers/structure/plan/enum.py +4 -0
- {openai_sdk_helpers-0.6.0.dist-info → openai_sdk_helpers-0.6.2.dist-info}/METADATA +12 -1
- {openai_sdk_helpers-0.6.0.dist-info → openai_sdk_helpers-0.6.2.dist-info}/RECORD +19 -18
- {openai_sdk_helpers-0.6.0.dist-info → openai_sdk_helpers-0.6.2.dist-info}/WHEEL +0 -0
- {openai_sdk_helpers-0.6.0.dist-info → openai_sdk_helpers-0.6.2.dist-info}/entry_points.txt +0 -0
- {openai_sdk_helpers-0.6.0.dist-info → openai_sdk_helpers-0.6.2.dist-info}/licenses/LICENSE +0 -0
|
@@ -14,6 +14,7 @@ from .validator import ValidatorAgent
|
|
|
14
14
|
from .utils import run_coroutine_agent_sync
|
|
15
15
|
from .search.vector import VectorAgentSearch
|
|
16
16
|
from .search.web import WebAgentSearch
|
|
17
|
+
from .files import build_agent_input_messages
|
|
17
18
|
|
|
18
19
|
__all__ = [
|
|
19
20
|
"AgentBase",
|
|
@@ -34,4 +35,5 @@ __all__ = [
|
|
|
34
35
|
"ValidatorAgent",
|
|
35
36
|
"VectorAgentSearch",
|
|
36
37
|
"WebAgentSearch",
|
|
38
|
+
"build_agent_input_messages",
|
|
37
39
|
]
|
openai_sdk_helpers/agent/base.py
CHANGED
|
@@ -6,7 +6,7 @@ import logging
|
|
|
6
6
|
import traceback
|
|
7
7
|
import uuid
|
|
8
8
|
from pathlib import Path
|
|
9
|
-
from typing import TYPE_CHECKING, Any, Dict, Optional, Protocol, cast
|
|
9
|
+
from typing import TYPE_CHECKING, Any, Dict, Literal, Optional, Protocol, cast
|
|
10
10
|
|
|
11
11
|
from agents import Agent, Handoff, InputGuardrail, OutputGuardrail, Session
|
|
12
12
|
from agents.model_settings import ModelSettings
|
|
@@ -33,6 +33,7 @@ from .runner import run_async, run_sync
|
|
|
33
33
|
if TYPE_CHECKING:
|
|
34
34
|
from ..settings import OpenAISettings
|
|
35
35
|
from ..response.base import ResponseBase
|
|
36
|
+
from ..files_api import FilePurpose, FilesAPIManager
|
|
36
37
|
|
|
37
38
|
|
|
38
39
|
class AgentConfigurationProtocol(Protocol):
|
|
@@ -184,6 +185,8 @@ class AgentBase(DataclassJSONSerializable):
|
|
|
184
185
|
Return response tool handler and definition for Responses API use.
|
|
185
186
|
build_response(openai_settings, data_path=None, tool_handlers=None, system_vector_store=None)
|
|
186
187
|
Build a ResponseBase instance based on this agent.
|
|
188
|
+
build_input_messages(content, files=None, files_manager=None, file_purpose="user_data", image_detail="auto")
|
|
189
|
+
Build Agents SDK input messages with optional file attachments.
|
|
187
190
|
save_error(exc)
|
|
188
191
|
Persist error details to a file named with the agent UUID.
|
|
189
192
|
close()
|
|
@@ -429,9 +432,16 @@ class AgentBase(DataclassJSONSerializable):
|
|
|
429
432
|
"""
|
|
430
433
|
return self._session
|
|
431
434
|
|
|
432
|
-
def get_agent(
|
|
435
|
+
def get_agent(
|
|
436
|
+
self, output_structure: Optional[type[StructureBase]] = None
|
|
437
|
+
) -> Agent:
|
|
433
438
|
"""Construct and return the configured :class:`agents.Agent` instance.
|
|
434
439
|
|
|
440
|
+
Parameters
|
|
441
|
+
----------
|
|
442
|
+
output_structure : type[StructureBase] or None, default=None
|
|
443
|
+
Optional override for the agent output schema.
|
|
444
|
+
|
|
435
445
|
Returns
|
|
436
446
|
-------
|
|
437
447
|
Agent
|
|
@@ -442,8 +452,9 @@ class AgentBase(DataclassJSONSerializable):
|
|
|
442
452
|
"instructions": self._configuration.instructions_text or ".",
|
|
443
453
|
"model": self._model,
|
|
444
454
|
}
|
|
445
|
-
|
|
446
|
-
|
|
455
|
+
output_type = output_structure or self._configuration.output_structure
|
|
456
|
+
if output_type is not None:
|
|
457
|
+
agent_config["output_type"] = output_type
|
|
447
458
|
if self._configuration.tools:
|
|
448
459
|
agent_config["tools"] = self._configuration.tools
|
|
449
460
|
if self._model_settings:
|
|
@@ -459,7 +470,7 @@ class AgentBase(DataclassJSONSerializable):
|
|
|
459
470
|
|
|
460
471
|
async def run_async(
|
|
461
472
|
self,
|
|
462
|
-
input: str,
|
|
473
|
+
input: str | list[dict[str, Any]],
|
|
463
474
|
*,
|
|
464
475
|
context: Optional[Dict[str, Any]] = None,
|
|
465
476
|
output_structure: Optional[type[StructureBase]] = None,
|
|
@@ -469,8 +480,8 @@ class AgentBase(DataclassJSONSerializable):
|
|
|
469
480
|
|
|
470
481
|
Parameters
|
|
471
482
|
----------
|
|
472
|
-
input : str
|
|
473
|
-
Prompt or
|
|
483
|
+
input : str or list[dict[str, Any]]
|
|
484
|
+
Prompt text or structured input for the agent.
|
|
474
485
|
context : dict or None, default=None
|
|
475
486
|
Optional dictionary passed to the agent.
|
|
476
487
|
output_structure : type[StructureBase] or None, default=None
|
|
@@ -490,7 +501,7 @@ class AgentBase(DataclassJSONSerializable):
|
|
|
490
501
|
session_to_use = session if session is not None else self._session
|
|
491
502
|
try:
|
|
492
503
|
return await run_async(
|
|
493
|
-
agent=self.get_agent(),
|
|
504
|
+
agent=self.get_agent(output_structure=output_structure),
|
|
494
505
|
input=input,
|
|
495
506
|
context=context,
|
|
496
507
|
output_structure=output_structure,
|
|
@@ -514,7 +525,7 @@ class AgentBase(DataclassJSONSerializable):
|
|
|
514
525
|
|
|
515
526
|
def run_sync(
|
|
516
527
|
self,
|
|
517
|
-
input: str,
|
|
528
|
+
input: str | list[dict[str, Any]],
|
|
518
529
|
*,
|
|
519
530
|
context: Optional[Dict[str, Any]] = None,
|
|
520
531
|
output_structure: Optional[type[StructureBase]] = None,
|
|
@@ -524,8 +535,8 @@ class AgentBase(DataclassJSONSerializable):
|
|
|
524
535
|
|
|
525
536
|
Parameters
|
|
526
537
|
----------
|
|
527
|
-
input : str
|
|
528
|
-
Prompt or
|
|
538
|
+
input : str or list[dict[str, Any]]
|
|
539
|
+
Prompt text or structured input for the agent.
|
|
529
540
|
context : dict or None, default=None
|
|
530
541
|
Optional dictionary passed to the agent.
|
|
531
542
|
output_structure : type[StructureBase] or None, default=None
|
|
@@ -545,7 +556,7 @@ class AgentBase(DataclassJSONSerializable):
|
|
|
545
556
|
session_to_use = session if session is not None else self._session
|
|
546
557
|
try:
|
|
547
558
|
return run_sync(
|
|
548
|
-
agent=self.get_agent(),
|
|
559
|
+
agent=self.get_agent(output_structure=output_structure),
|
|
549
560
|
input=input,
|
|
550
561
|
context=context,
|
|
551
562
|
output_structure=output_structure,
|
|
@@ -652,6 +663,71 @@ class AgentBase(DataclassJSONSerializable):
|
|
|
652
663
|
openai_settings=openai_settings,
|
|
653
664
|
)
|
|
654
665
|
|
|
666
|
+
@staticmethod
|
|
667
|
+
def build_input_messages(
|
|
668
|
+
content: str | list[str],
|
|
669
|
+
files: str | list[str] | None = None,
|
|
670
|
+
*,
|
|
671
|
+
files_manager: FilesAPIManager | None = None,
|
|
672
|
+
openai_settings: OpenAISettings | None = None,
|
|
673
|
+
file_purpose: FilePurpose = "user_data",
|
|
674
|
+
image_detail: Literal["low", "high", "auto"] = "auto",
|
|
675
|
+
) -> list[dict[str, Any]]:
|
|
676
|
+
"""Build Agents SDK input messages with file attachments.
|
|
677
|
+
|
|
678
|
+
Parameters
|
|
679
|
+
----------
|
|
680
|
+
content : str or list[str]
|
|
681
|
+
Prompt text or list of prompt texts to send.
|
|
682
|
+
files : str, list[str], or None, default None
|
|
683
|
+
Optional file path or list of file paths. Image files are sent as
|
|
684
|
+
base64-encoded ``input_image`` entries. Document files are uploaded
|
|
685
|
+
using ``files_manager`` and sent as ``input_file`` entries.
|
|
686
|
+
files_manager : FilesAPIManager or None, default None
|
|
687
|
+
File upload helper used to create file IDs for document uploads.
|
|
688
|
+
Required when ``files`` contains non-image documents.
|
|
689
|
+
openai_settings : OpenAISettings or None, default None
|
|
690
|
+
Optional OpenAI settings used to build a FilesAPIManager when one is
|
|
691
|
+
not provided. When supplied, ``openai_settings.create_client()`` is
|
|
692
|
+
used to initialize the Files API manager.
|
|
693
|
+
file_purpose : FilePurpose, default "user_data"
|
|
694
|
+
Purpose passed to the Files API when uploading document files.
|
|
695
|
+
image_detail : {"low", "high", "auto"}, default "auto"
|
|
696
|
+
Detail hint passed along with base64-encoded image inputs.
|
|
697
|
+
|
|
698
|
+
Returns
|
|
699
|
+
-------
|
|
700
|
+
list[dict[str, Any]]
|
|
701
|
+
Agents SDK input messages that include text and optional file entries.
|
|
702
|
+
|
|
703
|
+
Raises
|
|
704
|
+
------
|
|
705
|
+
ValueError
|
|
706
|
+
If document files are provided without a ``files_manager``.
|
|
707
|
+
|
|
708
|
+
Examples
|
|
709
|
+
--------
|
|
710
|
+
>>> from openai import OpenAI
|
|
711
|
+
>>> from openai_sdk_helpers.files_api import FilesAPIManager
|
|
712
|
+
>>> client = OpenAI()
|
|
713
|
+
>>> files_manager = FilesAPIManager(client)
|
|
714
|
+
>>> messages = AgentBase.build_input_messages(
|
|
715
|
+
... "Summarize this document",
|
|
716
|
+
... files="report.pdf",
|
|
717
|
+
... files_manager=files_manager,
|
|
718
|
+
... )
|
|
719
|
+
"""
|
|
720
|
+
from .files import build_agent_input_messages
|
|
721
|
+
|
|
722
|
+
return build_agent_input_messages(
|
|
723
|
+
content=content,
|
|
724
|
+
files=files,
|
|
725
|
+
files_manager=files_manager,
|
|
726
|
+
openai_settings=openai_settings,
|
|
727
|
+
file_purpose=file_purpose,
|
|
728
|
+
image_detail=image_detail,
|
|
729
|
+
)
|
|
730
|
+
|
|
655
731
|
def _build_response_parameters(self) -> dict[str, Any]:
|
|
656
732
|
"""Build the Responses API parameter schema for this agent tool.
|
|
657
733
|
|