livekit-plugins-addisai 0.1.0__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,209 @@
1
+ # Byte-compiled / optimized / DLL files
2
+ __pycache__/
3
+ *.py[codz]
4
+ *$py.class
5
+
6
+ # C extensions
7
+ *.so
8
+
9
+ # Distribution / packaging
10
+ .Python
11
+ build/
12
+ develop-eggs/
13
+ dist/
14
+ downloads/
15
+ eggs/
16
+ .eggs/
17
+ lib/
18
+ lib64/
19
+ parts/
20
+ sdist/
21
+ var/
22
+ wheels/
23
+ share/python-wheels/
24
+ *.egg-info/
25
+ .installed.cfg
26
+ *.egg
27
+ MANIFEST
28
+
29
+ # PyInstaller
30
+ # Usually these files are written by a python script from a template
31
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
32
+ *.manifest
33
+ *.spec
34
+
35
+ # Installer logs
36
+ pip-log.txt
37
+ pip-delete-this-directory.txt
38
+
39
+ # Unit test / coverage reports
40
+ htmlcov/
41
+ .tox/
42
+ .nox/
43
+ .coverage
44
+ .coverage.*
45
+ .cache
46
+ nosetests.xml
47
+ coverage.xml
48
+ *.cover
49
+ *.py.cover
50
+ .hypothesis/
51
+ .pytest_cache/
52
+ cover/
53
+
54
+ # Translations
55
+ *.mo
56
+ *.pot
57
+
58
+ # Django stuff:
59
+ *.log
60
+ local_settings.py
61
+ db.sqlite3
62
+ db.sqlite3-journal
63
+
64
+ # Flask stuff:
65
+ instance/
66
+ .webassets-cache
67
+
68
+ # Scrapy stuff:
69
+ .scrapy
70
+
71
+ # Sphinx documentation
72
+ docs/_build/
73
+
74
+ # PyBuilder
75
+ .pybuilder/
76
+ target/
77
+
78
+ # Jupyter Notebook
79
+ .ipynb_checkpoints
80
+
81
+ # IPython
82
+ profile_default/
83
+ ipython_config.py
84
+
85
+ # pyenv
86
+ # For a library or package, you might want to ignore these files since the code is
87
+ # intended to run in multiple environments; otherwise, check them in:
88
+ # .python-version
89
+
90
+ # pipenv
91
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
93
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
94
+ # install all needed dependencies.
95
+ #Pipfile.lock
96
+
97
+ # UV
98
+ # Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
99
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
100
+ # commonly ignored for libraries.
101
+ #uv.lock
102
+
103
+ # poetry
104
+ # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
105
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
106
+ # commonly ignored for libraries.
107
+ # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
108
+ #poetry.lock
109
+ #poetry.toml
110
+
111
+ # pdm
112
+ # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
113
+ # pdm recommends including project-wide configuration in pdm.toml, but excluding .pdm-python.
114
+ # https://pdm-project.org/en/latest/usage/project/#working-with-version-control
115
+ #pdm.lock
116
+ #pdm.toml
117
+ .pdm-python
118
+ .pdm-build/
119
+
120
+ # pixi
121
+ # Similar to Pipfile.lock, it is generally recommended to include pixi.lock in version control.
122
+ #pixi.lock
123
+ # Pixi creates a virtual environment in the .pixi directory, just like venv module creates one
124
+ # in the .venv directory. It is recommended not to include this directory in version control.
125
+ .pixi
126
+
127
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
128
+ __pypackages__/
129
+
130
+ # Celery stuff
131
+ celerybeat-schedule
132
+ celerybeat.pid
133
+
134
+ # SageMath parsed files
135
+ *.sage.py
136
+
137
+ # Environments
138
+ .env
139
+ .envrc
140
+ .venv
141
+ env/
142
+ venv/
143
+ ENV/
144
+ env.bak/
145
+ venv.bak/
146
+
147
+ # Spyder project settings
148
+ .spyderproject
149
+ .spyproject
150
+
151
+ # Rope project settings
152
+ .ropeproject
153
+
154
+ # mkdocs documentation
155
+ /site
156
+
157
+ # mypy
158
+ .mypy_cache/
159
+ .dmypy.json
160
+ dmypy.json
161
+
162
+ # Pyre type checker
163
+ .pyre/
164
+
165
+ # pytype static type analyzer
166
+ .pytype/
167
+
168
+ # Cython debug symbols
169
+ cython_debug/
170
+
171
+ # PyCharm
172
+ # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
173
+ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
174
+ # and can be added to the global gitignore or merged into this file. For a more nuclear
175
+ # option (not recommended) you can uncomment the following to ignore the entire idea folder.
176
+ #.idea/
177
+
178
+ # Abstra
179
+ # Abstra is an AI-powered process automation framework.
180
+ # Ignore directories containing user credentials, local state, and settings.
181
+ # Learn more at https://abstra.io/docs
182
+ .abstra/
183
+
184
+ # Visual Studio Code
185
+ # Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore
186
+ # that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
187
+ # and can be added to the global gitignore or merged into this file. However, if you prefer,
188
+ # you could uncomment the following to ignore the entire vscode folder
189
+ # .vscode/
190
+
191
+ # Ruff stuff:
192
+ .ruff_cache/
193
+
194
+ # PyPI configuration file
195
+ .pypirc
196
+
197
+ # Cursor
198
+ # Cursor is an AI-powered code editor. `.cursorignore` specifies files/directories to
199
+ # exclude from AI features like autocomplete and code analysis. Recommended for sensitive data
200
+ # refer to https://docs.cursor.com/context/ignore-files
201
+ .cursorignore
202
+ .cursorindexingignore
203
+
204
+ # Marimo
205
+ marimo/_static/
206
+ marimo/_lsp/
207
+ __marimo__/
208
+
209
+ notes.md
@@ -0,0 +1 @@
1
+ 3.11
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Dagmawi Solomon Bedane
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,129 @@
1
+ Metadata-Version: 2.5
2
+ Name: livekit-plugins-addisai
3
+ Version: 0.1.0
4
+ Summary: AddisAI STT and TTS plugin for LiveKit Agents
5
+ Project-URL: Homepage, https://github.com/DagmawiSolomon/livekit-plugins-addisai.git
6
+ License: MIT License
7
+
8
+ Copyright (c) 2026 Dagmawi Solomon Bedane
9
+
10
+ Permission is hereby granted, free of charge, to any person obtaining a copy
11
+ of this software and associated documentation files (the "Software"), to deal
12
+ in the Software without restriction, including without limitation the rights
13
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14
+ copies of the Software, and to permit persons to whom the Software is
15
+ furnished to do so, subject to the following conditions:
16
+
17
+ The above copyright notice and this permission notice shall be included in all
18
+ copies or substantial portions of the Software.
19
+
20
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26
+ SOFTWARE.
27
+ License-File: LICENSE
28
+ Requires-Python: <3.15,>=3.10
29
+ Requires-Dist: httpx>=0.28.1
30
+ Requires-Dist: livekit-agents>=1.5.1
31
+ Requires-Dist: miniaudio>=1.2
32
+ Description-Content-Type: text/markdown
33
+
34
+ # AddisAI LiveKit Plugins
35
+
36
+ > [!CAUTION]
37
+ > **This project is currently under active development.**
38
+ > It is not yet optimized for performance or hardened for production use. Expect potential bugs and lack of fault tolerance.
39
+
40
+ This repository provides LiveKit plugins for [AddisAI](https://addisai.ch/)'s STT (Speech-to-Text) and TTS (Text-to-Speech)
41
+
42
+ ## Setup Guide
43
+
44
+ Follow these steps to set up the AddisAI plugin with a LiveKit.
45
+
46
+ ### 1. Prerequisites
47
+
48
+ - [Python 3.10+](https://www.python.org/downloads/)
49
+ - [uv](https://github.com/astral-sh/uv) (recommended for fast package management)
50
+ ### 2. Clone the Repositories
51
+
52
+ First, clone this plugin repository:
53
+
54
+ ```bash
55
+ git clone https://github.com/DagmawiSolomon/livekit-plugins-addisai
56
+ ```
57
+
58
+ Then, clone the LiveKit Agent Starter (Python) repository into a separate folder:
59
+
60
+ ```bash
61
+ git clone https://github.com/livekit-examples/agent-starter-python
62
+ ```
63
+
64
+ ### 3. Environment Setup
65
+
66
+ Navigate to the `agent-starter-python` directory
67
+
68
+ ```bash
69
+ cd agent-starter-python
70
+ ```
71
+
72
+ Install the dependencies:
73
+
74
+ ```bash
75
+ uv sync
76
+ ```
77
+
78
+ ### 4. Plugin Installation
79
+
80
+ Add the `livekit-plugins-addisai` package to your project by providing the path to your local clone:
81
+
82
+ ```bash
83
+ uv add ../livekit-plugins-addisai
84
+ ```
85
+
86
+ ### 5. Configuration
87
+
88
+ Create a `.env.local` file in the `agent-starter-python` directory and add your credentials:
89
+
90
+ > [!IMPORTANT]
91
+ > Use `.env.local` as the filename or ensure you update the `load_dotenv` call in `src/agent.py`, as the starter repo is configured to load `.env.local` by default.
92
+
93
+ ```env
94
+ LIVEKIT_URL=<your-livekit-url>
95
+ LIVEKIT_API_KEY=<your-api-key>
96
+ LIVEKIT_API_SECRET=<your-api-secret>
97
+
98
+ # AddisAI Credentials
99
+ ADDISAI_API_KEY=<your-addisai-api-key>
100
+ ```
101
+
102
+ ### 6. Quick Start Example
103
+
104
+ You can use the AddisAI plugin in your agent by importing it and configuring it within your `AgentSession`:
105
+
106
+ ```python
107
+ from livekit.plugins import addisai
108
+ from livekit.agents import AgentSession
109
+
110
+ # ... inside your agent worker (e.g., my_agent function) ...
111
+ async def my_agent(ctx: JobContext):
112
+ # Set up the AgentSession with AddisAI components for Amharic
113
+ session = AgentSession(
114
+ # AddisAI STT for Amharic
115
+ stt=addisai.STT(language="am"),
116
+ llm=inference.LLM(model="openai/gpt-4.1-mini"),
117
+ # AddisAI TTS for Amharic
118
+ tts=addisai.TTS(language="am"),
119
+ )
120
+
121
+ # Start the session with your Assistant agent
122
+ await session.start(agent=Assistant(), room=ctx.room)
123
+ ```
124
+
125
+ Run your agent:
126
+
127
+ ```bash
128
+ uv run .\src\agent.py console
129
+ ```
@@ -0,0 +1,96 @@
1
+ # AddisAI LiveKit Plugins
2
+
3
+ > [!CAUTION]
4
+ > **This project is currently under active development.**
5
+ > It is not yet optimized for performance or hardened for production use. Expect potential bugs and lack of fault tolerance.
6
+
7
+ This repository provides LiveKit plugins for [AddisAI](https://addisai.ch/)'s STT (Speech-to-Text) and TTS (Text-to-Speech)
8
+
9
+ ## Setup Guide
10
+
11
+ Follow these steps to set up the AddisAI plugin with a LiveKit.
12
+
13
+ ### 1. Prerequisites
14
+
15
+ - [Python 3.10+](https://www.python.org/downloads/)
16
+ - [uv](https://github.com/astral-sh/uv) (recommended for fast package management)
17
+ ### 2. Clone the Repositories
18
+
19
+ First, clone this plugin repository:
20
+
21
+ ```bash
22
+ git clone https://github.com/DagmawiSolomon/livekit-plugins-addisai
23
+ ```
24
+
25
+ Then, clone the LiveKit Agent Starter (Python) repository into a separate folder:
26
+
27
+ ```bash
28
+ git clone https://github.com/livekit-examples/agent-starter-python
29
+ ```
30
+
31
+ ### 3. Environment Setup
32
+
33
+ Navigate to the `agent-starter-python` directory
34
+
35
+ ```bash
36
+ cd agent-starter-python
37
+ ```
38
+
39
+ Install the dependencies:
40
+
41
+ ```bash
42
+ uv sync
43
+ ```
44
+
45
+ ### 4. Plugin Installation
46
+
47
+ Add the `livekit-plugins-addisai` package to your project by providing the path to your local clone:
48
+
49
+ ```bash
50
+ uv add ../livekit-plugins-addisai
51
+ ```
52
+
53
+ ### 5. Configuration
54
+
55
+ Create a `.env.local` file in the `agent-starter-python` directory and add your credentials:
56
+
57
+ > [!IMPORTANT]
58
+ > Use `.env.local` as the filename or ensure you update the `load_dotenv` call in `src/agent.py`, as the starter repo is configured to load `.env.local` by default.
59
+
60
+ ```env
61
+ LIVEKIT_URL=<your-livekit-url>
62
+ LIVEKIT_API_KEY=<your-api-key>
63
+ LIVEKIT_API_SECRET=<your-api-secret>
64
+
65
+ # AddisAI Credentials
66
+ ADDISAI_API_KEY=<your-addisai-api-key>
67
+ ```
68
+
69
+ ### 6. Quick Start Example
70
+
71
+ You can use the AddisAI plugin in your agent by importing it and configuring it within your `AgentSession`:
72
+
73
+ ```python
74
+ from livekit.plugins import addisai
75
+ from livekit.agents import AgentSession
76
+
77
+ # ... inside your agent worker (e.g., my_agent function) ...
78
+ async def my_agent(ctx: JobContext):
79
+ # Set up the AgentSession with AddisAI components for Amharic
80
+ session = AgentSession(
81
+ # AddisAI STT for Amharic
82
+ stt=addisai.STT(language="am"),
83
+ llm=inference.LLM(model="openai/gpt-4.1-mini"),
84
+ # AddisAI TTS for Amharic
85
+ tts=addisai.TTS(language="am"),
86
+ )
87
+
88
+ # Start the session with your Assistant agent
89
+ await session.start(agent=Assistant(), room=ctx.room)
90
+ ```
91
+
92
+ Run your agent:
93
+
94
+ ```bash
95
+ uv run .\src\agent.py console
96
+ ```
@@ -0,0 +1,5 @@
1
+ from .stt import STT
2
+ from .tts import TTS
3
+
4
+ __all__ = ["STT","TTS"]
5
+
@@ -0,0 +1,13 @@
1
+ import httpx
2
+
3
+ API_BASE_URL = "https://api.addisassistant.com"
4
+ RETRIABLE_STATUS_CODES = {
5
+ httpx.codes.REQUEST_TIMEOUT,
6
+ httpx.codes.TOO_MANY_REQUESTS,
7
+ httpx.codes.INTERNAL_SERVER_ERROR,
8
+ httpx.codes.BAD_GATEWAY,
9
+ httpx.codes.SERVICE_UNAVAILABLE,
10
+ 522,
11
+ 524,
12
+ }
13
+
@@ -0,0 +1,221 @@
1
+
2
+
3
+ import os
4
+ import json
5
+ import logging
6
+ from typing import Any
7
+ from dataclasses import dataclass
8
+
9
+ import httpx
10
+
11
+ from enum import Enum
12
+ from livekit.agents import llm
13
+ from livekit.agents.llm import ChatContext, Tool, ToolChoice, ChatChunk, ChoiceDelta, ChatRole
14
+ from livekit.agents.types import DEFAULT_API_CONNECT_OPTIONS, APIConnectOptions, NOT_GIVEN, NotGivenOr
15
+ from livekit.agents.utils import is_given
16
+ from livekit.agents._exceptions import APIError
17
+ from livekit.agents.llm import LLMStream as BaseLLMStream
18
+
19
+ from .constants import API_BASE_URL
20
+
21
+ DEFAULT_LLM_URL = f"{API_BASE_URL}/api/v1/chat_generate"
22
+
23
+ logger = logging.getLogger(__name__)
24
+
25
+
26
+ class ADDIS_AI_LLM_LANGUAGES(str, Enum):
27
+ AM = "am"
28
+ OM = "om"
29
+
30
+ @dataclass(frozen=True)
31
+ class LLMOptions:
32
+ api_key: str
33
+ base_url: str
34
+ language: ADDIS_AI_LLM_LANGUAGES
35
+
36
+ temperature: float | None
37
+ top_p: float | None
38
+ top_k: int | None
39
+ max_output_tokens: int | None
40
+
41
+ class LLM(llm.LLM):
42
+ def __init__(
43
+ self,
44
+ *,
45
+ language: ADDIS_AI_LLM_LANGUAGES,
46
+ base_url: str = DEFAULT_LLM_URL,
47
+ api_key: NotGivenOr[str] = NOT_GIVEN,
48
+ generation_config: dict,
49
+ client: httpx.AsyncClient | None
50
+ ):
51
+ addisai_api_key = api_key if is_given(api_key) else os.environ.get("ADDISAI_API_KEY")
52
+ if not addisai_api_key:
53
+ raise ValueError(
54
+ "AddisAI API key is required, either as argument or set "
55
+ "ADDISAI_API_KEY environment variable"
56
+ )
57
+
58
+ self._opts = LLMOptions(
59
+ api_key= addisai_api_key,
60
+ language=ADDIS_AI_LLM_LANGUAGES(language),
61
+ base_url=base_url,
62
+ **generation_config
63
+ )
64
+
65
+ self._owns_client = client is None
66
+ self._client = client or httpx.AsyncClient()
67
+
68
+
69
+ @property
70
+ def model(self) -> str:
71
+ return "Addis-፩-አሌፍ"
72
+
73
+ @property
74
+ def provider(self) -> str:
75
+ return "AddisAI"
76
+
77
+
78
+
79
+
80
+ def chat(
81
+ self,
82
+ *,
83
+ chat_ctx: ChatContext,
84
+ tools: list[Tool] | None = None,
85
+ conn_options: APIConnectOptions = DEFAULT_API_CONNECT_OPTIONS,
86
+ parallel_tool_calls: NotGivenOr[bool] = NOT_GIVEN,
87
+ tool_choice: NotGivenOr[ToolChoice] = NOT_GIVEN,
88
+ extra_kwargs: NotGivenOr[dict[str, Any]] = NOT_GIVEN,
89
+ ) -> "LLMStream":
90
+ return LLMStream(
91
+ llm=self,
92
+ chat_ctx=chat_ctx,
93
+ tools=tools or [],
94
+ conn_options=conn_options,
95
+ )
96
+
97
+ async def aclose(self):
98
+ if self._owns_client:
99
+ await self._client.aclose()
100
+
101
+
102
+ class LLMStream(BaseLLMStream):
103
+ def __init__(
104
+ self,
105
+ llm: LLM,
106
+ *,
107
+ chat_ctx: ChatContext,
108
+ tools: list[Tool],
109
+ conn_options: APIConnectOptions,
110
+ ):
111
+ super().__init__(
112
+ llm=llm, chat_ctx=chat_ctx, tools=tools, conn_options=conn_options
113
+ )
114
+
115
+ async def _run(self) -> None:
116
+ llm: LLM = self._llm # type: ignore
117
+ opts = llm._opts
118
+ chat_ctx = self._chat_ctx
119
+
120
+ conversation_history = []
121
+ for item in chat_ctx.items[:-1]:
122
+ role = "assistant" if item.role == "assistant" else "user"
123
+ content = item.text_content if hasattr(item, "text_content") else str(item)
124
+ conversation_history.append({
125
+ "role": role,
126
+ "content": content,
127
+ })
128
+
129
+ if not chat_ctx.items:
130
+ raise ValueError("chat_ctx must have at least one item")
131
+
132
+ last_item = chat_ctx.items[-1]
133
+ prompt = last_item.text_content if hasattr(last_item, "text_content") else str(last_item)
134
+
135
+ generation_config = {}
136
+ if opts.temperature is not None:
137
+ generation_config["temperature"] = opts.temperature
138
+ if opts.max_output_tokens is not None:
139
+ generation_config["maxOutputTokens"] = opts.max_output_tokens
140
+ if opts.top_p is not None:
141
+ generation_config["topP"] = opts.top_p
142
+ if opts.top_k is not None:
143
+ generation_config["topK"] = opts.top_k
144
+
145
+ data = {
146
+ "prompt": prompt,
147
+ "target_language": opts.language.value,
148
+ }
149
+
150
+ if conversation_history:
151
+ data["conversation_history"] = conversation_history
152
+
153
+ if generation_config:
154
+ data["generation_config"] = generation_config
155
+
156
+ headers = {
157
+ "X-API-Key": opts.api_key,
158
+ "Content-Type": "application/json",
159
+ }
160
+
161
+ logger.info(
162
+ "llm_request",
163
+ extra={
164
+ "provider": llm.provider,
165
+ "model": llm.model,
166
+ "timeout": self._conn_options.timeout
167
+ }
168
+ )
169
+
170
+ try:
171
+ response = await llm._client.post(
172
+ opts.base_url,
173
+ json=data,
174
+ headers=headers,
175
+ timeout=self._conn_options.timeout
176
+ )
177
+ response.raise_for_status()
178
+
179
+ logger.info(
180
+ "llm_response_received",
181
+ extra={
182
+ "provider": llm.provider,
183
+ "model": llm.model,
184
+ "status": response.status_code,
185
+ },
186
+ )
187
+
188
+ response_data = response.json()
189
+ content = response_data.get("response_text", "")
190
+
191
+ usage_metadata = response_data.get("usage_metadata", {})
192
+ from livekit.agents.llm import CompletionUsage
193
+ usage = CompletionUsage(
194
+ completion_tokens=usage_metadata.get("candidates_token_count", 0),
195
+ prompt_tokens=usage_metadata.get("prompt_token_count", 0),
196
+ total_tokens=usage_metadata.get("total_token_count", 0),
197
+ ) if usage_metadata else None
198
+
199
+ request_id = httpx.utils.to_bytes(os.urandom(8)).hex()
200
+
201
+ self._event_ch.send_nowait(
202
+ ChatChunk(
203
+ id=request_id,
204
+ delta=ChoiceDelta(
205
+ role=ChatRole.ASSISTANT,
206
+ content=content,
207
+ ),
208
+ usage=usage,
209
+ )
210
+ )
211
+
212
+ except (httpx.TimeoutException, httpx.ConnectError, httpx.NetworkError) as e:
213
+ logger.warning(
214
+ "llm_network_error",
215
+ extra={
216
+ "provider": llm.provider,
217
+ "model": llm.model,
218
+ },
219
+ exc_info=e,
220
+ )
221
+ raise APIError("Network error contacting LLM provider") from e