Skip to main content
This is an interactive notebook. You can run it locally or use the links below:

How to use Weave with Audio Data: An OpenAI Example

This demo uses the OpenAI chat completions API with GPT 4o Audio Preview to generate audio responses to text prompts and track these in Weave. OpenAI chat completions API interface with GPT 4o Audio Preview integration and audio response generation workflow For the advanced use case, we leverage the OpenAI Realtime API to stream audio in realtime. Click the following thumbnail to view the video demonstration, or click here. Everything Is AWESOME

Setup

Start by installing the OpenAI (openai) and Weave (weave) dependencies, as well as API key management dependencey set-env.
Next, load the required API keys for OpenAI and Weave. Here, we use set_env which is compatible with google colab’s secret keys manager, and is an alternative to colab’s specific google.colab.userdata. See: here for usage instructions.
And finally import the required libraries.

Audio streaming and storage example

Now we will setup a call to OpenAI’s completions endpoint with audio modality enabled. First create the OpenAI client and initiate a Weave project.
Now we will define our OpenAI completions request and add our Weave decorator (op). Here, we define the function prompt_endpont_and_log_trace. This function has three primary steps:
  1. We make a completion object using the GPT 4o Audio Preview model that supports text and audio inputs and outputs.
    • We prompt the model to count to 13 slowly with varying accents.
    • We set the completion to “stream”.
  2. We open a new output file to which the streamed data is written chunk by chunk.
  3. We return an open file handler to the audio file so Weave logs the audio data in the trace.

Testing

Run the following cell. The system and user prompt will be stored in a Weave trace as well as the output audio. After running the cell, click the link next to the ”🍩” emoji to view your trace.

Advanced usage: Realtime Audio API with Weave

Realtime Audio API integration with Weave and streaming audio conversation interface OpenAI’s realtime API is a highly functional and reliable conversational API for building realtime audio and text assistants. Please note:
  • Review the cells in Microphone Configuration
  • Due to limitations of the Google Colab execution environment, this must be run on your host machine as a Jupyter Notebook. This cannot be ran in the browser.
    • On MacOS you will need to install portaudio via Brew (see here) for Pyaudio to function.
  • The enable_audio_playback toggle will cause playback of assistant outputted audio. Please note that headphones are required if this is enabled, as echo detection requires a highly complex implementation.

Requirements setup

Microphone configuration

Run the following cell to find all available audio devices. Then, populate the INPUT_DEVICE_INDEX and the OUTPUT_DEVICE_INDEX based on the devices listed. Your input device will have at least 1 input channels, and your output device will have at least 1 output channels.

OpenAI Realtime API schema implementation

The OpenAI Python SDK does not yet provide Realtime API support. We implement the complete OAI Realtime API schema in Pydantic for greater legibility, and may deprecate once official support is released.

Pydantic Schema for OpenAI Realtime API

Audio stream writer (to disk and in memory)

Realtime audio model

The realtime (RT) audio model uses a websocket to send events to OpenAI’s Realtime audio API. This works as follows:
  1. init: We initialize local buffers (input audio) and streams (assistant playback stream, user audio disk writer stream) and open a connection to the Realtime API.
  2. receive_messages_thread: A thread handles receiving messages from the API. Four primary event types are handled: - RESPONSE_AUDIO_TRANSCRIPT_DONE: The server indicates the assistant’s response is completed and provides the transcript.
    • CONVERSATION_ITEM_INPUT_AUDIO_TRANSCRIPTION_COMPLETED: The server indicates the user’s audio has been transcribed, and sends the transcript of the user’s audio. We log the transcript to Weave and print it for the user.
    • RESPONSE_AUDIO_DELTA: The server sends a new chunk of assistant response audio. We append this to the ongoing response data via the response ID, and add this to the output stream for playback.
    • RESPONSE_DONE: The server indicates completion of an assistant response. We get all audio chunks associated with the response, as well as the transcript, and log these in Weave.
    3.send_audio: A handler appends user audio chunks to a buffer, and sends chunks of audio when the audio buffer reaches a certain size.

Audio recorder

We use a pyaudio input stream with a handler linked to the send_audio method of the RTAudio model. The stream is returned to the main thread so it can be safely exited upon program completion.

Main Thread (Run me!)

The main thread initiates a Realtime Audio Model with Weave integrated. Next, a reccording is opened and we wait for a keyboard interrupt from the user.