OpenAI公布了五项重大创新,其中“实时 API”(Realtime API)的新功能,使得开发者能够创建具有低延迟、AI 生成的语音响应功能的应用程序。尽管这一功能不完全等同于 ChatGPT 的高级语音模式,但其能力已经非常接近,旨在帮助开发者为用户提供近乎实时的语音到语音互动体验。除此之外,OpenAI 还发布了其他一系列新功能,旨在进一步提升开发者的 AI 应用构建体验。
包括实时语音API、提示词缓存、模型蒸馏、视觉微调、新Playground。
这些创新将改变开发者与AI互动的方式,并有助于降低AI模型的使用成本,推动多模式的语音、视觉等应用的普及。
个人体验
项目本地部署
OpenAI 在 github 上面发布了最新的 realtime-api开源项目。该项目是 React 应用程序,用于使用 Realtime API 进行检查、构建和调试。
https://github.com/openai/openai-realtime-console.git
Realtime API 能够构建低延迟、多模式的对话体验。它目前支持将文本和音频作为输入和输出,以及函数调用。
API 的一些显著优势包括:
本机语音转语音:没有文本中介意味着低延迟、细致入微的输出。
自然、可操纵的声音:这些模特具有自然的语调变化,可以大笑、耳语并遵循语气方向。
同步多模态输出:文本有助于审核,比实时更快的音频确保稳定播放。
项目运行需要 node 环境,还有 openai 的 API KEY,并且需要有余额。
#拉取仓库代码gitclonehttps://github.com/openai/openai-realtime-console.git
cdopenai-realtime-console#安装依赖包npmi
#启动服务npmstart
启动后需要先填入 OpenAI API Key。
进入后就能够看到主界面。
点击右下角的 "connect" 后就会询问开启麦克风。
连接成功后即可进行语音实时聊天。
官方定价
从目前的定价来看,是贵的很,文字1M输入是5刀、输出是20刀,而语音1M输入是100刀、输出是200刀。玩不起
官方Realtime API使用示例
Realtime API 是一种基于事件的有状态 API,通过 WebSocket 进行通信。下面是一个使用 Node.js 中流行的 ws 库建立套接字连接、从客户端发送消息以及从服务器接收响应的简单示例。
import WebSocket from "ws";const url = "wss://api.openai.com/v1/realtime?model=gpt-4o-realtime-preview-2024-10-01";const ws = new WebSocket(url, {headers: {"Authorization": "Bearer " + process.env.OPENAI_API_KEY,"OpenAI-Beta": "realtime=v1",},});ws.on("open", function open() {console.log("Connected to server.");ws.send(JSON.stringify({type: "response.create",response: {modalities: ["text"],instructions: "lease assist the user.",
}}));});ws.on("message", function incoming(message) {console.log(JSON.parse(message.toString()));});
发送文本
constevent={type:'conversation.item.create',item:{type:'message',role:'user',content:[{type:'input_text',text:'Hello!'}]}};ws.send(JSON.stringify(event));ws.send(JSON.stringify({type:'response.create'}));发送音频
import fs from 'fs';import decodeAudio from 'audio-decode';// Converts Float32Array of audio data to PCM16 ArrayBufferfunction floatTo16BitPCM(float32Array) {const buffer = new ArrayBuffer(float32Array.length * 2);const view = new DataView(buffer);let offset = 0;for (let i = 0; i < float32Array.length; i++, offset += 2) {let s = Math.max(-1, Math.min(1, float32Array[i]));view.setInt16(offset, s < 0 ? s * 0x8000 : s * 0x7fff, true);}return buffer;}// Converts a Float32Array to base64-encoded PCM16 database64EncodeAudio(float32Array) {const arrayBuffer = floatTo16BitPCM(float32Array);let binary = '';let bytes = new Uint8Array(arrayBuffer);const chunkSize = 0x8000; // 32KB chunk sizefor (let i = 0; i < bytes.length; i += chunkSize) {let chunk = bytes.subarray(i, i + chunkSize);binary += String.fromCharCode.apply(null, chunk);}return btoa(binary);}// Using the "audio-decode" library to get raw audio bytesconst myAudio = fs.readFileSync('./path/to/audio.wav');const audioBuffer = await decodeAudio(myAudio);const channelData = audioBuffer.getChannelData(0); // only accepts monoconst base64AudioData = base64EncodeAudio(channelData);const event = {type: 'conversation.item.create',item: {type: 'message',role: 'user',content: [{type: 'input_audio',audio: base64AudioData}]}};ws.send(JSON.stringify(event));ws.send(JSON.stringify({type: 'response.create'}));
更多示例可以阅览官方的说明文档。
https://platform.openai.com/docs/guides/realtime
TO B:Azure-Open AI
Azure-OPen AI- audio-real-time 接口已经上线,大家可以通过Azure 国际站申请测试使用,非常方便
https://learn.microsoft.com/zh-cn/azure/ai-services/openai/how-to/audio-real-time
| 欢迎光临 链载Ai (https://www.lianzai.com/) | Powered by Discuz! X3.5 |