Skip to content

Instantly share code, notes, and snippets.

@titomus
Created April 21, 2022 09:31
Show Gist options
  • Save titomus/99d15c966be4119d58ff20460622561d to your computer and use it in GitHub Desktop.
Save titomus/99d15c966be4119d58ff20460622561d to your computer and use it in GitHub Desktop.
async function writeAboutTest(prompt,keywords = 'vie,ce que',exemple= '1.', engine = "davinci-instruct-beta-v3") { //text-davinci-002
let text = "";
/*
1. intro
2. sous titre
content
sous titre
content
3. conclusion
*/
//sommaire
promptSummary = `Créer un sommaire pour un article sur ${prompt}:\n\n${exemple}`;
const requestOptions = {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${gpt3_apikey}`,
},
body: JSON.stringify({
prompt: promptSummary,
temperature: 0.45,
max_tokens: 1000,
frequency_penalty: 0.5,
presence_penalty: 0.5,
best_of : 3,
/*n: 2,*/ //max repeat ???
}),
};
const res = await fetch(
`https://api.openai.com/v1/engines/${engine}/completions`,
requestOptions
);
const json = await res.json();
let result = await json.choices[0].text;
summary = `1.`+result.trim();
subTitles = summary.split("\n");
// on formate les titres
subTitles = subTitles.map((sub) => { // remove '1. 'truc muche
return sub.replace(/([0-9]{1,2})\.\s?/g,'').trim();
})
console.log(subTitles);
let i = 0;
for (const subTitle of subTitles) {
text += `<h2>${subTitle}</h2>`;
requestOptions.body = JSON.stringify({
prompt: `Expliquer en argumentant sur ${prompt} : ${subTitle} (mots-clés: ${keywords})\n`,
temperature: 0.7,
max_tokens: 1000,
top_p: 1,
frequency_penalty: 0.5,
presence_penalty: 0.5,
best_of : 3, //sort the best
n: 1, //sort one sentence
});
const res = await fetch(
`https://api.openai.com/v1/engines/${engine}/completions`,
requestOptions
).catch(error => console.log(error) );
const json = await res.json();
let result = await json.choices[0].text || '';
unformatedText = result.trim().split('\n').map((line) => {
text += `<p>${line.trim()}</p>`;
});
i++;
console.log(i);
}
console.log('text after loop paragraphes', text);
return text;
}
//a utiliser avec un formulaire ou autre
text = writeAboutTest(prompt,keywords)
.then((text)=> {
$("div#writeAboutResult").html(text);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment