#How in Python do I get the transcript from my conversational AI?
1 messages · Page 1 of 1 (latest)
To get the transcript from your conversational AI using the ElevenLabs Python SDK, you can use the callback_user_transcript parameter when initializing the Conversation instance. This parameter allows you to define a callback function that will be called with the user's transcript. Here's a basic example:
import os
import signal
from elevenlabs.client import ElevenLabs
from elevenlabs.conversational_ai.conversation import Conversation
from elevenlabs.conversational_ai.default_audio_interface import DefaultAudioInterface
# Load agent ID and API key from environment variables
agent_id = os.getenv("AGENT_ID")
api_key = os.getenv("ELEVENLABS_API_KEY")
# Create the ElevenLabs client instance
client = ElevenLabs(api_key=api_key)
# Initialize the Conversation instance
conversation = Conversation(
client,
agent_id,
requires_auth=bool(api_key),
audio_interface=DefaultAudioInterface(),
callback_user_transcript=lambda transcript: print(f"User: {transcript}"),
)
# Start the conversation
conversation.start_session()
# Handle clean shutdown on Ctrl+C
signal.signal(signal.SIGINT, lambda sig, frame: conversation.end_session())
# Wait for the conversation to end
conversation_id = conversation.wait_for_session_end()
print(f"Conversation ID: {conversation_id}")
In this example, the callback_user_transcript is set to a lambda function that prints the user's transcript to the console. You can modify this function to handle the transcript as needed in your application.
So in this code here, will it provide me with the transcript at the end of the convo:
@socketio.on('end_interview')
def handle_end_interview():
session_id = request.sid
conversation = active_conversations.get(session_id)
if conversation:
try:
# End the conversation session
transcript = conversation.callback_user_transcript() # Assuming the conversation object has this method
conversation.end_session()
del active_conversations[session_id]
# Evaluate the transcript
evaluation_result = evaluate_pitch(transcript)
The code snippet you provided seems to be designed to handle the end of a conversation session and retrieve the transcript. Here's a brief explanation of how it works:
-
Session Identification: It uses
request.sidto identify the current session and fetches the corresponding conversation object fromactive_conversations. -
Transcript Retrieval: It attempts to call
conversation.callback_user_transcript()to get the transcript. This assumes that theconversationobject has a method namedcallback_user_transcript()that returns the transcript. -
Session Termination: The conversation session is ended with
conversation.end_session(), and the session is removed fromactive_conversations. -
Transcript Evaluation: The transcript is then evaluated using a function
evaluate_pitch(transcript).
If the callback_user_transcript() method is correctly implemented in your conversation object, this code should provide you with the transcript at the end of the conversation. Make sure that all methods and objects used are properly defined and implemented in your codebase.
So help me fix this code:
@socketio.on('start_interview')
def handle_start_interview():
session_id = request.sid # Get the session ID
# Decorator to ensure safe request context within the callback function
@copy_current_request_context
def safe_emit_response(response):
# Use `socketio.emit` with `room=session_id` to target the specific client
#modified_response = f"<speak>{response}<break time='5s'/></speak>" #Supposed to be the time in which bot pauses between responding
socketio.emit('bot_response', {'message': response}, room=session_id)
try:
# Initialize the conversation with callbacks
conversation = Conversation(
client=client,
agent_id=AGENT_ID,
requires_auth=True,
audio_interface=DefaultAudioInterface(),
callback_agent_response=safe_emit_response,
callback_user_transcript=lambda transcript: socketio.emit('user_transcript', {'message': transcript}, room=session_id)
)
# Start the session and store the conversation in the active sessions
conversation.start_session()
active_conversations[session_id] = conversation
socketio.emit('interview_status', {'message': 'Interview started! Respond to the prompts as they appear.'}, room=session_id)
except Exception as e:
print(f"Error starting interview: {e}")
socketio.emit('interview_error', {'error': 'Failed to start interview session.'}, room=session_id)
I get this error:
Error ending session: handle_start_interview.<locals>.<lambda>() missing 1 required positional argument: 'transcript'
The error you're encountering suggests that the lambda function for callback_user_transcript is missing the required transcript argument. To fix this, ensure that the lambda function correctly accepts the transcript parameter. Here's how you can modify the code:
@socketio.on('start_interview')
def handle_start_interview():
session_id = request.sid # Get the session ID
# Decorator to ensure safe request context within the callback function
@copy_current_request_context
def safe_emit_response(response):
# Use `socketio.emit` with `room=session_id` to target the specific client
socketio.emit('bot_response', {'message': response}, room=session_id)
try:
# Initialize the conversation with callbacks
conversation = Conversation(
client=client,
agent_id=AGENT_ID,
requires_auth=True,
audio_interface=DefaultAudioInterface(),
callback_agent_response=safe_emit_response,
callback_user_transcript=lambda transcript: socketio.emit('user_transcript', {'message': transcript}, room=session_id)
)
# Start the session and store the conversation in the active sessions
conversation.start_session()
active_conversations[session_id] = conversation
socketio.emit('interview_status', {'message': 'Interview started! Respond to the prompts as they appear.'}, room=session_id)
except Exception as e:
print(f"Error starting interview: {e}")
socketio.emit('interview_error', {'error': 'Failed to start interview session.'}, room=session_id)
Make sure that the callback_user_transcript function is called with the transcript argument in the Conversation class. If the Conversation class is not correctly passing the transcript to the callback, you may need to check its implementation to ensure it does so.
I still get this error:
Error ending session: handle_start_interview.<locals>.<lambda>() missing 1 required positional argument: 'transcript'