-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-vector-search.ts
More file actions
210 lines (184 loc) · 6.29 KB
/
test-vector-search.ts
File metadata and controls
210 lines (184 loc) · 6.29 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
#!/usr/bin/env tsx
/**
* Test script for Phase 2 vector search functionality
*
* Prerequisites:
* 1. Run: npx tsx scripts/migrate-add-vectors.ts
* 2. Set: ENABLE_VECTOR_SEARCH=true
* 3. Set: OPENAI_API_KEY=your-key
*
* Usage:
* npx tsx test-vector-search.ts
*/
import { TursoDatabase } from './src/lib/turso-graph';
import type { CognitiveSpace } from './src/lib/turso-graph';
// Test data - sample cognitive spaces
const testSpaces: CognitiveSpace[] = [
{
metadata: {
id: 'test-trust',
title: 'Trust and Evidence',
description: 'Exploring the relationship between trust and evidence in belief formation',
createdAt: Date.now()
},
nodes: {
Trust: {
id: 'Trust',
meanings: [
{
content: 'Reliance on something without complete verification. A cognitive shortcut that enables action under uncertainty.',
confidence: 0.9,
timestamp: Date.now()
}
],
values: { importance: 0.8, fragility: 0.7 },
relationships: [],
history: []
},
Evidence: {
id: 'Evidence',
meanings: [
{
content: 'Information that supports or refutes a claim. The foundation of rational belief.',
confidence: 0.85,
timestamp: Date.now()
}
],
values: { strength: 0.9, availability: 0.6 },
relationships: [],
history: []
}
},
globalHistory: ['Space created', 'Trust node added', 'Evidence node added']
},
{
metadata: {
id: 'test-learning',
title: 'Learning and Memory',
description: 'How organisms acquire, store, and retrieve information',
createdAt: Date.now()
},
nodes: {
Encoding: {
id: 'Encoding',
meanings: [
{
content: 'The process of converting sensory information into memory traces. Initial registration of information.',
confidence: 0.88,
timestamp: Date.now()
}
],
values: { efficiency: 0.7, attention_required: 0.9 },
relationships: [],
history: []
},
Retrieval: {
id: 'Retrieval',
meanings: [
{
content: 'Accessing stored information from memory. The act of bringing memories back to consciousness.',
confidence: 0.82,
timestamp: Date.now()
}
],
values: { accuracy: 0.6, context_dependency: 0.85 },
relationships: [],
history: []
}
},
globalHistory: ['Space created', 'Encoding node added', 'Retrieval node added']
}
];
async function testVectorSearch() {
console.log('🧪 Testing Vector Search Functionality\n');
console.log('======================================\n');
// Check environment
if (!process.env.OPENAI_API_KEY) {
console.error('❌ OPENAI_API_KEY not set');
process.exit(1);
}
const db = new TursoDatabase({ enableVectorSearch: true });
try {
// Test 1: Insert spaces with embeddings
console.log('📝 Test 1: Inserting test spaces with embeddings...\n');
for (const space of testSpaces) {
console.log(` Inserting "${space.metadata.title}"...`);
await db.insertSpace(space);
console.log(` ✓ Inserted with auto-generated embeddings\n`);
}
// Small delay to ensure embeddings are written
await new Promise(resolve => setTimeout(resolve, 1000));
// Test 2: Search spaces by semantic similarity
console.log('🔍 Test 2: Semantic space search...\n');
const spaceQueries = [
'belief and verification',
'cognition and information storage',
'memory processes'
];
for (const query of spaceQueries) {
console.log(` Query: "${query}"`);
const results = await db.searchSpaces(query, 3);
if (results.length === 0) {
console.log(' ⚠️ No results found\n');
continue;
}
console.log(` Found ${results.length} results:\n`);
for (const result of results) {
console.log(` ${result.title}`);
console.log(` Similarity: ${(result.similarity * 100).toFixed(1)}% | Distance: ${result.distance.toFixed(3)}`);
console.log(` ${result.description}\n`);
}
}
// Test 3: Search nodes across all spaces
console.log('🔍 Test 3: Global node search...\n');
const nodeQueries = [
'information processing',
'belief without proof',
'accessing stored data'
];
for (const query of nodeQueries) {
console.log(` Query: "${query}"`);
const results = await db.searchAllNodes(query, 3, 0.4);
if (results.length === 0) {
console.log(' ⚠️ No results found\n');
continue;
}
console.log(` Found ${results.length} results:\n`);
for (const result of results) {
console.log(` Node: ${result.nodeKey} (Space: ${result.spaceId})`);
console.log(` Similarity: ${(result.similarity * 100).toFixed(1)}%`);
console.log(` Content: ${result.content.substring(0, 100)}...\n`);
}
}
// Test 4: Search nodes within specific space
console.log('🔍 Test 4: Space-specific node search...\n');
const spaceSpecificQuery = 'cognitive shortcuts';
const targetSpace = 'test-trust';
console.log(` Query: "${spaceSpecificQuery}" in space "${targetSpace}"`);
const results = await db.searchNodesInSpace(targetSpace, spaceSpecificQuery, 3, 0.3);
if (results.length === 0) {
console.log(' ⚠️ No results found\n');
} else {
console.log(` Found ${results.length} results:\n`);
for (const result of results) {
console.log(` Node: ${result.nodeKey}`);
console.log(` Similarity: ${(result.similarity * 100).toFixed(1)}%`);
console.log(` Content: ${result.content.substring(0, 100)}...\n`);
}
}
// Cleanup
console.log('🧹 Cleanup: Removing test spaces...\n');
for (const space of testSpaces) {
await db.deleteSpace(space.metadata.id);
console.log(` ✓ Deleted "${space.metadata.title}"`);
}
console.log('\n✅ All tests completed successfully!\n');
} catch (error) {
console.error('\n❌ Test failed:', error);
process.exit(1);
} finally {
await db.close();
}
}
// Run tests
testVectorSearch();