-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathonYourData.ts
68 lines (60 loc) · 2.02 KB
/
onYourData.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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
/**
* Demonstrates how to use Azure's Bring Your Own Data with Azure OpenAI Chat Completions.
*
* @summary chat completions with your own data.
* @azsdk-weight 100
*/
import { AzureOpenAI } from "openai";
import { DefaultAzureCredential, getBearerTokenProvider } from "@azure/identity";
import "@azure/openai/types";
// 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";
// Your Azure Cognitive Search endpoint, and index name
const azureSearchEndpoint = process.env["AZURE_SEARCH_ENDPOINT"] || "<search endpoint>";
const azureSearchIndexName = process.env["AZURE_SEARCH_INDEX"] || "<search index>";
export async function main(): Promise<void> {
console.log("== On Your Data Sample ==");
const scope = "https://cognitiveservices.azure.com/.default";
const azureADTokenProvider = getBearerTokenProvider(new DefaultAzureCredential(), scope);
const deployment = "gpt-4-1106-preview";
const apiVersion = "2025-03-01-preview";
const client = new AzureOpenAI({ azureADTokenProvider, deployment, apiVersion });
const events = await client.chat.completions.create({
stream: true,
messages: [
{
role: "user",
content:
"What's the most common feedback we received from our customers about the product?",
},
],
max_tokens: 128,
model: "",
data_sources: [
{
type: "azure_search",
parameters: {
endpoint: azureSearchEndpoint,
index_name: azureSearchIndexName,
authentication: {
type: "system_assigned_managed_identity",
},
},
},
],
});
for await (const event of events) {
for (const choice of event.choices) {
if (choice.delta?.content) {
process.stdout.write(choice.delta.content);
}
}
}
}
main().catch((err) => {
console.error("The sample encountered an error:", err);
});