how to use function calling with openai realtime m...
# ask-ai
a
how to use function calling with openai realtime model?
t
Thanks for reaching out, affandy. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hey there! If you want to use function calling with the OpenAI real-time model in LiveKit, here’s a friendly step-by-step to get you going: 1. Install Required Packages: First up, grab the OpenAI plugin for LiveKit by running:
pip install "livekit-agents[openai]~=1.2"
2. Set Up Authentication: Make sure to set your OpenAI API key in the
OPENAI_API_KEY
environment variable so everything can talk smoothly. 3. Define Functions: Use the handy
@function_tool
decorator to create Python functions the agent can call. For example:
Copy code
from livekit.agents.llm import function_tool

class Assistant(Agent):
    @function_tool
    async def add_numbers(self, context: RunContext, a: float, b: float):
        """Return the sum of two numbers."""
        return str(a + b)
4. Initialize the Agent: Next, set up the
RealtimeModel
to enable real-time audio and function calling. Here’s a quick example:
Copy code
from livekit.agents import MultimodalAgent
from livekit.plugins.openai.realtime import RealtimeModel

agent = MultimodalAgent(
    model=RealtimeModel(
        model="gpt-4o-realtime-preview",
        voice="alloy",
        temperature=0.8,
        tool_choice="auto",
        modalities=["text", "audio"],
        instructions="You are a helpful assistant."
    )
)
5. Connect and Start: Finally, define an entrypoint function to connect your agent to a room and kick things off:
Copy code
async def entrypoint(ctx: JobContext):
    await ctx.connect(auto_subscribe=AutoSubscribe.AUDIO_ONLY)
    agent.start(ctx.room)
If you want to dive deeper or need more info, check out the LiveKit OpenAI Realtime API guide. Happy coding! Sources: OpenAI Realtime API integration guide | LiveKit Docs | OpenAI and LiveKit partner to turn Advanced Voice into an API | plugins/openai/src/realtime/api_proto.ts | livekit-plugins/livekit-plugins-openai/livekit/plugins/openai/stt.py | solutions/openai_demo/README.md
a
which openai realtime models that support function calling?
t
Hey there! Just so you know, the OpenAI real-time model that works with function calling in LiveKit is GPT-4o, and the specific version is
gpt-4o-realtime-preview
. It’s designed to be super quick for voice interactions and comes with built-in function calling features. If you want to know more or need a hand with anything, just let me know—I’m here to help! Sources: OpenAI and LiveKit partner to turn Advanced Voice into an API | livekit-plugins/livekit-plugins-openai/livekit/plugins/openai/realtime/real... | plugins/openai/src/realtime/realtime_model.ts | Realtime model integrations | LiveKit Docs
r
is open ai realtime api ready to production
t
Great news! The OpenAI Realtime API is all set for production use in LiveKit, but there are a few things to keep in mind: • Session Duration: The API limits sessions to 15 minutes max. So, just make sure your LiveKit session timeout is a bit shorter than that to keep everything running smoothly. • Conversation History: When you load previous messages, the API might switch to text-only replies. To keep the audio consistent, you can use a separate TTS plugin like Azure OpenAI TTS. • Production Features: LiveKit’s got your back with handy features like WebRTC-to-WebSocket bridging, automatic reconnection, load balancing, and SIP telephony support—perfect for real-world setups. With these tweaks, you’re all set to roll out real-time conversational agents at scale. If you have any questions or need a hand, just give me a shout! Sources: OpenAI and LiveKit | LiveKit Docs | livekit-plugins/livekit-plugins-openai/livekit/plugins/openai/__init__.py | plugins/openai/src/realtime/api_proto.ts | OpenAI Realtime API integration guide | LiveKit Docs | OpenAI Realtime API and LiveKit | LiveKit Docs