admin管理员组文章数量:1334808
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
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
Share Improve this question asked May 6, 2022 at 10:05 ebsigmaebsigma 651 silver badge6 bronze badges2 Answers
Reset to default 4It took me a little while to figure this out.
- Go to https://beta.openai./playground and choose the settings you want.
- Then, click "View code" in the top right.
- Select "curl" as the code type you want to view.
- Copy the "curl" code.
- Go to: https://reqbin./req/javascript/c-wyuctivp/convert-curl-to-javascript.
- Convert the code from "curl" to "javascript/AJAX". This will change it from "curl" to a javascript/AJAX XMLHttpRequest.
Use the javascript as you would normally. The code will console log the response. You can change that to use the response in some other way in your code.
Tested and working examples (You could just adjust these examples to meet your needs, but it may be best to create your own to get the exact results you want):
Regular prompt:
let open_ai_response;
openai_test();
async function openai_test() {
var url = "https://api.openai./v1/engines/text-davinci-002/pletions";
var xhr = new XMLHttpRequest();
xhr.open("POST", url);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.setRequestHeader("Authorization", "Bearer YOUR_OPEN_AI_KEY_GOES_HERE");
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
console.log(xhr.status);
console.log(xhr.responseText);
open_ai_response = xhr.responseText;
console.log(open_ai_response);
}};
var data = `{
"prompt": "YOUR TEXT HERE.",
"temperature": 0.7,
"max_tokens": 256,
"top_p": 1,
"frequency_penalty": 0.75,
"presence_penalty": 0
}`;
xhr.send(data);
}
Using variables for the prompt:
let open_ai_response;
openai_test();
async function openai_test() {
var prompt_text = "YOUR TEXT HERE."
var prompt_text2 = "MORE TEXT HERE."
var url = "https://api.openai./v1/engines/text-davinci-002/pletions";
var xhr = new XMLHttpRequest();
xhr.open("POST", url);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.setRequestHeader("Authorization", "Bearer YOUR_OPEN_AI_KEY_GOES_HERE");
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
console.log(xhr.status);
console.log(xhr.responseText);
open_ai_response = xhr.responseText;
console.log(open_ai_response);
}};
var data = `{
"prompt": "${prompt_text + prompt_text2}",
"temperature": 0.7,
"max_tokens": 256,
"top_p": 1,
"frequency_penalty": 0.75,
"presence_penalty": 0
}`;
xhr.send(data);
}
Posting the correct link that currently works:
var url = "https://api.openai./v1/pletions";
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.
本文标签: javascriptHow can I load the openai api configuration through js in htmlStack Overflow
版权声明:本文标题:javascript - How can I load the openai api configuration through js in html? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742344385a2457230.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论