livekit-plugins-microsoft-ai 1.8.4__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.
- livekit/plugins/microsoft_ai/__init__.py +41 -0
- livekit/plugins/microsoft_ai/_http.py +144 -0
- livekit/plugins/microsoft_ai/log.py +19 -0
- livekit/plugins/microsoft_ai/py.typed +0 -0
- livekit/plugins/microsoft_ai/stt.py +674 -0
- livekit/plugins/microsoft_ai/tts.py +317 -0
- livekit/plugins/microsoft_ai/version.py +17 -0
- livekit_plugins_microsoft_ai-1.8.4.dist-info/METADATA +497 -0
- livekit_plugins_microsoft_ai-1.8.4.dist-info/RECORD +12 -0
- livekit_plugins_microsoft_ai-1.8.4.dist-info/WHEEL +4 -0
- livekit_plugins_microsoft_ai-1.8.4.dist-info/licenses/LICENSE +201 -0
- livekit_plugins_microsoft_ai-1.8.4.dist-info/licenses/NOTICE +18 -0
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# Copyright 2023 LiveKit, Inc.
|
|
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.
|
|
14
|
+
#
|
|
15
|
+
# Adapted from the OpenAI plugin for the Microsoft AI speech-only integration.
|
|
16
|
+
|
|
17
|
+
"""Microsoft AI STT and TTS using explicit, deployment-specific endpoints.
|
|
18
|
+
|
|
19
|
+
TTS uses Azure Speech; STT uses the transcription WebSocket API. See the package
|
|
20
|
+
README for supported contracts and the limits of the bounded live smoke coverage.
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
from .stt import STT
|
|
24
|
+
from .tts import TTS
|
|
25
|
+
from .version import __version__
|
|
26
|
+
|
|
27
|
+
__all__ = ["STT", "TTS", "__version__"]
|
|
28
|
+
|
|
29
|
+
from livekit.agents import Plugin
|
|
30
|
+
|
|
31
|
+
from .log import logger
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
class MicrosoftAIPlugin(Plugin):
|
|
35
|
+
def __init__(self) -> None:
|
|
36
|
+
super().__init__(__name__, __version__, __package__, logger)
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
Plugin.register_plugin(MicrosoftAIPlugin())
|
|
40
|
+
|
|
41
|
+
__pdoc__ = {name: False for name in dir() if name not in __all__}
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
# Copyright 2023 LiveKit, Inc.
|
|
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.
|
|
14
|
+
|
|
15
|
+
from __future__ import annotations
|
|
16
|
+
|
|
17
|
+
import math
|
|
18
|
+
import os
|
|
19
|
+
from collections.abc import Mapping
|
|
20
|
+
from pathlib import Path
|
|
21
|
+
from typing import Literal
|
|
22
|
+
from urllib.parse import urlsplit
|
|
23
|
+
|
|
24
|
+
import aiohttp
|
|
25
|
+
from dotenv import dotenv_values
|
|
26
|
+
|
|
27
|
+
from livekit.agents import APIError, APIStatusError
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class Configuration:
|
|
31
|
+
"""Read only an explicitly selected dotenv file, without changing process environment."""
|
|
32
|
+
|
|
33
|
+
def __init__(self, env_file: str | Path | None) -> None:
|
|
34
|
+
path = env_file if env_file is not None else os.environ.get("MICROSOFT_AI_ENV_FILE")
|
|
35
|
+
self._values: dict[str, str | None] = {}
|
|
36
|
+
if path is not None:
|
|
37
|
+
try:
|
|
38
|
+
with open(path, encoding="utf-8") as file:
|
|
39
|
+
self._values = dict(dotenv_values(stream=file, interpolate=False))
|
|
40
|
+
except (OSError, UnicodeError):
|
|
41
|
+
raise ValueError(
|
|
42
|
+
"Could not read the selected Microsoft AI environment file"
|
|
43
|
+
) from None
|
|
44
|
+
|
|
45
|
+
def get(self, name: str) -> str | None:
|
|
46
|
+
return os.environ.get(name, self._values.get(name))
|
|
47
|
+
|
|
48
|
+
def required(self, value: str | None, name: str) -> str:
|
|
49
|
+
resolved = value if value is not None else self.get(name)
|
|
50
|
+
if resolved is None or not resolved.strip():
|
|
51
|
+
raise ValueError(f"Set {name} or pass its constructor argument")
|
|
52
|
+
return resolved
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def positive_timeout(value: float, name: str) -> None:
|
|
56
|
+
if not math.isfinite(value) or value <= 0:
|
|
57
|
+
raise ValueError(f"{name} must be finite and greater than zero")
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def status_error(service: str, status: int) -> APIError:
|
|
61
|
+
# Core TTS treats 499 as local cancellation. A remote HTTP 499 is still a failure.
|
|
62
|
+
if status == 499:
|
|
63
|
+
return APIError(f"Microsoft AI {service} returned HTTP 499", retryable=False)
|
|
64
|
+
return APIStatusError(
|
|
65
|
+
f"Microsoft AI {service} request failed",
|
|
66
|
+
status_code=status,
|
|
67
|
+
retryable=status in (408, 429) or 500 <= status < 600,
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
class HTTPClient:
|
|
72
|
+
"""Own a lazy session, or borrow the caller's session without closing it."""
|
|
73
|
+
|
|
74
|
+
def __init__(
|
|
75
|
+
self,
|
|
76
|
+
*,
|
|
77
|
+
config: Configuration,
|
|
78
|
+
service: str,
|
|
79
|
+
url: str | None,
|
|
80
|
+
api_key: str | None,
|
|
81
|
+
headers: Mapping[str, str] | None,
|
|
82
|
+
http_session: aiohttp.ClientSession | None,
|
|
83
|
+
auth_header: Literal["Authorization", "api-key"] | None = None,
|
|
84
|
+
) -> None:
|
|
85
|
+
self.url = config.required(url, f"MICROSOFT_AI_{service}_URL")
|
|
86
|
+
parsed = urlsplit(self.url)
|
|
87
|
+
secure, local = ("wss", "ws") if service == "STT" else ("https", "http")
|
|
88
|
+
if (
|
|
89
|
+
parsed.scheme not in (secure, local)
|
|
90
|
+
or not parsed.hostname
|
|
91
|
+
or parsed.username is not None
|
|
92
|
+
or parsed.password is not None
|
|
93
|
+
or parsed.fragment
|
|
94
|
+
):
|
|
95
|
+
raise ValueError(f"MICROSOFT_AI_{service}_URL must be a full {secure} endpoint URL")
|
|
96
|
+
if parsed.scheme == local and parsed.hostname not in ("localhost", "127.0.0.1", "::1"):
|
|
97
|
+
raise ValueError(f"Microsoft AI {service} requires TLS except on loopback endpoints")
|
|
98
|
+
|
|
99
|
+
if headers is not None:
|
|
100
|
+
if api_key is not None:
|
|
101
|
+
raise ValueError("Pass either api_key or headers, not both")
|
|
102
|
+
if auth_header is not None:
|
|
103
|
+
raise ValueError("Pass either auth_header or headers, not both")
|
|
104
|
+
# An explicit mapping, including {}, deliberately bypasses API-key environment lookup.
|
|
105
|
+
self.headers = dict(headers)
|
|
106
|
+
else:
|
|
107
|
+
selected_header = "Ocp-Apim-Subscription-Key"
|
|
108
|
+
if service == "STT":
|
|
109
|
+
configured_header = (
|
|
110
|
+
auth_header
|
|
111
|
+
if auth_header is not None
|
|
112
|
+
else config.get("MICROSOFT_AI_STT_AUTH_HEADER")
|
|
113
|
+
)
|
|
114
|
+
selected_header = (
|
|
115
|
+
configured_header if configured_header is not None else "Authorization"
|
|
116
|
+
)
|
|
117
|
+
if selected_header not in ("Authorization", "api-key"):
|
|
118
|
+
raise ValueError(
|
|
119
|
+
"MICROSOFT_AI_STT_AUTH_HEADER/auth_header must be Authorization or api-key"
|
|
120
|
+
)
|
|
121
|
+
key = config.required(api_key, f"MICROSOFT_AI_{service}_API_KEY")
|
|
122
|
+
if any(ord(char) < 32 or ord(char) == 127 for char in key):
|
|
123
|
+
raise ValueError(
|
|
124
|
+
f"MICROSOFT_AI_{service}_API_KEY cannot contain control characters"
|
|
125
|
+
)
|
|
126
|
+
self.headers = {
|
|
127
|
+
selected_header: f"Bearer {key}" if selected_header == "Authorization" else key
|
|
128
|
+
}
|
|
129
|
+
self.headers.setdefault("User-Agent", "LiveKit Agents")
|
|
130
|
+
self._session = http_session
|
|
131
|
+
self._owns_session = http_session is None
|
|
132
|
+
self._closed = False
|
|
133
|
+
|
|
134
|
+
def session(self) -> aiohttp.ClientSession:
|
|
135
|
+
if self._closed:
|
|
136
|
+
raise RuntimeError("Microsoft AI provider is closed")
|
|
137
|
+
if self._session is None:
|
|
138
|
+
self._session = aiohttp.ClientSession()
|
|
139
|
+
return self._session
|
|
140
|
+
|
|
141
|
+
async def aclose(self) -> None:
|
|
142
|
+
self._closed = True
|
|
143
|
+
if self._owns_session and self._session is not None:
|
|
144
|
+
await self._session.close()
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# Copyright 2023 LiveKit, Inc.
|
|
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.
|
|
14
|
+
#
|
|
15
|
+
# Adapted from the OpenAI plugin for the Microsoft AI speech-only integration.
|
|
16
|
+
|
|
17
|
+
import logging
|
|
18
|
+
|
|
19
|
+
logger = logging.getLogger("livekit.plugins.microsoft_ai")
|
|
File without changes
|