Skip to content

Commit 64552b2

Browse files
authored
Merge pull request #250456 from glharper/openai-use-your-own-data-javascript
[Azure AI] [Azure OpenAI] Add Use Your Own Data quickstart for JS
2 parents 4578351 + 39782da commit 64552b2

File tree

3 files changed

+161
-0
lines changed

3 files changed

+161
-0
lines changed
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
---
2+
services: cognitive-services
3+
manager: nitinme
4+
author: glharper
5+
ms.author: glharper
6+
ms.service: cognitive-services
7+
ms.subservice: openai
8+
ms.topic: include
9+
ms.date: 09/06/2023
10+
---
11+
12+
[!INCLUDE [Set up required variables](./use-your-data-common-variables.md)]
13+
14+
15+
## Create a Node application
16+
17+
In a console window (such as cmd, PowerShell, or Bash), create a new directory for your app, and navigate to it. Then run the `npm init` command to create a node application with a _package.json_ file.
18+
19+
```console
20+
npm init
21+
```
22+
23+
## Install the client library
24+
25+
Install the Azure OpenAI client and Azure Identity libraries for JavaScript with npm:
26+
27+
```console
28+
npm install @azure/openai @azure/identity
29+
```
30+
31+
Your app's _package.json_ file will be updated with the dependencies.
32+
33+
## Create a sample application
34+
35+
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.
36+
37+
```javascript
38+
const { OpenAIClient } = require("@azure/openai");
39+
const { DefaultAzureCredential } = require("@azure/identity")
40+
41+
// Set the Azure and Cognitive Search values from environment variables
42+
const endpoint = process.env["AOAIEndpoint"];
43+
const azureApiKey = process.env["AOAIKey"];
44+
const searchEndpoint = process.env["SearchEndpoint"];
45+
const searchKey = process.env["SearchKey"];
46+
const searchIndex = process.env["SearchIndex"];
47+
const deploymentId = process.env["AOAIDeploymentId"];
48+
49+
async function main() {
50+
console.log("== Chat Using Your Own Data Sample ==");
51+
const client = new OpenAIClient(endpoint, new AzureKeyCredential(azureApiKey));
52+
53+
const messages = [
54+
{ role: "user", content: "What are the differences between Azure Machine Learning and Azure AI services?" },
55+
];
56+
57+
// Get chat responses from Azure OpenAI deployment using your own data via Azure Cognitive Search
58+
const events = client.listChatCompletions(deploymentId, messages, {
59+
azureExtensionOptions: {
60+
extensions: [
61+
{
62+
type: "AzureCognitiveSearch",
63+
parameters: {
64+
endpoint: searchEndpoint,
65+
key: searchKey,
66+
indexName: searchIndex,
67+
},
68+
},
69+
],
70+
},
71+
});
72+
73+
// Display chat responses
74+
for await (const event of events) {
75+
for (const choice of event.choices) {
76+
const delta = choice.delta?.content;
77+
const role = choice.delta?.role;
78+
if (delta && role) {
79+
console.log(`${role}: ${delta}`);
80+
81+
const contextMessages = choice.delta?.context?.messages;
82+
if (!!contextMessages) {
83+
console.log("===");
84+
85+
console.log("Context information (e.g. citations) from chat extensions:");
86+
console.log("===");
87+
for (const message of contextMessages) {
88+
// Display context included with chat responses (such as citations)
89+
console.log(message.content);
90+
}
91+
}
92+
}
93+
}
94+
}
95+
}
96+
97+
main().catch((err) => {
98+
console.error("The sample encountered an error:", err);
99+
});
100+
101+
module.exports = { main };
102+
```
103+
104+
> [!IMPORTANT]
105+
> For production, use a secure way of storing and accessing your credentials like [Azure Key Vault](../../../key-vault/general/overview.md). For more information about credential security, see the Azure AI services [security](../../security-features.md) article.
106+
107+
```cmd
108+
node.exe ChatWithOwnData.js
109+
```
110+
111+
## Output
112+
113+
```output
114+
== Chat With Your Own Data Sample ==
115+
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^].
116+
===
117+
Context information (e.g. citations) from chat extensions:
118+
===
119+
tool: {
120+
'citations': [
121+
{
122+
'content': '...',
123+
'id': null,
124+
'title': '...',
125+
'filepath': '...',
126+
'url': '...',
127+
'metadata': {
128+
"chunking': 'orignal document size=1011. Scores=3.6390076 and None.Org Highlight count=38.'
129+
},
130+
'chunk_id': '2'
131+
},
132+
...
133+
],
134+
'intent': '[\u0022What are the differences between Azure Machine Learning and Azure AI services?\u0022]'
135+
}
136+
137+
```
138+
139+
> [!div class="nextstepaction"]
140+
> [I ran into an issue when running the code sample.](https://microsoft.qualtrics.com/jfe/form/SV_0Cl5zkG3CnDjq6O?PLanguage=JAVASCRIPT&Pillar=AOAI&Product=ownData&Page=quickstart&Section=Create-application)

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,15 @@ zone_pivot_groups: openai-use-your-data
1616

1717
# Quickstart: Chat with Azure OpenAI models using your own data
1818

19+
::: zone pivot="programming-language-javascript"
20+
21+
[Reference](/javascript/api/@azure/openai) | [Source code](https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/openai/openai) | [Package (npm)](https://www.npmjs.com/package/@azure/openai) | [Samples](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/openai/Azure.AI.OpenAI/tests/Samples)
22+
23+
::: zone-end
24+
1925
In this quickstart you can use your own data with Azure OpenAI models. Using Azure OpenAI's models on your data can provide you with a powerful conversational AI platform that enables faster and more accurate communication.
2026

27+
2128
## Prerequisites
2229

2330
- An Azure subscription - <a href="https://azure.microsoft.com/free/cognitive-services" target="_blank">Create one for free</a>.
@@ -31,10 +38,16 @@ In this quickstart you can use your own data with Azure OpenAI models. Using Azu
3138

3239
- Be sure that you are assigned at least the [Cognitive Services Contributor](./how-to/role-based-access-control.md#cognitive-services-contributor) role for the Azure OpenAI resource.
3340

41+
::: zone pivot="programming-language-javascript"
42+
43+
- [LTS versions of Node.js](https://github.com/nodejs/release#release-schedule)
44+
45+
::: zone-end
3446

3547
> [!div class="nextstepaction"]
3648
> [I ran into an issue with the prerequisites.](https://microsoft.qualtrics.com/jfe/form/SV_0Cl5zkG3CnDjq6O?PLanguage=OVERVIEW&Pillar=AOAI&Product=ownData&Page=quickstart&Section=Prerequisites)
3749
50+
3851
[!INCLUDE [Connect your data to OpenAI](includes/connect-your-data-studio.md)]
3952

4053
::: zone pivot="programming-language-studio"
@@ -49,6 +62,12 @@ In this quickstart you can use your own data with Azure OpenAI models. Using Azu
4962

5063
::: zone-end
5164

65+
::: zone pivot="programming-language-javascript"
66+
67+
[!INCLUDE [JavaScript quickstart](includes/use-your-data-javascript.md)]
68+
69+
::: zone-end
70+
5271
::: zone pivot="rest-api"
5372

5473
[!INCLUDE [REST API quickstart](includes/use-your-data-rest.md)]

articles/zone-pivot-groups.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1800,6 +1800,8 @@ groups:
18001800
title: Studio
18011801
- id: programming-language-csharp
18021802
title: C#
1803+
- id: programming-language-javascript
1804+
title: JavaScript
18031805
- id: rest-api
18041806
title: REST
18051807
# Owner: pafarley

0 commit comments

Comments
 (0)