-
Notifications
You must be signed in to change notification settings - Fork 275
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for client-tools (Python SDK)
- Loading branch information
1 parent
d1f0878
commit e5bacd3
Showing
2 changed files
with
125 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import os | ||
import time | ||
|
||
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""" | ||
|
||
# Get API key from environment | ||
api_key = os.getenv("ELEVENLABS_API_KEY") | ||
if not api_key: | ||
raise ValueError("Please set ELEVENLABS_API_KEY environment variable") | ||
|
||
# Initialize client | ||
client = ElevenLabs(api_key=api_key) | ||
|
||
AGENT_ID = "<insert-testing-agent-id>" | ||
|
||
# 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_tool_handler(parameters): | ||
print("Tool called with parameters:", parameters) | ||
return f"Handled parameters: {parameters}" | ||
|
||
client_tools.register("test", test_tool_handler) | ||
|
||
# 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 30 seconds | ||
time.sleep(30) | ||
|
||
# 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() |