-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_custom.ts
More file actions
133 lines (110 loc) · 5.08 KB
/
example_custom.ts
File metadata and controls
133 lines (110 loc) · 5.08 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
import { SampleDataGenerator } from './src/sample-data.js';
import { GraphManager } from './src/graph.js';
import { ConversationAnalyzer } from './src/analyzer.js';
import { LLMClient } from './src/llm.js';
/**
* Example: Analyzing a custom conversation
* This shows how to use the system with your own conversation data
*/
async function analyzeCustomConversation() {
console.log('=== Custom Conversation Analysis Example ===\n');
// 1. Create your own conversation
const generator = new SampleDataGenerator();
const customConversation = generator.generateCustomConversation(
[
{ role: 'user', content: 'Hi! I\'m Alex and I love building web applications.' },
{ role: 'assistant', content: 'Hello Alex! That\'s great. What technologies do you use?' },
{ role: 'user', content: 'I mainly use Vue.js and Node.js. I also work with MongoDB.' },
{ role: 'assistant', content: 'Excellent stack! Do you work with anyone else?' },
{ role: 'user', content: 'Yes, my colleague Maria is great with DevOps. She knows Docker and Kubernetes.' },
{ role: 'assistant', content: 'That\'s a valuable skill set. What about your preferences?' },
{ role: 'user', content: 'I really enjoy pair programming, but I don\'t like long standup meetings.' },
{ role: 'assistant', content: 'That makes sense. Efficient meetings are important!' },
],
{
topic: 'Alex\'s Tech Stack',
participants: ['Alex', 'Maria'],
}
);
console.log('Custom Conversation Created:');
console.log(`- Topic: ${customConversation.metadata.topic}`);
console.log(`- Messages: ${customConversation.messages.length}`);
console.log(`- Participants: ${customConversation.metadata.participants?.join(', ')}\n`);
// 2. Initialize analyzer
const graphManager = new GraphManager();
const analyzer = new ConversationAnalyzer(graphManager);
// 3. Analyze the conversation
console.log('Analyzing conversation...\n');
const result = analyzer.analyzeConversation(customConversation);
// 4. Display results
console.log('📊 Analysis Results:');
console.log(`\nEntities Found (${result.entities.length}):`);
const entitiesByType = result.entities.reduce((acc, e) => {
if (!acc[e.type]) acc[e.type] = [];
acc[e.type].push(e.text);
return acc;
}, {} as Record<string, string[]>);
for (const [type, entities] of Object.entries(entitiesByType)) {
console.log(` ${type}: ${entities.join(', ')}`);
}
console.log(`\nRelationships Found (${result.relationships.length}):`);
result.relationships.slice(0, 10).forEach(rel => {
console.log(` ${rel.from} --[${rel.type}]--> ${rel.to} (strength: ${rel.strength})`);
});
if (result.relationships.length > 10) {
console.log(` ... and ${result.relationships.length - 10} more`);
}
console.log('\n💡 Insights:');
result.insights.forEach(insight => console.log(` • ${insight}`));
// 5. Explore the graph
console.log('\n🕸️ Knowledge Graph:');
const graph = graphManager.exportGraph();
console.log(` Nodes: ${graph.nodes.length}`);
console.log(` Edges: ${graph.edges.length}`);
// Find connections
const people = graphManager.getNodesByType('person');
const topics = graphManager.getNodesByType('topic');
if (people.length > 0) {
console.log(`\n👥 People in graph: ${people.map(p => p.label).join(', ')}`);
// Show what each person is connected to
for (const person of people) {
const connections = graphManager.getConnectedNodes(person.id);
console.log(` ${person.label} is connected to: ${connections.map(n => n.label).join(', ')}`);
}
}
if (topics.length > 0) {
console.log(`\n💻 Technologies mentioned: ${topics.map(t => t.label).join(', ')}`);
}
// 6. Query with LLM (if available)
const llmClient = new LLMClient();
if (llmClient.isAvailable()) {
console.log('\n🤖 Querying LLM with graph insights...\n');
const graphInsights = [
`Knowledge graph contains ${graph.nodes.length} nodes and ${graph.edges.length} edges.`,
`People: ${people.map(p => p.label).join(', ')}`,
`Technologies: ${topics.map(t => t.label).join(', ')}`,
...result.insights,
];
const questions = [
"What technologies does Alex use?",
"Who does Alex work with and what are their skills?",
"What are Alex's work preferences?",
];
for (const question of questions) {
console.log(`Q: ${question}`);
const response = await llmClient.query({ question, graphInsights });
console.log(`A: ${response.answer}\n`);
}
} else {
console.log('\n⚠️ LLM not available (set ANTHROPIC_API_KEY to enable)');
console.log('Graph insights are still available without LLM:\n');
result.insights.forEach(insight => console.log(` • ${insight}`));
}
console.log('\n✅ Analysis complete!');
console.log('\nThis example shows how to:');
console.log(' 1. Create custom conversations');
console.log(' 2. Extract entities and relationships');
console.log(' 3. Build a knowledge graph');
console.log(' 4. Query with LLM assistance');
}
analyzeCustomConversation().catch(console.error);