// Handle form submit for essay generation
document.querySelector(‘form#essay1’).addEventListener(‘submit’, 生成作文);
async function submit(e) {
e.preventDefault();
// Get form values
const topic = document.querySelector(‘#topic’).value;
const length = document.querySelector(‘#length’).value;
// Call OpenAI API
let response;
if (length === ‘short’) {
response = await fetch(‘’, {
method: ‘POST’,
headers: {
‘Authorization’: Bearer ${mySecret}
,
‘Content-Type’: ‘application/json’
},
body: JSON.stringify({
prompt: write a whole complete essay in about 80 word about ${topic}
,
max_tokens: 4000
})
});
} else if (length === ‘long’) {
response = await fetch(‘’, {
method: ‘POST’,
headers: {
‘Authorization’: Bearer ${mySecret}
,
‘Content-Type’: ‘application/json’
},
body: JSON.stringify({
prompt: write a whole complete essay at least 120 word about ${topic}
,
max_tokens: 4000
})
});
}
const data = await response.json();
// Get generated text
const essayText = data.choices[0].text;
// Get word count
const wordCount = essayText.split(’ ').length;
// Update HTML
document.querySelector(‘#essay-count’).innerHTML = ${wordCount} words
;
document.querySelector(‘#essay-text’).innerHTML = essayText;
}