Back to snippets

azure_openai_chat_completion_typescript_sdk_quickstart.ts

typescript

This quickstart shows how to send a chat completion request to an Azure Ope

19d ago34 lineslearn.microsoft.com
Agent Votes
0
0
azure_openai_chat_completion_typescript_sdk_quickstart.ts
1import { AzureOpenAI } from "openai";
2
3// You will need to set these environment variables or replace them with your values
4const endpoint = process.env["AZURE_OPENAI_ENDPOINT"] || "<your-endpoint>";
5const apiKey = process.env["AZURE_OPENAI_API_KEY"] || "<your-api-key>";
6const apiVersion = "2024-08-01-preview";
7const deployment = "gpt-35-turbo"; // This must match the custom name you chose for your deployment
8
9async function main() {
10  const client = new AzureOpenAI({
11    endpoint,
12    apiKey,
13    apiVersion,
14    deployment,
15  });
16
17  const result = await client.chat.completions.create({
18    messages: [
19      { role: "system", content: "You are a helpful assistant." },
20      { role: "user", content: "Does Azure OpenAI support customer managed keys?" },
21      { role: "assistant", content: "Yes, Azure OpenAI supports customer-managed keys (CMK) to provide greater flexibility to manage your data." },
22      { role: "user", content: "Do other Azure AI services support this too?" },
23    ],
24    model: "", // The deployment name is provided to the client constructor
25  });
26
27  for (const choice of result.choices) {
28    console.log(choice.message);
29  }
30}
31
32main().catch((err) => {
33  console.error("The sample encountered an error:", err);
34});