Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use Azure Openai Realtime API in this #2

Open
ihuzaifashoukat opened this issue Dec 23, 2024 · 7 comments
Open

How to use Azure Openai Realtime API in this #2

ihuzaifashoukat opened this issue Dec 23, 2024 · 7 comments
Labels
help wanted Extra attention is needed question Further information is requested

Comments

@ihuzaifashoukat
Copy link

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 });
}

}`

@maige-app maige-app bot added help wanted Extra attention is needed question Further information is requested labels Dec 23, 2024
@cameronking4
Copy link
Owner

cameronking4 commented Dec 24, 2024

on it!

@cameronking4
Copy link
Owner

Looks like Azure OpenAI doesn't support WebRTC yet

@ihuzaifashoukat
Copy link
Author

ihuzaifashoukat commented Dec 24, 2024 via email

@cameronking4
Copy link
Owner

@ihuzaifashoukat Yeah, I can try to do a clone using websocket. In meantime I found this one https://github.com/Azure-Samples/aoai-realtime-audio-sdk

@ihuzaifashoukat
Copy link
Author

ihuzaifashoukat commented Dec 27, 2024 via email

@manoharant
Copy link

Does anyone know, when the Azure Openai with WebRTC support available?

@cameronking4 cameronking4 pinned this issue Jan 6, 2025
@LI-Jialu
Copy link

LI-Jialu commented Jan 11, 2025

I'm also looking for it, I also hope to use Azure api for voice chat webiste. I am wondering if it's possible to use https://github.com/Azure-Samples/aoai-realtime-audio-sdk as the backend? And deploy frontend like this repo?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed question Further information is requested
Projects
None yet
Development

No branches or pull requests

4 participants