LiveKit OpenAI Plugin

1.5.2 · active · verified Sat Apr 11

The `livekit-plugins-openai` library provides an Agent Framework plugin for integrating OpenAI services, including the Realtime API, LLM, TTS, and STT capabilities. It also supports a wide range of OpenAI-compatible APIs such as Azure OpenAI, Cerebras, Fireworks, and Ollama. It is part of the LiveKit Agents ecosystem, designed for building real-time, multimodal AI applications. The library is actively maintained with frequent releases, with the current version being 1.5.2.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize a LiveKit AgentSession using the `livekit-plugins-openai` for real-time voice AI. It configures the session to use OpenAI's Realtime API, which integrates speech-to-text (STT), large language model (LLM), and text-to-speech (TTS) for low-latency, multimodal interactions. Ensure your `OPENAI_API_KEY` is set as an environment variable.

import os
from livekit.agents import AgentSession, JobContext
from livekit.plugins import openai

# Set your OpenAI API key as an environment variable or pass it directly.
# os.environ["OPENAI_API_KEY"] = "sk-..."

async def my_agent_entrypoint(ctx: JobContext):
    # Ensure OPENAI_API_KEY is set in your environment
    openai_api_key = os.environ.get('OPENAI_API_KEY', '')
    if not openai_api_key:
        print("Error: OPENAI_API_KEY environment variable not set.")
        return

    # Use the OpenAI Realtime API for a voice AI agent
    # The RealtimeModel combines STT, LLM, and TTS for low-latency interactions.
    # 'voice' parameter selects the voice for speech generation.
    session = AgentSession(
        llm=openai.realtime.RealtimeModel(
            voice="marin", # Example voice, see OpenAI docs for options
            api_key=openai_api_key
        )
    )
    print("LiveKit Agent with OpenAI Realtime API started. Connect a participant to interact.")

    # The agent will now handle real-time audio and text interactions
    # based on the configured OpenAI RealtimeModel.
    # For a full agent loop, you would typically yield control to the LiveKit framework.

# In a real LiveKit Agents application, `my_agent_entrypoint` would be
# registered with the agent server to run for each new session.
# This example demonstrates the core setup for the OpenAI plugin.

view raw JSON →