xiaozuoassistant 0.2.27 → 0.2.29
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,13 +26,25 @@ 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
|
+
}
|
|
29
41
|
if (!tableNames.includes('memories')) {
|
|
30
42
|
// Create table with dummy data to define schema, then delete it
|
|
31
43
|
// LanceDB schema inference is based on data
|
|
32
44
|
const dummyData = [{
|
|
33
45
|
id: 'init',
|
|
34
46
|
text: 'init',
|
|
35
|
-
vector: Array(
|
|
47
|
+
vector: Array(vectorDim).fill(0),
|
|
36
48
|
metadata: { type: 'recent', timestamp: Date.now(), sessionId: 'init', tags: '' }
|
|
37
49
|
}];
|
|
38
50
|
this.table = await this.db.createTable('memories', dummyData);
|
|
@@ -50,12 +62,10 @@ export class VectorMemory {
|
|
|
50
62
|
async getEmbedding(text) {
|
|
51
63
|
if (!this.openai) {
|
|
52
64
|
console.warn('OpenAI client not initialized (missing API Key), skipping embedding.');
|
|
53
|
-
return
|
|
65
|
+
return []; // Return empty instead of zero array
|
|
54
66
|
}
|
|
55
67
|
try {
|
|
56
68
|
// Use OpenAI compatible embedding endpoint
|
|
57
|
-
// Note: Some compatible providers might use different models/dimensions.
|
|
58
|
-
// Default to text-embedding-ada-002 or similar standard.
|
|
59
69
|
const model = config.llm.embeddingModel || 'text-embedding-ada-002';
|
|
60
70
|
const response = await this.openai.embeddings.create({
|
|
61
71
|
model: model,
|
|
@@ -65,9 +75,7 @@ export class VectorMemory {
|
|
|
65
75
|
}
|
|
66
76
|
catch (error) {
|
|
67
77
|
console.error('Embedding failed:', error);
|
|
68
|
-
//
|
|
69
|
-
// For MVP robustness, return zeros if embedding fails (search won't work but app won't crash)
|
|
70
|
-
return Array(1536).fill(0);
|
|
78
|
+
return []; // Return empty instead of zero array to trigger upstream handling
|
|
71
79
|
}
|
|
72
80
|
}
|
|
73
81
|
async addMemory(text, type, metadata = {}) {
|
|
@@ -76,6 +84,10 @@ export class VectorMemory {
|
|
|
76
84
|
if (!this.table)
|
|
77
85
|
return; // DB failed to init
|
|
78
86
|
const vector = await this.getEmbedding(text);
|
|
87
|
+
if (!vector || vector.length === 0) {
|
|
88
|
+
console.error('Failed to get valid embedding, skipping memory addition.');
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
79
91
|
const memory = {
|
|
80
92
|
id: Math.random().toString(36).substring(7),
|
|
81
93
|
text,
|
|
@@ -99,6 +111,10 @@ export class VectorMemory {
|
|
|
99
111
|
if (!this.table)
|
|
100
112
|
return [];
|
|
101
113
|
const vector = await this.getEmbedding(query);
|
|
114
|
+
if (!vector || vector.length === 0) {
|
|
115
|
+
console.error('Failed to get valid embedding for query, skipping search.');
|
|
116
|
+
return [];
|
|
117
|
+
}
|
|
102
118
|
// Check if table supports search
|
|
103
119
|
if (this.table.search) {
|
|
104
120
|
let search = this.table.search(vector).limit(limit);
|
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
import OpenAI from 'openai';
|
|
2
2
|
export function resolveBaseURL(config) {
|
|
3
|
+
// 优先使用配置文件中显式指定的 baseURL,除非它是空字符串或未定义
|
|
4
|
+
if (config.baseURL && config.baseURL.trim() !== '') {
|
|
5
|
+
return config.baseURL;
|
|
6
|
+
}
|
|
7
|
+
// 如果没有配置 baseURL,则根据 provider 提供默认的官方地址
|
|
3
8
|
switch (config.provider) {
|
|
4
9
|
case 'deepseek':
|
|
5
10
|
return 'https://api.deepseek.com';
|
|
@@ -12,7 +17,7 @@ export function resolveBaseURL(config) {
|
|
|
12
17
|
case 'custom':
|
|
13
18
|
case 'openai':
|
|
14
19
|
default:
|
|
15
|
-
return
|
|
20
|
+
return undefined; // 留空则使用 OpenAI 官方默认地址
|
|
16
21
|
}
|
|
17
22
|
}
|
|
18
23
|
export function createOpenAIClient(config) {
|