-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.go
More file actions
199 lines (180 loc) · 5.99 KB
/
main.go
File metadata and controls
199 lines (180 loc) · 5.99 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
package main
import (
"fmt"
"log"
"time"
graphiti "github.com/vxcontrol/graphiti-go-client"
"github.com/google/uuid"
)
// This example demonstrates basic usage of the Graphiti Go client.
//
// It covers:
// - Basic operations (health check, add messages, search, get memory)
// - Entity node management
// - Episode retrieval
// - Langfuse observation tracking (optional)
//
// For advanced search methods, see ../advanced-search-example/main.go
//
// Important: The /messages endpoint processes data asynchronously. This example
// polls for episodes to verify data was successfully created before searching.
//
// Troubleshooting: If you see "No episodes were created" errors:
// 1. Check server logs for "Error executing Neo4j query: Driver closed"
// 2. Ensure Neo4j is running and properly configured
// 3. Verify the Graphiti server has a persistent database connection
// 4. Check that the async worker is processing jobs successfully
func main() {
// Create a client with extended timeout for long-running operations
client := graphiti.NewClient("http://localhost:8000", graphiti.WithTimeout(60*time.Second))
// Health check
fmt.Println("=== Health Check ===")
health, err := client.HealthCheck()
if err != nil {
log.Fatalf("Health check failed: %v", err)
}
fmt.Printf("Status: %s\n\n", health.Status)
// Create a unique group ID for this example
groupID := uuid.New().String()
fmt.Printf("Using group ID: %s\n\n", groupID)
// Optional: Create Langfuse observation for tracking
// Note: In production, these IDs should come from your Langfuse instance
observation := &graphiti.Observation{
ID: uuid.New().String(),
TraceID: uuid.New().String(),
Time: time.Now(),
}
// Add messages with rich context for better demonstration
fmt.Println("=== Adding Messages ===")
now := time.Now()
messages := []graphiti.Message{
{
Content: "I love hiking in the mountains on weekends. My favorite trail is the Pacific Crest Trail.",
Author: "Alice",
Timestamp: now.Add(-5 * time.Hour),
},
{
Content: "That sounds amazing! Do you go camping as well?",
Author: "Assistant",
Timestamp: now.Add(-4 * time.Hour),
},
{
Content: "Yes! I usually camp near the trail. I also enjoy photography, especially nature and landscape shots.",
Author: "Alice",
Timestamp: now.Add(-3 * time.Hour),
},
{
Content: "I recently bought a new DSLR camera for my trips. It's a Canon EOS R5.",
Author: "Alice",
Timestamp: now.Add(-2 * time.Hour),
},
{
Content: "Last summer, I hiked the John Muir Trail and took some incredible photos of Yosemite.",
Author: "Alice",
Timestamp: now.Add(-1 * time.Hour),
},
}
addResult, err := client.AddMessages(graphiti.AddMessagesRequest{
GroupID: groupID,
Messages: messages,
Observation: observation,
})
if err != nil {
log.Fatalf("Failed to add messages: %v", err)
}
fmt.Printf("%s: %v\n\n", addResult.Message, addResult.Success)
// Wait for processing and verify data exists (poll for episodes)
fmt.Println("Waiting for messages to be processed...")
maxAttempts := 12
pollInterval := 5 * time.Second
var episodes []graphiti.Episode
for attempt := 1; attempt <= maxAttempts; attempt++ {
fmt.Printf(" Polling for episodes (attempt %d/%d)...\n", attempt, maxAttempts)
episodes, err = client.GetEpisodes(groupID, 10)
if err != nil {
log.Printf(" Warning: Failed to get episodes: %v", err)
} else if len(episodes) > 0 {
fmt.Printf(" ✓ Found %d episodes, processing complete!\n\n", len(episodes))
break
}
if attempt < maxAttempts {
time.Sleep(pollInterval)
}
}
if len(episodes) == 0 {
log.Fatalf("Timeout: No episodes were created after %v. The async job may have failed.", time.Duration(maxAttempts)*pollInterval)
}
// Basic Search
fmt.Println("=== Basic Search ===")
searchResult, err := client.Search(graphiti.SearchQuery{
Query: "What does the user like to do?",
MaxFacts: 5,
GroupIDs: &[]string{groupID},
Observation: observation,
})
if err != nil {
log.Fatalf("Search failed: %v", err)
}
fmt.Printf("Found %d facts:\n", len(searchResult.Facts))
for i, fact := range searchResult.Facts {
fmt.Printf("%d. %s\n (from: %s, created: %s)\n",
i+1, fact.Fact, fact.Name, fact.CreatedAt.Format(time.RFC3339))
}
fmt.Println()
// Get memory from messages
fmt.Println("=== Getting Memory ===")
memoryMessages := []graphiti.Message{
{
Content: "What hobbies and equipment does the user have?",
Author: "User",
Timestamp: time.Now(),
},
}
memoryResponse, err := client.GetMemory(graphiti.GetMemoryRequest{
GroupID: groupID,
MaxFacts: 10,
Messages: memoryMessages,
Observation: observation,
})
if err != nil {
log.Fatalf("Failed to get memory: %v", err)
}
fmt.Printf("Retrieved %d facts from memory:\n", len(memoryResponse.Facts))
for i, fact := range memoryResponse.Facts {
fmt.Printf("%d. %s\n", i+1, fact.Fact)
}
fmt.Println()
// Add an entity node
fmt.Println("=== Adding Entity Node ===")
entityUUID := uuid.New().String()
node, err := client.AddEntityNode(graphiti.AddEntityNodeRequest{
UUID: entityUUID,
GroupID: groupID,
Name: "User Interests",
Summary: "The user's hobbies and interests including hiking, camping, and photography",
Observation: observation,
})
if err != nil {
log.Fatalf("Failed to add entity node: %v", err)
}
fmt.Printf("Created entity node: %s (UUID: %s)\n\n", node.Name, node.UUID)
// Display episodes
fmt.Println("=== Episodes Summary ===")
fmt.Printf("Total episodes: %d\n", len(episodes))
for i, episode := range episodes {
preview := episode.Content
if len(preview) > 80 {
preview = preview[:80] + "..."
}
fmt.Printf("%d. %s: %s\n", i+1, episode.Name, preview)
}
fmt.Println()
// Cleanup: delete the group
fmt.Println("=== Cleanup ===")
deleteResult, err := client.DeleteGroup(groupID)
if err != nil {
log.Printf("Warning: Failed to delete group: %v", err)
} else {
fmt.Printf("%s: %v\n", deleteResult.Message, deleteResult.Success)
}
}