-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
99 lines (76 loc) · 2.33 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import { promises as fs } from 'fs';
import axios from 'axios';
import { getRandomDIDSession } from "./wallet.js";
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
const argv = yargs(hideBin(process.argv)).options({
server_url: { type: 'string', demandOption: true, describe: 'The server URL' },
}).argv;
const SERVER_URL = argv.server_url;
const saveSessionToFile = async (filename, sessionData) => {
try {
await fs.writeFile(filename, sessionData, 'utf8');
console.log('Session saved to', filename);
} catch (err) {
console.error('Error saving session to file:', err);
}
};
const extractTextFromNodes = async (session) => {
try {
// Read the JSON file
const data = await fs.readFile('sample_data.json', 'utf8');
// Parse the JSON data
const jsonData = JSON.parse(data);
let processedCount = 0;
// Process each node sequentially
for (let i = 0; i < jsonData.length; i++) {
const entry = jsonData[i];
if (entry.node && entry.node.item && entry.node.item.text) {
const document = { ...entry.node };
delete document.vector;
const jwe = await session.did.createDagJWE(
{
vector: entry.node.vector,
document,
},
[session.did.id]
);
const resp = await axios.post(SERVER_URL + "/index", { jwe }, {
headers: {
Authorization: `Bearer ${session.serialize()}`
}
});
processedCount++;
}
}
console.log(`Total nodes processed: ${processedCount}`);
} catch (err) {
console.error('Error reading or processing the file:', err);
}
};
const search = async (session) => {
try {
// Read the JSON file
const data = await fs.readFile('sample_data.json', 'utf8');
const jsonData = JSON.parse(data);
const entry = jsonData[100]
const resp = await axios.post(SERVER_URL + "/search", {
vector: entry.node.vector,
count: 5
}, {
headers: {
Authorization: `Bearer ${session.serialize()}`
}
})
const respData = resp.data
console.log(`Search result: ${JSON.stringify(respData,0 , 2)}`);
} catch (err) {
console.error('Error searching:', err);
}
};
const main = async () => {
const session = await getRandomDIDSession();
await extractTextFromNodes(session);
await search(session)
};
main();