give me an example code to capture the chat messag...
# ask-ai
a
give me an example code to capture the chat messages from user and assistant and store it in the supabase
g
It seems likely that you can capture chat messages from both the user and the assistant by listening to the
user_speech_committed
and
agent_speech_committed
events. You can then store these messages in a database like Supabase. Here's a basic example of how you might achieve this:
Copy code
import asyncio
import json
from livekit import rtc
from livekit.agents import VoicePipelineAgent, llm
from supabase import create_client, Client

# Initialize Supabase client
url: str = "your_supabase_url"
key: str = "your_supabase_key"
supabase: Client = create_client(url, key)

async def save_to_supabase(role, message):
    data = {"role": role, "message": message}
    response = supabase.table("chat_messages").insert(data).execute()
    print(response)

async def on_user_speech_committed(msg):
    await save_to_supabase("user", msg.content)

async def on_agent_speech_committed(msg):
    await save_to_supabase("assistant", msg.content)

# Initialize the voice pipeline agent
agent = VoicePipelineAgent(
    vad=rtc.VAD.load(),
    stt=rtc.STT(),
    llm=llm.LLM(),
    tts=rtc.TTS(),
)

# Register event listeners
agent.on("user_speech_committed", on_user_speech_committed)
agent.on("agent_speech_committed", on_agent_speech_committed)

# Start the agent
async def start_agent():
    await agent.start(room="your_room_id")

# Run the agent
asyncio.run(start_agent())
This code sets up event listeners for the
user_speech_committed
and
agent_speech_committed
events, and stores the messages in a Supabase table named
chat_messages
. You need to replace
"your_supabase_url"
,
"your_supabase_key"
, and
"your_room_id"
with your actual Supabase and LiveKit details. Would you like more detail on any part of this process? Sources: - https://livekit-users.slack.com/archives/C07FY8WHGPM/p1733317598.969739 - https://livekit-users.slack.com/archives/C07FY8WHGPM/p1731773981.585369 Note: I wasn’t able to find highly relevant data sources, but above are a few potentially relevant links. 👍 0 button 👎 0 button 💡 Hint: Mention @runllm in the thread for followups.