/
getEmbeddings.ts
41 lines (31 loc) · 1.28 KB
/
getEmbeddings.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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
/**
* Demonstrates how to get embedding vectors for a piece of text using Azure OpenAI.
*
* @summary generates embedding vectors from a prompt using Azure OpenAI Get Embeddings.
* @azsdk-weight 100
*/
import { OpenAIClient, AzureKeyCredential } from "@azure/openai";
// Load the .env file if it exists
import * as dotenv from "dotenv";
import { parseOpenAIError } from "./parseOpenAIError.js";
dotenv.config();
// You will need to set these environment variables or edit the following values
const endpoint = process.env["ENDPOINT"] || "<endpoint>";
const azureApiKey = process.env["AZURE_API_KEY"] || "<api key>";
// The prompt to generate the embeddings vector
const prompt = ["This is the sample text to be embedded"];
// The name of the model deployment
const deploymentName = "text-embedding-ada-002";
export async function main() {
console.log("== Get embeddings sample ==");
const client = new OpenAIClient(endpoint, new AzureKeyCredential(azureApiKey));
const embeddings = await client.getEmbeddings(deploymentName, prompt);
for (const embeddingData of embeddings.data) {
console.log(`The embedding values are ${embeddingData.embedding}`);
}
}
main().catch((err) => {
parseOpenAIError(err);
});