Update webapp.py

This commit is contained in:
Joseph Cheung 2023-02-23 12:38:50 +08:00
parent 75139c2e10
commit 5059a1ae1e

View file

@ -1012,9 +1012,9 @@ Authorization: "Bearer '''+ os.environ['GPTKEY'] + r'''",
}; };
let prompt = JSON.parse(document.querySelector("#prompt").textContent); let prompt = JSON.parse(document.querySelector("#prompt").textContent);
chatTextRawIntro = ""; chatTextRawIntro = "";
text_offset = -1; text_offset = -1;
const optionsIntro = { const optionsIntro = {
method: "POST", method: "POST",
headers: headers, headers: headers,
body: JSON.stringify({ body: JSON.stringify({
@ -1029,9 +1029,9 @@ let prompt = JSON.parse(document.querySelector("#prompt").textContent);
"logprobs": 0, "logprobs": 0,
"stream": true "stream": true
}) })
}; };
fetch("https://api.openai.com/v1/engines/text-davinci-003/completions", optionsIntro) fetch("https://api.openai.com/v1/engines/text-davinci-003/completions", optionsIntro)
.then((responseIntro) => { .then((responseIntro) => {
const readerIntro = responseIntro.body.getReader(); const readerIntro = responseIntro.body.getReader();
let result = ''; let result = '';
readerIntro.read().then(function processText({ done, value }) { readerIntro.read().then(function processText({ done, value }) {
@ -1041,23 +1041,10 @@ let prompt = JSON.parse(document.querySelector("#prompt").textContent);
if(v.length>6) result = v.slice(6); if(v.length>6) result = v.slice(6);
if(result == "[DONE]") if(result == "[DONE]")
{ {
return;
}
const { choices } = JSON.parse(result);
if(choices[0].logprobs.text_offset[0] > text_offset)
{
chatTextRawIntro+=choices[0].text
text_offset = choices[0].logprobs.text_offset[choices[0].logprobs.text_offset.length - 1]
}
markdownToHtml(beautify(chatTextRawIntro+'\n'), document.getElementById('chat_intro'));
})
return readerIntro.read().then(processText);
});
})
.catch((error) => {
console.error('Error:', error);
});
text_offset = -1; text_offset = -1;
const options = { const options = {
@ -1076,7 +1063,7 @@ let prompt = JSON.parse(document.querySelector("#prompt").textContent);
if(v.length>6) result = v.slice(6); if(v.length>6) result = v.slice(6);
if(result == "[DONE]") if(result == "[DONE]")
{ {
chatTextRawPlusComment = chatTextRaw+"\n"; chatTextRawPlusComment = chatTextRaw+"\n\n";
text_offset = -1; text_offset = -1;
const optionsPlus = { const optionsPlus = {
method: "POST", method: "POST",
@ -1142,6 +1129,36 @@ let prompt = JSON.parse(document.querySelector("#prompt").textContent);
console.error('Error:', error); console.error('Error:', error);
}); });
return;
}
const { choices } = JSON.parse(result);
if(choices[0].logprobs.text_offset[0] > text_offset)
{
chatTextRawIntro+=choices[0].text
text_offset = choices[0].logprobs.text_offset[choices[0].logprobs.text_offset.length - 1]
}
markdownToHtml(beautify(chatTextRawIntro+'\n'), document.getElementById('chat_intro'));
})
return readerIntro.read().then(processText);
});
})
.catch((error) => {
console.error('Error:', error);
});
</script> </script>
''' '''
# for i in range(1,16): # for i in range(1,16):