KITT AI Participant for LiveKit

repository·main·Indexed 43 days ago

https://github.com/livekit-examples/kitt

An AI-powered participant for WebRTC video conferences that integrates ChatGPT with Google Cloud Speech-to-Text and Text-to-Speech. KITT consists of a NextJS frontend (meet) and a Go-based backend (lkgpt-service) that manages GPT-powered agents within LiveKit rooms via webhooks and a join API.

Tokens
3.2K
Snippets
14
Records
19
Agent score
54%

What's inside KITT

  1. What is KITT and how it works

    main

    KITT is a ChatGPT-powered AI participant that lives within a WebRTC conference call. It is composed of two primary services:

    1. meet: A NextJS application that provides the video call user interface.
    2. lkgpt-service: The core KITT implementation.

    Workflow: When a room is created, a webhook triggers a handler in lkgpt-service which adds KITT as a participant to the room. KITT uses Google Cloud Platform (GCP) for Speech-to-Text and Text-to-Speech, combined with ChatGPT for intelligence.

  2. Run the Meet service locally

    main

    The meet service is the NextJS frontend. To run it:

    1. Navigate to the meet/ directory.
    2. Copy .env.example to .env.local.
    3. Populate .env.local with your LiveKit connection details.
    4. Install dependencies and start the development server.
    yarn install && yarn dev
  3. Prerequisites for running KITT

    main

    Before running KITT, ensure you have the following credentials and runtimes:

    • Google Cloud Credentials: A GOOGLE_APPLICATION_CREDENTIALS JSON file from a Google Cloud account.
    • OpenAI API Key: For ChatGPT capabilities.
    • LiveKit Credentials: api_key, api_secret, and url from LiveKit Cloud.
    • Runtimes: Go 1.19+ and Node.js.
  4. Run the lkgpt-service locally

    main

    The lkgpt-service is the backend service implementing KITT. To run it:

    1. Navigate to the lkgpt-service/ directory.
    2. Copy config-sample.yaml to config.yaml.
    3. Populate config.yaml with your LiveKit connection details and OpenAI API key.
    4. Run the server using go run, providing the config file and the path to your GCP credentials JSON file.
    go run /cmd/server/main.go --config config.yaml --gcp-credentials-path gcp-credentials.json
  5. Manually spawn KITT locally

    main

    When running locally, LiveKit webhooks cannot reach your local lkgpt-service. You must manually trigger the KITT spawning process by calling the join API. Use the room_name found in the URL slug when you enter a room in the Meet UI.

    curl -XPOST http://localhost:3001/join/<room_name>
  6. Configure environment variables for LiveKit Meet

    main

    To run the LiveKit Meet application locally, create a .env.local file in the project root by copying .env.example. You must provide credentials for your LiveKit server and define the endpoint used for generating access tokens.

    Required variables:

    • LIVEKIT_API_KEY: Your LiveKit API key (e.g., from the LiveKit Cloud dashboard).
    • LIVEKIT_API_SECRET: Your LiveKit API secret.
    • LIVEKIT_URL: The WebSocket URL of your LiveKit server (e.g., wss://your-project.livekit.cloud).
    • NEXT_PUBLIC_LK_TOKEN_ENDPOINT: The public URL path used by the client to request access tokens (e.g., /api/token).
    # 1. Copy this file and rename it to .env.local
    # 2. Update the enviroment variables below.
    
    LIVEKIT_API_KEY=devkey
    LIVEKIT_API_SECRET=secret
    LIVEKIT_URL=wss://my-livekit-project.livekit.cloud
    
    ## PUBLIC
    NEXT_PUBLIC_LK_TOKEN_ENDPOINT=/api/token
  7. Initialize a RoomServiceClient with getRoomClient()

    main

    The getRoomClient function returns an instance of RoomServiceClient from the livekit-server-sdk.

    Before returning the client, it performs a validation check to ensure that LIVEKIT_API_KEY and LIVEKIT_API_SECRET are defined in the environment. If these keys are missing, it throws an error. The client is initialized using the URL retrieved from getLiveKitURL().

    import { getRoomClient } from './server-utils';
    
    const roomClient = getRoomClient();
    // roomClient is now a ready-to-use RoomServiceClient
  8. Retrieve the LiveKit URL via getLiveKitURL()

    main

    The getLiveKitURL function retrieves the LiveKit server URL from environment variables. By default, it looks for LIVEKIT_URL. If a region argument is provided, it attempts to find a region-specific environment variable using the pattern LIVEKIT_URL_{REGION} (uppercase).

    If the requested environment variable is not defined, the function throws an error.

    // Get default URL from LIVEKIT_URL
    const url = getLiveKitURL();
    
    // Get region-specific URL from LIVEKIT_URL_US_EAST
    const regionalUrl = getLiveKitURL('us-east');
  9. Initialize and run the LiveGPT service

    main

    The LiveGPT struct is the core service responsible for managing GPT participants in LiveKit rooms. You can initialize it using NewLiveGPT and start the HTTP server (which handles webhooks and join requests) by calling Start().

    Requirements:

    • A valid config.Config object.
    • An initialized Google Cloud Speech-to-Text (stt.Client) client.
    • An initialized Google Cloud Text-to-Speech (tts.Client) client.
    • An OPENAI_API_KEY environment variable must be set if not provided in the config.

    To shut down the service gracefully, call Stop(), which will close the HTTP server and the STT/TTS clients.

    import (
    	"github.com/livekit-examples/livegpt/pkg/config"
    	stt "cloud.google.com/go/speech/apiv1"
    	tt "cloud.google.com/go/texttospeech/apiv1"
    )
    
    // ... setup clients ...
    
    liveGPT := service.NewLiveGPT(cfg, sttClient, ttsClient)
    
    // Start the service (blocks until Stop is called)
    err := liveGPT.Start()
    if err != nil {
    	// handle error
    }
    
    // To shut down
    liveGPT.Stop()
  10. Use the LiveGPT struct

    main

    The LiveGPT struct manages the lifecycle of GPT-powered participants within LiveKit rooms. It maintains an internal map of ActiveParticipant objects keyed by the room's SID. It exposes several HTTP endpoints:

    • POST /join/{roomName}: Manually triggers the service to join a specific room.
    • POST /webhook: Processes LiveKit webhooks (specifically looking for EventParticipantJoined to trigger the bot).
    • GET /: Health check endpoint.
  11. Reference: live-gpt CLI flags

    main

    The following flags are available for the live-gpt command-line tool:

    --config (string)
        LiveGPT yaml config file (Env: LIVEGPT_CONFIG_FILE)
    --config-body (string)
        LiveGPT yaml config body (Env: LIVEGPT_CONFIG_BODY)
    --gcp-credentials-path (string)
        Path to GCP credentials file (Env: GOOGLE_APPLICATION_CREDENTIALS)
    --gcp-credentials-body (string)
        GCP credentials json body (Env: GOOGLE_APPLICATION_CREDENTIALS_BODY)