swarmauri_provider_openai 0.1.0.dev3__tar.gz

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.
@@ -0,0 +1,13 @@
1
+ Copyright 2024 Jacob Stewart
2
+
3
+ Licensed under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License.
5
+ You may obtain a copy of the License at
6
+
7
+ http://www.apache.org/licenses/LICENSE-2.0
8
+
9
+ Unless required by applicable law or agreed to in writing, software
10
+ distributed under the License is distributed on an "AS IS" BASIS,
11
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ See the License for the specific language governing permissions and
13
+ limitations under the License.
@@ -0,0 +1,50 @@
1
+ Metadata-Version: 2.4
2
+ Name: swarmauri_provider_openai
3
+ Version: 0.1.0.dev3
4
+ Summary: Aggregated OpenAI provider resources for Swarmauri models.
5
+ License: Copyright 2024 Jacob Stewart
6
+
7
+ Licensed under the Apache License, Version 2.0 (the "License");
8
+ you may not use this file except in compliance with the License.
9
+ You may obtain a copy of the License at
10
+
11
+ http://www.apache.org/licenses/LICENSE-2.0
12
+
13
+ Unless required by applicable law or agreed to in writing, software
14
+ distributed under the License is distributed on an "AS IS" BASIS,
15
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ See the License for the specific language governing permissions and
17
+ limitations under the License.
18
+ License-File: LICENSE
19
+ Keywords: swarmauri,provider,openai,realtime,model catalog
20
+ Author: Jacob Stewart
21
+ Author-email: jacob@swarmauri.com
22
+ Requires-Python: >=3.10,<3.13
23
+ Classifier: Development Status :: 1 - Planning
24
+ Classifier: License :: OSI Approved :: Apache Software License
25
+ Classifier: Programming Language :: Python :: 3.10
26
+ Classifier: Programming Language :: Python :: 3.11
27
+ Classifier: Programming Language :: Python :: 3.12
28
+ Requires-Dist: swarmauri_base (>=0.11.0.dev5)
29
+ Requires-Dist: swarmauri_core (>=0.11.0.dev5)
30
+ Description-Content-Type: text/markdown
31
+
32
+ ![Swarmauri](https://github.com/swarmauri/swarmauri-sdk/blob/3d4d1cfa949399d7019ae9d8f296afba773dfb7f/assets/swarmauri.brand.theme.svg)
33
+
34
+ [![PyPI](https://img.shields.io/pypi/v/swarmauri_provider_openai.svg)](https://pypi.org/project/swarmauri_provider_openai/) [![Python](https://img.shields.io/pypi/pyversions/swarmauri_provider_openai.svg)](https://pypi.org/project/swarmauri_provider_openai/) [![License](https://img.shields.io/badge/license-Apache--2.0-blue)](LICENSE) [![Downloads](https://static.pepy.tech/badge/swarmauri_provider_openai)](https://pepy.tech/project/swarmauri_provider_openai) [![Hits](https://hits.sh/github.com/swarmauri/swarmauri-sdk.svg)](https://hits.sh/github.com/swarmauri/swarmauri-sdk/)
35
+
36
+ # swarmauri_provider_openai
37
+
38
+ ## Features
39
+
40
+ - Shared OpenAI credentials, endpoints, catalogs, errors, and tool schemas.
41
+ - Reusable across request/response and realtime model families.
42
+
43
+ ## Installation
44
+
45
+ `uv add swarmauri_provider_openai` or `pip install swarmauri_provider_openai`.
46
+
47
+ ## Usage
48
+
49
+ Construct `OpenAIProvider(api_key=...)` and inject it into an OpenAI model.
50
+
@@ -0,0 +1,18 @@
1
+ ![Swarmauri](https://github.com/swarmauri/swarmauri-sdk/blob/3d4d1cfa949399d7019ae9d8f296afba773dfb7f/assets/swarmauri.brand.theme.svg)
2
+
3
+ [![PyPI](https://img.shields.io/pypi/v/swarmauri_provider_openai.svg)](https://pypi.org/project/swarmauri_provider_openai/) [![Python](https://img.shields.io/pypi/pyversions/swarmauri_provider_openai.svg)](https://pypi.org/project/swarmauri_provider_openai/) [![License](https://img.shields.io/badge/license-Apache--2.0-blue)](LICENSE) [![Downloads](https://static.pepy.tech/badge/swarmauri_provider_openai)](https://pepy.tech/project/swarmauri_provider_openai) [![Hits](https://hits.sh/github.com/swarmauri/swarmauri-sdk.svg)](https://hits.sh/github.com/swarmauri/swarmauri-sdk/)
4
+
5
+ # swarmauri_provider_openai
6
+
7
+ ## Features
8
+
9
+ - Shared OpenAI credentials, endpoints, catalogs, errors, and tool schemas.
10
+ - Reusable across request/response and realtime model families.
11
+
12
+ ## Installation
13
+
14
+ `uv add swarmauri_provider_openai` or `pip install swarmauri_provider_openai`.
15
+
16
+ ## Usage
17
+
18
+ Construct `OpenAIProvider(api_key=...)` and inject it into an OpenAI model.
@@ -0,0 +1,31 @@
1
+ [project]
2
+ name = "swarmauri_provider_openai"
3
+ version = "0.1.0.dev3"
4
+ description = "Aggregated OpenAI provider resources for Swarmauri models."
5
+ readme = "README.md"
6
+ requires-python = ">=3.10,<3.13"
7
+ authors = [{ name = "Jacob Stewart", email = "jacob@swarmauri.com" }]
8
+ license = { file = "LICENSE" }
9
+ dependencies = ["swarmauri_core>=0.11.0.dev5", "swarmauri_base>=0.11.0.dev5"]
10
+ classifiers = ["Development Status :: 1 - Planning", "License :: OSI Approved :: Apache Software License", "Programming Language :: Python :: 3.10", "Programming Language :: Python :: 3.11", "Programming Language :: Python :: 3.12"]
11
+ keywords = ["swarmauri", "provider", "openai", "realtime", "model catalog"]
12
+
13
+ [tool.uv.sources]
14
+ swarmauri_core = { workspace = true }
15
+ swarmauri_base = { workspace = true }
16
+
17
+ [project.entry-points."swarmauri.model_providers"]
18
+ OpenAIProvider = "swarmauri_provider_openai:OpenAIProvider"
19
+
20
+ [build-system]
21
+ requires = ["poetry-core>=1.8.0"]
22
+ build-backend = "poetry.core.masonry.api"
23
+
24
+ [dependency-groups]
25
+ dev = ["pytest>=8.0", "ruff>=0.9.9", "swarmauri_tests_component"]
26
+
27
+ [tool.ruff]
28
+ extend = "../../pyproject.toml"
29
+
30
+ [tool.ruff.lint]
31
+ ignore = ["N999", "RUF012"]
@@ -0,0 +1,27 @@
1
+ from collections.abc import Mapping
2
+ from typing import Literal
3
+
4
+ from pydantic import Field, SecretStr
5
+ from swarmauri_base.model_providers.ProviderAuthBase import ProviderAuthBase
6
+ from swarmauri_base.ComponentBase import ComponentBase
7
+
8
+
9
+ @ComponentBase.register_type(ProviderAuthBase, "OpenAIAuthConfig")
10
+ class OpenAIAuthConfig(ProviderAuthBase):
11
+ api_key: SecretStr | None = Field(default=None, exclude=True)
12
+ organization: str | None = None
13
+ project: str | None = None
14
+ type: Literal["OpenAIAuthConfig"] = "OpenAIAuthConfig"
15
+
16
+ def apply(self, headers: Mapping[str, str]) -> dict[str, str]:
17
+ authorized = dict(headers)
18
+ if self.api_key is None:
19
+ raise ValueError("OpenAIProvider requires an api_key")
20
+ authorized["Authorization"] = (
21
+ f"Bearer {self.api_key.get_secret_value()}"
22
+ )
23
+ if self.organization is not None:
24
+ authorized["OpenAI-Organization"] = self.organization
25
+ if self.project is not None:
26
+ authorized["OpenAI-Project"] = self.project
27
+ return authorized
@@ -0,0 +1,27 @@
1
+ from typing import Literal
2
+
3
+ from swarmauri_base.model_providers.ProviderEndpointBase import (
4
+ ProviderEndpointBase,
5
+ )
6
+ from swarmauri_base.ComponentBase import ComponentBase
7
+
8
+
9
+ @ComponentBase.register_type(ProviderEndpointBase, "OpenAIEndpoints")
10
+ class OpenAIEndpoints(ProviderEndpointBase):
11
+ base_url: str = "https://api.openai.com/v1"
12
+ realtime_url: str = "wss://api.openai.com/v1/realtime"
13
+ responses_websocket_url: str = "wss://api.openai.com/v1/responses"
14
+ type: Literal["OpenAIEndpoints"] = "OpenAIEndpoints"
15
+
16
+ def resolve(self, capability: str) -> str:
17
+ endpoints = {
18
+ "api": self.base_url,
19
+ "realtime.connect": self.realtime_url,
20
+ "responses.connect": self.responses_websocket_url,
21
+ }
22
+ try:
23
+ return endpoints[capability]
24
+ except KeyError as error:
25
+ raise ValueError(
26
+ f"OpenAI does not support capability {capability!r}"
27
+ ) from error
@@ -0,0 +1,14 @@
1
+ from typing import Any, Literal
2
+
3
+ from swarmauri_base.model_providers.ProviderErrorMapperBase import (
4
+ ProviderErrorMapperBase,
5
+ )
6
+ from swarmauri_base.ComponentBase import ComponentBase
7
+
8
+
9
+ @ComponentBase.register_type(ProviderErrorMapperBase, "OpenAIErrorMapper")
10
+ class OpenAIErrorMapper(ProviderErrorMapperBase):
11
+ type: Literal["OpenAIErrorMapper"] = "OpenAIErrorMapper"
12
+
13
+ def map_error(self, error: BaseException | Any) -> BaseException:
14
+ return RuntimeError(f"OpenAI provider error: {error}")
@@ -0,0 +1,17 @@
1
+ from typing import Literal
2
+
3
+ from swarmauri_base.model_providers.ModelCatalogBase import ModelCatalogBase
4
+ from swarmauri_base.ComponentBase import ComponentBase
5
+
6
+
7
+ @ComponentBase.register_type(ModelCatalogBase, "OpenAIModelCatalog")
8
+ class OpenAIModelCatalog(ModelCatalogBase):
9
+ models: dict = {
10
+ "gpt-realtime": {
11
+ "name": "gpt-realtime",
12
+ "input_modalities": ["text", "audio", "image"],
13
+ "output_modalities": ["text", "audio"],
14
+ "features": ["realtime", "tools", "interruption"],
15
+ }
16
+ }
17
+ type: Literal["OpenAIModelCatalog"] = "OpenAIModelCatalog"
@@ -0,0 +1,72 @@
1
+ from typing import Any, Literal
2
+
3
+ from pydantic import Field, model_validator
4
+ from swarmauri_base.ComponentBase import ComponentBase
5
+ from swarmauri_base.model_providers.ModelProviderBase import ModelProviderBase
6
+ from swarmauri_base.model_providers.ProviderConnectionConfig import (
7
+ ProviderConnectionConfig,
8
+ )
9
+
10
+ from .OpenAIAuthConfig import OpenAIAuthConfig
11
+ from .OpenAIEndpoints import OpenAIEndpoints
12
+ from .OpenAIErrorMapper import OpenAIErrorMapper
13
+ from .OpenAIModelCatalog import OpenAIModelCatalog
14
+
15
+
16
+ @ComponentBase.register_type(ModelProviderBase, "OpenAIProvider")
17
+ class OpenAIProvider(ModelProviderBase):
18
+ provider_name: Literal["openai"] = "openai"
19
+ endpoints: OpenAIEndpoints = Field(default_factory=OpenAIEndpoints)
20
+ auth: OpenAIAuthConfig = Field(
21
+ default_factory=OpenAIAuthConfig, exclude=True
22
+ )
23
+ catalog: OpenAIModelCatalog = Field(default_factory=OpenAIModelCatalog)
24
+ error_mapper: OpenAIErrorMapper = Field(default_factory=OpenAIErrorMapper)
25
+ type: Literal["OpenAIProvider"] = "OpenAIProvider"
26
+
27
+ @model_validator(mode="after")
28
+ def configure(self) -> "OpenAIProvider":
29
+ self.base_url = self.base_url or self.endpoints.resolve("api")
30
+ self.endpoints.base_url = self.base_url
31
+ if self.api_key is not None:
32
+ self.auth.api_key = self.api_key
33
+ elif self.auth.api_key is not None:
34
+ self.api_key = self.auth.api_key
35
+ if self.organization is not None:
36
+ self.auth.organization = self.organization
37
+ elif self.auth.organization is not None:
38
+ self.organization = self.auth.organization
39
+ if self.project is not None:
40
+ self.auth.project = self.project
41
+ elif self.auth.project is not None:
42
+ self.project = self.auth.project
43
+ if self.organization:
44
+ self.default_headers.setdefault(
45
+ "OpenAI-Organization", self.organization
46
+ )
47
+ if self.project:
48
+ self.default_headers.setdefault("OpenAI-Project", self.project)
49
+ return self
50
+
51
+ def connection_config(
52
+ self,
53
+ capability: str,
54
+ model: str | None = None,
55
+ ) -> ProviderConnectionConfig:
56
+ endpoint = self.endpoints.resolve(capability)
57
+ headers = dict(self.default_headers)
58
+ query: dict[str, str] = {}
59
+ transport_options: dict[str, Any] = {}
60
+ if capability == "realtime.connect":
61
+ headers["OpenAI-Beta"] = "realtime=v1"
62
+ if model is not None:
63
+ query["model"] = model
64
+ elif capability == "api":
65
+ transport_options = super().connection_options()
66
+ return ProviderConnectionConfig(
67
+ endpoint=endpoint,
68
+ headers=headers,
69
+ query=query,
70
+ transport_options=transport_options,
71
+ auth_context=self.auth,
72
+ )
@@ -0,0 +1,19 @@
1
+ from typing import Any
2
+
3
+
4
+ class OpenAIToolSchemaConverter:
5
+ def convert(self, tool: Any) -> dict[str, Any]:
6
+ parameters = getattr(tool, "parameters", [])
7
+ properties = {
8
+ parameter.name: {
9
+ "type": getattr(parameter, "type", "string"),
10
+ "description": getattr(parameter, "description", None),
11
+ }
12
+ for parameter in parameters
13
+ }
14
+ return {
15
+ "type": "function",
16
+ "name": tool.name,
17
+ "description": getattr(tool, "description", None),
18
+ "parameters": {"type": "object", "properties": properties},
19
+ }
@@ -0,0 +1,15 @@
1
+ from .OpenAIAuthConfig import OpenAIAuthConfig
2
+ from .OpenAIEndpoints import OpenAIEndpoints
3
+ from .OpenAIErrorMapper import OpenAIErrorMapper
4
+ from .OpenAIModelCatalog import OpenAIModelCatalog
5
+ from .OpenAIProvider import OpenAIProvider
6
+ from .OpenAIToolSchemaConverter import OpenAIToolSchemaConverter
7
+
8
+ __all__ = [
9
+ "OpenAIAuthConfig",
10
+ "OpenAIEndpoints",
11
+ "OpenAIErrorMapper",
12
+ "OpenAIModelCatalog",
13
+ "OpenAIProvider",
14
+ "OpenAIToolSchemaConverter",
15
+ ]