Azure OpenAI与JavaScript的连接

hivapdat  于 2023-04-12  发布在  Java
关注(0)|答案(1)|浏览(240)

我已经用javascript创建了我的聊天机器人,并使用了open ai。我需要将其更改为azure open ai,但无法找到javascript的连接细节。这是我如何与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")

下面是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);

所以我需要javascript中的连接

9rnv2umw

9rnv2umw1#

我相信OpenAI package还不支持Azure端点。你可以在GitHub仓库上参考this open issue
所以现在你有两个选择:使用fetchaxios进行原始API调用。您可以在Azure网站上参考API reference documentation

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();

或者使用azure-api 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},
      }
   }),
);

相关问题