AI Quickstart
Get up and running with the Azure OpenAI service using the official JavaScript SDK.
Prerequisites
- Node.js ≥ 14
- An Azure subscription with an Azure OpenAI resource
- API key and endpoint for the OpenAI resource
Install the SDK
npm install @azure/openai
Sample Code
import { OpenAIClient, AzureKeyCredential } from "@azure/openai";
const endpoint = "YOUR_ENDPOINT";
const apiKey = "YOUR_API_KEY";
const client = new OpenAIClient(endpoint, new AzureKeyCredential(apiKey));
async function ask(question) {
const result = await client.getCompletions("deployment-name", [
{ role: "user", content: question }
]);
return result.choices[0].message.content;
}
// Example usage
ask("Explain quantum computing in simple terms.")
.then(console.log)
.catch(console.error);