An adapter to integrate OpenAI Agents as LLM providers for LiveKit.
Project description
LiveKit OpenAI Agents Adapter
This library provides an adapter to integrate agents built with the openai-agents library (specifically, its agents module) as LLM providers within the LiveKit Agents framework.
It allows you to plug in your openai-agents based orchestrators, leveraging their capabilities like conversation history management, tool usage, and handoffs, while still utilizing the real-time audio and agent lifecycle management features of LiveKit Agents.
Features
- Integrates agents built with the
openai-agentslibrary into LiveKit. - Facilitates the use of
openai-agentsfeatures (like handoffs, structured output if designed in your agent) within LiveKit. - Includes a utility to extract the last user message from the chat context for the agent.
Installation
pip install livekit_openai_agents
(Note: Ensure this package name matches your published package on PyPI.)
Alternatively, if you have this project cloned:
pip install .
Dependencies
Based on pyproject.toml:
livekit-agents[openai,elevenlabs,silero,turn-detector] == 1.0.17(or your current version)livekit-plugins-noise-cancellation~=0.2openai-agents >= 0.0.14pyee >= 9.0.0
Please refer to your pyproject.toml for the most up-to-date list of dependencies.
Usage
1. Define Your openai-agents Agent
First, create your agent(s) using the agents library. For example:
# your_openai_agents.py
from agents import Agent
from pydantic import BaseModel
class MathResponse(BaseModel):
explanation: str
answer: float
math_tutor_agent = Agent(
name="MathTutor",
description="A specialized agent that helps with math problems and provides explanations.",
instructions="You are a math tutor. Explain your reasoning step-by-step and provide the final answer.",
output_type=MathResponse, # Example of structured output
# You can add handoffs, tools, etc. as per openai-agents documentation
)
# You might have other agents or a more complex setup, e.g., a triage agent
# from agents import Runner
# result = await Runner.run(math_tutor_agent, "What is 2+2?")
# print(result.final_output) # Output: MathResponse(explanation='...', answer=4.0)
2. Use the Adapter in Your LiveKit Agent
Import OpenAIAgentAdapter from this library and your openai-agents agent. Then, initialize the adapter and use it in your LiveKit AgentSession.
# your_livekit_app.py
import asyncio
from dotenv import load_dotenv, find_dotenv
from livekit import agents
from livekit.agents import Agent as LiveKitAgent, AgentSession, RoomInputOptions # Renamed to avoid clash
from livekit.plugins import openai, silero # or your preferred STT/TTS/VAD
# Import the adapter and your openai-agent
from livekit_openai_agents.adapter import OpenAIAgentAdapter # Assuming 'livekit_openai_agents' is the package name
from your_openai_agents import math_tutor_agent # The agent defined in step 1
# Load .env for API keys if necessary
# load_dotenv(find_dotenv())
class MyLiveKitAssistant(LiveKitAgent): # Renamed to avoid clash with openai-agents' Agent
def __init__(self) -> None:
super().__init__(instructions="You are a helpful voice AI assistant that can call specialized tutors.")
async def entrypoint(ctx: agents.JobContext):
await ctx.connect()
# 1. Initialize your openai-agent (it's already defined, just use the instance)
# math_tutor_agent is already an instance
# 2. Initialize the adapter with your openai-agent instance
openai_agent_llm_adapter = OpenAIAgentAdapter(orchestrator=math_tutor_agent)
# 3. Set up the AgentSession
session = AgentSession(
stt=openai.STT(), # Replace with your STT
llm=openai_agent_llm_adapter, # Use the adapter here
tts=openai.TTS(), # Replace with your TTS
vad=silero.VAD.load(),
)
await session.start(
room=ctx.room,
agent=MyLiveKitAssistant(),
# ... other options
)
# Example: Generate an initial greeting
await session.generate_reply(
instructions="Greet the user and ask how you can help."
)
print("Agent is ready and listening.")
if __name__ == "__main__":
# Load .env for API keys (e.g., OPENAI_API_KEY, LIVEKIT_URL, LIVEKIT_API_KEY)
dotenv_path = find_dotenv()
if dotenv_path:
print(f"Loading .env file from: {dotenv_path}")
load_dotenv(dotenv_path)
else:
print("No .env file found. Ensure API keys and LiveKit connection info are set as environment variables.")
agents.cli.run_app(agents.WorkerOptions(entrypoint_fnc=entrypoint))
See the examples/ directory (e.g., examples/tutors/adapter_example.py) for a more detailed, runnable example.
Development
To set up for development:
git clone https://github.com/anilaltuner/livekit-openai-agents.git
cd livekit-openai-agents
pip install -e .
License
This project is licensed under the MIT License - see the LICENSE file for details.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file livekit_openai_agents-0.2.0.tar.gz.
File metadata
- Download URL: livekit_openai_agents-0.2.0.tar.gz
- Upload date:
- Size: 6.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.3 CPython/3.11.11 Darwin/23.6.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4ce3c54bf28ae79b315629576b5523f9c6c45c7a9e0167e6c5cad344a0d9c87c
|
|
| MD5 |
031a8acbec7542995f31a9827c1b9f11
|
|
| BLAKE2b-256 |
3f349586255c165164cb453537ea687ff967bc3b7e37eeef8bb43ee20ee7b401
|
File details
Details for the file livekit_openai_agents-0.2.0-py3-none-any.whl.
File metadata
- Download URL: livekit_openai_agents-0.2.0-py3-none-any.whl
- Upload date:
- Size: 7.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.3 CPython/3.11.11 Darwin/23.6.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
32bd148537aa300c627034b897ea9372d3a4fd25b9239e171020a87e46d14227
|
|
| MD5 |
1b24b5e65c07b3e320fdf70303246e21
|
|
| BLAKE2b-256 |
26205120acd05cdfe3e07a948cb2910c233dfa2e4f8b9ddd05a5847c62730c3c
|