|
| 1 | +# Agoraio Python Library |
| 2 | + |
| 3 | +[](https://buildwithfern.com?utm_source=github&utm_medium=github&utm_campaign=readme&utm_source=https%3A%2F%2Fgithub.com%2Ffern-demo%2Fagoraio-python-sdk) |
| 4 | +[](https://pypi.python.org/pypi/agoraio-sdk) |
| 5 | + |
| 6 | +The Agoraio Python library provides convenient access to the Agoraio APIs from Python. |
| 7 | + |
| 8 | +## Table of Contents |
| 9 | + |
| 10 | +- [Installation](#installation) |
| 11 | +- [Reference](#reference) |
| 12 | +- [Usage](#usage) |
| 13 | +- [Async Client](#async-client) |
| 14 | +- [Exception Handling](#exception-handling) |
| 15 | +- [Pagination](#pagination) |
| 16 | +- [Advanced](#advanced) |
| 17 | + - [Access Raw Response Data](#access-raw-response-data) |
| 18 | + - [Retries](#retries) |
| 19 | + - [Timeouts](#timeouts) |
| 20 | + - [Custom Client](#custom-client) |
| 21 | +- [Contributing](#contributing) |
| 22 | + |
| 23 | +## Installation |
| 24 | + |
| 25 | +```sh |
| 26 | +pip install agoraio-sdk |
| 27 | +``` |
| 28 | + |
| 29 | +## Reference |
| 30 | + |
| 31 | +A full reference for this library is available [here](https://github.com/fern-demo/agoraio-python-sdk/blob/HEAD/./reference.md). |
| 32 | + |
| 33 | +## Usage |
| 34 | + |
| 35 | +Instantiate and use the client with the following: |
| 36 | + |
| 37 | +```python |
| 38 | +from agoraio import Agora |
| 39 | +from agoraio.agent_management import ( |
| 40 | + StartAgentManagementRequestProperties, |
| 41 | + StartAgentManagementRequestPropertiesAdvancedFeatures, |
| 42 | + StartAgentManagementRequestPropertiesAsr, |
| 43 | + StartAgentManagementRequestPropertiesLlm, |
| 44 | + StartAgentManagementRequestPropertiesTts, |
| 45 | +) |
| 46 | + |
| 47 | +client = Agora( |
| 48 | + username="YOUR_USERNAME", |
| 49 | + password="YOUR_PASSWORD", |
| 50 | +) |
| 51 | +client.agent_management.start( |
| 52 | + appid="appid", |
| 53 | + name="unique_name", |
| 54 | + properties=StartAgentManagementRequestProperties( |
| 55 | + channel="channel_name", |
| 56 | + token="token", |
| 57 | + agent_rtc_uid="1001", |
| 58 | + remote_rtc_uids=["1002"], |
| 59 | + idle_timeout=120, |
| 60 | + advanced_features=StartAgentManagementRequestPropertiesAdvancedFeatures( |
| 61 | + enable_aivad=True, |
| 62 | + ), |
| 63 | + asr=StartAgentManagementRequestPropertiesAsr( |
| 64 | + language="en-US", |
| 65 | + ), |
| 66 | + tts=StartAgentManagementRequestPropertiesTts( |
| 67 | + vendor="microsoft", |
| 68 | + params={ |
| 69 | + "key": "<your_tts_api_key>", |
| 70 | + "region": "eastus", |
| 71 | + "voice_name": "en-US-AndrewMultilingualNeural", |
| 72 | + }, |
| 73 | + ), |
| 74 | + llm=StartAgentManagementRequestPropertiesLlm( |
| 75 | + url="https://api.openai.com/v1/chat/completions", |
| 76 | + api_key="<your_llm_key>", |
| 77 | + system_messages=[ |
| 78 | + {"role": "system", "content": "You are a helpful chatbot."} |
| 79 | + ], |
| 80 | + params={"model": "gpt-4o-mini"}, |
| 81 | + max_history=32, |
| 82 | + greeting_message="Hello, how can I assist you today?", |
| 83 | + failure_message="Please hold on a second.", |
| 84 | + ), |
| 85 | + ), |
| 86 | +) |
| 87 | +``` |
| 88 | + |
| 89 | +## Async Client |
| 90 | + |
| 91 | +The SDK also exports an `async` client so that you can make non-blocking calls to our API. Note that if you are constructing an Async httpx client class to pass into this client, use `httpx.AsyncClient()` instead of `httpx.Client()` (e.g. for the `httpx_client` parameter of this client). |
| 92 | + |
| 93 | +```python |
| 94 | +import asyncio |
| 95 | + |
| 96 | +from agoraio import AsyncAgora |
| 97 | +from agoraio.agent_management import ( |
| 98 | + StartAgentManagementRequestProperties, |
| 99 | + StartAgentManagementRequestPropertiesAdvancedFeatures, |
| 100 | + StartAgentManagementRequestPropertiesAsr, |
| 101 | + StartAgentManagementRequestPropertiesLlm, |
| 102 | + StartAgentManagementRequestPropertiesTts, |
| 103 | +) |
| 104 | + |
| 105 | +client = AsyncAgora( |
| 106 | + username="YOUR_USERNAME", |
| 107 | + password="YOUR_PASSWORD", |
| 108 | +) |
| 109 | + |
| 110 | + |
| 111 | +async def main() -> None: |
| 112 | + await client.agent_management.start( |
| 113 | + appid="appid", |
| 114 | + name="unique_name", |
| 115 | + properties=StartAgentManagementRequestProperties( |
| 116 | + channel="channel_name", |
| 117 | + token="token", |
| 118 | + agent_rtc_uid="1001", |
| 119 | + remote_rtc_uids=["1002"], |
| 120 | + idle_timeout=120, |
| 121 | + advanced_features=StartAgentManagementRequestPropertiesAdvancedFeatures( |
| 122 | + enable_aivad=True, |
| 123 | + ), |
| 124 | + asr=StartAgentManagementRequestPropertiesAsr( |
| 125 | + language="en-US", |
| 126 | + ), |
| 127 | + tts=StartAgentManagementRequestPropertiesTts( |
| 128 | + vendor="microsoft", |
| 129 | + params={ |
| 130 | + "key": "<your_tts_api_key>", |
| 131 | + "region": "eastus", |
| 132 | + "voice_name": "en-US-AndrewMultilingualNeural", |
| 133 | + }, |
| 134 | + ), |
| 135 | + llm=StartAgentManagementRequestPropertiesLlm( |
| 136 | + url="https://api.openai.com/v1/chat/completions", |
| 137 | + api_key="<your_llm_key>", |
| 138 | + system_messages=[ |
| 139 | + {"role": "system", "content": "You are a helpful chatbot."} |
| 140 | + ], |
| 141 | + params={"model": "gpt-4o-mini"}, |
| 142 | + max_history=32, |
| 143 | + greeting_message="Hello, how can I assist you today?", |
| 144 | + failure_message="Please hold on a second.", |
| 145 | + ), |
| 146 | + ), |
| 147 | + ) |
| 148 | + |
| 149 | + |
| 150 | +asyncio.run(main()) |
| 151 | +``` |
| 152 | + |
| 153 | +## Exception Handling |
| 154 | + |
| 155 | +When the API returns a non-success status code (4xx or 5xx response), a subclass of the following error |
| 156 | +will be thrown. |
| 157 | + |
| 158 | +```python |
| 159 | +from agoraio.core.api_error import ApiError |
| 160 | + |
| 161 | +try: |
| 162 | + client.agent_management.start(...) |
| 163 | +except ApiError as e: |
| 164 | + print(e.status_code) |
| 165 | + print(e.body) |
| 166 | +``` |
| 167 | + |
| 168 | +## Pagination |
| 169 | + |
| 170 | +Paginated requests will return a `SyncPager` or `AsyncPager`, which can be used as generators for the underlying object. |
| 171 | + |
| 172 | +```python |
| 173 | +from agoraio import Agora |
| 174 | + |
| 175 | +client = Agora( |
| 176 | + username="YOUR_USERNAME", |
| 177 | + password="YOUR_PASSWORD", |
| 178 | +) |
| 179 | +response = client.agent_management.list( |
| 180 | + appid="appid", |
| 181 | +) |
| 182 | +for item in response: |
| 183 | + yield item |
| 184 | +# alternatively, you can paginate page-by-page |
| 185 | +for page in response.iter_pages(): |
| 186 | + yield page |
| 187 | +``` |
| 188 | + |
| 189 | +```python |
| 190 | +# You can also iterate through pages and access the typed response per page |
| 191 | +pager = client.agent_management.list(...) |
| 192 | +for page in pager.iter_pages(): |
| 193 | + print(page.response) # access the typed response for each page |
| 194 | + for item in page: |
| 195 | + print(item) |
| 196 | +``` |
| 197 | + |
| 198 | +## Advanced |
| 199 | + |
| 200 | +### Access Raw Response Data |
| 201 | + |
| 202 | +The SDK provides access to raw response data, including headers, through the `.with_raw_response` property. |
| 203 | +The `.with_raw_response` property returns a "raw" client that can be used to access the `.headers` and `.data` attributes. |
| 204 | + |
| 205 | +```python |
| 206 | +from agoraio import Agora |
| 207 | + |
| 208 | +client = Agora( |
| 209 | + ..., |
| 210 | +) |
| 211 | +response = client.agent_management.with_raw_response.start(...) |
| 212 | +print(response.headers) # access the response headers |
| 213 | +print(response.data) # access the underlying object |
| 214 | +pager = client.agent_management.list(...) |
| 215 | +print(pager.response) # access the typed response for the first page |
| 216 | +for item in pager: |
| 217 | + print(item) # access the underlying object(s) |
| 218 | +for page in pager.iter_pages(): |
| 219 | + print(page.response) # access the typed response for each page |
| 220 | + for item in page: |
| 221 | + print(item) # access the underlying object(s) |
| 222 | +``` |
| 223 | + |
| 224 | +### Retries |
| 225 | + |
| 226 | +The SDK is instrumented with automatic retries with exponential backoff. A request will be retried as long |
| 227 | +as the request is deemed retryable and the number of retry attempts has not grown larger than the configured |
| 228 | +retry limit (default: 2). |
| 229 | + |
| 230 | +A request is deemed retryable when any of the following HTTP status codes is returned: |
| 231 | + |
| 232 | +- [408](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/408) (Timeout) |
| 233 | +- [429](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/429) (Too Many Requests) |
| 234 | +- [5XX](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/500) (Internal Server Errors) |
| 235 | + |
| 236 | +Use the `max_retries` request option to configure this behavior. |
| 237 | + |
| 238 | +```python |
| 239 | +client.agent_management.start(..., request_options={ |
| 240 | + "max_retries": 1 |
| 241 | +}) |
| 242 | +``` |
| 243 | + |
| 244 | +### Timeouts |
| 245 | + |
| 246 | +The SDK defaults to a 60 second timeout. You can configure this with a timeout option at the client or request level. |
| 247 | + |
| 248 | +```python |
| 249 | + |
| 250 | +from agoraio import Agora |
| 251 | + |
| 252 | +client = Agora( |
| 253 | + ..., |
| 254 | + timeout=20.0, |
| 255 | +) |
| 256 | + |
| 257 | + |
| 258 | +# Override timeout for a specific method |
| 259 | +client.agent_management.start(..., request_options={ |
| 260 | + "timeout_in_seconds": 1 |
| 261 | +}) |
| 262 | +``` |
| 263 | + |
| 264 | +### Custom Client |
| 265 | + |
| 266 | +You can override the `httpx` client to customize it for your use-case. Some common use-cases include support for proxies |
| 267 | +and transports. |
| 268 | + |
| 269 | +```python |
| 270 | +import httpx |
| 271 | +from agoraio import Agora |
| 272 | + |
| 273 | +client = Agora( |
| 274 | + ..., |
| 275 | + httpx_client=httpx.Client( |
| 276 | + proxy="http://my.test.proxy.example.com", |
| 277 | + transport=httpx.HTTPTransport(local_address="0.0.0.0"), |
| 278 | + ), |
| 279 | +) |
| 280 | +``` |
| 281 | + |
| 282 | +## Contributing |
| 283 | + |
| 284 | +While we value open-source contributions to this SDK, this library is generated programmatically. |
| 285 | +Additions made directly to this library would have to be moved over to our generation code, |
| 286 | +otherwise they would be overwritten upon the next generated release. Feel free to open a PR as |
| 287 | +a proof of concept, but know that we will not be able to merge it as-is. We suggest opening |
| 288 | +an issue first to discuss with us! |
| 289 | + |
| 290 | +On the other hand, contributions to the README are always very welcome! |
0 commit comments