@@ -4,7 +4,13 @@ const app = express();
4
4
const PORT = process . env . PORT || 3000 ;
5
5
const bodyParser = require ( 'body-parser' ) ;
6
6
require ( 'dotenv' ) . config ( ) ;
7
- const { chatCompletion, embedding, transcript } = require ( './openAPI/index.js' ) ;
7
+ const { generateEmbeddings } = require ( './embeddings/index.js' ) ;
8
+ const { fetchJSONData } = require ( './io/index.js' ) ;
9
+ const { chatCompletion } = require ( './openAPI/index.js' ) ;
10
+ const JSONStream = require ( 'JSONStream' ) ;
11
+ const fs = require ( 'fs' ) ;
12
+
13
+ const chatFilePath = 'chat.json' ;
8
14
9
15
const APIKey = process . env [ 'API_KEY' ] ;
10
16
@@ -32,10 +38,109 @@ app.post('/answer/', async (req, res) => {
32
38
res . json ( { reply : response . choices [ 0 ] . message . content } ) ;
33
39
} ) ;
34
40
} ) ;
41
+ app . post ( '/chat/' , async ( req , res ) => {
42
+ let previousChat = '[' ;
43
+ setTimeout ( async ( ) => {
44
+ try {
45
+ const stream = fs . createReadStream ( chatFilePath , { encoding : 'utf8' } ) ;
46
+ const jsonStream = JSONStream . parse ( '*' ) ;
47
+ stream . pipe ( jsonStream ) ;
48
+ jsonStream . on ( 'data' , async ( data ) => {
49
+ console . log ( 'Data: ' + data ) ;
50
+ previousChat += '{"' + data + '"},' ;
51
+ } ) ;
52
+
53
+ // Handle errors
54
+ stream . on ( 'error' , ( err ) => {
55
+ console . error ( 'Error reading file:' , err ) ;
56
+ } ) ;
57
+ stream . on ( 'end' , async ( ) => {
58
+ previousChat += '].'
59
+ console . log ( 'Previous chat in end function (' + previousChat . length + '): ' + previousChat )
60
+ const jsonData = req . body ;
61
+ // Do something with the JSON data
62
+ //console.log('Received JSON data:', jsonData);
63
+ fs . appendFileSync ( chatFilePath , JSON . stringify ( { question : jsonData . question } ) + '\n' ) ;
64
+ const systemPrompt = `Anwser all user questions.
65
+ Parse user question for answer or find it in previous user prompts.
66
+ This are previous prompts from user use it as context for answers: ` + previousChat + '. This is current question:' +
67
+ 'Dont elaborate just provide anwsers based on data you have without extra chit chat' ;
68
+ console . log ( 'System prompt: ' + systemPrompt )
69
+ await chatCompletion ( {
70
+ messages : [
71
+ {
72
+ role : 'system' ,
73
+ content : systemPrompt
74
+ } ,
75
+ {
76
+ role : 'user' ,
77
+ content : jsonData . question
78
+ } ] ,
79
+ model : 'gpt-3.5-turbo'
80
+ } ) . then ( async ( response ) => {
81
+ res . json ( { reply : response . choices [ 0 ] . message . content } ) ;
82
+ } ) ;
83
+ } ) ;
84
+ } catch ( error ) {
85
+ console . error ( 'An error occurred:' , error ) ;
86
+ }
87
+ } , 5000 ) ; // Resolve the promise with the accumulated data
88
+
89
+ // Access JSON data from the request body
90
+
91
+
92
+ // await generateEmbeddings(jsonData.question).then(async (response) => {
93
+ // console.log(response);
94
+ // const upsertObject = {
95
+ // wait: true,
96
+ // points: [{
97
+ // id,
98
+ // vector: {response},
99
+ // payload: jsonData[id]
100
+ // }]
101
+ // }
102
+ // })
103
+
104
+ // await chatCompletion({
105
+ // messages: [
106
+ // {
107
+ // role: 'system',
108
+ // content: `Find shared details about the user and return JSON with it.`
109
+ // },
110
+ // {
111
+ // role: 'user',
112
+ // content: jsonData.question
113
+ // }],
114
+ // model: 'gpt-3.5-turbo'
115
+ // }).then(async (response) => {
116
+ // res.json({ reply: response.choices[0].message.content });
117
+ // });
118
+
119
+
120
+ // // Send a response
121
+ // await chatCompletion({
122
+ // messages: [
123
+ // {
124
+ // role: 'system',
125
+ // content: information.toString()
126
+ // },
127
+ // {
128
+ // role: 'user',
129
+ // content: jsonData.question
130
+ // }],
131
+ // model: 'gpt-3.5-turbo'
132
+ // }).then(async (response) => {
133
+ // res.json({ reply: response.choices[0].message.content });
134
+ // });
135
+ } ) ;
35
136
36
137
app . get ( '/' , ( req , res ) => {
37
138
res . send ( 'Hello, Aiva!' ) ;
38
139
} ) ;
140
+ app . get ( '/clearJSON' , ( req , res ) => {
141
+ fs . writeFileSync ( chatFilePath , '' ) ;
142
+ res . send ( 'JSon Cleared!' ) ;
143
+ } ) ;
39
144
40
145
app . listen ( PORT , ( ) => {
41
146
console . log ( `Server is running on port ${ PORT } ` ) ;
0 commit comments