swarmauri_provider_xai 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,60 @@
1
+ Metadata-Version: 2.4
2
+ Name: swarmauri_provider_xai
3
+ Version: 0.1.0.dev3
4
+ Summary: Aggregated xAI 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,xai,grok,realtime
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_xai.svg)](https://pypi.org/project/swarmauri_provider_xai/) [![Python](https://img.shields.io/pypi/pyversions/swarmauri_provider_xai.svg)](https://pypi.org/project/swarmauri_provider_xai/) [![License](https://img.shields.io/badge/license-Apache--2.0-blue)](LICENSE) [![Downloads](https://static.pepy.tech/badge/swarmauri_provider_xai)](https://pepy.tech/project/swarmauri_provider_xai) [![Hits](https://hits.sh/github.com/swarmauri/swarmauri-sdk.svg)](https://hits.sh/github.com/swarmauri/swarmauri-sdk/)
35
+
36
+ # swarmauri_provider_xai
37
+
38
+ ## Features
39
+
40
+ - xAI authentication, endpoints, catalogs, errors, and tool schemas.
41
+ - Capability-aware chat, model-listing, and realtime connection configs.
42
+ - Runtime authorization with credentials excluded from serialized configs.
43
+ - Shared provider resource for xAI model families.
44
+
45
+ ## Installation
46
+
47
+ `uv add swarmauri_provider_xai` or `pip install swarmauri_provider_xai`.
48
+
49
+ ## Usage
50
+
51
+ ```python
52
+ from swarmauri_provider_xai import XAIProvider
53
+
54
+ provider = XAIProvider(api_key="...")
55
+ config = provider.connection_config("chat.completions", model="grok-4.3")
56
+ headers = provider.authorize(config)
57
+ ```
58
+
59
+ Inject the provider into an xAI model adapter; the provider describes the connection but never performs network I/O.
60
+
@@ -0,0 +1,28 @@
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_xai.svg)](https://pypi.org/project/swarmauri_provider_xai/) [![Python](https://img.shields.io/pypi/pyversions/swarmauri_provider_xai.svg)](https://pypi.org/project/swarmauri_provider_xai/) [![License](https://img.shields.io/badge/license-Apache--2.0-blue)](LICENSE) [![Downloads](https://static.pepy.tech/badge/swarmauri_provider_xai)](https://pepy.tech/project/swarmauri_provider_xai) [![Hits](https://hits.sh/github.com/swarmauri/swarmauri-sdk.svg)](https://hits.sh/github.com/swarmauri/swarmauri-sdk/)
4
+
5
+ # swarmauri_provider_xai
6
+
7
+ ## Features
8
+
9
+ - xAI authentication, endpoints, catalogs, errors, and tool schemas.
10
+ - Capability-aware chat, model-listing, and realtime connection configs.
11
+ - Runtime authorization with credentials excluded from serialized configs.
12
+ - Shared provider resource for xAI model families.
13
+
14
+ ## Installation
15
+
16
+ `uv add swarmauri_provider_xai` or `pip install swarmauri_provider_xai`.
17
+
18
+ ## Usage
19
+
20
+ ```python
21
+ from swarmauri_provider_xai import XAIProvider
22
+
23
+ provider = XAIProvider(api_key="...")
24
+ config = provider.connection_config("chat.completions", model="grok-4.3")
25
+ headers = provider.authorize(config)
26
+ ```
27
+
28
+ Inject the provider into an xAI model adapter; the provider describes the connection but never performs network I/O.
@@ -0,0 +1,27 @@
1
+ [project]
2
+ name = "swarmauri_provider_xai"
3
+ version = "0.1.0.dev3"
4
+ description = "Aggregated xAI 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", "xai", "grok", "realtime"]
12
+ [tool.uv.sources]
13
+ swarmauri_core = { workspace = true }
14
+ swarmauri_base = { workspace = true }
15
+ [project.entry-points."swarmauri.model_providers"]
16
+ XAIProvider = "swarmauri_provider_xai:XAIProvider"
17
+ [build-system]
18
+ requires = ["poetry-core>=1.8.0"]
19
+ build-backend = "poetry.core.masonry.api"
20
+ [dependency-groups]
21
+ dev = ["pytest>=8.0", "ruff>=0.9.9", "swarmauri_tests_component"]
22
+
23
+ [tool.ruff]
24
+ extend = "../../pyproject.toml"
25
+
26
+ [tool.ruff.lint]
27
+ ignore = ["N999", "RUF012"]
@@ -0,0 +1,21 @@
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, "XAIAuthConfig")
10
+ class XAIAuthConfig(ProviderAuthBase):
11
+ api_key: SecretStr | None = Field(default=None, exclude=True)
12
+ type: Literal["XAIAuthConfig"] = "XAIAuthConfig"
13
+
14
+ def apply(self, headers: Mapping[str, str]) -> dict[str, str]:
15
+ authorized = dict(headers)
16
+ if self.api_key is None:
17
+ raise ValueError("XAIProvider requires an api_key")
18
+ authorized["Authorization"] = (
19
+ f"Bearer {self.api_key.get_secret_value()}"
20
+ )
21
+ return authorized
@@ -0,0 +1,30 @@
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, "XAIEndpoints")
10
+ class XAIEndpoints(ProviderEndpointBase):
11
+ base_url: str = "https://api.x.ai/v1"
12
+ chat_completions_path: str = "/chat/completions"
13
+ models_path: str = "/language-models"
14
+ websocket_url: str = "wss://api.x.ai/v1/realtime"
15
+ type: Literal["XAIEndpoints"] = "XAIEndpoints"
16
+
17
+ def resolve(self, capability: str) -> str:
18
+ paths = {
19
+ "chat.completions": self.chat_completions_path,
20
+ "models.list": self.models_path,
21
+ }
22
+ if capability == "realtime.connect":
23
+ return self.websocket_url
24
+ try:
25
+ path = paths[capability]
26
+ except KeyError as error:
27
+ raise ValueError(
28
+ f"xAI does not support capability {capability!r}"
29
+ ) from error
30
+ return f"{self.base_url.rstrip('/')}/{path.lstrip('/')}"
@@ -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, "XAIErrorMapper")
10
+ class XAIErrorMapper(ProviderErrorMapperBase):
11
+ type: Literal["XAIErrorMapper"] = "XAIErrorMapper"
12
+
13
+ def map_error(self, error: BaseException | Any) -> BaseException:
14
+ return RuntimeError(f"xAI provider error: {error}")
@@ -0,0 +1,20 @@
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, "XAIModelCatalog")
8
+ class XAIModelCatalog(ModelCatalogBase):
9
+ models: dict = {
10
+ "grok-4.3": {
11
+ "name": "grok-4.3",
12
+ "capabilities": [
13
+ "chat_completions",
14
+ "multimodal_input",
15
+ "streaming",
16
+ "tools",
17
+ ],
18
+ }
19
+ }
20
+ type: Literal["XAIModelCatalog"] = "XAIModelCatalog"
@@ -0,0 +1,60 @@
1
+ from typing import 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 .XAIAuthConfig import XAIAuthConfig
11
+ from .XAIEndpoints import XAIEndpoints
12
+ from .XAIErrorMapper import XAIErrorMapper
13
+ from .XAIModelCatalog import XAIModelCatalog
14
+
15
+
16
+ @ComponentBase.register_type(ModelProviderBase, "XAIProvider")
17
+ class XAIProvider(ModelProviderBase):
18
+ provider_name: Literal["xai"] = "xai"
19
+ endpoints: XAIEndpoints = Field(default_factory=XAIEndpoints)
20
+ catalog: XAIModelCatalog = Field(default_factory=XAIModelCatalog)
21
+ error_mapper: XAIErrorMapper = Field(default_factory=XAIErrorMapper)
22
+ auth: XAIAuthConfig = Field(default_factory=XAIAuthConfig, exclude=True)
23
+ type: Literal["XAIProvider"] = "XAIProvider"
24
+
25
+ @model_validator(mode="after")
26
+ def configure(self) -> "XAIProvider":
27
+ self.base_url = self.base_url or self.endpoints.base_url
28
+ self.endpoints.base_url = self.base_url
29
+ if self.api_key is not None:
30
+ self.auth.api_key = self.api_key
31
+ elif self.auth.api_key is not None:
32
+ self.api_key = self.auth.api_key
33
+ return self
34
+
35
+ def connection_config(
36
+ self,
37
+ capability: str,
38
+ model: str | None = None,
39
+ ) -> ProviderConnectionConfig:
40
+ endpoint = self.endpoints.resolve(capability)
41
+ options = super().connection_options()
42
+ if model is not None:
43
+ options["model"] = model
44
+ return ProviderConnectionConfig(
45
+ endpoint=endpoint,
46
+ headers={
47
+ **self.default_headers,
48
+ "Accept": "application/json",
49
+ "Content-Type": "application/json",
50
+ },
51
+ transport_options=options,
52
+ auth_context=self.auth,
53
+ )
54
+
55
+ @staticmethod
56
+ def authorize(config: ProviderConnectionConfig) -> dict[str, str]:
57
+ auth = config.auth_context
58
+ if not isinstance(auth, XAIAuthConfig):
59
+ raise TypeError("xAI connection configuration has no auth context")
60
+ return auth.apply(config.headers)
@@ -0,0 +1,30 @@
1
+ from typing import Any
2
+
3
+
4
+ class XAIToolSchemaConverter:
5
+ def convert(self, tool: Any) -> dict[str, Any]:
6
+ properties: dict[str, Any] = {}
7
+ required: list[str] = []
8
+ for parameter in getattr(tool, "parameters", []):
9
+ schema: dict[str, Any] = {
10
+ "type": parameter.input_type,
11
+ "description": parameter.description,
12
+ }
13
+ if parameter.enum:
14
+ schema["enum"] = parameter.enum
15
+ properties[parameter.name] = schema
16
+ if parameter.required:
17
+ required.append(parameter.name)
18
+ return {
19
+ "type": "function",
20
+ "function": {
21
+ "name": tool.name,
22
+ "description": getattr(tool, "description", None),
23
+ "parameters": {
24
+ "type": "object",
25
+ "properties": properties,
26
+ "required": required,
27
+ "additionalProperties": False,
28
+ },
29
+ },
30
+ }
@@ -0,0 +1,15 @@
1
+ from .XAIAuthConfig import XAIAuthConfig
2
+ from .XAIEndpoints import XAIEndpoints
3
+ from .XAIErrorMapper import XAIErrorMapper
4
+ from .XAIModelCatalog import XAIModelCatalog
5
+ from .XAIProvider import XAIProvider
6
+ from .XAIToolSchemaConverter import XAIToolSchemaConverter
7
+
8
+ __all__ = [
9
+ "XAIAuthConfig",
10
+ "XAIEndpoints",
11
+ "XAIErrorMapper",
12
+ "XAIModelCatalog",
13
+ "XAIProvider",
14
+ "XAIToolSchemaConverter",
15
+ ]