Skip to content

How to use Azure Openai Realtime API in this #2

@ihuzaifashoukat

Description

@ihuzaifashoukat

Hey,

I am using azure openai services and i tried adding those details but failed to do it was getting 400 error Below is the api route example.

`import { NextResponse } from 'next/server';
import { WebSocket } from 'ws';

export async function POST(request) {
try {
const apiKey = process.env.OPENAI_API_KEY;
if (!apiKey) {
throw new Error('OPENAI_API_KEY is not set');
}

    const apiVersion = '2024-10-01-preview';
    const url = `azure URL`;

    return new Promise((resolve, reject) => {
        const ws = new WebSocket(url, {
            headers: {
                'api-key': apiKey,
            }
        });

        ws.on('open', () => {
            console.log('WebSocket connection established');
            const message = JSON.stringify({
                voice: 'alloy',
                modalities: ['audio', 'text'],
                instructions: "Start conversation with the user by saying 'Hello, how can I help you today?'",
                tools: [
                    {
                        type: 'function',
                        name: 'getPageHTML',
                        description: 'Gets the HTML for the current page',
                        parameters: {
                            type: 'object',
                            properties: {}
                        }
                    },
                    {
                        type: 'function',
                        name: 'getWeather',
                        description: 'Gets the current weather',
                        parameters: {
                            type: 'object',
                            properties: {}
                        }
                    },
                    {
                        type: 'function',
                        name: 'getCurrentTime',
                        description: 'Gets the current time',
                        parameters: {
                            type: 'object',
                            properties: {}
                        }
                    },
                ],
                tool_choice: 'auto',
            });
            ws.send(message);
        });

        ws.on('message', (data) => {
            console.log('Received:', data);
            const parsedData = JSON.parse(data);
            ws.close();
            resolve(NextResponse.json(parsedData));
        });

        ws.on('error', (error) => {
            console.error('WebSocket error:', error);
            reject(error);
        });

        ws.on('close', (code, reason) => {
            console.log(`WebSocket closed with code ${code} and reason: ${reason}`);
        });
    });
} catch (error) {
    console.error('Error in session API:', error);
    return NextResponse.json({ error: error.message || 'An unexpected error occurred' }, { status: 500 });
}

}import { NextResponse } from 'next/server';
import { WebSocket } from 'ws';

export async function POST(request) {
try {
const apiKey = process.env.OPENAI_API_KEY;
if (!apiKey) {
throw new Error('OPENAI_API_KEY is not set');
}

    const apiVersion = '2024-10-01-preview';
    const url = `azure URL`;

    return new Promise((resolve, reject) => {
        const ws = new WebSocket(url, {
            headers: {
                'api-key': apiKey,
            }
        });

        ws.on('open', () => {
            console.log('WebSocket connection established');
            const message = JSON.stringify({
                voice: 'alloy',
                modalities: ['audio', 'text'],
                instructions: "Start conversation with the user by saying 'Hello, how can I help you today?'",
                tools: [
                    {
                        type: 'function',
                        name: 'getPageHTML',
                        description: 'Gets the HTML for the current page',
                        parameters: {
                            type: 'object',
                            properties: {}
                        }
                    },
                    {
                        type: 'function',
                        name: 'getWeather',
                        description: 'Gets the current weather',
                        parameters: {
                            type: 'object',
                            properties: {}
                        }
                    },
                    {
                        type: 'function',
                        name: 'getCurrentTime',
                        description: 'Gets the current time',
                        parameters: {
                            type: 'object',
                            properties: {}
                        }
                    },
                ],
                tool_choice: 'auto',
            });
            ws.send(message);
        });

        ws.on('message', (data) => {
            console.log('Received:', data);
            const parsedData = JSON.parse(data);
            ws.close();
            resolve(NextResponse.json(parsedData));
        });

        ws.on('error', (error) => {
            console.error('WebSocket error:', error);
            reject(error);
        });

        ws.on('close', (code, reason) => {
            console.log(`WebSocket closed with code ${code} and reason: ${reason}`);
        });
    });
} catch (error) {
    console.error('Error in session API:', error);
    return NextResponse.json({ error: error.message || 'An unexpected error occurred' }, { status: 500 });
}

}`

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedExtra attention is neededquestionFurther information is requested

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions