swarmauri_live_model_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,52 @@
1
+ Metadata-Version: 2.4
2
+ Name: swarmauri_live_model_xai
3
+ Version: 0.1.0.dev3
4
+ Summary: xAI realtime bidirectional model adapter for Swarmauri.
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,xai,grok,realtime,websocket
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
+ Requires-Dist: swarmauri_provider_xai (>=0.1.0.dev3)
31
+ Requires-Dist: swarmauri_transport_websocket (>=0.1.0.dev4)
32
+ Description-Content-Type: text/markdown
33
+
34
+ ![Swarmauri](https://github.com/swarmauri/swarmauri-sdk/blob/3d4d1cfa949399d7019ae9d8f296afba773dfb7f/assets/swarmauri.brand.theme.svg)
35
+
36
+ [![PyPI](https://img.shields.io/pypi/v/swarmauri_live_model_xai.svg)](https://pypi.org/project/swarmauri_live_model_xai/) [![Python](https://img.shields.io/pypi/pyversions/swarmauri_live_model_xai.svg)](https://pypi.org/project/swarmauri_live_model_xai/) [![License](https://img.shields.io/badge/license-Apache--2.0-blue)](LICENSE) [![Downloads](https://static.pepy.tech/badge/swarmauri_live_model_xai)](https://pepy.tech/project/swarmauri_live_model_xai) [![Hits](https://hits.sh/github.com/swarmauri/swarmauri-sdk.svg)](https://hits.sh/github.com/swarmauri/swarmauri-sdk/)
37
+
38
+ # swarmauri_live_model_xai
39
+
40
+ ## Features
41
+
42
+ - xAI realtime text/audio sessions and function calls.
43
+ - Typed Swarmauri live events over WebSocket.
44
+
45
+ ## Installation
46
+
47
+ `uv add swarmauri_live_model_xai` or `pip install swarmauri_live_model_xai`.
48
+
49
+ ## Usage
50
+
51
+ Inject `XAIProvider` and consume events from `XAIRealtimeModel`.
52
+
@@ -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_live_model_xai.svg)](https://pypi.org/project/swarmauri_live_model_xai/) [![Python](https://img.shields.io/pypi/pyversions/swarmauri_live_model_xai.svg)](https://pypi.org/project/swarmauri_live_model_xai/) [![License](https://img.shields.io/badge/license-Apache--2.0-blue)](LICENSE) [![Downloads](https://static.pepy.tech/badge/swarmauri_live_model_xai)](https://pepy.tech/project/swarmauri_live_model_xai) [![Hits](https://hits.sh/github.com/swarmauri/swarmauri-sdk.svg)](https://hits.sh/github.com/swarmauri/swarmauri-sdk/)
4
+
5
+ # swarmauri_live_model_xai
6
+
7
+ ## Features
8
+
9
+ - xAI realtime text/audio sessions and function calls.
10
+ - Typed Swarmauri live events over WebSocket.
11
+
12
+ ## Installation
13
+
14
+ `uv add swarmauri_live_model_xai` or `pip install swarmauri_live_model_xai`.
15
+
16
+ ## Usage
17
+
18
+ Inject `XAIProvider` and consume events from `XAIRealtimeModel`.
@@ -0,0 +1,29 @@
1
+ [project]
2
+ name = "swarmauri_live_model_xai"
3
+ version = "0.1.0.dev3"
4
+ description = "xAI realtime bidirectional model adapter for Swarmauri."
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", "swarmauri_provider_xai>=0.1.0.dev3", "swarmauri_transport_websocket>=0.1.0.dev4"]
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", "xai", "grok", "realtime", "websocket"]
12
+ [tool.uv.sources]
13
+ swarmauri_core = { workspace = true }
14
+ swarmauri_base = { workspace = true }
15
+ swarmauri_provider_xai = { workspace = true }
16
+ swarmauri_transport_websocket = { workspace = true }
17
+ [project.entry-points."swarmauri.live_models"]
18
+ XAIRealtimeModel = "swarmauri_live_model_xai:XAIRealtimeModel"
19
+ [build-system]
20
+ requires = ["poetry-core>=1.8.0"]
21
+ build-backend = "poetry.core.masonry.api"
22
+ [dependency-groups]
23
+ dev = ["pytest>=8.0", "pytest-asyncio>=0.24", "ruff>=0.9.9", "swarmauri_tests_component"]
24
+
25
+ [tool.ruff]
26
+ extend = "../../pyproject.toml"
27
+
28
+ [tool.ruff.lint]
29
+ ignore = ["BLE001", "N999"]
@@ -0,0 +1,92 @@
1
+ import base64
2
+ import json
3
+
4
+ from swarmauri_base.live_models.events.AudioDeltaEvent import AudioDeltaEvent
5
+ from swarmauri_base.live_models.events.SessionErrorEvent import (
6
+ SessionErrorEvent,
7
+ )
8
+ from swarmauri_base.live_models.events.TextDeltaEvent import TextDeltaEvent
9
+ from swarmauri_base.live_models.events.ToolCallEvent import ToolCallEvent
10
+ from swarmauri_base.live_models.events.TurnCompleteEvent import (
11
+ TurnCompleteEvent,
12
+ )
13
+
14
+
15
+ class XAIRealtimeCodec:
16
+ def encode(self, input_data):
17
+ if input_data.kind == "text":
18
+ return json.dumps(
19
+ {
20
+ "type": "conversation.item.create",
21
+ "item": {
22
+ "role": "user",
23
+ "content": [
24
+ {"type": "input_text", "text": input_data.payload}
25
+ ],
26
+ },
27
+ }
28
+ )
29
+ if input_data.kind == "audio":
30
+ return json.dumps(
31
+ {
32
+ "type": "input_audio_buffer.append",
33
+ "audio": base64.b64encode(input_data.payload).decode(),
34
+ }
35
+ )
36
+ if input_data.kind == "tool_result":
37
+ return json.dumps(
38
+ {
39
+ "type": "conversation.item.create",
40
+ "item": {
41
+ "type": "function_call_output",
42
+ "call_id": input_data.call_id,
43
+ "output": json.dumps(input_data.payload),
44
+ },
45
+ }
46
+ )
47
+ if input_data.kind == "interrupt":
48
+ return json.dumps({"type": "response.cancel"})
49
+ return json.dumps({"type": "response.create"})
50
+
51
+ def decode(self, message):
52
+ data = json.loads(message)
53
+ kind = data.get("type", "")
54
+ correlation = {
55
+ "provider_event_id": data.get("event_id"),
56
+ "call_id": data.get("call_id"),
57
+ }
58
+ if kind.endswith("text.delta"):
59
+ return [
60
+ TextDeltaEvent(
61
+ payload=data.get("delta", ""), correlation=correlation
62
+ )
63
+ ]
64
+ if kind.endswith("audio.delta"):
65
+ return [
66
+ AudioDeltaEvent(
67
+ payload=base64.b64decode(data.get("delta", "")),
68
+ correlation=correlation,
69
+ )
70
+ ]
71
+ if kind == "response.function_call_arguments.done":
72
+ args = data.get("arguments", {})
73
+ return [
74
+ ToolCallEvent(
75
+ call_id=data.get("call_id", ""),
76
+ tool_name=data.get("name", ""),
77
+ arguments=json.loads(args)
78
+ if isinstance(args, str)
79
+ else args,
80
+ correlation=correlation,
81
+ )
82
+ ]
83
+ if kind == "response.done":
84
+ return [TurnCompleteEvent(correlation=correlation)]
85
+ if kind == "error":
86
+ return [
87
+ SessionErrorEvent(
88
+ message=str(data.get("error", data)),
89
+ correlation=correlation,
90
+ )
91
+ ]
92
+ return []
@@ -0,0 +1,12 @@
1
+ from swarmauri_base.live_models.LiveConfigBase import LiveConfigBase
2
+
3
+
4
+ class XAIRealtimeConfigMapper:
5
+ def map(self, config: LiveConfigBase) -> dict:
6
+ return {
7
+ "type": "session.update",
8
+ "session": {
9
+ "modalities": config.requested_modalities,
10
+ **config.provider_options.get("session", {}),
11
+ },
12
+ }
@@ -0,0 +1,43 @@
1
+ from typing import Any, Literal
2
+
3
+ from pydantic import ConfigDict, Field
4
+ from swarmauri_base.ComponentBase import ComponentBase
5
+ from swarmauri_base.live_models.LiveCapabilitiesBase import (
6
+ LiveCapabilitiesBase,
7
+ )
8
+ from swarmauri_base.live_models.LiveConfigBase import LiveConfigBase
9
+ from swarmauri_base.live_models.LiveModelBase import LiveModelBase
10
+ from swarmauri_provider_xai import XAIProvider
11
+ from swarmauri_transport_websocket import WebSocketClient
12
+
13
+ from .XAIRealtimeConfigMapper import XAIRealtimeConfigMapper
14
+ from .XAIRealtimeSession import XAIRealtimeSession
15
+
16
+
17
+ @ComponentBase.register_type(LiveModelBase, "XAIRealtimeModel")
18
+ class XAIRealtimeModel(LiveModelBase):
19
+ name: str = "grok-realtime"
20
+ provider: XAIProvider = Field(default_factory=XAIProvider)
21
+ transport: Any = Field(default_factory=WebSocketClient, exclude=True)
22
+ capabilities: LiveCapabilitiesBase = Field(
23
+ default_factory=lambda: LiveCapabilitiesBase(
24
+ input_modalities=frozenset({"text", "audio"}),
25
+ output_modalities=frozenset({"text", "audio"}),
26
+ features=frozenset({"tools"}),
27
+ session_features=frozenset({"realtime"}),
28
+ )
29
+ )
30
+ type: Literal["XAIRealtimeModel"] = "XAIRealtimeModel"
31
+ model_config = ConfigDict(arbitrary_types_allowed=True, extra="allow")
32
+
33
+ async def aconnect(self, config: LiveConfigBase):
34
+ connection_config = self.provider.prepare_connection(
35
+ "realtime.connect", model=self.name
36
+ )
37
+ connection = await self.transport.aconnect(
38
+ connection_config.resolved_url(),
39
+ headers=connection_config.headers,
40
+ )
41
+ return await XAIRealtimeSession.create(
42
+ connection, XAIRealtimeConfigMapper().map(config)
43
+ )
@@ -0,0 +1,53 @@
1
+ import asyncio
2
+ import json
3
+ from contextlib import suppress
4
+ from typing import Literal
5
+
6
+ from pydantic import PrivateAttr
7
+ from swarmauri_base.live_models.events.SessionErrorEvent import (
8
+ SessionErrorEvent,
9
+ )
10
+ from swarmauri_base.live_models.LiveSessionBase import LiveSessionBase
11
+ from swarmauri_base.ComponentBase import ComponentBase
12
+
13
+ from .XAIRealtimeCodec import XAIRealtimeCodec
14
+
15
+
16
+ @ComponentBase.register_type(LiveSessionBase, "XAIRealtimeSession")
17
+ class XAIRealtimeSession(LiveSessionBase):
18
+ type: Literal["XAIRealtimeSession"] = "XAIRealtimeSession"
19
+ _connection = PrivateAttr()
20
+ _codec = PrivateAttr(default_factory=XAIRealtimeCodec)
21
+ _receiver = PrivateAttr(default=None)
22
+
23
+ @classmethod
24
+ async def create(cls, connection, setup):
25
+ session = cls()
26
+ session._connection = connection
27
+ await connection.asend(json.dumps(setup))
28
+ session.mark_open()
29
+ session._receiver = asyncio.create_task(session._receive_loop())
30
+ return session
31
+
32
+ async def _receive_loop(self) -> None:
33
+ try:
34
+ async for message in self._connection.aiter_messages():
35
+ for event in self._codec.decode(message):
36
+ await self.emit(event)
37
+ except asyncio.CancelledError:
38
+ raise
39
+ except Exception as exc:
40
+ await self.emit(SessionErrorEvent(message=str(exc)))
41
+ await self.fail(exc)
42
+ else:
43
+ await self.finish()
44
+
45
+ async def _asend_impl(self, input_data):
46
+ await self._connection.asend(self._codec.encode(input_data))
47
+
48
+ async def _aclose_impl(self):
49
+ if self._receiver and self._receiver is not asyncio.current_task():
50
+ self._receiver.cancel()
51
+ with suppress(asyncio.CancelledError):
52
+ await self._receiver
53
+ await self._connection.aclose()
@@ -0,0 +1,6 @@
1
+ from swarmauri_base.live_models.inputs.ToolResultInput import ToolResultInput
2
+
3
+
4
+ class XAIRealtimeToolAdapter:
5
+ def result(self, call_id, value):
6
+ return ToolResultInput(call_id=call_id, payload=value)
@@ -0,0 +1,13 @@
1
+ from .XAIRealtimeCodec import XAIRealtimeCodec
2
+ from .XAIRealtimeConfigMapper import XAIRealtimeConfigMapper
3
+ from .XAIRealtimeModel import XAIRealtimeModel
4
+ from .XAIRealtimeSession import XAIRealtimeSession
5
+ from .XAIRealtimeToolAdapter import XAIRealtimeToolAdapter
6
+
7
+ __all__ = [
8
+ "XAIRealtimeCodec",
9
+ "XAIRealtimeConfigMapper",
10
+ "XAIRealtimeModel",
11
+ "XAIRealtimeSession",
12
+ "XAIRealtimeToolAdapter",
13
+ ]