Skip to content

Commit ae77283

Browse files
committed
OwnAPIPro
1 parent 047f891 commit ae77283

File tree

2 files changed

+193
-2
lines changed

2 files changed

+193
-2
lines changed

API/app.js

+106-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@ const app = express();
44
const PORT = process.env.PORT || 3000;
55
const bodyParser = require('body-parser');
66
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';
814

915
const APIKey = process.env['API_KEY'];
1016

@@ -32,10 +38,109 @@ app.post('/answer/', async (req, res) => {
3238
res.json({ reply: response.choices[0].message.content });
3339
});
3440
});
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+
});
35136

36137
app.get('/', (req, res) => {
37138
res.send('Hello, Aiva!');
38139
});
140+
app.get('/clearJSON', (req, res) => {
141+
fs.writeFileSync(chatFilePath, '');
142+
res.send('JSon Cleared!');
143+
});
39144

40145
app.listen(PORT, () => {
41146
console.log(`Server is running on port ${PORT}`);

prompt.js

+87-1
Original file line numberDiff line numberDiff line change
@@ -822,4 +822,90 @@ fetch('https://tasks.aidevs.pl/token/ownapi', {
822822
// const data = await response.json();
823823
// console.log(data);
824824
// })
825-
// .catch(error => console.error('Error:', error));
825+
// .catch(error => console.error('Error:', error));
826+
827+
fetch('https://tasks.aidevs.pl/token/ownapipro', {
828+
829+
method: 'POST',
830+
headers: {
831+
'Content-Type': 'application/json'
832+
},
833+
body: JSON.stringify({ apikey: APIKey })
834+
})
835+
.then(async (response) => {
836+
const data = await response.json();
837+
const token = data.token;
838+
const taskUrl = `https://tasks.aidevs.pl/task/${token}`;
839+
const response2 = await makeRequestWithDelay(taskUrl, {
840+
method: 'GET',
841+
headers: {
842+
'Content-Type': 'application/json'
843+
}
844+
}, 10);
845+
console.log(response2)
846+
const response4 = await makeRequestWithDelay(`https://tasks.aidevs.pl/answer/${token}`, {
847+
method: 'POST',
848+
headers: {
849+
'Content-Type': 'application/json'
850+
},
851+
body: JSON.stringify({answer: 'https://frog01-21730.wykr.es/chat'})
852+
}, 10);
853+
console.log('Answer from API', response4);
854+
})
855+
.catch(error => console.error('Error:', error));
856+
857+
// Example chat
858+
// fetch('http://localhost:3000/chat', {
859+
860+
// method: 'POST',
861+
// headers: {
862+
// 'Content-Type': 'application/json'
863+
// },
864+
// body: JSON.stringify({ question: "I'm 5 years old. How old am I?" })
865+
// })
866+
// .then(async (response) => {
867+
// const data = await response.json();
868+
// const answer = data.reply;
869+
// console.log(answer);
870+
// fetch('http://localhost:3000/chat', {
871+
872+
// method: 'POST',
873+
// headers: {
874+
// 'Content-Type': 'application/json'
875+
// },
876+
// body: JSON.stringify({ question: "My name is Mike. How old am I?" })
877+
// }).then(async (response) => {
878+
// const data = await response.json();
879+
// const answer = data.reply;
880+
// console.log(answer);
881+
// fetch('http://localhost:3000/chat', {
882+
883+
// method: 'POST',
884+
// headers: {
885+
// 'Content-Type': 'application/json'
886+
// },
887+
// body: JSON.stringify({ question: "What's my name? I'm from Ohio." })
888+
// }).then(async (response) => {
889+
// const data = await response.json();
890+
// const answer = data.reply;
891+
// console.log(answer);
892+
// });
893+
// });
894+
// // const taskUrl = `https://tasks.aidevs.pl/task/${token}`;
895+
// // const response2 = await makeRequestWithDelay(taskUrl, {
896+
// // method: 'GET',
897+
// // headers: {
898+
// // 'Content-Type': 'application/json'
899+
// // }
900+
// // }, 10);
901+
// // console.log(response2)
902+
// // const response4 = await makeRequestWithDelay(`https://tasks.aidevs.pl/answer/${token}`, {
903+
// // method: 'POST',
904+
// // headers: {
905+
// // 'Content-Type': 'application/json'
906+
// // },
907+
// // body: JSON.stringify({answer: 'https://frog01-21730.wykr.es/answer'})
908+
// // }, 10);
909+
// // console.log('Answer from API', response4);
910+
// })
911+
// .catch(error => console.error('Error:', error));

0 commit comments

Comments
 (0)