Skip to content

Commit e157f3c

Browse files
committed
SERP Search
1 parent 7ba7795 commit e157f3c

File tree

4 files changed

+133
-3
lines changed

4 files changed

+133
-3
lines changed

API/app.js

+14
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ const { fetchJSONData } = require('./io/index.js');
99
const { chatCompletion } = require('./openAPI/index.js');
1010
const JSONStream = require('JSONStream');
1111
const fs = require('fs');
12+
const { getJson } = require("serpapi");
1213

1314
const chatFilePath = 'chat.json';
1415

1516
const APIKey = process.env['API_KEY'];
17+
const SERPAPIKEy = process.env['SERP_API'];
1618

1719
app.use(bodyParser.json());
1820
app.post('/answer/', async (req, res) => {
@@ -137,6 +139,18 @@ app.post('/answer/', async (req, res) => {
137139
app.get('/', (req, res) => {
138140
res.send('Hello, Aiva!');
139141
});
142+
app.post('/search', (req, res) => {
143+
const jsonData = req.body;
144+
getJson({
145+
engine: "google",
146+
api_key: SERPAPIKEy, // Get your API_KEY from https://serpapi.com/manage-api-key
147+
q: jsonData.question,
148+
location: "Austin, Texas",
149+
}, (json) => {
150+
console.log(json["organic_results"]);
151+
res.json({ reply: json["organic_results"][0].link });
152+
});
153+
});
140154
app.get('/clearJSON', (req, res) => {
141155
fs.writeFileSync(chatFilePath, '');
142156
res.send('JSon Cleared!');

API/package-lock.json

+71-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

API/package.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@
1717
"eslint-plugin-vue": "^9.24.1"
1818
},
1919
"dependencies": {
20+
"axios": "^1.6.8",
2021
"body-parser": "^1.20.2",
2122
"dotenv": "^16.4.5",
2223
"express": "^4.19.2",
23-
"openai": "^4.33.0"
24+
"JSONStream": "^1.3.5",
25+
"openai": "^4.33.0",
26+
"serpapi": "^2.1.0"
2427
}
2528
}

prompt.js

+44-1
Original file line numberDiff line numberDiff line change
@@ -1012,4 +1012,47 @@ fetch('https://tasks.aidevs.pl/token/optimaldb', {
10121012
} catch (error) {
10131013
console.error('An error occurred:', error);
10141014
}})
1015-
.catch(error => console.error('Error:', error));
1015+
.catch(error => console.error('Error:', error));
1016+
1017+
fetch('https://tasks.aidevs.pl/token/google', {
1018+
method: 'POST',
1019+
headers: {
1020+
'Content-Type': 'application/json'
1021+
},
1022+
body: JSON.stringify({ apikey: APIKey })
1023+
})
1024+
.then(async (response) => {
1025+
const data = await response.json();
1026+
const token = data.token;
1027+
const taskUrl = `https://tasks.aidevs.pl/task/${token}`;
1028+
const response2 = await makeRequestWithDelay(taskUrl, {
1029+
method: 'GET',
1030+
headers: {
1031+
'Content-Type': 'application/json'
1032+
}
1033+
}, 10);
1034+
console.log(response2)
1035+
const response4 = await makeRequestWithDelay(`https://tasks.aidevs.pl/answer/${token}`, {
1036+
method: 'POST',
1037+
headers: {
1038+
'Content-Type': 'application/json'
1039+
},
1040+
body: JSON.stringify({answer: 'https://frog01-21730.wykr.es/search'})
1041+
}, 10);
1042+
console.log('Answer from API', response4);
1043+
})
1044+
.catch(error => console.error('Error:', error));
1045+
1046+
// Example URL search
1047+
// fetch('https://frog01-21730.wykr.es/search', {
1048+
// method: 'POST',
1049+
// headers: {
1050+
// 'Content-Type': 'application/json'
1051+
// },
1052+
// body: JSON.stringify({ question: "What is the weather today?" })
1053+
// })
1054+
// .then(async (response) => {
1055+
// const data = await response.json();
1056+
// console.log(data.reply);
1057+
// })
1058+
// .catch(error => console.error('Error:', error));

0 commit comments

Comments
 (0)