xiaozuoassistant 0.2.29 → 0.2.31
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.
|
@@ -26,21 +26,20 @@ export class VectorMemory {
|
|
|
26
26
|
this.db = await lancedb.connect(VECTOR_DB_DIR);
|
|
27
27
|
// Ensure table exists
|
|
28
28
|
const tableNames = await this.db.tableNames();
|
|
29
|
-
let vectorDim = 1536; // Default to text-embedding-ada-002 dimension
|
|
30
|
-
// Determine dimension based on configured model
|
|
31
|
-
if (config.llm.embeddingModel === 'text-embedding-v1' || config.llm.embeddingModel === 'text-embedding-v2' || config.llm.embeddingModel === 'text-embedding-v3') {
|
|
32
|
-
// Aliyun / Dashscope embedding dimension is 1536
|
|
33
|
-
vectorDim = 1536;
|
|
34
|
-
}
|
|
35
|
-
else if (config.llm.embeddingModel === 'text-embedding-3-small') {
|
|
36
|
-
vectorDim = 1536;
|
|
37
|
-
}
|
|
38
|
-
else if (config.llm.embeddingModel === 'text-embedding-3-large') {
|
|
39
|
-
vectorDim = 3072;
|
|
40
|
-
}
|
|
41
29
|
if (!tableNames.includes('memories')) {
|
|
42
30
|
// Create table with dummy data to define schema, then delete it
|
|
43
31
|
// LanceDB schema inference is based on data
|
|
32
|
+
let vectorDim = 1536; // Fallback default
|
|
33
|
+
// 动态探测向量维度:通过调用一次真实的 embedding API,获取准确的维度
|
|
34
|
+
console.log(`[VectorMemory] Detecting embedding dimension for model: ${config.llm.embeddingModel || 'default'}...`);
|
|
35
|
+
const testVector = await this.getEmbedding('init');
|
|
36
|
+
if (testVector && testVector.length > 0) {
|
|
37
|
+
vectorDim = testVector.length;
|
|
38
|
+
console.log(`[VectorMemory] Detected actual embedding dimension: ${vectorDim}`);
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
console.warn(`[VectorMemory] Failed to detect dimension, falling back to ${vectorDim}`);
|
|
42
|
+
}
|
|
44
43
|
const dummyData = [{
|
|
45
44
|
id: 'init',
|
|
46
45
|
text: 'init',
|
|
@@ -52,6 +51,18 @@ export class VectorMemory {
|
|
|
52
51
|
}
|
|
53
52
|
else {
|
|
54
53
|
this.table = await this.db.openTable('memories');
|
|
54
|
+
// 如果表存在,且当前模型变更导致查询时维度不匹配,必须提供处理机制或抛出明确错误
|
|
55
|
+
// 为了稳定运行,我们在打开表时验证当前模型的维度是否与表结构匹配
|
|
56
|
+
console.log(`[VectorMemory] Validating embedding dimension for existing table...`);
|
|
57
|
+
const testVector = await this.getEmbedding('init');
|
|
58
|
+
if (testVector && testVector.length > 0) {
|
|
59
|
+
// 读取表中的一行来获取表的维度
|
|
60
|
+
const sample = await this.table.search(Array(testVector.length).fill(0)).limit(1).execute().catch(() => null);
|
|
61
|
+
if (sample && sample.length > 0 && sample[0].vector.length !== testVector.length) {
|
|
62
|
+
console.error(`[VectorMemory] CRITICAL: Existing database dimension (${sample[0].vector.length}) does not match current model dimension (${testVector.length}).`);
|
|
63
|
+
console.error(`[VectorMemory] You MUST delete the vector database directory manually to reset it.`);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
55
66
|
}
|
|
56
67
|
this.isInitialized = true;
|
|
57
68
|
}
|