livekit-plugins-aws 0.1.1__tar.gz → 1.0.0.dev5__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.
Potentially problematic release.
This version of livekit-plugins-aws might be problematic. Click here for more details.
- livekit_plugins_aws-1.0.0.dev5/.gitignore +168 -0
- {livekit_plugins_aws-0.1.1 → livekit_plugins_aws-1.0.0.dev5}/PKG-INFO +13 -23
- livekit_plugins_aws-1.0.0.dev5/livekit/plugins/aws/llm.py +270 -0
- {livekit_plugins_aws-0.1.1 → livekit_plugins_aws-1.0.0.dev5}/livekit/plugins/aws/stt.py +34 -53
- {livekit_plugins_aws-0.1.1 → livekit_plugins_aws-1.0.0.dev5}/livekit/plugins/aws/tts.py +8 -7
- livekit_plugins_aws-1.0.0.dev5/livekit/plugins/aws/utils.py +135 -0
- {livekit_plugins_aws-0.1.1 → livekit_plugins_aws-1.0.0.dev5}/livekit/plugins/aws/version.py +1 -1
- livekit_plugins_aws-1.0.0.dev5/pyproject.toml +44 -0
- livekit_plugins_aws-0.1.1/livekit/plugins/aws/_utils.py +0 -216
- livekit_plugins_aws-0.1.1/livekit/plugins/aws/llm.py +0 -350
- livekit_plugins_aws-0.1.1/livekit_plugins_aws.egg-info/PKG-INFO +0 -53
- livekit_plugins_aws-0.1.1/livekit_plugins_aws.egg-info/SOURCES.txt +0 -17
- livekit_plugins_aws-0.1.1/livekit_plugins_aws.egg-info/dependency_links.txt +0 -1
- livekit_plugins_aws-0.1.1/livekit_plugins_aws.egg-info/requires.txt +0 -4
- livekit_plugins_aws-0.1.1/livekit_plugins_aws.egg-info/top_level.txt +0 -1
- livekit_plugins_aws-0.1.1/pyproject.toml +0 -3
- livekit_plugins_aws-0.1.1/setup.cfg +0 -4
- livekit_plugins_aws-0.1.1/setup.py +0 -61
- {livekit_plugins_aws-0.1.1 → livekit_plugins_aws-1.0.0.dev5}/README.md +0 -0
- {livekit_plugins_aws-0.1.1 → livekit_plugins_aws-1.0.0.dev5}/livekit/plugins/aws/__init__.py +0 -0
- {livekit_plugins_aws-0.1.1 → livekit_plugins_aws-1.0.0.dev5}/livekit/plugins/aws/log.py +0 -0
- {livekit_plugins_aws-0.1.1 → livekit_plugins_aws-1.0.0.dev5}/livekit/plugins/aws/models.py +0 -0
- {livekit_plugins_aws-0.1.1 → livekit_plugins_aws-1.0.0.dev5}/livekit/plugins/aws/py.typed +0 -0
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
**/.vscode
|
|
2
|
+
**/.DS_Store
|
|
3
|
+
|
|
4
|
+
# Byte-compiled / optimized / DLL files
|
|
5
|
+
__pycache__/
|
|
6
|
+
*.py[cod]
|
|
7
|
+
*$py.class
|
|
8
|
+
|
|
9
|
+
# C extensions
|
|
10
|
+
*.so
|
|
11
|
+
|
|
12
|
+
# Distribution / packaging
|
|
13
|
+
.Python
|
|
14
|
+
build/
|
|
15
|
+
develop-eggs/
|
|
16
|
+
dist/
|
|
17
|
+
downloads/
|
|
18
|
+
eggs/
|
|
19
|
+
.eggs/
|
|
20
|
+
lib/
|
|
21
|
+
lib64/
|
|
22
|
+
parts/
|
|
23
|
+
sdist/
|
|
24
|
+
var/
|
|
25
|
+
wheels/
|
|
26
|
+
share/python-wheels/
|
|
27
|
+
*.egg-info/
|
|
28
|
+
.installed.cfg
|
|
29
|
+
*.egg
|
|
30
|
+
MANIFEST
|
|
31
|
+
|
|
32
|
+
# PyInstaller
|
|
33
|
+
# Usually these files are written by a python script from a template
|
|
34
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
35
|
+
*.manifest
|
|
36
|
+
*.spec
|
|
37
|
+
|
|
38
|
+
# Installer logs
|
|
39
|
+
pip-log.txt
|
|
40
|
+
pip-delete-this-directory.txt
|
|
41
|
+
|
|
42
|
+
# Unit test / coverage reports
|
|
43
|
+
htmlcov/
|
|
44
|
+
.tox/
|
|
45
|
+
.nox/
|
|
46
|
+
.coverage
|
|
47
|
+
.coverage.*
|
|
48
|
+
.cache
|
|
49
|
+
nosetests.xml
|
|
50
|
+
coverage.xml
|
|
51
|
+
*.cover
|
|
52
|
+
*.py,cover
|
|
53
|
+
.hypothesis/
|
|
54
|
+
.pytest_cache/
|
|
55
|
+
cover/
|
|
56
|
+
|
|
57
|
+
# Translations
|
|
58
|
+
*.mo
|
|
59
|
+
*.pot
|
|
60
|
+
|
|
61
|
+
# Django stuff:
|
|
62
|
+
*.log
|
|
63
|
+
local_settings.py
|
|
64
|
+
db.sqlite3
|
|
65
|
+
db.sqlite3-journal
|
|
66
|
+
|
|
67
|
+
# Flask stuff:
|
|
68
|
+
instance/
|
|
69
|
+
.webassets-cache
|
|
70
|
+
|
|
71
|
+
# Scrapy stuff:
|
|
72
|
+
.scrapy
|
|
73
|
+
|
|
74
|
+
# Sphinx documentation
|
|
75
|
+
docs/_build/
|
|
76
|
+
|
|
77
|
+
# PyBuilder
|
|
78
|
+
.pybuilder/
|
|
79
|
+
target/
|
|
80
|
+
|
|
81
|
+
# Jupyter Notebook
|
|
82
|
+
.ipynb_checkpoints
|
|
83
|
+
|
|
84
|
+
# IPython
|
|
85
|
+
profile_default/
|
|
86
|
+
ipython_config.py
|
|
87
|
+
|
|
88
|
+
# pyenv
|
|
89
|
+
# For a library or package, you might want to ignore these files since the code is
|
|
90
|
+
# intended to run in multiple environments; otherwise, check them in:
|
|
91
|
+
# .python-version
|
|
92
|
+
|
|
93
|
+
# pipenv
|
|
94
|
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
95
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
96
|
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
97
|
+
# install all needed dependencies.
|
|
98
|
+
#Pipfile.lock
|
|
99
|
+
|
|
100
|
+
# poetry
|
|
101
|
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
|
102
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
103
|
+
# commonly ignored for libraries.
|
|
104
|
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
|
105
|
+
#poetry.lock
|
|
106
|
+
|
|
107
|
+
# pdm
|
|
108
|
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
|
109
|
+
#pdm.lock
|
|
110
|
+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
|
111
|
+
# in version control.
|
|
112
|
+
# https://pdm.fming.dev/#use-with-ide
|
|
113
|
+
.pdm.toml
|
|
114
|
+
|
|
115
|
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
|
116
|
+
__pypackages__/
|
|
117
|
+
|
|
118
|
+
# Celery stuff
|
|
119
|
+
celerybeat-schedule
|
|
120
|
+
celerybeat.pid
|
|
121
|
+
|
|
122
|
+
# SageMath parsed files
|
|
123
|
+
*.sage.py
|
|
124
|
+
|
|
125
|
+
# Environments
|
|
126
|
+
.env
|
|
127
|
+
.venv
|
|
128
|
+
env/
|
|
129
|
+
venv/
|
|
130
|
+
ENV/
|
|
131
|
+
env.bak/
|
|
132
|
+
venv.bak/
|
|
133
|
+
|
|
134
|
+
# Spyder project settings
|
|
135
|
+
.spyderproject
|
|
136
|
+
.spyproject
|
|
137
|
+
|
|
138
|
+
# Rope project settings
|
|
139
|
+
.ropeproject
|
|
140
|
+
|
|
141
|
+
# mkdocs documentation
|
|
142
|
+
/site
|
|
143
|
+
|
|
144
|
+
# mypy
|
|
145
|
+
.mypy_cache/
|
|
146
|
+
.dmypy.json
|
|
147
|
+
dmypy.json
|
|
148
|
+
|
|
149
|
+
# Pyre type checker
|
|
150
|
+
.pyre/
|
|
151
|
+
|
|
152
|
+
# pytype static type analyzer
|
|
153
|
+
.pytype/
|
|
154
|
+
|
|
155
|
+
# Cython debug symbols
|
|
156
|
+
cython_debug/
|
|
157
|
+
|
|
158
|
+
# PyCharm
|
|
159
|
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
|
160
|
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
|
161
|
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
|
162
|
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
|
163
|
+
.idea/
|
|
164
|
+
|
|
165
|
+
node_modules
|
|
166
|
+
|
|
167
|
+
credentials.json
|
|
168
|
+
pyrightconfig.json
|
|
@@ -1,38 +1,28 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
2
|
Name: livekit-plugins-aws
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 1.0.0.dev5
|
|
4
4
|
Summary: LiveKit Agents Plugin for services from AWS
|
|
5
|
-
Home-page: https://github.com/livekit/agents
|
|
6
|
-
License: Apache-2.0
|
|
7
5
|
Project-URL: Documentation, https://docs.livekit.io
|
|
8
6
|
Project-URL: Website, https://livekit.io/
|
|
9
7
|
Project-URL: Source, https://github.com/livekit/agents
|
|
10
|
-
|
|
8
|
+
Author-email: LiveKit <support@livekit.io>
|
|
9
|
+
License-Expression: Apache-2.0
|
|
10
|
+
Keywords: audio,aws,livekit,realtime,video,webrtc
|
|
11
11
|
Classifier: Intended Audience :: Developers
|
|
12
12
|
Classifier: License :: OSI Approved :: Apache Software License
|
|
13
|
-
Classifier: Topic :: Multimedia :: Sound/Audio
|
|
14
|
-
Classifier: Topic :: Multimedia :: Video
|
|
15
|
-
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
16
13
|
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
17
15
|
Classifier: Programming Language :: Python :: 3.9
|
|
18
16
|
Classifier: Programming Language :: Python :: 3.10
|
|
19
|
-
Classifier:
|
|
17
|
+
Classifier: Topic :: Multimedia :: Sound/Audio
|
|
18
|
+
Classifier: Topic :: Multimedia :: Video
|
|
19
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
20
20
|
Requires-Python: >=3.9.0
|
|
21
|
-
Description-Content-Type: text/markdown
|
|
22
|
-
Requires-Dist: livekit-agents[codecs]<1.0.0,>=0.12.16
|
|
23
21
|
Requires-Dist: aiobotocore==2.19.0
|
|
24
|
-
Requires-Dist: boto3==1.36.3
|
|
25
22
|
Requires-Dist: amazon-transcribe>=0.6.2
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
Dynamic: home-page
|
|
30
|
-
Dynamic: keywords
|
|
31
|
-
Dynamic: license
|
|
32
|
-
Dynamic: project-url
|
|
33
|
-
Dynamic: requires-dist
|
|
34
|
-
Dynamic: requires-python
|
|
35
|
-
Dynamic: summary
|
|
23
|
+
Requires-Dist: boto3==1.36.3
|
|
24
|
+
Requires-Dist: livekit-agents>=1.0.0.dev5
|
|
25
|
+
Description-Content-Type: text/markdown
|
|
36
26
|
|
|
37
27
|
# LiveKit Plugins AWS
|
|
38
28
|
|
|
@@ -50,4 +40,4 @@ pip install livekit-plugins-aws
|
|
|
50
40
|
|
|
51
41
|
## Pre-requisites
|
|
52
42
|
|
|
53
|
-
You'll need to specify an AWS Access Key and a Deployment Region. They can be set as environment variables: `AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY` and `AWS_DEFAULT_REGION`, respectively.
|
|
43
|
+
You'll need to specify an AWS Access Key and a Deployment Region. They can be set as environment variables: `AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY` and `AWS_DEFAULT_REGION`, respectively.
|
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
# Copyright 2023 LiveKit, Inc.
|
|
2
|
+
#
|
|
3
|
+
|
|
4
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
# you may not use this file except in compliance with the License.
|
|
6
|
+
# You may obtain a copy of the License at
|
|
7
|
+
#
|
|
8
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
#
|
|
10
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
# See the License for the specific language governing permissions and
|
|
14
|
+
# limitations under the License.
|
|
15
|
+
from __future__ import annotations
|
|
16
|
+
|
|
17
|
+
import asyncio
|
|
18
|
+
import os
|
|
19
|
+
from dataclasses import dataclass
|
|
20
|
+
from typing import Any, Literal
|
|
21
|
+
|
|
22
|
+
import boto3
|
|
23
|
+
|
|
24
|
+
from livekit.agents import APIConnectionError, APIStatusError, llm
|
|
25
|
+
from livekit.agents.llm import ChatContext, FunctionTool, FunctionToolCall, ToolChoice
|
|
26
|
+
from livekit.agents.types import (
|
|
27
|
+
DEFAULT_API_CONNECT_OPTIONS,
|
|
28
|
+
NOT_GIVEN,
|
|
29
|
+
APIConnectOptions,
|
|
30
|
+
NotGivenOr,
|
|
31
|
+
)
|
|
32
|
+
from livekit.agents.utils import is_given
|
|
33
|
+
|
|
34
|
+
from .log import logger
|
|
35
|
+
from .utils import get_aws_credentials, to_chat_ctx, to_fnc_ctx
|
|
36
|
+
|
|
37
|
+
TEXT_MODEL = Literal["anthropic.claude-3-5-sonnet-20241022-v2:0"]
|
|
38
|
+
DEFAULT_REGION = "us-east-1"
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
@dataclass
|
|
42
|
+
class _LLMOptions:
|
|
43
|
+
model: str | TEXT_MODEL
|
|
44
|
+
temperature: NotGivenOr[float]
|
|
45
|
+
tool_choice: NotGivenOr[ToolChoice | Literal["auto", "required", "none"]]
|
|
46
|
+
max_output_tokens: NotGivenOr[int]
|
|
47
|
+
top_p: NotGivenOr[float]
|
|
48
|
+
additional_request_fields: NotGivenOr[dict[str, Any]]
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
class LLM(llm.LLM):
|
|
52
|
+
def __init__(
|
|
53
|
+
self,
|
|
54
|
+
*,
|
|
55
|
+
model: NotGivenOr[str | TEXT_MODEL] = NOT_GIVEN,
|
|
56
|
+
api_key: NotGivenOr[str] = NOT_GIVEN,
|
|
57
|
+
api_secret: NotGivenOr[str] = NOT_GIVEN,
|
|
58
|
+
region: NotGivenOr[str] = NOT_GIVEN,
|
|
59
|
+
temperature: NotGivenOr[float] = NOT_GIVEN,
|
|
60
|
+
max_output_tokens: NotGivenOr[int] = NOT_GIVEN,
|
|
61
|
+
top_p: NotGivenOr[float] = NOT_GIVEN,
|
|
62
|
+
tool_choice: NotGivenOr[ToolChoice | Literal["auto", "required", "none"]] = NOT_GIVEN,
|
|
63
|
+
additional_request_fields: NotGivenOr[dict[str, Any]] = NOT_GIVEN,
|
|
64
|
+
) -> None:
|
|
65
|
+
"""
|
|
66
|
+
Create a new instance of AWS Bedrock LLM.
|
|
67
|
+
|
|
68
|
+
``api_key`` and ``api_secret`` must be set to your AWS Access key id and secret access key, either using the argument or by setting the
|
|
69
|
+
``AWS_ACCESS_KEY_ID`` and ``AWS_SECRET_ACCESS_KEY`` environmental variables.
|
|
70
|
+
|
|
71
|
+
See https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/bedrock-runtime/client/converse_stream.html for more details on the AWS Bedrock Runtime API.
|
|
72
|
+
|
|
73
|
+
Args:
|
|
74
|
+
model (TEXT_MODEL, optional): model or inference profile arn to use(https://docs.aws.amazon.com/bedrock/latest/userguide/inference-profiles-use.html). Defaults to 'anthropic.claude-3-5-sonnet-20240620-v1:0'.
|
|
75
|
+
api_key(str, optional): AWS access key id.
|
|
76
|
+
api_secret(str, optional): AWS secret access key
|
|
77
|
+
region (str, optional): The region to use for AWS API requests. Defaults value is "us-east-1".
|
|
78
|
+
temperature (float, optional): Sampling temperature for response generation. Defaults to 0.8.
|
|
79
|
+
max_output_tokens (int, optional): Maximum number of tokens to generate in the output. Defaults to None.
|
|
80
|
+
top_p (float, optional): The nucleus sampling probability for response generation. Defaults to None.
|
|
81
|
+
tool_choice (ToolChoice or Literal["auto", "required", "none"], optional): Specifies whether to use tools during response generation. Defaults to "auto".
|
|
82
|
+
additional_request_fields (dict[str, Any], optional): Additional request fields to send to the AWS Bedrock Converse API. Defaults to None.
|
|
83
|
+
"""
|
|
84
|
+
super().__init__()
|
|
85
|
+
self._api_key, self._api_secret, self._region = get_aws_credentials(
|
|
86
|
+
api_key, api_secret, region
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
model = model or os.environ.get("BEDROCK_INFERENCE_PROFILE_ARN")
|
|
90
|
+
if not is_given(model):
|
|
91
|
+
raise ValueError(
|
|
92
|
+
"model or inference profile arn must be set using the argument or by setting the BEDROCK_INFERENCE_PROFILE_ARN environment variable."
|
|
93
|
+
)
|
|
94
|
+
self._opts = _LLMOptions(
|
|
95
|
+
model=model,
|
|
96
|
+
temperature=temperature,
|
|
97
|
+
tool_choice=tool_choice,
|
|
98
|
+
max_output_tokens=max_output_tokens,
|
|
99
|
+
top_p=top_p,
|
|
100
|
+
additional_request_fields=additional_request_fields,
|
|
101
|
+
)
|
|
102
|
+
|
|
103
|
+
def chat(
|
|
104
|
+
self,
|
|
105
|
+
*,
|
|
106
|
+
chat_ctx: ChatContext,
|
|
107
|
+
tools: list[FunctionTool] | None = None,
|
|
108
|
+
conn_options: APIConnectOptions = DEFAULT_API_CONNECT_OPTIONS,
|
|
109
|
+
temperature: NotGivenOr[float] = NOT_GIVEN,
|
|
110
|
+
tool_choice: NotGivenOr[ToolChoice | Literal["auto", "required", "none"]] = NOT_GIVEN,
|
|
111
|
+
) -> LLMStream:
|
|
112
|
+
opts = {}
|
|
113
|
+
|
|
114
|
+
if is_given(self._opts.model):
|
|
115
|
+
opts["modelId"] = self._opts.model
|
|
116
|
+
|
|
117
|
+
def _get_tool_config() -> dict[str, Any] | None:
|
|
118
|
+
nonlocal tool_choice
|
|
119
|
+
|
|
120
|
+
if not tools:
|
|
121
|
+
return None
|
|
122
|
+
|
|
123
|
+
tool_config: dict[str, Any] = {"tools": to_fnc_ctx(tools)}
|
|
124
|
+
tool_choice = tool_choice if is_given(tool_choice) else self._opts.tool_choice
|
|
125
|
+
if is_given(tool_choice):
|
|
126
|
+
if isinstance(tool_choice, ToolChoice):
|
|
127
|
+
tool_config["toolChoice"] = {"tool": {"name": tool_choice.name}}
|
|
128
|
+
elif tool_choice == "required":
|
|
129
|
+
tool_config["toolChoice"] = {"any": {}}
|
|
130
|
+
elif tool_choice == "auto":
|
|
131
|
+
tool_config["toolChoice"] = {"auto": {}}
|
|
132
|
+
else:
|
|
133
|
+
return None
|
|
134
|
+
|
|
135
|
+
return tool_config
|
|
136
|
+
|
|
137
|
+
tool_config = _get_tool_config()
|
|
138
|
+
if tool_config:
|
|
139
|
+
opts["toolConfig"] = tool_config
|
|
140
|
+
messages, system_message = to_chat_ctx(chat_ctx, id(self))
|
|
141
|
+
opts["messages"] = messages
|
|
142
|
+
if system_message:
|
|
143
|
+
opts["system"] = [system_message]
|
|
144
|
+
|
|
145
|
+
inference_config = {}
|
|
146
|
+
if is_given(self._opts.max_output_tokens):
|
|
147
|
+
inference_config["maxTokens"] = self._opts.max_output_tokens
|
|
148
|
+
temperature = temperature if is_given(temperature) else self._opts.temperature
|
|
149
|
+
if is_given(temperature):
|
|
150
|
+
inference_config["temperature"] = temperature
|
|
151
|
+
if is_given(self._opts.top_p):
|
|
152
|
+
inference_config["topP"] = self._opts.top_p
|
|
153
|
+
|
|
154
|
+
opts["inferenceConfig"] = inference_config
|
|
155
|
+
if is_given(self._opts.additional_request_fields):
|
|
156
|
+
opts["additionalModelRequestFields"] = self._opts.additional_request_fields
|
|
157
|
+
|
|
158
|
+
return LLMStream(
|
|
159
|
+
self,
|
|
160
|
+
aws_access_key_id=self._api_key,
|
|
161
|
+
aws_secret_access_key=self._api_secret,
|
|
162
|
+
region_name=self._region,
|
|
163
|
+
chat_ctx=chat_ctx,
|
|
164
|
+
tools=tools,
|
|
165
|
+
conn_options=conn_options,
|
|
166
|
+
extra_kwargs=opts,
|
|
167
|
+
)
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
class LLMStream(llm.LLMStream):
|
|
171
|
+
def __init__(
|
|
172
|
+
self,
|
|
173
|
+
llm: LLM,
|
|
174
|
+
*,
|
|
175
|
+
aws_access_key_id: str,
|
|
176
|
+
aws_secret_access_key: str,
|
|
177
|
+
region_name: str,
|
|
178
|
+
chat_ctx: ChatContext,
|
|
179
|
+
conn_options: APIConnectOptions,
|
|
180
|
+
tools: list[FunctionTool] | None,
|
|
181
|
+
extra_kwargs: dict[str, Any],
|
|
182
|
+
) -> None:
|
|
183
|
+
super().__init__(llm, chat_ctx=chat_ctx, tools=tools, conn_options=conn_options)
|
|
184
|
+
self._client = boto3.client(
|
|
185
|
+
"bedrock-runtime",
|
|
186
|
+
region_name=region_name,
|
|
187
|
+
aws_access_key_id=aws_access_key_id,
|
|
188
|
+
aws_secret_access_key=aws_secret_access_key,
|
|
189
|
+
)
|
|
190
|
+
self._llm: LLM = llm
|
|
191
|
+
self._opts = extra_kwargs
|
|
192
|
+
|
|
193
|
+
self._tool_call_id: str | None = None
|
|
194
|
+
self._fnc_name: str | None = None
|
|
195
|
+
self._fnc_raw_arguments: str | None = None
|
|
196
|
+
self._text: str = ""
|
|
197
|
+
|
|
198
|
+
async def _run(self) -> None:
|
|
199
|
+
retryable = True
|
|
200
|
+
try:
|
|
201
|
+
response = self._client.converse_stream(**self._opts) # type: ignore
|
|
202
|
+
request_id = response["ResponseMetadata"]["RequestId"]
|
|
203
|
+
if response["ResponseMetadata"]["HTTPStatusCode"] != 200:
|
|
204
|
+
raise APIStatusError(
|
|
205
|
+
f"aws bedrock llm: error generating content: {response}",
|
|
206
|
+
retryable=False,
|
|
207
|
+
request_id=request_id,
|
|
208
|
+
)
|
|
209
|
+
|
|
210
|
+
for chunk in response["stream"]:
|
|
211
|
+
chat_chunk = self._parse_chunk(request_id, chunk)
|
|
212
|
+
if chat_chunk is not None:
|
|
213
|
+
retryable = False
|
|
214
|
+
self._event_ch.send_nowait(chat_chunk)
|
|
215
|
+
|
|
216
|
+
# Let other coroutines run
|
|
217
|
+
await asyncio.sleep(0)
|
|
218
|
+
|
|
219
|
+
except Exception as e:
|
|
220
|
+
raise APIConnectionError(
|
|
221
|
+
f"aws bedrock llm: error generating content: {e}",
|
|
222
|
+
retryable=retryable,
|
|
223
|
+
) from e
|
|
224
|
+
|
|
225
|
+
def _parse_chunk(self, request_id: str, chunk: dict) -> llm.ChatChunk | None:
|
|
226
|
+
if "contentBlockStart" in chunk:
|
|
227
|
+
tool_use = chunk["contentBlockStart"]["start"]["toolUse"]
|
|
228
|
+
self._tool_call_id = tool_use["toolUseId"]
|
|
229
|
+
self._fnc_name = tool_use["name"]
|
|
230
|
+
self._fnc_raw_arguments = ""
|
|
231
|
+
elif "contentBlockDelta" in chunk:
|
|
232
|
+
delta = chunk["contentBlockDelta"]["delta"]
|
|
233
|
+
if "toolUse" in delta:
|
|
234
|
+
self._fnc_raw_arguments += delta["toolUse"]["input"]
|
|
235
|
+
elif "text" in delta:
|
|
236
|
+
self._text += delta["text"]
|
|
237
|
+
elif "contentBlockStop" in chunk:
|
|
238
|
+
if self._text:
|
|
239
|
+
chat_chunk = llm.ChatChunk(
|
|
240
|
+
id=request_id,
|
|
241
|
+
delta=llm.ChoiceDelta(content=self._text, role="assistant"),
|
|
242
|
+
)
|
|
243
|
+
self._text = ""
|
|
244
|
+
return chat_chunk
|
|
245
|
+
elif self._tool_call_id:
|
|
246
|
+
if self._tool_call_id is None:
|
|
247
|
+
logger.warning("aws bedrock llm: no tool call id in the response")
|
|
248
|
+
return None
|
|
249
|
+
if self._fnc_name is None:
|
|
250
|
+
logger.warning("aws bedrock llm: no function name in the response")
|
|
251
|
+
return None
|
|
252
|
+
if self._fnc_raw_arguments is None:
|
|
253
|
+
logger.warning("aws bedrock llm: no function arguments in the response")
|
|
254
|
+
return None
|
|
255
|
+
chat_chunk = llm.ChatChunk(
|
|
256
|
+
id=request_id,
|
|
257
|
+
delta=llm.ChoiceDelta(
|
|
258
|
+
role="assistant",
|
|
259
|
+
tool_calls=[
|
|
260
|
+
FunctionToolCall(
|
|
261
|
+
arguments=self._fnc_raw_arguments,
|
|
262
|
+
name=self._fnc_name,
|
|
263
|
+
call_id=self._tool_call_id,
|
|
264
|
+
),
|
|
265
|
+
],
|
|
266
|
+
),
|
|
267
|
+
)
|
|
268
|
+
self._tool_call_id = self._fnc_name = self._fnc_raw_arguments = None
|
|
269
|
+
return chat_chunk
|
|
270
|
+
return None
|
|
@@ -14,20 +14,15 @@ from __future__ import annotations
|
|
|
14
14
|
|
|
15
15
|
import asyncio
|
|
16
16
|
from dataclasses import dataclass
|
|
17
|
-
from typing import Optional
|
|
18
17
|
|
|
19
18
|
from amazon_transcribe.client import TranscribeStreamingClient
|
|
20
19
|
from amazon_transcribe.model import Result, TranscriptEvent
|
|
20
|
+
|
|
21
21
|
from livekit import rtc
|
|
22
|
-
from livekit.agents import
|
|
23
|
-
|
|
24
|
-
APIConnectOptions,
|
|
25
|
-
stt,
|
|
26
|
-
utils,
|
|
27
|
-
)
|
|
28
|
-
|
|
29
|
-
from ._utils import _get_aws_credentials
|
|
22
|
+
from livekit.agents import DEFAULT_API_CONNECT_OPTIONS, APIConnectOptions, stt, utils
|
|
23
|
+
|
|
30
24
|
from .log import logger
|
|
25
|
+
from .utils import get_aws_credentials
|
|
31
26
|
|
|
32
27
|
|
|
33
28
|
@dataclass
|
|
@@ -36,16 +31,16 @@ class STTOptions:
|
|
|
36
31
|
sample_rate: int
|
|
37
32
|
language: str
|
|
38
33
|
encoding: str
|
|
39
|
-
vocabulary_name:
|
|
40
|
-
session_id:
|
|
41
|
-
vocab_filter_method:
|
|
42
|
-
vocab_filter_name:
|
|
43
|
-
show_speaker_label:
|
|
44
|
-
enable_channel_identification:
|
|
45
|
-
number_of_channels:
|
|
46
|
-
enable_partial_results_stabilization:
|
|
47
|
-
partial_results_stability:
|
|
48
|
-
language_model_name:
|
|
34
|
+
vocabulary_name: str | None
|
|
35
|
+
session_id: str | None
|
|
36
|
+
vocab_filter_method: str | None
|
|
37
|
+
vocab_filter_name: str | None
|
|
38
|
+
show_speaker_label: bool | None
|
|
39
|
+
enable_channel_identification: bool | None
|
|
40
|
+
number_of_channels: int | None
|
|
41
|
+
enable_partial_results_stabilization: bool | None
|
|
42
|
+
partial_results_stability: str | None
|
|
43
|
+
language_model_name: str | None
|
|
49
44
|
|
|
50
45
|
|
|
51
46
|
class STT(stt.STT):
|
|
@@ -58,26 +53,24 @@ class STT(stt.STT):
|
|
|
58
53
|
sample_rate: int = 48000,
|
|
59
54
|
language: str = "en-US",
|
|
60
55
|
encoding: str = "pcm",
|
|
61
|
-
vocabulary_name:
|
|
62
|
-
session_id:
|
|
63
|
-
vocab_filter_method:
|
|
64
|
-
vocab_filter_name:
|
|
65
|
-
show_speaker_label:
|
|
66
|
-
enable_channel_identification:
|
|
67
|
-
number_of_channels:
|
|
68
|
-
enable_partial_results_stabilization:
|
|
69
|
-
partial_results_stability:
|
|
70
|
-
language_model_name:
|
|
56
|
+
vocabulary_name: str | None = None,
|
|
57
|
+
session_id: str | None = None,
|
|
58
|
+
vocab_filter_method: str | None = None,
|
|
59
|
+
vocab_filter_name: str | None = None,
|
|
60
|
+
show_speaker_label: bool | None = None,
|
|
61
|
+
enable_channel_identification: bool | None = None,
|
|
62
|
+
number_of_channels: int | None = None,
|
|
63
|
+
enable_partial_results_stabilization: bool | None = None,
|
|
64
|
+
partial_results_stability: str | None = None,
|
|
65
|
+
language_model_name: str | None = None,
|
|
71
66
|
):
|
|
72
|
-
super().__init__(
|
|
73
|
-
capabilities=stt.STTCapabilities(streaming=True, interim_results=True)
|
|
74
|
-
)
|
|
67
|
+
super().__init__(capabilities=stt.STTCapabilities(streaming=True, interim_results=True))
|
|
75
68
|
|
|
76
|
-
self._api_key, self._api_secret =
|
|
69
|
+
self._api_key, self._api_secret, self._speech_region = get_aws_credentials(
|
|
77
70
|
api_key, api_secret, speech_region
|
|
78
71
|
)
|
|
79
72
|
self._config = STTOptions(
|
|
80
|
-
speech_region=
|
|
73
|
+
speech_region=self._speech_region,
|
|
81
74
|
language=language,
|
|
82
75
|
sample_rate=sample_rate,
|
|
83
76
|
encoding=encoding,
|
|
@@ -100,16 +93,14 @@ class STT(stt.STT):
|
|
|
100
93
|
language: str | None,
|
|
101
94
|
conn_options: APIConnectOptions,
|
|
102
95
|
) -> stt.SpeechEvent:
|
|
103
|
-
raise NotImplementedError(
|
|
104
|
-
"Amazon Transcribe does not support single frame recognition"
|
|
105
|
-
)
|
|
96
|
+
raise NotImplementedError("Amazon Transcribe does not support single frame recognition")
|
|
106
97
|
|
|
107
98
|
def stream(
|
|
108
99
|
self,
|
|
109
100
|
*,
|
|
110
101
|
language: str | None = None,
|
|
111
102
|
conn_options: APIConnectOptions = DEFAULT_API_CONNECT_OPTIONS,
|
|
112
|
-
) ->
|
|
103
|
+
) -> SpeechStream:
|
|
113
104
|
return SpeechStream(
|
|
114
105
|
stt=self,
|
|
115
106
|
conn_options=conn_options,
|
|
@@ -124,9 +115,7 @@ class SpeechStream(stt.SpeechStream):
|
|
|
124
115
|
opts: STTOptions,
|
|
125
116
|
conn_options: APIConnectOptions = DEFAULT_API_CONNECT_OPTIONS,
|
|
126
117
|
) -> None:
|
|
127
|
-
super().__init__(
|
|
128
|
-
stt=stt, conn_options=conn_options, sample_rate=opts.sample_rate
|
|
129
|
-
)
|
|
118
|
+
super().__init__(stt=stt, conn_options=conn_options, sample_rate=opts.sample_rate)
|
|
130
119
|
self._opts = opts
|
|
131
120
|
self._client = TranscribeStreamingClient(region=self._opts.speech_region)
|
|
132
121
|
|
|
@@ -151,9 +140,7 @@ class SpeechStream(stt.SpeechStream):
|
|
|
151
140
|
async def input_generator():
|
|
152
141
|
async for frame in self._input_ch:
|
|
153
142
|
if isinstance(frame, rtc.AudioFrame):
|
|
154
|
-
await stream.input_stream.send_audio_event(
|
|
155
|
-
audio_chunk=frame.data.tobytes()
|
|
156
|
-
)
|
|
143
|
+
await stream.input_stream.send_audio_event(audio_chunk=frame.data.tobytes())
|
|
157
144
|
await stream.input_stream.end_stream()
|
|
158
145
|
|
|
159
146
|
@utils.log_exceptions(logger=logger)
|
|
@@ -184,9 +171,7 @@ class SpeechStream(stt.SpeechStream):
|
|
|
184
171
|
self._event_ch.send_nowait(
|
|
185
172
|
stt.SpeechEvent(
|
|
186
173
|
type=stt.SpeechEventType.INTERIM_TRANSCRIPT,
|
|
187
|
-
alternatives=[
|
|
188
|
-
_streaming_recognize_response_to_speech_data(resp)
|
|
189
|
-
],
|
|
174
|
+
alternatives=[_streaming_recognize_response_to_speech_data(resp)],
|
|
190
175
|
)
|
|
191
176
|
)
|
|
192
177
|
|
|
@@ -194,16 +179,12 @@ class SpeechStream(stt.SpeechStream):
|
|
|
194
179
|
self._event_ch.send_nowait(
|
|
195
180
|
stt.SpeechEvent(
|
|
196
181
|
type=stt.SpeechEventType.FINAL_TRANSCRIPT,
|
|
197
|
-
alternatives=[
|
|
198
|
-
_streaming_recognize_response_to_speech_data(resp)
|
|
199
|
-
],
|
|
182
|
+
alternatives=[_streaming_recognize_response_to_speech_data(resp)],
|
|
200
183
|
)
|
|
201
184
|
)
|
|
202
185
|
|
|
203
186
|
if not resp.is_partial:
|
|
204
|
-
self._event_ch.send_nowait(
|
|
205
|
-
stt.SpeechEvent(type=stt.SpeechEventType.END_OF_SPEECH)
|
|
206
|
-
)
|
|
187
|
+
self._event_ch.send_nowait(stt.SpeechEvent(type=stt.SpeechEventType.END_OF_SPEECH))
|
|
207
188
|
|
|
208
189
|
|
|
209
190
|
def _streaming_recognize_response_to_speech_data(resp: Result) -> stt.SpeechData:
|