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.

所以目前你有两个选择:使用fetch或axios进行原始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},
      }
   }),
);
相关推荐
2501_944521594 小时前
Ramda 跨端三方库仓颉适配全流程
开发语言·javascript·flutter·php
Ken_11156 小时前
小体积附件下载正常,大附件下载异常
javascript
wangchunyu1146 小时前
JAVA CompletableFuture异步编程实战
java·开发语言·python
变与不变8067 小时前
jQuery怎么使用
前端·javascript
paopaokaka_luck7 小时前
流浪动物救助与收养系统(DeepSeek AI对话、 ECharts多维分析、救助流程衔接、领养与回访衔接、 捐赠确认与公示、志愿者协作)
java·前端·javascript·人工智能·mysql·echarts·推荐算法
仰望.7 小时前
vue 表格组件 vxe-table 树表格懒加载支持拖拽排序的详细用法
前端·javascript·vue.js·vxe-table
2501_944521598 小时前
Immer 跨端三方库仓颉适配全流程
python·eclipse·django
警醒与鞭策8 小时前
Codex桌面版与Claude桌面版使用设置
android·unity·ai·性能优化
信誓旦旦的程序猿8 小时前
【Python量化实战宝典 #26】止损与回撤控制手册:移动止损、最大回撤(Python 可跑)
python·1024程序员节·股票数据api·股票数据api接口·股票api数据接口·股票量化数据api·股票量化数据接口
2501_944521598 小时前
Deepmerge 跨端三方库仓颉适配全流程
java·python·eclipse·github