livekit / agents

Build real-time multimodal AI applications 🤖🎙️📹
https://docs.livekit.io/agents
Apache License 2.0
1.16k stars 228 forks source link

metadata example when creating rooms #774

Open ryanleecode opened 1 week ago

ryanleecode commented 1 week ago

hi. is there an example of how to create rooms w/ metadata?

anupriya17 commented 1 week ago

To create room metadata for a LiveKit room, you can pass a Metadata field in the CreateRoomRequest as you've shown in your code. The metadata is a string, but you typically serialize a structured object (e.g., JSON) into that string. Here's an example of how you might create and set metadata for the room:

import (
    "context"
    "encoding/json"
    "log"

    livekit "github.com/livekit/protocol/livekit"
)

// Define your room metadata struct
type RoomMetadata struct {
    AgentType      string `json:"agent_type"`      // e.g., "AgentA" or "AgentB"
    LanguageModel  string `json:"language_model"`  // e.g., "en-US" or "fr-FR"
    VoiceType      string `json:"voice_type"`      // e.g., "male" or "female"
    RoomPurpose    string `json:"room_purpose"`    // e.g., "support" or "sales"
}

func createRoomWithMetadata() error {
    roomClient := // Initialize LiveKit RoomClient

    // Create room metadata as a JSON object
    roomMetadata := RoomMetadata{
        AgentType:     "AgentA",
        LanguageModel: "en-US",
        VoiceType:     "female",
        RoomPurpose:   "support",
    }

    // Serialize metadata to JSON
    rmetaBytes, err := json.Marshal(roomMetadata)
    if err != nil {
        log.Println("Error marshaling room metadata:", err)
        return err
    }

    // Create the room with metadata
    _, err = roomClient.CreateRoom(context.Background(), &livekit.CreateRoomRequest{
        Name:             "room-id",
        EmptyTimeout:     2 * 60, // 2 minutes
        DepartureTimeout: 1 * 60, // 1 minute
        MaxParticipants:  4,
        Metadata:         string(rmetaBytes), // Set metadata as a JSON string
    })

    if err != nil {
        log.Println("Unable to create LiveKit room:", err)
        return err
    }

    log.Println("Room created successfully with metadata")
    return nil
}

Note: code generated by chatGPT