What’s the syntax for using the OpenAI realtime AP...
# ask-ai
m
What’s the syntax for using the OpenAI realtime API just for transcription and it’s detection?
t
Thanks for reaching out, Alexander. 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 the OpenAI Realtime API for transcription and detection, you can set up the
RealtimeModel
with the right parameters. Here's a friendly example in Python to get you started:
Copy code
from livekit.plugins import openai
from openai.types.beta.realtime.session import TurnDetection

# Setting up the RealtimeModel
realtime_model = openai.realtime.RealtimeModel(
    model="gpt-4o-realtime-preview",
    voice="alloy",
    input_audio_transcription=openai.realtime.InputAudioTranscription(
        model="gpt-4o-mini-transcribe"
    ),
    turn_detection=TurnDetection(
        type="server_vad",
        threshold=0.5,
        prefix_padding_ms=300,
        silence_duration_ms=200,
        create_response=True,
        interrupt_response=True,
    ),
    temperature=0.8,
    modalities=["text", "audio"]
)

# Using it in an AgentSession
session = AgentSession(llm=realtime_model)
If you're working with Node.js, here’s how you can do it:
Copy code
import * as openai from '@livekit/agents-plugin-openai';

const realtimeModel = new openai.realtime.RealtimeModel({
  model: 'gpt-4o-realtime-preview',
  voice: 'alloy',
  inputAudioTranscription: {
    model: 'gpt-4o-mini-transcribe',
  },
  turnDetection: {
    type: 'server_vad',
    threshold: 0.5,
    prefix_padding_ms: 300,
    silence_duration_ms: 200,
    create_response: true,
    interrupt_response: true,
  },
  temperature: 0.8,
  modalities: ['text', 'audio'],
});

const session = new AgentSession({ llm: realtimeModel });
If you have any questions or want me to walk you through anything else, just let me know—I’m here to help! Sources: OpenAI Realtime API integration guide | LiveKit Docs | livekit-plugins/livekit-plugins-turn-detector/README.md | plugins/openai/src/realtime/api_proto.ts | Azure OpenAI Realtime API and LiveKit | LiveKit Docs | Azure AI Services and LiveKit | LiveKit Docs
m
I want to use realtime just for transcription and turn detection, but a different LLM and TTS provider for LLM and TTS
t
Hey there! You can totally use the OpenAI Realtime API for transcription and turn detection while plugging in a different LLM and TTS provider. Here’s a quick guide on how to set it up: Python Example:
Copy code
from livekit.plugins import openai, cartesia

session = AgentSession(
    llm=openai.realtime.RealtimeModel(
        modalities=["text"],  # Text-only mode
        turn_detection=None,  # Disable OpenAI's turn detection
        input_audio_transcription=openai.realtime.InputAudioTranscription(
            model="gpt-4o-mini-transcribe"
        )
    ),
    tts=cartesia.TTS()  # Swap this out for your favorite TTS provider
)
Key Points: • Set
modalities
to
["text"]
to turn off audio output from OpenAI. • Disable OpenAI’s turn detection by setting
turn_detection=None
. • Use
input_audio_transcription
to get transcription going. • Plug in whichever TTS provider you prefer (like Cartesia, ElevenLabs, etc.). If you want to use LiveKit’s turn detection model instead, you can pair it with a separate STT plugin (like Deepgram) for transcription and tweak the session setup. Just give me a shout if you want me to share an example for that! Sources: examples/voice_agents/realtime_turn_detector.py | OpenAI Realtime API integration guide | LiveKit Docs | Azure OpenAI Realtime API and LiveKit | LiveKit Docs | Realtime model integrations | LiveKit Docs | plugins/openai/src/realtime/api_proto.ts
m
But you set the LLM to openAI realtime?
t
You're absolutely right that the example uses OpenAI Realtime for the LLM. If you'd like to switch to a different LLM, no worries! You can easily set up your session to use your favorite LLM while still taking advantage of OpenAI Realtime for transcription and turn detection. Here's a refreshed example for you:
Copy code
from livekit.plugins import openai, your_llm_provider, cartesia

session = AgentSession(
    llm=your_llm_provider.LLM(),  # Swap this out with your preferred LLM
    tts=cartesia.TTS(),  # Use your go-to TTS provider here
    stt=openai.realtime.RealtimeModel(
        modalities=["text"],  # Text-only mode for transcription
        turn_detection=None,  # Turning off OpenAI's turn detection
        input_audio_transcription=openai.realtime.InputAudioTranscription(
            model="gpt-4o-mini-transcribe"
        )
    )
)
Here’s what’s happening in this setup: • The
llm
parameter points to your chosen LLM provider. • OpenAI Realtime is dedicated just to transcription (
stt
), with turn detection turned off. • Your preferred TTS provider takes care of converting text to speech. If you want me to walk you through it more or share some extra examples, just let me know—I’m here to help!