How do I use a custom chat completion api as the l...
# ask-ai
s
How do I use a custom chat completion api as the llm in the AgentSession? I have the link for the chat completion api and some metadata that I want to pass to it.
t
Thanks for reaching out, Anuj. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'm happy to help you use a custom chat completion API as the LLM in an
AgentSession
. You have two friendly options to choose from: 1. Using an OpenAI-Compatible Endpoint: • If your API supports OpenAI’s
/chat/completions
format, you can easily set it up using LiveKit’s built-in inference module. Just set the
base_url
to your API endpoint, add your API key, and specify the model ID. This approach is straightforward—no need for subclassing—and it even supports streaming responses. 2. Overriding the
llm_node
Method:
• If your API uses a custom format, you can override the
llm_node
method in your
Agent
subclass. This gives you the flexibility to handle custom request and response structures, implement streaming, and manage authentication smoothly. Just keep in mind that this method should be an async generator that yields text chunks or structured
ChatChunk
objects. Please feel free to reach out if you'd like detailed examples or any further guidance with either approach—I’m here to support you every step of the way! Sources: agents/src/inference/llm.ts | Google Gemini LLM | LiveKit Docs | OpenAI LLM models | LiveKit Docs | examples/voice_agents/llamaindex-rag/chat_engine.py | tools/server/public_simplechat/readme.md
s
can you share code snippets for python code for the first option?
t
I'd love to share a Python code snippet that shows you how to use an OpenAI-compatible endpoint as the LLM in an
AgentSession
. Here it is:
Copy code
from livekit.plugins.openai import LLM

# Configure the LLM instance
llm_instance = LLM(
    model="your-model-id",  # Replace with your model ID
    api_key="your-api-key",  # Replace with your API key
    base_url="<BASE_API_URL>",  # Replace with your API endpoint
    extra_headers={"X-Custom-Header": "value"},  # Optional custom headers
    temperature=0.7,  # Adjust as needed
)

# Use the LLM instance in your agent
from livekit.agents import Agent
from livekit.plugins import deepgram, silero

agent = Agent(
    instructions="You are a helpful assistant.",
    vad=silero.VAD.load(),
    stt=deepgram.STT(),
    llm=llm_instance,  # Pass the configured LLM instance
    tts=None,  # Replace with your TTS instance if needed
)

# Start the AgentSession
from livekit.agents import AgentSession

session = AgentSession()
await session.start(agent=agent, room="your-room-id")  # Replace with your room ID
Just be sure to swap out placeholders like
your-model-id
,
your-api-key
,
<BASE_API_URL>
, and
your-room-id
with your actual details. If anything feels unclear or if you'd like me to walk you through more examples, please don’t hesitate to ask—I’m here to help!
s
if i have a particular model in the form of a string like "openai/gpt-5", can i pass it directly to AgentSession directly? give python examples
t
If you have a specific model string like "openai/gpt-5", you can easily pass it directly to the
AgentSession
by setting up the LLM instance with that model string. Here’s a simple example to guide you:
Copy code
from livekit.plugins.openai import LLM
from livekit.agents import Agent
from livekit.plugins import deepgram, silero
from livekit.agents import AgentSession

# Configure the LLM instance with the model string
llm_instance = LLM(
    model="openai/gpt-5",  # Pass your model string here
    api_key="your-api-key",  # Replace with your API key
    base_url="<BASE_API_URL>",  # Replace with your API endpoint
    temperature=0.7,  # Adjust as needed
)

# Create the agent with the configured LLM
agent = Agent(
    instructions="You are a helpful assistant.",
    vad=silero.VAD.load(),
    stt=deepgram.STT(),
    llm=llm_instance,  # Pass the configured LLM instance
    tts=None,  # Replace with your TTS instance if needed
)

# Start the AgentSession
session = AgentSession()
await session.start(agent=agent, room="your-room-id")  # Replace with your room ID
Just be sure to replace placeholders like
your-api-key
,
<BASE_API_URL>
, and
your-room-id
with your actual details. If you have any questions or need a hand with anything else, feel free to ask—I’m here to help!
s
i do not want to use my own BASE_API_URL, i want to use the actual open ai or whatever is the provider
t
If you'd like to use the actual OpenAI API or another supported provider, you can simply leave out the
base_url
parameter when setting up your LLM instance. Here’s a friendly example to guide you:
Copy code
from livekit.plugins.openai import LLM
from livekit.agents import Agent
from livekit.plugins import deepgram, silero
from livekit.agents import AgentSession

# Configure the LLM instance with the model string
llm_instance = LLM(
    model="openai/gpt-5",  # Pass your model string here
    api_key="your-api-key",  # Replace with your OpenAI API key
    temperature=0.7,  # Adjust as needed
)

# Create the agent with the configured LLM
agent = Agent(
    instructions="You are a helpful assistant.",
    vad=silero.VAD.load(),
    stt=deepgram.STT(),
    llm=llm_instance,  # Pass the configured LLM instance
    tts=None,  # Replace with your TTS instance if needed
)

# Start the AgentSession
session = AgentSession()
await session.start(agent=agent, room="your-room-id")  # Replace with your room ID
With this setup, your LLM will connect to the default OpenAI API endpoint. Just be sure to swap out
your-api-key
and
your-room-id
with your actual details. If anything feels unclear or you want to dive deeper, please don’t hesitate to ask—I’m here to help!
s
if i have OPENAI_API_KEY in .env.local, will it be able to pick that up directly?
t
Absolutely! If you have your
OPENAI_API_KEY
set up in your
.env.local
file, the LLM instance will automatically pick it up, as long as your environment variables are properly loaded. To make sure everything runs smoothly, it's a good idea to use a library like
python-dotenv
to load those variables from your
.env.local
file into your application. Here’s a friendly example to help you load the environment variables:
Copy code
from dotenv import load_dotenv
import os
from livekit.plugins.openai import LLM
from livekit.agents import Agent
from livekit.plugins import deepgram, silero
from livekit.agents import AgentSession

# Load environment variables from .env.local
load_dotenv(".env.local")

# Configure the LLM instance
llm_instance = LLM(
    model="openai/gpt-5",  # Pass your model string here
    temperature=0.7,  # Adjust as needed
)

# Create the agent with the configured LLM
agent = Agent(
    instructions="You are a helpful assistant.",
    vad=silero.VAD.load(),
    stt=deepgram.STT(),
    llm=llm_instance,  # Pass the configured LLM instance
    tts=None,  # Replace with your TTS instance if needed
)

# Start the AgentSession
session = AgentSession()
await session.start(agent=agent, room="your-room-id")  # Replace with your room ID
Just double-check that your
OPENAI_API_KEY
is correctly set in your
.env.local
file. If you run into any bumps or have questions along the way, please don’t hesitate to reach out—I’m here to help!