1

I have created my chatbot with javascript and used open ai. I need to change it to azure open ai but can not find the connection details for javascript. This is how i connect with python :

import os
import openai
openai.api_type = "azure"
openai.api_base = "https://test-azure-openai-d.openai.azure.com/"
openai.api_version = "2022-12-01"
openai.api_key = os.getenv("OPENAI_API_KEY")

This was the code in js for openai :

import express from 'express';
import * as dotenv from 'dotenv';
import cors from 'cors';
import { Configuration, OpenAIApi } from 'openai';

dotenv.config()

//console.log(process.env.OPENAI_API_KEY)
const configuration = new Configuration({
  apiKey: process.env.OPENAI_API_KEY,
});

const openai = new OpenAIApi(configuration);

So i would need the connection in javascrip

Talha Tayyab
  • 8,111
  • 25
  • 27
  • 44
Giorgi
  • 15
  • 3

2 Answers2

0

I believe that the OpenAI package does not support Azure endpoints yet. You can refer to this open issue on the GitHub repo.

So for now you have two options: Make raw API calls using fetch or axios. You can refer to API reference documentation on Azure website.

const basePath = "https://test-azure-openai-d.openai.azure.com/"
const apiVersion = "2022-12-01"
const apiKey = process.env.OPENAI_API_KEY

const url = `${basePath}/openai/deployments/${data.model}/completions?api-version=${apiVersion}`;

  const response = await fetch(url, {
     method: 'POST',
     headers: {
       'Content-Type': 'application/json',
        'api-key': `${apiKey}`,
     },
     body: JSON.stringify(data),
  });

  return await response.json();

Or use the azure-openai package

import { Configuration, OpenAIApi } from "azure-openai"; 

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},
      }
   }),
);
mvermand
  • 5,829
  • 7
  • 48
  • 74
picsoung
  • 6,314
  • 1
  • 18
  • 35
  • Hello @picsoung, thank you so much for your answer, i have put the 1st code into my script and i just get one error: file:///C:/Users/gyorg/Desktop/OpenAI/server/server.js:20 const url = `${basePath}/openai/deployments/${data.model}/completions?api-version=${apiVersion}`; ^ ReferenceError: data is not defined Datamodel is not defined - what should i write here And i commented out the response part – Giorgi Apr 06 '23 at 08:24
  • According to the [API reference](https://learn.microsoft.com/en-us/azure/cognitive-services/openai/reference) it refers to `The deployment name you chose when you deployed the model.` – picsoung Apr 06 '23 at 20:03
0

You can now use the Azure OpenAi library

navotgil
  • 329
  • 5
  • 16