|
| 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) |
0 commit comments