How can I load the openai api configuration through js in html?

题意:怎样在HTML中通过JavaScript加载OpenAI API配置

问题背景:

I am trying to send a request through js in my html so that openai analyzes it and sends a response, but if in the js I put the following:

我正在尝试通过HTML中的JavaScript发送一个请求,以便让OpenAI分析它并发送响应,但如果我在JavaScript中放入以下内容:

const { Configuration, OpenAIApi } = require("openai");

const configuration = new Configuration({
apiKey: "sk-0000000000000ZXXXXXXXXXXXXXX",
});
const openai = new OpenAIApi(configuration);

async function test() {
console("test")
const response = await openai.createCompletion("text-davinci-002", {
prompt: "hello",
temperature: 0.7,
max_tokens: 64,
top_p: 1,
frequency_penalty: 0,
presence_penalty: 0,
});
console.log(response)
}
test();

return console these error        在控制台返回错误信息

Uncaught ReferenceError: require is not defined
at buttons.js:94:38

I have tried to install it with node.js and it works fine but I don't know how to make it work in my own html

“我已经尝试使用node.js安装它,并且它工作正常,但我不知道如何在我的HTML中使其工作”

问题解决:

Posting the correct link that currently works:

“发布当前有效的正确链接:”

var url = "https://api.openai.com/v1/completions";

and here is how the data should look like for question answering with davinchi-003 engine:

“以下是使用davinci-003引擎进行问答时数据应有的样子:”

var data = `{
              "model": "text-davinci-003",
              "prompt": "${prompt_text+question_out}",
              "temperature": 0.9,
              "max_tokens": 150,
              "top_p": 1,
              "frequency_penalty": 0.0,
              "presence_penalty": 0.6,
              "stop": [" Human:", " AI:"]
            }`;

here the prompt_text is the prompt text I sent o the engine and question_out is the question I want the engine to answer based on the prompt.

“在这里,prompt_text 是我发送给引擎的提示文本,而 question_out 是我希望引擎根据提示来回答的问题。”

How can I load the openai api configuration through js in html?_第1张图片

你可能感兴趣的:(AI,ai,python,javascript,html,node.js,openai)