-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsentence-transformer.mjs
51 lines (40 loc) · 1.31 KB
/
sentence-transformer.mjs
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
class SentenceTransformer {
constructor(api_key) {
this.api_key = api_key;
}
async generate(texts) {
// do things to turn texts into embeddings with an api_key perhaps
const embeddings = await this.postApiData("http://0.0.0.0:8080/embeddings", texts);
// console.log(embeddings)
const vectorEmbeddings = [];
embeddings.data.forEach(element => {
vectorEmbeddings.push(element.embedding);
});
return vectorEmbeddings;
}
async postApiData(url, sentences) {
const data = {
"input": sentences,
"model": "all-MiniLM-L6-v2",
"user": "unknown"
}
try {
const options = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data)
};
const response = await fetch(url, options);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const responseData = await response.json();
return responseData;
} catch (error) {
console.error('Fetch error:', error.message);
}
}
}
export default SentenceTransformer;