Building an AI Assistant Using gpt-4o Audio-Preview API

Building an AI Assistant Using gpt-4o Audio-Preview API

Before I get into extra particulars of utilizing this API, I wish to name out that this API is totally different from the gpt-4o Realtime API.

 

Feature GPT-4o Realtime API GPT-4o Audio-Preview API
Purpose Designed for low-latency, real-time conversational interactions with speech enter and output. Supports audio inputs and outputs within the Chat Completions API, appropriate for asynchronous interactions.
Use Cases Ideal for stay interactions comparable to buyer help brokers, voice assistants, and real-time translators. Suitable for purposes that deal with textual content and audio inputs/outputs with out the necessity for real-time processing.
Integration Method Utilizes a persistent WebSocket connection for streaming communication. Operates through commonplace API calls inside the Chat Completions framework.
Latency Offers low-latency responses, enabling pure conversational experiences. Not optimized for low-latency; higher fitted to non-real-time interactions.

 

 

The steps to make use of this API are:

1. Capture person audio enter

Accept audio enter from the person and add that to the request payload. While the System Message is of kind ‘textual content’, the person enter is of kind ‘input_audio’.

audio_value = st.audio_input("Ask your query!")

encoded_string = None
if audio_value:
    audio_data = audio_value.learn()
    encoded_audio_string = base64.b64encode(audio_data).decode("utf-8")

    st.session_state.messages.append(
        {
            "position": "person",
            "content material": [
                {
                    "type": "input_audio",
                    "input_audio": {"data": encoded_audio_string, "format": "wav"},
                }
            ],
        }
    )

2. Invoke the Chat completions endpoint:

Specify the modalities that have to be supported, for e.g. by the configuration proven under, each textual content and audio output might be supported, utilizing the neural voice specified. Include the perform definitions to make use of throughout software calling.

    completion = None
    attempt:
        completion = shopper.chat.completions.create(
            mannequin=config.mannequin,
            modalities=["text", "audio"],
            audio={"voice": "alloy", "format": "wav"},
            features=st.session_state["connection"].features,
            function_call="auto",
            messages=st.session_state.messages,
        )
    besides Exception as e:
        print("Error in completion", e)
        st.write("Error in completion", e)
        st.cease()

3. Pass the output from software calling to gpt-4o to generate an audio response:

Pass the software calling response together with the audio enter from the person to gpt-4o to generate the audio response.

        l_completion = shopper.chat.completions.create(
            mannequin=config.mannequin,
            modalities=["text", "audio"],
            audio={"voice": "alloy", "format": "wav"},
            messages=[
                {
                    "role": "system",
                    "content": [{"type": "text", "text": system_prompt_response}],
                },
                {
                    "position": "person",
                    "content material": [
                        {
                            "type": "text",
                            "text": "---- context -----n"+str(function_response) + "n --- User Query----:n",
                        },
                        {
                            "type": "input_audio",
                            "input_audio": {
                                "data": encoded_audio_string,
                                "format": "wav",
                            },
                        },
                    ],
                },
            ],
        )
        wav_bytes = base64.b64decode(l_completion.decisions[0].message.audio.knowledge)

4. Extract the textual content transcript of the response

In addition to enjoying the audio response over the speaker, we are able to additionally populate the chat dialog with the textual content transcript of the audio response.

        transcript_out = l_completion.decisions[0].message.audio.transcript
        st.session_state.messages.append(
            {
                "position": "assistant",
                "content material": transcript_out,
            }
        )

Note: The gpt-4o audio-preview API is just not out there in Azure as of this writing. This pattern makes use of the API from OpenAI instantly.

The pattern utility under, powered by gpt-4o audio-preview API, showcases a buyer querying their gaming progress and grievance standing. Tool integration is used to:

  • Pull data from an Azure SQL database for factors and achievements.
  • Perform Azure AI Search-driven Q&A over paperwork and person manuals.
  • Register and retrieve grievances through API integration with Jira Cloud.

See a demo of this utility in motion under

 

The code for this utility is on the market right here

HI-FI News

through Azure Feeds https://azurefeeds.com

December 19, 2024 at 06:56AM