Skip to content

Commit a663eaf

Browse files
committed
I have cleaned up the code. AND implemented a removal of ' characters in the messages feeded to the database. So that there is no sql injections
1 parent 0ef620b commit a663eaf

File tree

1 file changed

+10
-31
lines changed

1 file changed

+10
-31
lines changed

app.ts

+10-31
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,12 @@ function Initialize_gpt(){
2929
});
3030
}
3131
const openai = Initialize_gpt();
32-
send_question(false, "All of your answers should be formatted with html objects instead.", "");
32+
send_question(false, "All of your answers should be formatted with html objects.", "");
3333

3434
// Server set up
3535
const app = express();
3636
const port = 3000;
3737

38-
// Because we let user set his own id, we have a chance that two users get the same id.
39-
// This would result, in weird behaviour.
40-
4138
// Decoding from ansi values fitted to 3 digits.
4239
function decode(encoded_string: string){
4340
let indexs = encoded_string.match(/%[0-9][0-9][0-9]/g)
@@ -96,17 +93,18 @@ async function handle_requests(req:any, res:any){
9693
}
9794

9895
// Here I would want to check if the password is correct, but since it is not implemented it doesn't matter
99-
const insert_user_question = `INSERT INTO messages(user_id, sender, message, time) VALUES('${user_id}', 'user', '${question}', current_timestamp)`;
96+
const formatted_question = question.replaceAll("'", "")
97+
const insert_user_question = `INSERT INTO messages(user_id, sender, message, time) VALUES('${user_id}', 'user', '${formatted_question}', current_timestamp)`;
10098
const error=`error happened when inserting message for user:${user_id}`;
10199
await communicate_with_database(insert_user_question, error);
102-
const insert_answer = `INSERT INTO messages(user_id, sender, message, time) VALUES('${user_id}', 'gpt', '${answer}', current_timestamp)`;
103-
await communicate_with_database(insert_answer, error);
100+
if(answer != null){
101+
const formatted_answer = answer.replaceAll("'", "")
102+
const insert_answer = `INSERT INTO messages(user_id, sender, message, time) VALUES('${user_id}', 'gpt', '${formatted_answer}', current_timestamp)`;
103+
await communicate_with_database(insert_answer, error);
104+
}
104105
}
105106

106-
107-
// The funny thing is that if two requests are close engouh together, from two users
108-
// This would be messed up.
109-
console.log("I was asked: " +question);
107+
console.log("I was asked: " + question);
110108
console.log("ChatGPT answered with: " + answer);
111109

112110
res.send(JSON.stringify(answer));
@@ -136,27 +134,8 @@ async function handle_requests(req:any, res:any){
136134
}
137135

138136
async function send_question(remember_conversation: boolean, question: string, user_id: string){
139-
/*
140-
This is from a forgotten time
141-
let formatted_question = ""
142-
if(remember_conversation && conversations.has(user_id)){
143-
formatted_question = "We have had the following conversation:\n"
144-
145-
const conversation = conversations.get(user_id);
146-
for(let i=0; i<conversation["user_questions"].length; i++){
147-
let temp = "me > " + conversation["user_questions"][i] + "\n";
148-
temp += "you > " + conversation["gpt_answers"][i] + "\n";
149-
formatted_question += temp;
150-
}
151-
152-
formatted_question = formatted_question + "\nBased on this, answer the question: " + question;
153-
} else {
154-
formatted_question = question
155-
} */
156-
const formatted_question = question
157-
158137
const params: OpenAI.Chat.ChatCompletionCreateParams = {
159-
messages: [{ role: 'user', content: formatted_question }],
138+
messages: [{ role: 'user', content: question }],
160139
model: 'gpt-3.5-turbo',
161140
};
162141
const chatCompletion: OpenAI.Chat.ChatCompletion = await openai.chat.completions.create(params);

0 commit comments

Comments
 (0)