Azure openai connection with javascript

**题意:**使用JavaScript与Azure OpenAI进行连接

问题背景:

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 :

我已经使用JavaScript创建了聊天机器人,并使用了OpenAI。我现在需要将其更改为Azure OpenAI,但找不到用于JavaScript的连接详细信息。以下是我如何使用Python进行连接的方法:

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 :

这是用于OpenAI的JavaScript代码:

python 复制代码
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 javascript

所以,我需要的是JavaScript中的连接(方式/代码)。

问题解决:

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

我相信OpenAI包目前还不支持Azure端点。您可以参考GitHub仓库上的这个开放问题。

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.

所以目前你有两个选择:使用fetchaxios进行原始API调用。你可以参考Azure网站上的API参考文档。

python 复制代码
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

或者使用azure-openai

python 复制代码
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},
      }
   }),
);
相关推荐
还是大剑师兰特12 小时前
用豆包生成PPT的详细操作步骤
ai·powerpoint·大剑师
OpenAnolis小助手13 小时前
基于Anolis OS的国产CPU性能优化实践,共推多芯混部时代操作系统新范式
ai·性能优化·开源·操作系统·龙蜥社区·openanolis
tang&13 小时前
【Python自动化测试】Selenium常用函数详解
开发语言·python·selenium
蓝冰露13 小时前
把chatgpt聊天记录保存下来
chrome·ai·chatgpt·大模型·markdown·chrome devtools
路边草随风18 小时前
milvus向量数据库使用尝试
人工智能·python·milvus
m0_4711996318 小时前
【vue】通俗详解package-lock文件的作用
前端·javascript·vue.js
newobut18 小时前
vscode远程调试python程序,基于debugpy库
vscode·python·调试·debugpy
今天不要写bug19 小时前
vue项目基于vue-cropper实现图片裁剪与图片压缩
前端·javascript·vue.js·typescript
APIshop19 小时前
用 Python 把“API 接口”当数据源——从找口子到落库的全流程实战
开发语言·python