-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch_data.js
70 lines (51 loc) · 1.44 KB
/
fetch_data.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
import pkg from "pg";
const { Client } = pkg;
import { pipeline } from "@xenova/transformers";
import ndarray from "ndarray";
async function loadModel() {
const extractor = await pipeline(
"feature-extraction",
"Xenova/bge-small-en-v1.5"
);
return extractor;
}
async function get_emb(ques){
const bgeModel = await loadModel();
const queryEmbedding = await bgeModel(ques,{
pooling: 'mean',
normalize: true,
})
return queryEmbedding;
}
const client = new Client({
user: "USER",
host: "HOST",
password: "PASSWORD",
database: "DATABASE",
port: PORT,
});
client
.connect()
.then(() => console.log("Connected to PostgreSQL"))
.catch((err) => console.error("Connection error", err.stack));
async function fetch_query(question,issue,os){
var queryEmbedding1 = await get_emb(question);
var embeddingArray1 = ndarray(queryEmbedding1.data, [
queryEmbedding1.data.length,
]);
client.query(
`SELECT answer FROM embedding_db WHERE os='${os}' and issue='${issue}' ORDER BY embeddings <=> '[${embeddingArray1["data"]}]' LIMIT 3`
)
.then(async (res) => {
console.log("Query result:", res.rows);
})
.catch((err) => {
console.error("Query error", err.stack);
})
}
var question = "My computer is overheating";
var filters = {
issue:"freezing",
os:"windows",
}
await fetch_query(question,filters.issue,filters.os)