how can i get the conversation id of the whole ses...
# ask-ai
a
how can i get the conversation id of the whole session.history? could be the OpenAI conversation id or a LiveKit one, but needs to be one that links all the transcripts from the chat history
t
Thanks for reaching out, Sebastián. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'd be happy to help you retrieve a conversation ID that links all transcripts from a chat session. Here are a few ways you can do this: • LiveKit-native session history: You can use the
session.history
property to access the full conversation history during a session. This gives you a complete record of the chat, which is really handy. • OpenAI Realtime API: When the
conversation.created
event occurs, it includes a
ConversationResource
object with an
id
field. This ID is your conversation ID, making it easy to track. • Azure OpenAI Realtime API: Just like with OpenAI, the
conversation.created
event provides a
ConversationResource
that contains the conversation ID. To keep everything safe and accessible, I recommend saving the conversation ID and history during session shutdown using callbacks. If you'd like, I can walk you through how to implement any of these methods—just let me know!
a
how can i get the conversation.created so i save the ID?
t
I'd be happy to help you capture the
conversation.created
event and save the conversation ID. Here's a simple way to do it: 1. First, set up an event listener in your application for the
conversation.created
event. This event fires whenever a new conversation is created. 2. When this event happens, you'll receive a
ConversationResource
object. Inside this object, you'll find the
id
field—that's the conversation ID you want to save. 3. Next, save this
id
to your preferred storage, like a database or a file, so you can easily access it later. If you'd like, I can provide some specific code examples or guide you through the implementation. Just let me know which platform you're working with—whether it's LiveKit, OpenAI, or Azure OpenAI—and I'll be glad to assist you further!
To help you capture the
conversation.created
event and save the conversation ID, you can set up an event listener in your application. Let me guide you through it step-by-step: Python Example:
Copy code
from livekit import agents
from livekit.plugins import openai

# Create a RealtimeModel instance with your OpenAI API key
oai_model = openai.realtime.RealtimeModel(api_key="your_openai_api_key")

# Create a session
session = oai_model.session()

# Define the event listener for server events
@session.on("openai_server_event_received")
def on_server_event_received(event):
    # Check if the event is conversation.created
    if event["type"] == "conversation.created":
        conversation = event["conversation"]
        print(f"Conversation created: {conversation['id']}")
Node.js Example:
Copy code
import { RealtimeModel } from '@livekit/agents-js/plugins/openai';

// Create a RealtimeModel instance with your OpenAI API key
const oaiModel = new RealtimeModel({ apiKey: "your_openai_api_key" });

// Create a session
const session = oaiModel.session();

// Define the event listener for server events
session.on('openai_server_event_received', (event) => {
  // Check if the event is conversation.created
  if (event.type === 'conversation.created') {
    const conversation = event.conversation;
    console.log(`Conversation created: ${conversation.id}`);
  }
});
Just a quick reminder to replace
your_openai_api_key
with your actual API key. If you run into any questions or need a hand with this setup, please don’t hesitate to reach out—I’m here to help! Sources: livekit-plugins/livekit-plugins-google/livekit/plugins/google/beta/realtime... | OpenAI Realtime API integration guide | LiveKit Docs | Azure OpenAI Realtime API and LiveKit | LiveKit Docs | plugins/openai/src/realtime/realtime_model.ts
a
cool, and how could i force to refresh the Realtime session? so i get a new conversation id
t
I don't have the answer you're looking for. You could also try asking your question: • in one of the other Slack channels or • to https://deepwiki.com/livekit/livekit_composite which is trained on all LiveKit source code If you find the answer, please post it here to help others!