how to get conversation of user and agent?
# ask-ai
s
how to get conversation of user and agent?
t
Thanks for reaching out, SajithlalDev. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To retrieve the conversation between a user and an agent in LiveKit's Agents framework, you can use the following methods: • Using event listeners to create transcriptions: Save the text of a conversation by listening for agent events such as
user_speech_committed
(when user speech is committed) and
agent_stopped_speaking
(when the agent stops speaking). • Accessing session history: Use the
session.history
property to access the entire conversation history. You can also set up a shutdown callback to save this history when the session ends. For example:
Copy code
async def write_transcript():
  current_date = datetime.now().strftime("%Y%m%d_%H%M%S")
  filename = f"/tmp/transcript_{ctx.room.name}_{current_date}.json"
  with open(filename, 'w') as f:
    json.dump(session.history.to_dict(), f, indent=2)
  print(f"Transcript for {ctx.room.name} saved to {filename}")

ctx.add_shutdown_callback(write_transcript)
Additionally, for real-time access to the conversation, you can listen for events like
conversation_item_added
(emitted whenever an item is added to the chat history) and
user_input_transcribed
(emitted whenever user input is transcribed). For more details and example code, you can refer to the MultimodalAgent example at: https://github.com/livekit/agents/blob/main/examples/conversation_persistor.py. Sources: Recording agent sessions | LiveKit Docs | Session recording and transcripts | LiveKit Docs