-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbasic-usage.ts
More file actions
224 lines (190 loc) · 6.9 KB
/
Copy pathbasic-usage.ts
File metadata and controls
224 lines (190 loc) · 6.9 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
import { KnowledgeGraph, SQLiteAdapter, CommonEdgeType, KnowledgeExtractor } from '../src';
async function main() {
// Create a knowledge graph with SQLite backend
const adapter = new SQLiteAdapter({
connection: './knowledge.db', // Use ':memory:' for in-memory database
debug: true,
});
const graph = new KnowledgeGraph(adapter);
await graph.initialize();
console.log('📊 Knowledge Graph initialized\n');
// ============ Basic Node and Edge Operations ============
console.log('=== Creating Nodes ===');
// Add some people
const alice = await graph.addNode({
type: 'PERSON',
label: 'Alice Johnson',
properties: {
email: 'alice@example.com',
age: 28,
occupation: 'Software Engineer',
},
confidence: 1.0,
});
console.log(`✅ Created person: ${alice.label}`);
const bob = await graph.addNode({
type: 'PERSON',
label: 'Bob Smith',
properties: {
email: 'bob@example.com',
age: 32,
occupation: 'Product Manager',
},
confidence: 1.0,
});
console.log(`✅ Created person: ${bob.label}`);
// Add a company
const techCorp = await graph.addNode({
type: 'ORGANIZATION',
label: 'TechCorp Inc',
properties: {
industry: 'Technology',
founded: 2015,
employees: 500,
},
confidence: 1.0,
});
console.log(`✅ Created organization: ${techCorp.label}`);
// Add a location
const office = await graph.addNode({
type: 'LOCATION',
label: 'TechCorp HQ',
properties: {
address: '123 Tech Street, San Francisco, CA',
type: 'office',
},
confidence: 1.0,
});
console.log(`✅ Created location: ${office.label}`);
console.log('\n=== Creating Relationships ===');
// Create relationships
await graph.addEdge({
type: CommonEdgeType.EMPLOYED_BY,
fromNodeId: alice.id,
toNodeId: techCorp.id,
properties: {
since: '2020-01-15',
position: 'Senior Software Engineer',
},
});
console.log(`✅ ${alice.label} -> EMPLOYED_BY -> ${techCorp.label}`);
await graph.addEdge({
type: CommonEdgeType.EMPLOYED_BY,
fromNodeId: bob.id,
toNodeId: techCorp.id,
properties: {
since: '2019-06-01',
position: 'Product Manager',
},
});
console.log(`✅ ${bob.label} -> EMPLOYED_BY -> ${techCorp.label}`);
await graph.addEdge({
type: CommonEdgeType.COLLEAGUE_OF,
fromNodeId: alice.id,
toNodeId: bob.id,
bidirectional: true, // Creates edges in both directions
});
console.log(`✅ ${alice.label} <-> COLLEAGUE_OF <-> ${bob.label}`);
await graph.addEdge({
type: CommonEdgeType.LOCATED_IN,
fromNodeId: techCorp.id,
toNodeId: office.id,
});
console.log(`✅ ${techCorp.label} -> LOCATED_IN -> ${office.label}`);
// ============ Querying the Graph ============
console.log('\n=== Querying the Graph ===');
// Query by type
const allPeople = await graph.queryByType('PERSON');
console.log(`\nFound ${allPeople.nodes.length} people:`);
for (const person of allPeople.nodes) {
console.log(` - ${person.label} (${person.properties.occupation})`);
}
// Query related nodes
const aliceNetwork = await graph.queryRelated(alice.id, {
depth: 2,
includeEdges: true,
});
console.log(`\nAlice's network (depth 2):`);
console.log(` - ${aliceNetwork.nodes.length} nodes`);
console.log(` - ${aliceNetwork.edges.length} edges`);
// Find shortest path
const path = await graph.findShortestPath(alice.id, office.id);
if (path) {
console.log(`\nShortest path from ${alice.label} to ${office.label}:`);
console.log(` Path length: ${path.length}`);
console.log(` Nodes: ${path.nodes.map(n => n.label).join(' -> ')}`);
}
// ============ Knowledge Extraction ============
console.log('\n=== Knowledge Extraction ===');
const extractor = new KnowledgeExtractor(graph);
// Extract from text
const text = `
Carol Davis is a Data Scientist at TechCorp Inc. She joined the company in 2021
and works closely with Alice Johnson on machine learning projects. Carol has a
PhD from Stanford University and specializes in natural language processing.
Her email is carol.davis@techcorp.com and she can be reached at +1-555-0123.
She recently published a paper on knowledge graphs that costs $29.99.
`;
const extraction = await extractor.extractFromText(text, {
extractEntities: true,
extractRelationships: true,
minConfidence: 0.5,
});
console.log(`\nExtracted from text:`);
console.log(` - ${extraction.nodes.length} entities`);
console.log(` - ${extraction.edges.length} relationships`);
console.log(` - Overall confidence: ${extraction.confidence.toFixed(2)}`);
// Process the extracted knowledge
const { nodesAdded, edgesAdded } = await extractor.processExtractedKnowledge(
extraction,
{
mergeStrategy: 'merge',
sessionId: 'example-session',
}
);
console.log(`\nAdded to graph:`);
console.log(` - ${nodesAdded} new nodes`);
console.log(` - ${edgesAdded} new edges`);
// Extract from conversation
const conversation = [
{ role: 'user', content: 'I need to schedule a meeting with David Brown from Marketing.' },
{ role: 'assistant', content: 'I can help you schedule a meeting with David Brown.' },
{ role: 'user', content: 'He works in the New York office and handles social media campaigns.' },
{ role: 'assistant', content: 'Got it. David Brown from Marketing in the New York office.' },
];
const conversationExtraction = await extractor.extractFromConversation(conversation, {
extractEntities: true,
extractTopics: true,
});
console.log(`\nExtracted from conversation:`);
console.log(` - ${conversationExtraction.nodes.length} entities`);
const topics = conversationExtraction.metadata?.topics;
console.log(` - Topics: ${Array.isArray(topics) ? topics.join(', ') : 'none'}`);
// ============ Graph Statistics ============
console.log('\n=== Graph Statistics ===');
const stats = await graph.getStats();
console.log(`\nGraph statistics:`);
console.log(` - Total nodes: ${stats.nodeCount}`);
console.log(` - Total edges: ${stats.edgeCount}`);
console.log(` - Average degree: ${stats.averageDegree.toFixed(2)}`);
console.log(` - Graph density: ${stats.density.toFixed(4)}`);
console.log(`\nNodes by type:`);
for (const [type, count] of Object.entries(stats.nodesByType)) {
console.log(` - ${type}: ${count}`);
}
// ============ Search ============
console.log('\n=== Search Operations ===');
const searchResults = await graph.search({
query: 'engineer tech',
limit: 5,
});
console.log(`\nSearch results for "engineer tech":`);
for (const node of searchResults.nodes) {
console.log(` - ${node.label} (${node.type})`);
}
// ============ Cleanup ============
await graph.close();
console.log('\n✨ Knowledge Graph closed');
}
// Run the example
main().catch(console.error);