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},
      }
   }),
);
相关推荐
颜酱11 分钟前
用导游的例子来理解 Visitor 模式,实现AST 转换
前端·javascript·算法
七牛云行业应用15 分钟前
从API调用到智能体编排:GPT-5时代的AI开发新模式
大数据·人工智能·gpt·openai·agent开发
StarPrayers.17 分钟前
用 PyTorch 搭建 CIFAR10 线性分类器:从数据加载到模型推理全流程解析
人工智能·pytorch·python
程序员杰哥19 分钟前
UI自动化测试实战:从入门到精通
自动化测试·软件测试·python·selenium·测试工具·ui·职场和发展
木易 士心20 分钟前
Nginx 基本使用和高级用法详解
运维·javascript·nginx
SunnyRivers22 分钟前
通俗易懂理解python yield
python
mortimer22 分钟前
Python 进阶:彻底理解类属性、类方法与静态方法
后端·python
知其然亦知其所以然30 分钟前
SpringAI让Java会画画?用Azure OpenAI生成AI图片太惊艳了!
后端·spring·openai
幸运小圣37 分钟前
Iterator迭代器 【ES6】
开发语言·javascript·es6
葱头的故事38 分钟前
将传给后端的数据转换为以formData的类型传递
开发语言·前端·javascript