2f5edb71 by Leff Tubat

Fixed issue

1 parent eccfda0c
......@@ -112,6 +112,7 @@ function App() {
}
let intervalId = startInterval();
try {
const response = await fetch(process.env.REACT_APP_SERVER_URL + "/api", {
method: "POST",
......@@ -125,8 +126,8 @@ function App() {
})
});
const data = await response.json();
const parsedData = data.message.trim();
const parsedData = data.message ? data.message.trim() : "";
if(data.status === 'invalid'){
if(data.limited) {
window.btutil_modalRegisterUpgrade();
......@@ -190,6 +191,7 @@ function App() {
}
} catch (error) {
console.log(error)
const errorMsg = "We apologize for any inconvenience caused due to the delay in the response time. Please try again.";
setChatLog([...chatLogNew, { user: "gpt", message: `<div class="errormsg"><span>i</span><div class="msg">${errorMsg}</div></div>`} ])
}
......
......@@ -79,27 +79,29 @@ const user_secret_id = process.env.USER_SECRET_ID || "aiwp_logged_in";
const aiwp_app_id = "chatbot+";
// Primary Open AI Route
app.post('/api', async (req, res) => {
if(!req.get('origin') || (!req.get('origin').includes(req.get('host')))) {
res.status(401);
res.send('Method Not Allowed');
return;
}
// if(!req.get('origin') || (!req.get('origin').includes(req.get('host')))) {
// res.status(401);
// res.send('Method Not Allowed');
// return;
// }
const { message, currentModel, temperature } = req.body;
const validate = await validation(aiwp_app_id, req, res);
if(!validate) return;
const { IS_FREE_USER, aiwp_logged_in, TRIED_USAGE} = validate;
if (currentModel == "gpt-3.5-turbo" || currentModel == "gpt-3.5-turbo-0301") {
runGPTTurbo(req, res);
return;
}
if (currentModel == "openchat_3.5" || currentModel == "zephyr-7B-beta") {
runOpensource(req, res);
return;
}
const validate = await validation(aiwp_app_id, req, res);
if(!validate) return;
const { IS_FREE_USER, aiwp_logged_in, TRIED_USAGE} = validate;
let greetingPrompt = 'Hello, how can I assist you?'
const greetings = ['hi', 'hello', 'hey']
if (greetings.some((greeting) => message.toLowerCase().includes(greeting))) {
......@@ -188,7 +190,11 @@ async function runGPTTurbo(req, res) {
const moderation = await axios.post("https://api.openai.com/v1/moderations", {
input: query_prompt
}, { headers: { 'content-type': 'application/json', 'Authorization': `Bearer ${process.env.OPENAI_API_KEY}` } });
const validate = await validation(aiwp_app_id, req, res);
if(!validate) return;
const { IS_FREE_USER, aiwp_logged_in, TRIED_USAGE} = validate;
console.log({validate})
if (moderation.data.results[0].flagged) {
res.json({
success: false,
......@@ -224,7 +230,11 @@ async function runGPTTurbo(req, res) {
} catch (e) {
console.log('Error encoding prompt text', e);
}
if(IS_FREE_USER) {
await setUsage({
aiwp_logged_in, app: 'chatbot+', prompt_token: usage.prompt_tokens, total_token: usage.total_tokens, aiwp_app_id, usage_tries: TRIED_USAGE
});
}
res.json({
prompt: JSON.parse(message),
usage: usage,
......@@ -258,7 +268,11 @@ async function runOpensource(req, res) {
var input = '';
const message_history = JSON.parse(message);
const query_prompt = message_history.length ? message_history[message_history.length - 1].content : "";
const validate = await validation(aiwp_app_id, req, res);
if(!validate) return;
const { IS_FREE_USER, aiwp_logged_in, TRIED_USAGE} = validate;
try {
let error_msg = "";
const endpoint_api_url = get_endpoint_api_url(currentModel);
......@@ -413,7 +427,7 @@ async function validation (aiwp_app_id, req, res) {
if (data.usage !== null) {
TRIED_USAGE = parseInt(data.usage);
}
return { IS_FREE_USER, aiwp_logged_in, TRIED_USAGE };
}
if (IS_FREE_USER && TRIED_USAGE >= limit) {
res.json({ status: "invalid", limited: true });
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!