@@ -29,15 +29,12 @@ function Initialize_gpt(){
29
29
} ) ;
30
30
}
31
31
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." , "" ) ;
33
33
34
34
// Server set up
35
35
const app = express ( ) ;
36
36
const port = 3000 ;
37
37
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
-
41
38
// Decoding from ansi values fitted to 3 digits.
42
39
function decode ( encoded_string : string ) {
43
40
let indexs = encoded_string . match ( / % [ 0 - 9 ] [ 0 - 9 ] [ 0 - 9 ] / g)
@@ -96,17 +93,18 @@ async function handle_requests(req:any, res:any){
96
93
}
97
94
98
95
// 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)` ;
100
98
const error = `error happened when inserting message for user:${ user_id } ` ;
101
99
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
+ }
104
105
}
105
106
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 ) ;
110
108
console . log ( "ChatGPT answered with: " + answer ) ;
111
109
112
110
res . send ( JSON . stringify ( answer ) ) ;
@@ -136,27 +134,8 @@ async function handle_requests(req:any, res:any){
136
134
}
137
135
138
136
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
-
158
137
const params : OpenAI . Chat . ChatCompletionCreateParams = {
159
- messages : [ { role : 'user' , content : formatted_question } ] ,
138
+ messages : [ { role : 'user' , content : question } ] ,
160
139
model : 'gpt-3.5-turbo' ,
161
140
} ;
162
141
const chatCompletion : OpenAI . Chat . ChatCompletion = await openai . chat . completions . create ( params ) ;
0 commit comments