-
Notifications
You must be signed in to change notification settings - Fork 0
/
load.html
72 lines (60 loc) · 2.62 KB
/
load.html
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
<!-- Load TensorFlow.js -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>
<!-- Load Universal Sentence Encoder -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow-models/universal-sentence-encoder"></script>
<!-- Add the WebGPU backend to the global backend registry -->
<!--<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs-backend-webgpu/dist/tf-backend-webgpu.js"></script>-->
<script>
// Define the main function
async function main() {
try {
// Set the backend to WebGPU and wait for the module to be ready
// await tf.setBackend('webgpu');
// Example usage
const question = "Calculate min value in a column";
const responses = await loadEmbeddingsFromFile("embeddings_use.json");
await findBestResponse(question, responses);
} catch (error) {
console.error("An error occurred:", error);
}
}
// Call the main function after the page has finished loading
window.addEventListener('load', main);
async function loadEmbeddingsFromFile(filePath) {
const response = await fetch(filePath);
// assuming the embeddings are stored in the first key
return await response.json();
}
async function findBestResponse(task, functionsInfo) {
try {
// Load the Universal Sentence Encoder model
const model = await use.load();
// Preprocess the input question
// Encode the question
const taskEmbedding = await model.embed(task);
// Find the most relevant response
let bestResponse = null;
let bestScore = -1;
for (const functionInfo of functionsInfo) {
const functionEmbedding = tf.tensor(functionInfo.e);
// Calculate the similarity score between the question and response embeddings
const similarity = taskEmbedding.dot(functionEmbedding.transpose()).arraySync()[0][0];
console.log(bestScore)
if (similarity > bestScore) {
bestScore = similarity;
bestResponse = functionInfo;
}
}
if (bestResponse == null) {
console.error("Unable to find a best response");
} else {
console.log("Best response:", bestResponse);
}
console.log(bestResponse)
return bestResponse;
} catch (error) {
console.error("An error occurred while finding the best response:", error);
return null;
}
}
</script>