Skip to content

Commit 7fff5bb

Browse files
authored
Merge pull request #268004 from aahill/js-update
JavaScript code update
2 parents 2f2c3c2 + 5a698cb commit 7fff5bb

File tree

2 files changed

+26
-54
lines changed

2 files changed

+26
-54
lines changed

articles/ai-services/openai/includes/use-your-data-javascript.md

Lines changed: 25 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ author: glharper
55
ms.author: glharper
66
ms.service: azure-ai-openai
77
ms.topic: include
8-
ms.date: 09/06/2023
8+
ms.date: 03/04/2024
99
---
1010

1111
[!INCLUDE [Set up required variables](./use-your-data-common-variables.md)]
@@ -33,9 +33,10 @@ Your app's _package.json_ file will be updated with the dependencies.
3333

3434
Open a command prompt where you want the new project, and create a new file named ChatWithOwnData.js. Copy the following code into the ChatWithOwnData.js file.
3535

36+
37+
3638
```javascript
37-
const { OpenAIClient } = require("@azure/openai");
38-
const { DefaultAzureCredential } = require("@azure/identity")
39+
const { OpenAIClient, AzureKeyCredential } = require("@azure/openai");
3940

4041
// Set the Azure and AI Search values from environment variables
4142
const endpoint = process.env["AZURE_OPENAI_ENDPOINT"];
@@ -45,58 +46,49 @@ const searchKey = process.env["AZURE_AI_SEARCH_API_KEY"];
4546
const searchIndex = process.env["AZURE_AI_SEARCH_INDEX"];
4647
const deploymentId = process.env["AZURE_OPEN_AI_DEPLOYMENT_ID"];
4748

48-
async function main() {
49-
console.log("== Chat Using Your Own Data Sample ==");
49+
50+
async function main(){
5051
const client = new OpenAIClient(endpoint, new AzureKeyCredential(azureApiKey));
5152

5253
const messages = [
53-
{ role: "user", content: "What are the differences between Azure Machine Learning and Azure AI services?" },
54+
{ role: "user", content: "Tell me something interesting" },
5455
];
5556

56-
// Get chat responses from Azure OpenAI deployment using your own data via Azure AI Search
57-
const events = client.listChatCompletions(deploymentId, messages, {
57+
console.log(`Message: ${messages.map((m) => m.content).join("\n")}`);
58+
59+
const events = await client.streamChatCompletions(deploymentId, messages, {
60+
maxTokens: 128,
5861
azureExtensionOptions: {
5962
extensions: [
6063
{
6164
type: "AzureCognitiveSearch",
62-
parameters: {
63-
endpoint: searchEndpoint,
64-
key: searchKey,
65-
indexName: searchIndex,
66-
},
65+
endpoint: searchEndpoint,
66+
key: searchKey,
67+
indexName: searchIndex,
6768
},
6869
],
6970
},
7071
});
71-
72-
// Display chat responses
72+
let response = "";
7373
for await (const event of events) {
7474
for (const choice of event.choices) {
75-
const delta = choice.delta?.content;
76-
const role = choice.delta?.role;
77-
if (delta && role) {
78-
console.log(`${role}: ${delta}`);
79-
80-
const contextMessages = choice.delta?.context?.messages;
81-
if (!!contextMessages) {
82-
console.log("===");
83-
84-
console.log("Context information (e.g. citations) from chat extensions:");
85-
console.log("===");
86-
for (const message of contextMessages) {
87-
// Display context included with chat responses (such as citations)
88-
console.log(message.content);
89-
}
90-
}
75+
const newText = choice.delta?.content;
76+
if (!!newText) {
77+
response += newText;
78+
// To see streaming results as they arrive, uncomment line below
79+
// console.log(newText);
9180
}
9281
}
9382
}
83+
console.log(response);
9484
}
9585

9686
main().catch((err) => {
9787
console.error("The sample encountered an error:", err);
9888
});
9989

90+
91+
10092
module.exports = { main };
10193
```
10294
@@ -110,28 +102,8 @@ node.exe ChatWithOwnData.js
110102
## Output
111103
112104
```output
113-
== Chat With Your Own Data Sample ==
114-
assistant: Azure Machine Learning is a cloud-based service that provides tools and services to build, train, and deploy machine learning models. It offers a collaborative environment for data scientists, developers, and domain experts to work together on machine learning projects. Azure Machine Learning supports various programming languages, frameworks, and libraries, including Python, R, TensorFlow, and PyTorch [^1^].
115-
===
116-
Context information (e.g. citations) from chat extensions:
117-
===
118-
tool: {
119-
'citations': [
120-
{
121-
'content': '...',
122-
'id': null,
123-
'title': '...',
124-
'filepath': '...',
125-
'url': '...',
126-
'metadata': {
127-
"chunking': 'orignal document size=1011. Scores=3.6390076 and None.Org Highlight count=38.'
128-
},
129-
'chunk_id': '2'
130-
},
131-
...
132-
],
133-
'intent': '[\u0022What are the differences between Azure Machine Learning and Azure AI services?\u0022]'
134-
}
105+
Message: What are the differences between Azure Machine Learning and Azure AI services?
106+
Based on the retrieved document, an interesting fact is...
135107

136108
```
137109

articles/ai-services/openai/use-your-data-quickstart.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ ms.custom: devx-track-dotnet, devx-track-extended-java, devx-track-js, devx-trac
99
ms.topic: quickstart
1010
author: aahill
1111
ms.author: aahi
12-
ms.date: 11/22/2023
12+
ms.date: 03/04/2024
1313
recommendations: false
1414
zone_pivot_groups: openai-use-your-data
1515
---

0 commit comments

Comments
 (0)