Search code examples
javascripthtmlnode.jsopenai-api

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


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:

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


Solution

  • 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:

    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.