@restregular/dsh-memory 0.3.0 → 0.4.0
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/README.md +37 -58
- package/cordis.patch.yml +6 -8
- package/icon.svg +1 -0
- package/lib/client.js +859 -0
- package/lib/index.js +72 -0
- package/lib/inject/apply.js +80 -80
- package/lib/inject/inject.js +127 -127
- package/lib/panel.js +353 -0
- package/lib/provider/apply.js +133 -128
- package/lib/provider/embed-install.js +1144 -1002
- package/lib/provider/embed-server.js +308 -294
- package/lib/provider/embed.js +103 -103
- package/lib/provider/hardware.js +225 -0
- package/lib/provider/index.js +1271 -903
- package/lib/provider/models.js +253 -0
- package/lib/provider/recommend.js +187 -0
- package/lib/provider/rerank-cache.js +62 -62
- package/lib/provider/rerank.js +169 -169
- package/lib/provider/scoring.js +143 -143
- package/lib/provider/secrets.js +55 -55
- package/lib/provider/store.js +491 -485
- package/lib/tools/apply.js +104 -104
- package/lib/tools/index.js +224 -224
- package/package.json +17 -6
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 可安装的 embedding 模型清单(数据层,零 I/O)。
|
|
3
|
+
*
|
|
4
|
+
* 硬约束:清单里每个 URL 都是**实测可达**的(HTTP 200 + 已知字节数),不写猜测的仓库 ——
|
|
5
|
+
* 一个死链会让"一键安装"变成一条看不懂的失败信息,而这正是要消灭的体验。
|
|
6
|
+
* 想加模型:先 `curl -I`(或 HEAD)确认 URL 与 content-length,再往这里加一条。
|
|
7
|
+
*
|
|
8
|
+
* 每个模型必须声明三件与运行强相关的事实,它们会一起写回配置:
|
|
9
|
+
* - dims: 服务端输出维度。客户端会校验(见 embed.ts),写错会让检索**静默降级**为词法。
|
|
10
|
+
* - pooling: llama.cpp 的 `--pooling`。BGE 系用 cls,Qwen3-Embedding 用 last,
|
|
11
|
+
* nomic 用 mean;'auto' 表示不传该参数、由 GGUF 元数据决定。
|
|
12
|
+
* - prefixes:有些模型要靠前缀区分查询与文档(e5 的 "query:/passage:"、nomic 的
|
|
13
|
+
* "search_query:/search_document:"),不带前缀质量会明显下降。
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
/** llama.cpp 的池化方式;'auto' = 不传 --pooling,由 GGUF 元数据决定。 */
|
|
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
|
+
const HF = 'https://hf-mirror.com'
|
|
52
|
+
const HF_OFFICIAL = 'https://huggingface.co'
|
|
53
|
+
/** ModelScope 只对已核实过的仓库列出(它的路径是 /resolve/master/ 而不是 /main/)。 */
|
|
54
|
+
const MS = 'https://modelscope.cn/models'
|
|
55
|
+
|
|
56
|
+
function hfSources(repo , file ) {
|
|
57
|
+
return [`${HF}/${repo}/resolve/main/${file}`, `${HF_OFFICIAL}/${repo}/resolve/main/${file}`]
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export const EMBEDDING_MODELS = [
|
|
61
|
+
{
|
|
62
|
+
id: 'bge-m3',
|
|
63
|
+
label: 'BGE-M3',
|
|
64
|
+
note: '多语言、8192 上下文,中英混排的稳妥默认值',
|
|
65
|
+
dims: 1024,
|
|
66
|
+
pooling: 'cls',
|
|
67
|
+
contextTokens: 8192,
|
|
68
|
+
languages: 'multilingual',
|
|
69
|
+
quality: 4,
|
|
70
|
+
speed: 3,
|
|
71
|
+
variants: [
|
|
72
|
+
{
|
|
73
|
+
id: 'q4_k_m', label: 'Q4_K_M', file: 'bge-m3-Q4_K_M.gguf', bytes: 437_778_496, isDefault: true,
|
|
74
|
+
sources: [
|
|
75
|
+
`${HF}/gpustack/bge-m3-GGUF/resolve/main/bge-m3-Q4_K_M.gguf`,
|
|
76
|
+
`${MS}/gpustack/bge-m3-GGUF/resolve/master/bge-m3-Q4_K_M.gguf`,
|
|
77
|
+
`${HF_OFFICIAL}/gpustack/bge-m3-GGUF/resolve/main/bge-m3-Q4_K_M.gguf`,
|
|
78
|
+
],
|
|
79
|
+
},
|
|
80
|
+
{
|
|
81
|
+
id: 'q8_0', label: 'Q8_0', file: 'bge-m3-Q8_0.gguf', bytes: 634_567_104,
|
|
82
|
+
sources: [
|
|
83
|
+
`${HF}/gpustack/bge-m3-GGUF/resolve/main/bge-m3-Q8_0.gguf`,
|
|
84
|
+
`${MS}/gpustack/bge-m3-GGUF/resolve/master/bge-m3-Q8_0.gguf`,
|
|
85
|
+
`${HF_OFFICIAL}/gpustack/bge-m3-GGUF/resolve/main/bge-m3-Q8_0.gguf`,
|
|
86
|
+
],
|
|
87
|
+
},
|
|
88
|
+
],
|
|
89
|
+
},
|
|
90
|
+
{
|
|
91
|
+
id: 'qwen3-embedding-0.6b',
|
|
92
|
+
label: 'Qwen3-Embedding-0.6B',
|
|
93
|
+
note: '同体积下检索质量明显更强,推荐优先试它',
|
|
94
|
+
dims: 1024,
|
|
95
|
+
pooling: 'last',
|
|
96
|
+
contextTokens: 32768,
|
|
97
|
+
languages: 'multilingual',
|
|
98
|
+
quality: 5,
|
|
99
|
+
speed: 3,
|
|
100
|
+
variants: [
|
|
101
|
+
{
|
|
102
|
+
id: 'q8_0', label: 'Q8_0', file: 'Qwen3-Embedding-0.6B-Q8_0.gguf', bytes: 639_171_584, isDefault: true,
|
|
103
|
+
sources: hfSources('Qwen/Qwen3-Embedding-0.6B-GGUF', 'Qwen3-Embedding-0.6B-Q8_0.gguf'),
|
|
104
|
+
},
|
|
105
|
+
],
|
|
106
|
+
},
|
|
107
|
+
{
|
|
108
|
+
id: 'qwen3-embedding-4b',
|
|
109
|
+
label: 'Qwen3-Embedding-4B',
|
|
110
|
+
note: '质量更强、体积也更大;CPU 上每次检索会明显变慢,有显卡再选',
|
|
111
|
+
dims: 2560,
|
|
112
|
+
pooling: 'last',
|
|
113
|
+
contextTokens: 32768,
|
|
114
|
+
languages: 'multilingual',
|
|
115
|
+
quality: 6,
|
|
116
|
+
speed: 1,
|
|
117
|
+
variants: [
|
|
118
|
+
{
|
|
119
|
+
id: 'q4_k_m', label: 'Q4_K_M', file: 'Qwen3-Embedding-4B-Q4_K_M.gguf', bytes: 2_496_737_280, isDefault: true,
|
|
120
|
+
sources: hfSources('Qwen/Qwen3-Embedding-4B-GGUF', 'Qwen3-Embedding-4B-Q4_K_M.gguf'),
|
|
121
|
+
},
|
|
122
|
+
],
|
|
123
|
+
},
|
|
124
|
+
{
|
|
125
|
+
id: 'qwen3-embedding-8b',
|
|
126
|
+
label: 'Qwen3-Embedding-8B',
|
|
127
|
+
note: '最强档,约 4.5GB;只建议有大显存或 32GB 以上内存的机器',
|
|
128
|
+
dims: 4096,
|
|
129
|
+
pooling: 'last',
|
|
130
|
+
contextTokens: 32768,
|
|
131
|
+
languages: 'multilingual',
|
|
132
|
+
quality: 7,
|
|
133
|
+
speed: 1,
|
|
134
|
+
variants: [
|
|
135
|
+
{
|
|
136
|
+
id: 'q4_k_m', label: 'Q4_K_M', file: 'Qwen3-Embedding-8B-Q4_K_M.gguf', bytes: 4_676_403_200, isDefault: true,
|
|
137
|
+
sources: hfSources('Qwen/Qwen3-Embedding-8B-GGUF', 'Qwen3-Embedding-8B-Q4_K_M.gguf'),
|
|
138
|
+
},
|
|
139
|
+
],
|
|
140
|
+
},
|
|
141
|
+
{
|
|
142
|
+
id: 'nomic-embed-text-v1.5',
|
|
143
|
+
label: 'Nomic Embed v1.5',
|
|
144
|
+
note: '英文为主、体积最小;必须带 query/document 前缀,已内置',
|
|
145
|
+
dims: 768,
|
|
146
|
+
pooling: 'mean',
|
|
147
|
+
contextTokens: 8192,
|
|
148
|
+
languages: 'en',
|
|
149
|
+
quality: 3,
|
|
150
|
+
speed: 5,
|
|
151
|
+
queryPrefix: 'search_query: ',
|
|
152
|
+
documentPrefix: 'search_document: ',
|
|
153
|
+
variants: [
|
|
154
|
+
{
|
|
155
|
+
id: 'q4_k_m', label: 'Q4_K_M', file: 'nomic-embed-text-v1.5.Q4_K_M.gguf', bytes: 84_099_072, isDefault: true,
|
|
156
|
+
sources: hfSources('nomic-ai/nomic-embed-text-v1.5-GGUF', 'nomic-embed-text-v1.5.Q4_K_M.gguf'),
|
|
157
|
+
},
|
|
158
|
+
{
|
|
159
|
+
id: 'f16', label: 'F16', file: 'nomic-embed-text-v1.5.f16.gguf', bytes: 274_313_216,
|
|
160
|
+
sources: hfSources('nomic-ai/nomic-embed-text-v1.5-GGUF', 'nomic-embed-text-v1.5.f16.gguf'),
|
|
161
|
+
},
|
|
162
|
+
],
|
|
163
|
+
},
|
|
164
|
+
{
|
|
165
|
+
id: 'bge-small-zh-v1.5',
|
|
166
|
+
label: 'BGE Small 中文',
|
|
167
|
+
note: '中文轻量档,约 46MB,老机器也能秒检索',
|
|
168
|
+
dims: 512,
|
|
169
|
+
pooling: 'cls',
|
|
170
|
+
contextTokens: 512,
|
|
171
|
+
languages: 'zh',
|
|
172
|
+
quality: 3,
|
|
173
|
+
speed: 5,
|
|
174
|
+
variants: [
|
|
175
|
+
{
|
|
176
|
+
id: 'f16', label: 'F16', file: 'bge-small-zh-v1.5-f16.gguf', bytes: 47_919_104, isDefault: true,
|
|
177
|
+
sources: hfSources('CompendiumLabs/bge-small-zh-v1.5-gguf', 'bge-small-zh-v1.5-f16.gguf'),
|
|
178
|
+
},
|
|
179
|
+
],
|
|
180
|
+
},
|
|
181
|
+
{
|
|
182
|
+
id: 'bge-base-zh-v1.5',
|
|
183
|
+
label: 'BGE Base 中文',
|
|
184
|
+
note: '中文中等档,768 维,约 195MB',
|
|
185
|
+
dims: 768,
|
|
186
|
+
pooling: 'cls',
|
|
187
|
+
contextTokens: 512,
|
|
188
|
+
languages: 'zh',
|
|
189
|
+
quality: 4,
|
|
190
|
+
speed: 4,
|
|
191
|
+
variants: [
|
|
192
|
+
{
|
|
193
|
+
id: 'f16', label: 'F16', file: 'bge-base-zh-v1.5-f16.gguf', bytes: 204_052_480, isDefault: true,
|
|
194
|
+
sources: hfSources('CompendiumLabs/bge-base-zh-v1.5-gguf', 'bge-base-zh-v1.5-f16.gguf'),
|
|
195
|
+
},
|
|
196
|
+
],
|
|
197
|
+
},
|
|
198
|
+
{
|
|
199
|
+
id: 'bge-large-zh-v1.5',
|
|
200
|
+
label: 'BGE Large 中文',
|
|
201
|
+
note: '中文强档,1024 维,约 620MB',
|
|
202
|
+
dims: 1024,
|
|
203
|
+
pooling: 'cls',
|
|
204
|
+
contextTokens: 512,
|
|
205
|
+
languages: 'zh',
|
|
206
|
+
quality: 5,
|
|
207
|
+
speed: 3,
|
|
208
|
+
variants: [
|
|
209
|
+
{
|
|
210
|
+
id: 'f16', label: 'F16', file: 'bge-large-zh-v1.5-f16.gguf', bytes: 650_117_120, isDefault: true,
|
|
211
|
+
sources: hfSources('CompendiumLabs/bge-large-zh-v1.5-gguf', 'bge-large-zh-v1.5-f16.gguf'),
|
|
212
|
+
},
|
|
213
|
+
],
|
|
214
|
+
},
|
|
215
|
+
]
|
|
216
|
+
|
|
217
|
+
/** 不做任何配置时的模型 —— 与合并前的老行为完全一致(Q4_K_M 的 BGE-M3)。 */
|
|
218
|
+
export const DEFAULT_MODEL_ID = 'bge-m3'
|
|
219
|
+
|
|
220
|
+
export function findModel(id ) {
|
|
221
|
+
if (id === undefined) return undefined
|
|
222
|
+
return EMBEDDING_MODELS.find((m) => m.id === id)
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
export function defaultVariant(model ) {
|
|
226
|
+
return model.variants.find((v) => v.isDefault) ?? model.variants[0]
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
export function findVariant(model , variantId ) {
|
|
230
|
+
if (variantId === undefined) return defaultVariant(model)
|
|
231
|
+
return model.variants.find((v) => v.id === variantId) ?? defaultVariant(model)
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/** 所有模型的档位展开成一张扁平表(面板的"模型 / 档位"下拉用)。 */
|
|
235
|
+
export function variantOptions() {
|
|
236
|
+
const out = []
|
|
237
|
+
for (const model of EMBEDDING_MODELS) {
|
|
238
|
+
for (const variant of model.variants) {
|
|
239
|
+
out.push({
|
|
240
|
+
modelId: model.id,
|
|
241
|
+
modelLabel: model.label,
|
|
242
|
+
variantId: variant.id,
|
|
243
|
+
label: `${model.label} · ${variant.label}`,
|
|
244
|
+
bytes: variant.bytes,
|
|
245
|
+
dims: model.dims,
|
|
246
|
+
})
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
return out
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
//# sourceURL=source/memory-local/src/models.ts
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 按宿主硬件推荐模型与运行时(纯函数,零 I/O —— 便于用假硬件做测试)。
|
|
3
|
+
*
|
|
4
|
+
* 打分是**可解释**的:推荐结果旁边必须能说出"为什么是它",以及"为什么不是更大的那个"。
|
|
5
|
+
* 面板直接显示 reason/warnings,所以这里输出的都是人话,不是分数。
|
|
6
|
+
*
|
|
7
|
+
* 规则(全部可测):
|
|
8
|
+
* 质量 ×2 + 语言契合(中文环境优先中文/多语言) + 速度契合(CPU 上惩罚大模型)
|
|
9
|
+
* + 上下文契合(≤512 截断扣分) + 显存余量充足时的 +1(鼓励用大模型)
|
|
10
|
+
* 装不下的候选不参与竞争,但仍列在 candidates 里并带上原因,面板可以解释清楚。
|
|
11
|
+
*/
|
|
12
|
+
import { EMBEDDING_MODELS, defaultVariant, } from './models.js'
|
|
13
|
+
import { MIN_USEFUL_VRAM_MB, } from './hardware.js'
|
|
14
|
+
|
|
15
|
+
/** 权重 1.15 ≈ 权重 + 运行时缓冲;再加一段固定开销给 KV/上下文。 */
|
|
16
|
+
export function estimateFootprintMb(bytes , onGpu ) {
|
|
17
|
+
return Math.round((bytes / 1048576) * 1.15) + (onGpu ? 250 : 400)
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** 留给宿主与系统的余量:总内存的 60% 再扣 512MB。 */
|
|
21
|
+
export function usableRamMb(totalRamMb ) {
|
|
22
|
+
return Math.max(512, Math.round(totalRamMb * 0.6) - 512)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** 显存只用到 85%,留出驱动与碎片的空间。 */
|
|
26
|
+
export function usableVramMb(vramMb ) {
|
|
27
|
+
return Math.round(vramMb * 0.85)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function pickFlavor(hardware ) {
|
|
31
|
+
return hardware.flavors[0] ?? 'cpu'
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function languageFit(model , chineseUi ) {
|
|
35
|
+
if (chineseUi) return model.languages === 'zh' ? 3 : model.languages === 'multilingual' ? 2 : 0
|
|
36
|
+
return model.languages === 'en' ? 2 : 2
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function speedFit(model , onGpu ) {
|
|
40
|
+
// GPU 上速度差异不重要(整块在显存里),CPU 上 speed<=2 的模型是**每分钟级**的
|
|
41
|
+
// 差别(4B/8B 每次检索要几秒),所以给很重的惩罚。
|
|
42
|
+
if (onGpu) return 2
|
|
43
|
+
return model.speed >= 5 ? 4 : model.speed === 4 ? 3 : model.speed === 3 ? 2 : model.speed === 2 ? -3 : -6
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function contextFit(model ) {
|
|
47
|
+
return model.contextTokens >= 8192 ? 1 : model.contextTokens >= 1024 ? 0 : -1
|
|
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
|
+
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
function label(model , variant ) {
|
|
82
|
+
return `${model.label} · ${variant.label}`
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** 磁盘文案:MB 就是 MB,不足 1GB 时不要显示成 "0GB"。 */
|
|
86
|
+
export function diskText(freeDiskMb ) {
|
|
87
|
+
return freeDiskMb >= 1024 ? `${(freeDiskMb / 1024).toFixed(0)}GB` : `${freeDiskMb.toFixed(0)}MB`
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export function recommendModel(input ) {
|
|
91
|
+
const hw = input.hardware
|
|
92
|
+
const chineseUi = input.locale.toLowerCase().startsWith('zh')
|
|
93
|
+
const flavor = pickFlavor(hw)
|
|
94
|
+
const gpuCapable = flavor === 'cuda' || flavor === 'vulkan' || flavor === 'metal'
|
|
95
|
+
const ramBudget = usableRamMb(hw.totalRamMb)
|
|
96
|
+
const vramBudget = hw.gpu?.vramMb != null ? usableVramMb(hw.gpu.vramMb) : 0
|
|
97
|
+
|
|
98
|
+
const candidates = []
|
|
99
|
+
for (const model of EMBEDDING_MODELS) {
|
|
100
|
+
for (const variant of model.variants) {
|
|
101
|
+
const rawFootprint = estimateFootprintMb(variant.bytes, false)
|
|
102
|
+
// 能整块放进显存才算"上了 GPU";否则仍然按内存算(只是跑得慢)。
|
|
103
|
+
const onGpu = gpuCapable && vramBudget > 0 && estimateFootprintMb(variant.bytes, true) <= vramBudget
|
|
104
|
+
const footprintMb = estimateFootprintMb(variant.bytes, onGpu)
|
|
105
|
+
// 权重走 mmap,显存放得下时内存压力小得多,所以内存门槛按一半算(再加固定开销)。
|
|
106
|
+
const ramNeed = onGpu ? Math.min(footprintMb, Math.round(footprintMb * 0.5) + 200) : footprintMb
|
|
107
|
+
// 质量优先;速度只在 CPU 上参与(GPU 上恒为常数),上下文太短扣一分。
|
|
108
|
+
const score = model.quality * 2 + languageFit(model, chineseUi) + speedFit(model, onGpu) + contextFit(model)
|
|
109
|
+
let blocked = null
|
|
110
|
+
if (ramNeed > ramBudget) {
|
|
111
|
+
blocked = `约需 ${(ramNeed / 1024).toFixed(1)}GB 可用内存,当前可用约 ${(ramBudget / 1024).toFixed(1)}GB`
|
|
112
|
+
} else if (hw.freeDiskMb !== null && hw.freeDiskMb < (variant.bytes / 1048576) * 1.2 + 800) {
|
|
113
|
+
blocked = `磁盘剩余 ${(hw.freeDiskMb / 1024).toFixed(1)}GB 不够(模型约 ${(variant.bytes / 1048576 / 1024).toFixed(2)}GB)`
|
|
114
|
+
}
|
|
115
|
+
candidates.push({
|
|
116
|
+
modelId: model.id, variantId: variant.id, label: label(model, variant),
|
|
117
|
+
bytes: variant.bytes, footprintMb, onGpu, score, blocked,
|
|
118
|
+
})
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const viable = candidates.filter((c) => c.blocked === null)
|
|
123
|
+
// 一个都装不下时退到**体积最小**的那个(不是评分最高的:这台机器装不下任何模型,
|
|
124
|
+
// 小一点才有可能装上),并把原因写进 warnings,而不是抛错。
|
|
125
|
+
const ranked = (viable.length > 0
|
|
126
|
+
? viable.slice().sort((a, b) => b.score - a.score || a.bytes - b.bytes)
|
|
127
|
+
: candidates.slice().sort((a, b) => a.bytes - b.bytes))
|
|
128
|
+
const best = ranked[0]
|
|
129
|
+
|
|
130
|
+
const bestModel = EMBEDDING_MODELS.find((m) => m.id === best.modelId)
|
|
131
|
+
const reason = []
|
|
132
|
+
const machine = [
|
|
133
|
+
`${hw.cpuCount} 核`,
|
|
134
|
+
hw.totalRamMb > 0 ? `${Math.round(hw.totalRamMb / 1024)}GB 内存` : null,
|
|
135
|
+
hw.gpu !== null ? `显卡 ${hw.gpu.name}${hw.gpu.vramMb !== null ? `(${Math.round(hw.gpu.vramMb / 1024)}GB 显存)` : ''}` : '未检测到独立显卡',
|
|
136
|
+
hw.freeDiskMb !== null ? `磁盘剩余 ${diskText(hw.freeDiskMb)}` : null,
|
|
137
|
+
].filter(Boolean).join(' · ')
|
|
138
|
+
reason.push(`这台机器:${machine}`)
|
|
139
|
+
reason.push(`推荐 ${best.label}:${bestModel.note}${best.onGpu ? ',可整块放进显存' : `,占用约 ${(best.footprintMb / 1024).toFixed(1)}GB 内存`}`)
|
|
140
|
+
if (flavor === 'cuda') reason.push('运行时选 CUDA:检测到 NVIDIA 显卡与够新的驱动,llama.cpp 会把计算交给显卡')
|
|
141
|
+
else if (flavor === 'vulkan') reason.push('运行时选 Vulkan:显卡可用且不需要 CUDA 环境,是最省事的加速方式')
|
|
142
|
+
else if (flavor === 'metal') reason.push('运行时选 Metal:macOS 上 llama.cpp 默认走 Metal')
|
|
143
|
+
else reason.push(`运行时选 CPU:没有检测到可用显卡,或显存不足 ${(MIN_USEFUL_VRAM_MB / 1024).toFixed(0)}GB`)
|
|
144
|
+
if (chineseUi) reason.push('系统语言是中文:优先多语言/中文模型')
|
|
145
|
+
|
|
146
|
+
const warnings = []
|
|
147
|
+
if (input.recordCount > 0) {
|
|
148
|
+
warnings.push(`换模型会让已存的 ${input.recordCount} 条记忆重新嵌入(向量作废重算),期间检索暂时退化为词法匹配`)
|
|
149
|
+
}
|
|
150
|
+
if (!viable.includes(best)) warnings.push('没有找到完全适配这台机器的模型,已退到体积最小的那个')
|
|
151
|
+
if (flavor === 'cpu' && bestModel.speed <= 2) warnings.push('这个模型在 CPU 上每次检索要几秒;有显卡的话建议改用 Vulkan/CUDA 运行时')
|
|
152
|
+
if (hw.gpu !== null && hw.gpu.vendor === 'nvidia' && (hw.gpu.driver === null || Number.parseInt(hw.gpu.driver.split('.')[0] ?? '', 10) < 550)) {
|
|
153
|
+
warnings.push('NVIDIA 驱动版本读不到或偏旧,已按 Vulkan 推荐(CUDA 12.x 需要驱动 ≥ 550)')
|
|
154
|
+
}
|
|
155
|
+
if (flavor === 'vulkan' && hw.platform === 'linux') warnings.push('Linux 上 Vulkan 需要系统装了 Vulkan 驱动(通常 mesa-vulkan-drivers)')
|
|
156
|
+
|
|
157
|
+
const alternatives = ranked.slice(1, 4).map((c) => {
|
|
158
|
+
const model = EMBEDDING_MODELS.find((m) => m.id === c.modelId)
|
|
159
|
+
const sizeGb = (c.bytes / 1048576 / 1024).toFixed(2)
|
|
160
|
+
const why = c.blocked !== null
|
|
161
|
+
? `装不了:${c.blocked}`
|
|
162
|
+
: c.bytes > best.bytes
|
|
163
|
+
? `更大(${sizeGb}GB),质量略高但更吃内存`
|
|
164
|
+
: `更小(${sizeGb}GB),更省内存${model.speed >= 4 ? '、更快' : ''}`
|
|
165
|
+
return { modelId: c.modelId, variantId: c.variantId, label: c.label, why }
|
|
166
|
+
})
|
|
167
|
+
|
|
168
|
+
return {
|
|
169
|
+
modelId: best.modelId,
|
|
170
|
+
variantId: best.variantId,
|
|
171
|
+
flavor,
|
|
172
|
+
reason,
|
|
173
|
+
warnings,
|
|
174
|
+
alternatives,
|
|
175
|
+
candidates,
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/** 面板/日志用:把推荐结果压成一行。 */
|
|
180
|
+
export function describeRecommendation(rec ) {
|
|
181
|
+
const model = EMBEDDING_MODELS.find((m) => m.id === rec.modelId)
|
|
182
|
+
const variant = model !== undefined ? (model.variants.find((v) => v.id === rec.variantId) ?? defaultVariant(model)) : undefined
|
|
183
|
+
return variant !== undefined ? `${model .label} · ${variant.label}(${rec.flavor})` : `${rec.modelId}/${rec.variantId}(${rec.flavor})`
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
//# sourceURL=source/memory-local/src/recommend.ts
|
|
@@ -1,64 +1,64 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Rerank cache (design §12): identical (query, candidate set, judge model)
|
|
3
|
-
* repeats — common during iterative agent loops — reuse the previous LLM
|
|
4
|
-
* verdict instead of spending budget again. Entries are invalidated when the
|
|
5
|
-
* candidate set membership changes (id set hash), capped, and TTL-expires.
|
|
6
|
-
*/
|
|
7
|
-
import { createHash } from 'node:crypto'
|
|
8
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Rerank cache (design §12): identical (query, candidate set, judge model)
|
|
3
|
+
* repeats — common during iterative agent loops — reuse the previous LLM
|
|
4
|
+
* verdict instead of spending budget again. Entries are invalidated when the
|
|
5
|
+
* candidate set membership changes (id set hash), capped, and TTL-expires.
|
|
6
|
+
*/
|
|
7
|
+
import { createHash } from 'node:crypto'
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
export class RerankCache {
|
|
16
|
+
entries = new Map ()
|
|
17
|
+
maxEntries
|
|
18
|
+
ttlMs
|
|
19
|
+
|
|
20
|
+
constructor(maxEntries = 256, ttlMs = 30 * 60_000) {
|
|
21
|
+
this.maxEntries = maxEntries
|
|
22
|
+
this.ttlMs = ttlMs
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
static key(query , candidateIds , judgeModel ) {
|
|
26
|
+
return createHash('sha256')
|
|
27
|
+
.update(query)
|
|
28
|
+
.update('\u0000')
|
|
29
|
+
.update([...candidateIds].sort().join(','))
|
|
30
|
+
.update('\u0000')
|
|
31
|
+
.update(judgeModel)
|
|
32
|
+
.digest('hex')
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
get(key , now = Date.now()) {
|
|
36
|
+
const e = this.entries.get(key)
|
|
37
|
+
if (!e) return null
|
|
38
|
+
if (now - e.at > this.ttlMs) {
|
|
39
|
+
this.entries.delete(key)
|
|
40
|
+
return null
|
|
41
|
+
}
|
|
42
|
+
return e.outcome
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
set(key , outcome , now = Date.now()) {
|
|
46
|
+
// simple FIFO cap; insertion order in Map is stable
|
|
47
|
+
if (this.entries.size >= this.maxEntries) {
|
|
48
|
+
const oldest = this.entries.keys().next().value
|
|
49
|
+
if (oldest !== undefined) this.entries.delete(oldest)
|
|
50
|
+
}
|
|
51
|
+
this.entries.set(key, { outcome, at: now })
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
get size() {
|
|
55
|
+
return this.entries.size
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
clear() {
|
|
59
|
+
this.entries.clear()
|
|
60
|
+
}
|
|
61
|
+
}
|
|
9
62
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
63
|
|
|
15
|
-
|
|
16
|
-
entries = new Map ()
|
|
17
|
-
maxEntries
|
|
18
|
-
ttlMs
|
|
19
|
-
|
|
20
|
-
constructor(maxEntries = 256, ttlMs = 30 * 60_000) {
|
|
21
|
-
this.maxEntries = maxEntries
|
|
22
|
-
this.ttlMs = ttlMs
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
static key(query , candidateIds , judgeModel ) {
|
|
26
|
-
return createHash('sha256')
|
|
27
|
-
.update(query)
|
|
28
|
-
.update('\u0000')
|
|
29
|
-
.update([...candidateIds].sort().join(','))
|
|
30
|
-
.update('\u0000')
|
|
31
|
-
.update(judgeModel)
|
|
32
|
-
.digest('hex')
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
get(key , now = Date.now()) {
|
|
36
|
-
const e = this.entries.get(key)
|
|
37
|
-
if (!e) return null
|
|
38
|
-
if (now - e.at > this.ttlMs) {
|
|
39
|
-
this.entries.delete(key)
|
|
40
|
-
return null
|
|
41
|
-
}
|
|
42
|
-
return e.outcome
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
set(key , outcome , now = Date.now()) {
|
|
46
|
-
// simple FIFO cap; insertion order in Map is stable
|
|
47
|
-
if (this.entries.size >= this.maxEntries) {
|
|
48
|
-
const oldest = this.entries.keys().next().value
|
|
49
|
-
if (oldest !== undefined) this.entries.delete(oldest)
|
|
50
|
-
}
|
|
51
|
-
this.entries.set(key, { outcome, at: now })
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
get size() {
|
|
55
|
-
return this.entries.size
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
clear() {
|
|
59
|
-
this.entries.clear()
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
//# sourceURL=D:\.deepseek\memory-plugin\packages\memory-local\src\rerank-cache.ts
|
|
64
|
+
//# sourceURL=source/memory-local/src/rerank-cache.ts
|