Skip to content

Instantly share code, notes, and snippets.

@pietheinstrengholt
Last active August 29, 2023 14:48
Show Gist options
  • Select an option

  • Save pietheinstrengholt/dff8b87d3520f527015954f24f5ec255 to your computer and use it in GitHub Desktop.

Select an option

Save pietheinstrengholt/dff8b87d3520f527015954f24f5ec255 to your computer and use it in GitHub Desktop.
Azure OpenAI demo using azure-sdk-for-js
OPENAI_API_HOST=https://endpointname.openai.azure.com/
OPENAI_API_KEY=xxxxxxxxxxxxxxxxxxxxxx
AZURE_DEPLOYMENT_ID=text-davinci-003
import { OpenAIClient, AzureKeyCredential } from "@azure/openai";
// Load the .env file if it exists
import * as dotenv from "dotenv";
dotenv.config();
// You will need to set these environment variables or edit the following values
const endpoint = process.env["OPENAI_API_HOST"] || "<endpoint>";
const azureApiKey = process.env["OPENAI_API_KEY"] || "<api key>";
const deploymentId = process.env["AZURE_DEPLOYMENT_ID"] || "<text-davinci-003>";
const prompt = ["What is Azure OpenAI?"];
export async function main() {
const client = new OpenAIClient(endpoint, new AzureKeyCredential(azureApiKey));
const result = await client.getCompletions(deploymentId, prompt, { maxTokens: 2048 });
console.log(result.choices[0].text);
}
main().catch((err) => {
console.error("The sample encountered an error:", err);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment