swarmauri_live_model_amazon_nova 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_amazon_nova
3
+ Version: 0.1.0.dev3
4
+ Summary: Amazon Nova Sonic bidirectional HTTP/2 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,amazon nova,nova sonic,audio,http2
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_amazon_nova (>=0.1.0.dev3)
31
+ Requires-Dist: swarmauri_transport_aws_eventstream (>=0.1.0.dev1)
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_amazon_nova.svg)](https://pypi.org/project/swarmauri_live_model_amazon_nova/) [![Python](https://img.shields.io/pypi/pyversions/swarmauri_live_model_amazon_nova.svg)](https://pypi.org/project/swarmauri_live_model_amazon_nova/) [![License](https://img.shields.io/badge/license-Apache--2.0-blue)](LICENSE) [![Downloads](https://static.pepy.tech/badge/swarmauri_live_model_amazon_nova)](https://pepy.tech/project/swarmauri_live_model_amazon_nova) [![Hits](https://hits.sh/github.com/swarmauri/swarmauri-sdk.svg)](https://hits.sh/github.com/swarmauri/swarmauri-sdk/)
37
+
38
+ # swarmauri_live_model_amazon_nova
39
+
40
+ ## Features
41
+
42
+ - Nova Sonic text/audio sessions over an injected bidirectional HTTP/2 transport.
43
+ - Typed audio, text, tool, usage, completion, and error events.
44
+
45
+ ## Installation
46
+
47
+ `uv add swarmauri_live_model_amazon_nova` or `pip install swarmauri_live_model_amazon_nova`.
48
+
49
+ ## Usage
50
+
51
+ Inject `AmazonNovaProvider` and an `IBidirectionalStreamClient` into `NovaSonicLiveModel`.
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_amazon_nova.svg)](https://pypi.org/project/swarmauri_live_model_amazon_nova/) [![Python](https://img.shields.io/pypi/pyversions/swarmauri_live_model_amazon_nova.svg)](https://pypi.org/project/swarmauri_live_model_amazon_nova/) [![License](https://img.shields.io/badge/license-Apache--2.0-blue)](LICENSE) [![Downloads](https://static.pepy.tech/badge/swarmauri_live_model_amazon_nova)](https://pepy.tech/project/swarmauri_live_model_amazon_nova) [![Hits](https://hits.sh/github.com/swarmauri/swarmauri-sdk.svg)](https://hits.sh/github.com/swarmauri/swarmauri-sdk/)
4
+
5
+ # swarmauri_live_model_amazon_nova
6
+
7
+ ## Features
8
+
9
+ - Nova Sonic text/audio sessions over an injected bidirectional HTTP/2 transport.
10
+ - Typed audio, text, tool, usage, completion, and error events.
11
+
12
+ ## Installation
13
+
14
+ `uv add swarmauri_live_model_amazon_nova` or `pip install swarmauri_live_model_amazon_nova`.
15
+
16
+ ## Usage
17
+
18
+ Inject `AmazonNovaProvider` and an `IBidirectionalStreamClient` into `NovaSonicLiveModel`.
@@ -0,0 +1,29 @@
1
+ [project]
2
+ name = "swarmauri_live_model_amazon_nova"
3
+ version = "0.1.0.dev3"
4
+ description = "Amazon Nova Sonic bidirectional HTTP/2 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_amazon_nova>=0.1.0.dev3", "swarmauri_transport_aws_eventstream>=0.1.0.dev1"]
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", "amazon nova", "nova sonic", "audio", "http2"]
12
+ [tool.uv.sources]
13
+ swarmauri_core = { workspace = true }
14
+ swarmauri_base = { workspace = true }
15
+ swarmauri_provider_amazon_nova = { workspace = true }
16
+ swarmauri_transport_aws_eventstream = { workspace = true }
17
+ [project.entry-points."swarmauri.live_models"]
18
+ NovaSonicLiveModel = "swarmauri_live_model_amazon_nova:NovaSonicLiveModel"
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,84 @@
1
+ import base64
2
+
3
+ from swarmauri_base.live_models.events.AudioDeltaEvent import AudioDeltaEvent
4
+ from swarmauri_base.live_models.events.SessionErrorEvent import (
5
+ SessionErrorEvent,
6
+ )
7
+ from swarmauri_base.live_models.events.TextDeltaEvent import TextDeltaEvent
8
+ from swarmauri_base.live_models.events.ToolCallEvent import ToolCallEvent
9
+ from swarmauri_base.live_models.events.TurnCompleteEvent import (
10
+ TurnCompleteEvent,
11
+ )
12
+ from swarmauri_base.live_models.events.UsageEvent import UsageEvent
13
+
14
+
15
+ class NovaSonicCodec:
16
+ def encode(self, input_data):
17
+ if input_data.kind == "audio":
18
+ return {
19
+ "event": {
20
+ "audioInput": {
21
+ "content": base64.b64encode(
22
+ input_data.payload
23
+ ).decode()
24
+ }
25
+ }
26
+ }
27
+ if input_data.kind == "text":
28
+ return {"event": {"textInput": {"content": input_data.payload}}}
29
+ if input_data.kind == "tool_result":
30
+ return {
31
+ "event": {
32
+ "toolResult": {
33
+ "toolUseId": input_data.call_id,
34
+ "content": [{"json": input_data.payload}],
35
+ "status": "error"
36
+ if input_data.is_error
37
+ else "success",
38
+ }
39
+ }
40
+ }
41
+ if input_data.kind == "commit_turn":
42
+ return {"event": {"contentEnd": {}}}
43
+ if input_data.kind == "interrupt":
44
+ return {"event": {"playbackCompletion": {}}}
45
+ raise ValueError(f"Unsupported Nova Sonic input: {input_data.kind}")
46
+
47
+ def decode(self, data):
48
+ event = data.get("event", data)
49
+ if "audioOutput" in event:
50
+ return [
51
+ AudioDeltaEvent(
52
+ payload=base64.b64decode(
53
+ event["audioOutput"].get("content", "")
54
+ )
55
+ )
56
+ ]
57
+ if "textOutput" in event:
58
+ return [
59
+ TextDeltaEvent(payload=event["textOutput"].get("content", ""))
60
+ ]
61
+ if "toolUse" in event:
62
+ use = event["toolUse"]
63
+ return [
64
+ ToolCallEvent(
65
+ call_id=use.get("toolUseId", ""),
66
+ tool_name=use.get("toolName", ""),
67
+ arguments=use.get("content", {}),
68
+ )
69
+ ]
70
+ if "completionEnd" in event or "contentEnd" in event:
71
+ return [TurnCompleteEvent()]
72
+ if "usageEvent" in event:
73
+ return [
74
+ UsageEvent(
75
+ payload={
76
+ key: int(value)
77
+ for key, value in event["usageEvent"].items()
78
+ if isinstance(value, int)
79
+ }
80
+ )
81
+ ]
82
+ if "error" in event:
83
+ return [SessionErrorEvent(message=str(event["error"]))]
84
+ return []
@@ -0,0 +1,38 @@
1
+ from swarmauri_base.live_models.LiveConfigBase import LiveConfigBase
2
+
3
+
4
+ class NovaSonicConfigMapper:
5
+ def map(self, model: str, config: LiveConfigBase) -> list[dict]:
6
+ return [
7
+ {
8
+ "event": {
9
+ "sessionStart": {
10
+ "inferenceConfiguration": config.provider_options.get(
11
+ "inference", {}
12
+ )
13
+ }
14
+ }
15
+ },
16
+ {
17
+ "event": {
18
+ "promptStart": {
19
+ "promptName": config.provider_options.get(
20
+ "prompt_name", "swarmauri"
21
+ ),
22
+ "textOutputConfiguration": {"mediaType": "text/plain"},
23
+ "audioOutputConfiguration": config.provider_options.get( # noqa: E501
24
+ "audio_output",
25
+ {
26
+ "mediaType": "audio/lpcm",
27
+ "sampleRateHertz": 24000,
28
+ "sampleSizeBits": 16,
29
+ "channelCount": 1,
30
+ "voiceId": "matthew",
31
+ "encoding": "base64",
32
+ "audioType": "SPEECH",
33
+ },
34
+ ),
35
+ }
36
+ }
37
+ },
38
+ ]
@@ -0,0 +1,49 @@
1
+ from typing import 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_core.transports.i_bidirectional_stream import (
11
+ IBidirectionalStreamClient,
12
+ )
13
+ from swarmauri_provider_amazon_nova import AmazonNovaProvider
14
+ from swarmauri_transport_aws_eventstream import AwsEventStreamClient
15
+
16
+ from .NovaSonicConfigMapper import NovaSonicConfigMapper
17
+ from .NovaSonicLiveSession import NovaSonicLiveSession
18
+
19
+
20
+ @ComponentBase.register_type(LiveModelBase, "NovaSonicLiveModel")
21
+ class NovaSonicLiveModel(LiveModelBase):
22
+ name: str = "amazon.nova-sonic-v1:0"
23
+ provider: AmazonNovaProvider = Field(default_factory=AmazonNovaProvider)
24
+ transport: IBidirectionalStreamClient = Field(
25
+ default_factory=AwsEventStreamClient, exclude=True
26
+ )
27
+ capabilities: LiveCapabilitiesBase = Field(
28
+ default_factory=lambda: LiveCapabilitiesBase(
29
+ input_modalities=frozenset({"text", "audio"}),
30
+ output_modalities=frozenset({"text", "audio"}),
31
+ features=frozenset({"tools"}),
32
+ session_features=frozenset({"realtime", "bidirectional_http2"}),
33
+ )
34
+ )
35
+ type: Literal["NovaSonicLiveModel"] = "NovaSonicLiveModel"
36
+ model_config = ConfigDict(arbitrary_types_allowed=True, extra="allow")
37
+
38
+ async def aconnect(self, config: LiveConfigBase):
39
+ connection_config = self.provider.prepare_connection(
40
+ "live.connect", model=self.name
41
+ )
42
+ connection = await self.transport.aconnect(
43
+ connection_config.resolved_url(),
44
+ headers=connection_config.headers,
45
+ **connection_config.transport_options,
46
+ )
47
+ return await NovaSonicLiveSession.create(
48
+ connection, NovaSonicConfigMapper().map(self.name, config)
49
+ )
@@ -0,0 +1,53 @@
1
+ import asyncio
2
+ from contextlib import suppress
3
+ from typing import Literal
4
+
5
+ from pydantic import PrivateAttr
6
+ from swarmauri_base.live_models.events.SessionErrorEvent import (
7
+ SessionErrorEvent,
8
+ )
9
+ from swarmauri_base.live_models.LiveSessionBase import LiveSessionBase
10
+ from swarmauri_base.ComponentBase import ComponentBase
11
+
12
+ from .NovaSonicCodec import NovaSonicCodec
13
+
14
+
15
+ @ComponentBase.register_type(LiveSessionBase, "NovaSonicLiveSession")
16
+ class NovaSonicLiveSession(LiveSessionBase):
17
+ type: Literal["NovaSonicLiveSession"] = "NovaSonicLiveSession"
18
+ _connection = PrivateAttr()
19
+ _codec = PrivateAttr(default_factory=NovaSonicCodec)
20
+ _receiver = PrivateAttr(default=None)
21
+
22
+ @classmethod
23
+ async def create(cls, connection, setup_events):
24
+ session = cls()
25
+ session._connection = connection
26
+ for event in setup_events:
27
+ await connection.asend(event)
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 data in self._connection.aiter_events():
35
+ for event in self._codec.decode(data):
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,8 @@
1
+ from swarmauri_base.live_models.inputs.ToolResultInput import ToolResultInput
2
+
3
+
4
+ class NovaSonicToolAdapter:
5
+ def result(self, call_id, value, *, is_error=False):
6
+ return ToolResultInput(
7
+ call_id=call_id, payload=value, is_error=is_error
8
+ )
@@ -0,0 +1,13 @@
1
+ from .NovaSonicCodec import NovaSonicCodec
2
+ from .NovaSonicConfigMapper import NovaSonicConfigMapper
3
+ from .NovaSonicLiveModel import NovaSonicLiveModel
4
+ from .NovaSonicLiveSession import NovaSonicLiveSession
5
+ from .NovaSonicToolAdapter import NovaSonicToolAdapter
6
+
7
+ __all__ = [
8
+ "NovaSonicCodec",
9
+ "NovaSonicConfigMapper",
10
+ "NovaSonicLiveModel",
11
+ "NovaSonicLiveSession",
12
+ "NovaSonicToolAdapter",
13
+ ]