JSFiddle - React, Tailwind, and code Playground
by Lazaro Contreras
HTML
<textarea id="entrada"></textarea>
<button id="boton">Solicitar</button>
<textarea id="respuesta"></textarea>
JavaScript
const promptBase = `
With the following sentence fill in this json if you can't find the data, leave it blank.
json:
\´\´\´
{
"clientId": "",
"firstName": "",
"lastName": "",
"amount": 0.0,
"periods": 0,
"product": ""
}
\`\`\`
sentence:
`
const OPENAI_API_KEY = "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";
boton.onclick = () => {
let prompt = entrada.value;
fetch("https://api.openai.com/v1/chat/completions", {
method: "POST",
headers: {
"Authorization": `Bearer ${OPENAI_API_KEY}`,
"Content-Type": "application/json"
},
body: JSON.stringify({
"model": "gpt-3.5-turbo",
"messages": [
{"role": "user", "content": promptBase + prompt},
],
"temperature": 2
})
})
.then(response => response.json())
.then(data => {
console.log(data);
respuesta.value = data.choices[0].message.content
}
)
.catch(error => console.error(error));
}