Back to Examples

LLM Integration Example

This example demonstrates how to use OpenAI's AsyncOpenAI client to build an agent that answers user questions with GPT-4.1-mini and responds with audio.

Features

  • Uses OpenAI's AsyncOpenAI client for LLM integration
  • Retrieves variables from the context (added as environment variables)
  • Handles session start and user text input
  • Streams LLM responses to the user
  • Responds with audio using TextToSpeechEvent
  • Simple, production-ready async pattern

Code Example

from openai import AsyncOpenAI
from primfunctions.events import Event, StartEvent, TextEvent, TextToSpeechEvent
from primfunctions.context import Context
from primfunctions.utils.streaming import stream_sentences

openai_client = AsyncOpenAI(api_key=context.variables.get("OPENAI_API_KEY"))

async def handler(event: Event, context: Context):
    if isinstance(event, StartEvent):
        yield TextToSpeechEvent(
            text="Hello! Ask me anything and I'll answer using GPT-4.1-mini.",
            voice="nova"
        )

    if isinstance(event, TextEvent):
        user_message = event.data.get("text", "")

        response = await openai_client.chat.completions.create(
            model="gpt-4.1-mini",
            messages=[
                {"role": "system", "content": "You are a helpful assistant."},
                {"role": "user", "content": user_message}
            ],
            stream=True
        )

        for chunk in stream_sentences(response):
            if "content" in chunk:
                yield TextToSpeechEvent(
                    text=chunk["content"],
                    voice="nova"
                )

How It Works

OpenAI Client Initialization

The AsyncOpenAI client is created once at the top of the file for efficient reuse across requests.

Session Start

When a session begins (StartEvent), the agent greets the user and prompts them to ask a question.

Handling User Input

On each TextEvent, the user's message is sent to OpenAI's GPT-4.1-mini model using the async client. The LLM's response is extracted from the API result.

Audio Response

The agent replies to the user by yielding a TextToSpeechEvent with the LLM's answer, using the 'nova' voice.

This example shows a minimal, production-ready pattern for integrating OpenAI LLMs into your agent using async Python and VoiceRun events.