qwen-embedder 0.1.1 → 0.1.2
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 +1 -1
- package/src/embedder.ts +21 -9
package/package.json
CHANGED
package/src/embedder.ts
CHANGED
|
@@ -78,7 +78,8 @@ export class Embedder {
|
|
|
78
78
|
}
|
|
79
79
|
|
|
80
80
|
const results: (number[] | undefined)[] = new Array(texts.length)
|
|
81
|
-
const
|
|
81
|
+
const uncachedByText = new Map<string, number[]>()
|
|
82
|
+
const uncachedTexts: string[] = []
|
|
82
83
|
|
|
83
84
|
for (let i = 0; i < texts.length; i++) {
|
|
84
85
|
const t = texts[i]
|
|
@@ -87,23 +88,33 @@ export class Embedder {
|
|
|
87
88
|
}
|
|
88
89
|
const cached = this.cache.get(t)
|
|
89
90
|
if (cached !== undefined) {
|
|
90
|
-
results[i] = cached
|
|
91
|
+
results[i] = cached.slice()
|
|
91
92
|
} else {
|
|
92
|
-
|
|
93
|
+
const existing = uncachedByText.get(t)
|
|
94
|
+
if (existing !== undefined) {
|
|
95
|
+
existing.push(i)
|
|
96
|
+
} else {
|
|
97
|
+
uncachedByText.set(t, [i])
|
|
98
|
+
uncachedTexts.push(t)
|
|
99
|
+
}
|
|
93
100
|
}
|
|
94
101
|
}
|
|
95
102
|
|
|
96
|
-
if (
|
|
103
|
+
if (uncachedTexts.length === 0) {
|
|
97
104
|
return results as number[][]
|
|
98
105
|
}
|
|
99
106
|
|
|
100
|
-
const uncachedTexts = uncached.map(i => texts[i])
|
|
101
107
|
const inferred = await this.runInference(uncachedTexts)
|
|
102
108
|
|
|
103
|
-
for (let j = 0; j <
|
|
109
|
+
for (let j = 0; j < uncachedTexts.length; j++) {
|
|
110
|
+
const text = uncachedTexts[j]
|
|
104
111
|
const vector = inferred[j]
|
|
105
|
-
|
|
106
|
-
|
|
112
|
+
const stored = vector.slice()
|
|
113
|
+
this.setCache(text, stored)
|
|
114
|
+
const positions = uncachedByText.get(text) ?? []
|
|
115
|
+
for (const position of positions) {
|
|
116
|
+
results[position] = stored.slice()
|
|
117
|
+
}
|
|
107
118
|
}
|
|
108
119
|
|
|
109
120
|
return results as number[][]
|
|
@@ -135,12 +146,13 @@ export class Embedder {
|
|
|
135
146
|
private setCache(key: string, value: number[]): void {
|
|
136
147
|
if (this.maxCacheSize === 0) return
|
|
137
148
|
|
|
149
|
+
const stored = value.slice()
|
|
138
150
|
if (this.cache.size >= this.maxCacheSize) {
|
|
139
151
|
const oldest = this.cache.keys().next()
|
|
140
152
|
if (!oldest.done) {
|
|
141
153
|
this.cache.delete(oldest.value)
|
|
142
154
|
}
|
|
143
155
|
}
|
|
144
|
-
this.cache.set(key,
|
|
156
|
+
this.cache.set(key, stored)
|
|
145
157
|
}
|
|
146
158
|
}
|