Skip to content

Commit ab823bf

Browse files
authored
Add asyncio event loop (#616)
* Add asyncio event loop * address feedback * fix
1 parent 3de53a6 commit ab823bf

3 files changed

Lines changed: 494 additions & 59 deletions

File tree

README.md

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,103 @@ async def print_models() -> None:
156156
asyncio.run(print_models())
157157
```
158158

159+
## Conversational AI
160+
161+
Build interactive AI agents with real-time audio capabilities using ElevenLabs Conversational AI.
162+
163+
### Basic Usage
164+
165+
```python
166+
from elevenlabs.client import ElevenLabs
167+
from elevenlabs.conversational_ai.conversation import Conversation, ClientTools
168+
from elevenlabs.conversational_ai.default_audio_interface import DefaultAudioInterface
169+
170+
client = ElevenLabs(api_key="YOUR_API_KEY")
171+
172+
# Create audio interface for real-time audio input/output
173+
audio_interface = DefaultAudioInterface()
174+
175+
# Create conversation
176+
conversation = Conversation(
177+
client=client,
178+
agent_id="your-agent-id",
179+
requires_auth=True,
180+
audio_interface=audio_interface,
181+
)
182+
183+
# Start the conversation
184+
conversation.start_session()
185+
186+
# The conversation runs in background until you call:
187+
conversation.end_session()
188+
```
189+
190+
### Custom Event Loop Support
191+
192+
For advanced use cases involving context propagation, resource reuse, or specific event loop management, `ClientTools` supports custom asyncio event loops:
193+
194+
```python
195+
import asyncio
196+
from elevenlabs.conversational_ai.conversation import ClientTools
197+
198+
async def main():
199+
# Get the current event loop
200+
custom_loop = asyncio.get_running_loop()
201+
202+
# Create ClientTools with custom loop to prevent "different event loop" errors
203+
client_tools = ClientTools(loop=custom_loop)
204+
205+
# Register your tools
206+
async def get_weather(params):
207+
location = params.get("location", "Unknown")
208+
# Your async logic here
209+
return f"Weather in {location}: Sunny, 72°F"
210+
211+
client_tools.register("get_weather", get_weather, is_async=True)
212+
213+
# Use with conversation
214+
conversation = Conversation(
215+
client=client,
216+
agent_id="your-agent-id",
217+
requires_auth=True,
218+
audio_interface=audio_interface,
219+
client_tools=client_tools
220+
)
221+
222+
asyncio.run(main())
223+
```
224+
225+
**Benefits of Custom Event Loop:**
226+
- **Context Propagation**: Maintain request-scoped state across async operations
227+
- **Resource Reuse**: Share existing async resources like HTTP sessions or database pools
228+
- **Loop Management**: Prevent "Task got Future attached to a different event loop" errors
229+
- **Performance**: Better control over async task scheduling and execution
230+
231+
**Important:** When using a custom loop, you're responsible for its lifecycle
232+
Don't close the loop while ClientTools are still using it.
233+
234+
### Tool Registration
235+
236+
Register custom tools that the AI agent can call during conversations:
237+
238+
```python
239+
client_tools = ClientTools()
240+
241+
# Sync tool
242+
def calculate_sum(params):
243+
numbers = params.get("numbers", [])
244+
return sum(numbers)
245+
246+
# Async tool
247+
async def fetch_data(params):
248+
url = params.get("url")
249+
# Your async HTTP request logic
250+
return {"data": "fetched"}
251+
252+
client_tools.register("calculate_sum", calculate_sum, is_async=False)
253+
client_tools.register("fetch_data", fetch_data, is_async=True)
254+
```
255+
159256
## Languages Supported
160257

161258
Explore [all models & languages](https://elevenlabs.io/docs/models).

0 commit comments

Comments
 (0)