I made a website that amended on the chatbot from this video https://www.youtube.com/watch?v=hQo1TYbFOHs
I didn't originally have many problems but as the openai API's traffic has gone up and therefore the time it takes for it to write, I found that I have been having webmethodhost timeouts which mean that while I do get a return from openai, I can't use/see it within the frontend after just 14 seconds.
the link to my website is ignusai.com and here is the affected code:
Frontend:
import { getAnswerFromAI } from 'backend/open-ai.jsw'
import { getAnswerFromAI2 } from 'backend/open-ai.jsw'
import { getAnswerFromAI3 } from 'backend/open-ai.jsw'
import { promptGen } from 'backend/promptgen.jsw'
$w.onReady(function () {
const askQuestion = async () => {
const wordsIn = Number($w("#wordCount").value);
if (wordsIn >= 1001) {
const answer = "Due to high search volume, Ignus AI has 1000 word limit.";
$w("#answerText").text = answer;
$w("#answerText").show();
} else {
if (wordsIn <= 500) {
try {
$w("#loadingAnimation").show();
$w("#answerText").hide();
$w("#answerTitle").hide();
const p1 = $w("#promptInput").value;
const creaTemp = (parseInt(Number($w("#creativity").value)) * 0.01);
const wordCount = (parseInt($w("#wordCount").value) * 1.4);
const paragraphs = (parseInt(wordCount * 0.01));
const textType = $w("#textType").value;
const titleGen = ("Write a title for a " + textType + " discussing " + p1);
const ansTitle = await promptGen(titleGen);
$w("#answerTitle").text = ansTitle;
$w("#answerTitle").show();
const referType = $w("#referType").value;
const prompt = ("Discuss " + p1 + ' as a ' + textType + ' in ' + paragraphs + ' full, comprehensive, engaging paragraphs including ' + referType + ' referencing');
console.log(prompt);
const answer = await getAnswerFromAI(prompt, creaTemp, wordCount);
const wu = answer.split(" ").length;
$w("#wordsUsed").text = (wu + ' words used.');
$w("#answerText").text = (answer);
$w("#answerText").show();
$w("#wordsUsed").show();
$w("#loadingAnimation").hide();
} catch (error) {
const answer = "An error occurred: Due to high traffic, we are having difficulties scalling our service for larger generations. Decrease the word count or try again later.";
$w("#answerText").text = answer;
$w("#answerText").show();
$w("#loadingAnimation").hide();
}
} else {
try {
$w("#loadingAnimation").show();
$w("#answerText").hide();
$w("#answerTitle").hide();
const p1 = $w("#promptInput").value;
const p2 = ("Write a relevant prompt to " + p1);
const p3 = await promptGen(p1)
const creaTemp = (parseInt(Number($w("#creativity").value)) * 0.01);
const wordCount = (parseInt($w("#wordCount").value) * 0.7);
const paragraphs = (parseInt(wordCount * 0.01));
const textType = $w("#textType").value;
const titleGen = ("Write a title for a " + textType + " discussing " + p1);
const ansTitle = await promptGen(titleGen);
$w("#answerTitle").text = ansTitle;
$w("#answerTitle").show();
const prompt = ("Introduce " + p1 + ' as a ' + textType + ' in ' + paragraphs + ' full, comprehensive, engaging paragraphs without a conclusion');
const prompt2 = ("Discuss " + p1 + ' as a ' + textType + ' in ' + paragraphs + ' full, comprehensive, engaging paragraphs without a conclusion');
const referType = $w("#referType").value;
const prompt3 = ("Conclude " + p3 + ' as a ' + textType + ' in ' + paragraphs + ' full, comprehensive, engaging paragraphs without an introduction including ' + referType + ' referencing');
const answer1 = await getAnswerFromAI(prompt, creaTemp, wordCount);
const answer2 = await getAnswerFromAI2(prompt2, creaTemp, wordCount);
const answer3 = await getAnswerFromAI3(prompt3, creaTemp, wordCount);
const answer = (answer1 + " " + answer2)
const wu = answer.split(" ").length;
$w("#wordsUsed").text = (wu + ' words used.');
$w("#answerText").text = (answer);
$w("#answerText").show();
$w("#wordsUsed").show();
$w("#loadingAnimation").hide();
} catch (error) {
const answer = "An error occurred: Due to high traffic, we are having difficulties scalling our service for larger generations. Decrease the word count or try again later.";
$w("#answerText").text = answer;
$w("#answerText").show();
$w("#loadingAnimation").hide();
}
}
}
}
$w("#askButton").onClick(askQuestion);
})
Backend
import { fetch } from 'wix-fetch';
import { getSecret } from 'wix-secrets-backend';
export const getAnswerFromAI = async ( prompt, wordCount, creaTemp ) => {
const url = "https://api.openai.com/v1/completions"
const apiKey = await getSecret("OPENAI-API-KEY");
const body = {
"model": "text-davinci-003",
"prompt": prompt,
"max_tokens": parseInt(creaTemp),
"temperature": wordCount,
"top_p": 1,
"frequency_penalty": 1.4,
"presence_penalty": 0.7,
}
const