-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathws.ts
76 lines (63 loc) · 2.18 KB
/
ws.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
/**
* Demonstrates how to use the Realtime Websocket API to interact with the OpenAI service.
*
* @summary converse with Realtime API.
* @azsdk-weight 100
*/
import { DefaultAzureCredential, getBearerTokenProvider } from "@azure/identity";
import { OpenAIRealtimeWS } from "openai/beta/realtime/ws";
import { AzureOpenAI } from "openai";
// Set AZURE_OPENAI_ENDPOINT to the endpoint of your
// OpenAI resource. You can find this in the Azure portal.
// Load the .env file if it exists
import "dotenv/config";
async function main(): Promise<void> {
const cred = new DefaultAzureCredential();
const scope = "https://cognitiveservices.azure.com/.default";
const deploymentName = "gpt-4o-realtime-preview-1001";
const azureADTokenProvider = getBearerTokenProvider(cred, scope);
const client = new AzureOpenAI({
azureADTokenProvider,
apiVersion: "2024-10-01-preview",
deployment: deploymentName,
});
const rt = await OpenAIRealtimeWS.azure(client);
// access the underlying `ws.WebSocket` instance
rt.socket.on("open", () => {
console.log("Connection opened!");
rt.send({
type: "session.update",
session: {
modalities: ["text"],
model: "gpt-4o-realtime-preview",
},
});
rt.send({
type: "conversation.item.create",
item: {
type: "message",
role: "user",
content: [{ type: "input_text", text: "Say a couple paragraphs!" }],
},
});
rt.send({ type: "response.create" });
});
rt.on("error", (err) => {
// in a real world scenario this should be logged somewhere as you
// likely want to continue procesing events regardless of any errors
throw err;
});
rt.on("session.created", (event) => {
console.log("session created!", event.session);
console.log();
});
rt.on("response.text.delta", (event) => process.stdout.write(event.delta));
rt.on("response.text.done", () => console.log());
rt.on("response.done", () => rt.close());
rt.socket.on("close", () => console.log("\nConnection closed!"));
}
main().catch((err) => {
console.error("The sample encountered an error:", err);
});