Node.js library for the Azure OpenAI API
npm install azure-openaibash
$ npm install azure-openai
`
Usage
The library needs to be configured with your Azure OpenAI's key, endpoint and deploymentId. you can obtain these credentials from Azure Portal. Please see the below screenshot:
!image
To migrate from the official OpenAI model to the Azure OpenAI model, you can just simply add azure info into configuration to migrate, that is it. You donot need to change any code. Please see the below steps:
1. Install the library by running the following command:
`bash
npm install azure-openai
`
2. Update the import statement from "openai" to "azure-openai":
`typescript
//import { Configuration, OpenAIApi } from "openai";
import { Configuration, OpenAIApi } from "azure-openai";
`
3. Add the Azure OpenAI information to your project configuration:
`typescript
this.openAiApi = new OpenAIApi(
new Configuration({
apiKey: this.apiKey,
// add azure info into configuration
azure: {
apiKey: {your-azure-openai-resource-key},
endpoint: {your-azure-openai-resource-endpoint},
// deploymentName is optional, if you donot set it, you need to set it in the request parameter
deploymentName: {your-azure-openai-resource-deployment-name},
}
}),
);
`
4. run your code. That's it.
5. optional, if you use stream = true. you may need to change response
`
// Azure OpenAI donot response delta data, so you need to change the response to text
// const delta = parsed.choices[0].delta.content;
const delta = parsed.choices[0].text;
`
6. optional, you can also set your Azure deploymentName by replacing your model with deployment name, like this:
`
const response = await this.openAiApi.createCompletion({
model: {your-azure-openai-resource-deployment-name},
prompt: prompt,
maxTokens: 100,
temperature: 0.9,
topP: 1,
presencePenalty: 0,
frequencyPenalty: 0,
bestOf: 1,
});
``