@adaas/a-frame 0.0.7 → 0.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@adaas/a-frame",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.8",
|
|
4
4
|
"description": "A-frame is index build engine for A-Concept ecosystem. It provides a way to build index structures for A-Concept basics",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
@@ -61,7 +61,7 @@
|
|
|
61
61
|
"build": "tsup --config tsup.config.ts"
|
|
62
62
|
},
|
|
63
63
|
"dependencies": {
|
|
64
|
-
"@adaas/a-concept": "^0.2.
|
|
64
|
+
"@adaas/a-concept": "^0.2.6"
|
|
65
65
|
},
|
|
66
66
|
"devDependencies": {
|
|
67
67
|
"@types/chai": "^4.3.14",
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { A_Component } from "@adaas/a-concept"
|
|
2
|
+
import { A_FrameVector } from "../../entities/A_FrameVector/A_FrameVector.entity"
|
|
3
|
+
|
|
4
|
+
export class A_FrameSearch extends A_Component {
|
|
5
|
+
|
|
6
|
+
static cosineSimilarity(
|
|
7
|
+
a: A_FrameVector,
|
|
8
|
+
b: A_FrameVector
|
|
9
|
+
): number {
|
|
10
|
+
const dot = a.dot(b)
|
|
11
|
+
const mag = a.magnitude() * b.magnitude()
|
|
12
|
+
return mag === 0 ? 0 : dot / mag
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
static euclideanDistance(
|
|
16
|
+
a: A_FrameVector,
|
|
17
|
+
b: A_FrameVector
|
|
18
|
+
): number {
|
|
19
|
+
A_FrameSearch.assertSameLength(a, b)
|
|
20
|
+
|
|
21
|
+
let sum = 0
|
|
22
|
+
for (let i = 0; i < a.length; i++) {
|
|
23
|
+
const diff = a.values[i] - b.values[i]
|
|
24
|
+
sum += diff * diff
|
|
25
|
+
}
|
|
26
|
+
return Math.sqrt(sum)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
static nearest(
|
|
30
|
+
query: A_FrameVector,
|
|
31
|
+
vectors: readonly A_FrameVector[],
|
|
32
|
+
limit = 1
|
|
33
|
+
): A_FrameVector[] {
|
|
34
|
+
return [...vectors]
|
|
35
|
+
.map(v => ({
|
|
36
|
+
vector: v,
|
|
37
|
+
score: A_FrameSearch.cosineSimilarity(query, v),
|
|
38
|
+
}))
|
|
39
|
+
.sort((a, b) => b.score - a.score)
|
|
40
|
+
.slice(0, limit)
|
|
41
|
+
.map(r => r.vector)
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
static rank(
|
|
45
|
+
query: A_FrameVector,
|
|
46
|
+
vectors: readonly A_FrameVector[]
|
|
47
|
+
): Array<{ vector: A_FrameVector; score: number }> {
|
|
48
|
+
return vectors
|
|
49
|
+
.map(v => ({
|
|
50
|
+
vector: v,
|
|
51
|
+
score: A_FrameSearch.cosineSimilarity(query, v),
|
|
52
|
+
}))
|
|
53
|
+
.sort((a, b) => b.score - a.score)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
private static assertSameLength(
|
|
57
|
+
a: A_FrameVector,
|
|
58
|
+
b: A_FrameVector
|
|
59
|
+
) {
|
|
60
|
+
if (a.length !== b.length) {
|
|
61
|
+
throw new Error(
|
|
62
|
+
`Embedding dimension mismatch: ${a.length} vs ${b.length}`
|
|
63
|
+
)
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { A_Entity } from "@adaas/a-concept"
|
|
2
|
+
|
|
3
|
+
export class A_FrameVector extends A_Entity {
|
|
4
|
+
readonly values: Float32Array
|
|
5
|
+
|
|
6
|
+
constructor(values: number[] | Float32Array) {
|
|
7
|
+
super();
|
|
8
|
+
this.values = values instanceof Float32Array
|
|
9
|
+
? values
|
|
10
|
+
: new Float32Array(values)
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
get length(): number {
|
|
14
|
+
return this.values.length
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
magnitude(): number {
|
|
18
|
+
let sum = 0
|
|
19
|
+
for (let i = 0; i < this.values.length; i++) {
|
|
20
|
+
sum += this.values[i] ** 2
|
|
21
|
+
}
|
|
22
|
+
return Math.sqrt(sum)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
normalize(): A_FrameVector {
|
|
26
|
+
const mag = this.magnitude()
|
|
27
|
+
if (mag === 0) return this
|
|
28
|
+
|
|
29
|
+
const normalized = new Float32Array(this.length)
|
|
30
|
+
for (let i = 0; i < this.length; i++) {
|
|
31
|
+
normalized[i] = this.values[i] / mag
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return new A_FrameVector(normalized)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
dot(other: A_FrameVector): number {
|
|
38
|
+
this.assertSameLength(other)
|
|
39
|
+
|
|
40
|
+
let sum = 0
|
|
41
|
+
for (let i = 0; i < this.length; i++) {
|
|
42
|
+
sum += this.values[i] * other.values[i]
|
|
43
|
+
}
|
|
44
|
+
return sum
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
private assertSameLength(other: A_FrameVector) {
|
|
48
|
+
if (this.length !== other.length) {
|
|
49
|
+
throw new Error(
|
|
50
|
+
`Embedding dimension mismatch: ${this.length} vs ${other.length}`
|
|
51
|
+
)
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|