File size: 2,334 Bytes
9410704 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | import os
import time
import asyncio
import pytest
from elevenlabs import ElevenLabs
from elevenlabs.conversational_ai.conversation import Conversation, ClientTools
from elevenlabs.conversational_ai.default_audio_interface import DefaultAudioInterface
@pytest.mark.skipif(os.getenv("CI") == "true", reason="Skip live conversation test in CI environment")
def test_live_conversation():
"""Test a live conversation with actual audio I/O"""
api_key = os.getenv("ELEVENLABS_API_KEY")
if not api_key:
raise ValueError("ELEVENLABS_API_KEY environment variable missing.")
agent_id = os.getenv("AGENT_ID")
if not api_key or not agent_id:
raise ValueError("AGENT_ID environment variable missing.")
client = ElevenLabs(api_key=api_key)
# Create conversation handlers
def on_agent_response(text: str):
print(f"Agent: {text}")
def on_user_transcript(text: str):
print(f"You: {text}")
def on_latency(ms: int):
print(f"Latency: {ms}ms")
# Initialize client tools
client_tools = ClientTools()
def test(parameters):
print("Sync tool called with parameters:", parameters)
return "Tool called successfully"
async def test_async(parameters):
# Simulate some async work
await asyncio.sleep(10)
print("Async tool called with parameters:", parameters)
return "Tool called successfully"
client_tools.register("test", test)
client_tools.register("test_async", test_async, is_async=True)
# Initialize conversation
conversation = Conversation(
client=client,
agent_id=agent_id,
requires_auth=False,
audio_interface=DefaultAudioInterface(),
callback_agent_response=on_agent_response,
callback_user_transcript=on_user_transcript,
callback_latency_measurement=on_latency,
client_tools=client_tools,
)
# Start the conversation
conversation.start_session()
# Let it run for 100 seconds
time.sleep(100)
# End the conversation
conversation.end_session()
conversation.wait_for_session_end()
# Get the conversation ID for reference
conversation_id = conversation._conversation_id
print(f"Conversation ID: {conversation_id}")
if __name__ == "__main__":
test_live_conversation()
|