can i deploy with livekit cloud if i use save the ...
# ask-ai
h
can i deploy with livekit cloud if i use save the conversation in files with the function on_conversation_item_added?
t
Thanks for reaching out, Ju. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Absolutely! You can definitely deploy with LiveKit Cloud and use the
on_conversation_item_added
function to save conversations in files. This handy function listens for the
conversation_item_added
event, which happens whenever something new is added to the chat history. From there, you can set up your own logic to save those items to a file. If you'd like, I’m happy to share more details or examples to help you get started—just let me know!
h
i want to save those (conversaton) in a database, how is this connected with livekit cloud?
t
Hey there! You can easily save conversation data to a database in real-time by using the
on_conversation_item_added
event in LiveKit Cloud. This handy event lets you grab each conversation item as it pops up in the chat history, so you can pull out the info you need and store it right away. Here’s a simple way to get started: • Set up an event listener for
conversation_item_added
to catch those conversation items as they come in. • Pull out the key details from the event, like
role
,
text_content
, and other types of content (think images or audio). • Use your favorite database library (like SQLAlchemy) to save everything asynchronously—this way, your agent keeps running smoothly without any hiccups. If you'd like, I can share a detailed code example or help you with the database setup. Just give me a shout!
h
yes please share some codwe
t
Here’s a friendly example to help you save conversation data to a database using the
on_conversation_item_added
event in LiveKit Cloud. We’re using SQLAlchemy here for database operations, but feel free to tweak it to fit your favorite database library:
Copy code
import asyncio
from datetime import datetime
from livekit.agents import ConversationItemAddedEvent
from sqlalchemy import create_engine, Column, String, Text, DateTime
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker

# Database setup
Base = declarative_base()
engine = create_engine('sqlite:///conversations.db')  # Swap this out with your own database URL
Session = sessionmaker(bind=engine)
db_session = Session()

# Define a table for storing conversation data
class ConversationItem(Base):
    __tablename__ = 'conversation_items'
    id = Column(String, primary_key=True)
    role = Column(String)
    text_content = Column(Text)
    timestamp = Column(DateTime, default=datetime.utcnow)

# Create the table
Base.metadata.create_all(engine)

# Function to save conversation item to the database
async def save_to_database(chat_message):
    try:
        conversation_item = ConversationItem(
            id=chat_message.id,  # Make sure each message has a unique ID
            role=chat_message.role,
            text_content=chat_message.text_content
        )
        db_session.add(conversation_item)
        db_session.commit()
    except Exception as e:
        print(f"Error saving to database: {e}")

# Event listener for conversation_item_added
@session.on("conversation_item_added")
def on_conversation_item_added(event: ConversationItemAddedEvent):
    asyncio.create_task(save_to_database(event.item))
Here’s what this example covers: • Setting up a database table to keep your conversation items organized. • Saving important details like the role and text content into the database. • Handling database operations asynchronously so your app keeps running smoothly. If you want to customize this further or have any specific needs, just give me a shout—I’m here to help!
h
is it best to have a table for database or to use nosql, and save it as a file/json object?
t
When it comes to storing conversation data, both NoSQL (like saving as JSON objects) and relational databases (using tables) can work well. It really depends on what you need: • If you want a flexible schema and expect to handle complex conversation structures, NoSQL might be the way to go. • If you need structured relationships, complex queries, or ACID transactions, a relational database could be a better fit. I'd love to hear more about your application and how you’re thinking of querying or managing the conversation data—so we can find the best fit together! Sources: models/templates/CohereForAI-c4ai-command-r-plus-tool_use.jinja | complex-agents/personal_shopper/database.py | Session recording and transcripts | LiveKit Docs | Sending text | LiveKit Docs
h
i just want to save the conversations, what is the nomral way to go?
t
If your main goal is just to save conversations without diving into complex queries or relationships, going with a NoSQL approach using JSON objects is usually the easiest and most flexible way to go. You can store each conversation as a JSON object in a NoSQL database like MongoDB, or even just save them as JSON files if you don’t need heavy-duty persistence. That said, if you think you might want to run more structured queries or connect with other relational data down the road, a relational database with a simple table setup (like storing conversations as rows with JSON content) could be a smarter choice in the long run. Just let me know if you want me to share a code example for either option—I’m happy to help!