-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestLlmDocuments.js
More file actions
243 lines (221 loc) · 8.55 KB
/
testLlmDocuments.js
File metadata and controls
243 lines (221 loc) · 8.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import BosBase from 'bosbase';
const baseUrl =
process.env.BOSBASE_BASE_URL ?? 'http://127.0.0.1:8090';
const authEmail =
process.env.BOSBASE_EMAIL ??
process.env.BOSBASE_SUPERUSER_EMAIL ??
'try@bosbase.com';
const authPassword =
process.env.BOSBASE_PASSWORD ??
process.env.BOSBASE_SUPERUSER_PASSWORD ??
'bosbasepass';
async function main() {
try {
const pb = new BosBase(baseUrl);
// Authenticate as superuser
console.log('[INFO] Authenticating as superuser...');
const userData = await pb
.collection('_superusers')
.authWithPassword(authEmail, authPassword);
console.log('[SUCCESS] Authenticated as superuser');
const collectionName = `test-knowledge-base-${Date.now()}`;
// Test 1: Create collection
console.log('\n[INFO] Test 1: Creating LLM document collection...');
await pb.llmDocuments.createCollection(collectionName, {
domain: 'test',
});
console.log(`[SUCCESS] Collection "${collectionName}" created`);
// Test 2: Insert document without ID (auto-generated)
console.log('\n[INFO] Test 2: Inserting document without ID...');
const doc1 = await pb.llmDocuments.insert(
{
content: 'Leaves are green because chlorophyll absorbs red and blue light.',
metadata: { topic: 'biology' },
},
{ collection: collectionName }
);
console.log('[SUCCESS] Document inserted');
console.log('Document ID:', doc1.id);
console.log('Document:', JSON.stringify(doc1, null, 2));
// Test 3: Insert several additional documents (auto-generated IDs)
console.log('\n[INFO] Test 3: Inserting additional documents...');
const additionalDocs = [
{
content: 'The sky is blue because of Rayleigh scattering.',
metadata: { topic: 'physics' },
},
{
content: 'Water is essential for all known forms of life.',
metadata: { topic: 'biology' },
},
{
content: 'Gravity keeps planets in orbit around the sun.',
metadata: { topic: 'physics' },
},
];
const insertedDocs = [];
for (const payload of additionalDocs) {
const created = await pb.llmDocuments.insert(payload, {
collection: collectionName,
});
insertedDocs.push(created);
console.log(`[SUCCESS] Inserted document ${created.id}`);
}
const targetDocId = insertedDocs[0]?.id ?? doc1.id;
// Test 4: Query documents by semantic similarity
console.log('\n[INFO] Test 4: Querying documents by semantic similarity...');
const queryResult = await pb.llmDocuments.query(
{
queryText: 'Why is the sky blue?',
limit: 3,
where: { topic: 'physics' },
},
{ collection: collectionName }
);
console.log('[SUCCESS] Query executed');
console.log('Query results:', JSON.stringify(queryResult, null, 2));
if (queryResult.results) {
queryResult.results.forEach((match, index) => {
console.log(
` Result ${index + 1}: ID=${match.id}, Similarity=${match.similarity}, Topic=${match.metadata?.topic}, Content="${match.content}"`
);
});
}
// Test 5: Query without where clause
console.log('\n[INFO] Test 5: Querying documents without where clause...');
const queryResult2 = await pb.llmDocuments.query(
{
queryText: 'What is green?',
limit: 5,
},
{ collection: collectionName }
);
console.log('[SUCCESS] Query executed');
console.log('Query results count:', queryResult2.results?.length ?? 0);
if (queryResult2.results) {
queryResult2.results.forEach((match, index) => {
console.log(` Result ${index + 1}: ID=${match.id}, Similarity=${match.similarity}`);
});
}
// Test 6: List documents with pagination
console.log('\n[INFO] Test 6: Listing documents with pagination...');
const listResult = await pb.llmDocuments.list({
collection: collectionName,
page: 1,
perPage: 25,
});
console.log('[SUCCESS] Documents listed');
console.log('Total items:', listResult.totalItems);
console.log('Items on page:', listResult.items?.length ?? 0);
if (listResult.items) {
listResult.items.forEach((item, index) => {
console.log(` Item ${index + 1}: ID=${item.id}`);
});
}
// Test 7: Get specific document by ID
console.log('\n[INFO] Test 7: Fetching specific document by ID...');
try {
const fetchedDoc = await pb.llmDocuments.get(targetDocId, {
collection: collectionName,
});
console.log('[SUCCESS] Document fetched');
console.log('Fetched document:', fetchedDoc.id);
} catch (error) {
console.log('[WARNING] Could not fetch document');
if (error?.response) {
console.log('Status:', error.response.status);
console.log('Error data:', JSON.stringify(error.response.data, null, 2));
}
}
// Test 8: Update document
console.log('\n[INFO] Test 8: Updating document...');
const updateResult = await pb.llmDocuments.update(
targetDocId,
{ metadata: { topic: 'physics', reviewed: 'true' } },
{ collection: collectionName }
);
console.log('[SUCCESS] Document updated');
console.log('Updated document:', JSON.stringify(updateResult, null, 2));
// Test 9: List collections
console.log('\n[INFO] Test 9: Listing all collections...');
try {
const collections = await pb.llmDocuments.listCollections();
console.log('[SUCCESS] Collections listed');
console.log('Collections:', JSON.stringify(collections, null, 2));
} catch (error) {
console.log('[WARNING] Could not list collections (method may not exist)');
if (error?.response) {
console.log('Status:', error.response.status);
console.log('Error data:', JSON.stringify(error.response.data, null, 2));
}
}
// Test 10: Delete document
console.log('\n[INFO] Test 10: Deleting document...');
await pb.llmDocuments.delete(targetDocId, { collection: collectionName });
console.log(`[SUCCESS] Document "${targetDocId}" deleted`);
// Verify deletion by listing again
console.log('\n[INFO] Verifying deletion by listing documents...');
const listAfterDelete = await pb.llmDocuments.list({
collection: collectionName,
page: 1,
perPage: 25,
});
console.log('[SUCCESS] Documents listed after deletion');
console.log('Total items after deletion:', listAfterDelete.totalItems);
console.log('Items on page:', listAfterDelete.items?.length ?? 0);
// Test 11: Error handling - query non-existent collection
console.log('\n[INFO] Test 11: Testing error handling with non-existent collection...');
try {
await pb.llmDocuments.query(
{
queryText: 'test query',
limit: 1,
},
{ collection: 'non-existent-collection' }
);
console.log('[WARNING] Query on non-existent collection did not throw an error');
} catch (error) {
console.log('[SUCCESS] Non-existent collection query was caught');
if (error?.response) {
console.log('Status:', error.response.status);
console.log('Error data:', JSON.stringify(error.response.data, null, 2));
} else {
console.log('Error:', error.message);
}
}
// Test 12: Error handling - delete non-existent document
console.log('\n[INFO] Test 12: Testing error handling with non-existent document...');
try {
await pb.llmDocuments.delete('non-existent-id', {
collection: collectionName,
});
console.log('[WARNING] Delete of non-existent document did not throw an error');
} catch (error) {
console.log('[SUCCESS] Non-existent document delete was caught');
if (error?.response) {
console.log('Status:', error.response.status);
console.log('Error data:', JSON.stringify(error.response.data, null, 2));
} else {
console.log('Error:', error.message);
}
}
// Test 13: Delete collection
console.log('\n[INFO] Test 13: Deleting collection...');
await pb.llmDocuments.deleteCollection(collectionName);
console.log(`[SUCCESS] Collection "${collectionName}" deleted`);
console.log('\n========== All Tests Completed ==========');
} catch (error) {
console.error('[ERROR] Request failed:');
if (error?.response) {
console.error('Status:', error.response.status);
console.error('Data:', JSON.stringify(error.response.data, null, 2));
if (error.response.data?.message) {
console.error('Message:', error.response.data.message);
}
} else {
console.error(error);
}
process.exit(1);
}
}
main();