Repository navigation
Expand file tree
/
Copy pathtemplate_agent_workflow.py
More file actions
198 lines (164 loc) · 6.55 KB
/
Copy pathtemplate_agent_workflow.py
File metadata and controls
198 lines (164 loc) · 6.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
"""
template_agent_workflow.py — Template agent script for ingesting + querying the context graph.
This script demonstrates the complete workflow an agent should follow:
1. Read markdown guidance files
2. Extract entities/relations via LLM reasoning
3. Call Python methods to persist
4. Query the graph
5. Handle errors gracefully
Copy and adapt this template for your agent implementation.
"""
import json
import sys
from pathlib import Path
# Add tools to path
sys.path.insert(0, str(Path(__file__).parent))
from contextgraph import ContextGraphSkill
def ingest_document(skill: ContextGraphSkill, document: str) -> dict:
"""
Step 1: Agent reads ingestion.md and ontology.md
Step 2: Agent uses LLM to extract entities and relations
Step 3: Call Python methods to persist (mimicked here with static extraction)
In a real agent, replace the static extraction with LLM calls.
"""
print(f"\n[INGEST] Processing document:\n{document}\n")
# --- STEP 1 & 2: LLM EXTRACTION PHASE (Guided by ingestion.md + ontology.md) ---
# In a real agent, this would use LLM reasoning.
# For now, we'll mock an extraction result:
extraction_result = {
"entities": [
{"name": "memory leak", "type": "issue"},
{"name": "system crash", "type": "issue"},
{"name": "object", "type": "component"},
],
"relations": [
{
"source": "memory leak",
"target": "system crash",
"type": "causes",
"confidence": 1.0,
},
{
"source": "object",
"target": "memory leak",
"type": "contributes to",
"confidence": 0.9,
},
],
}
print(f"[LLM] Extracted entities + relations:")
print(json.dumps(extraction_result, indent=2))
# --- STEP 3: PERSIST PHASE (Call Python methods) ---
errors = []
added_nodes = {}
for entity in extraction_result["entities"]:
try:
node_id = skill.add_node(entity["name"], entity["type"])
added_nodes[entity["name"]] = node_id
print(f" ✓ Added node: {entity['name']} (id: {node_id}, type: {entity['type']})")
except Exception as e:
errors.append(f"Failed to add node {entity['name']}: {e}")
print(f" ✗ Error adding node {entity['name']}: {e}")
for relation in extraction_result["relations"]:
# Validate both endpoints exist
if relation["source"] not in added_nodes or relation["target"] not in added_nodes:
error_msg = f"Cannot add edge: source or target missing"
errors.append(error_msg)
print(f" ✗ Skip edge {relation['source']} → {relation['target']}: {error_msg}")
continue
# Validate confidence threshold
if relation["confidence"] < 0.6:
error_msg = f"Confidence {relation['confidence']} < 0.6 (minimum threshold)"
errors.append(error_msg)
print(f" ✗ Skip edge {relation['source']} → {relation['target']}: {error_msg}")
continue
try:
skill.add_edge(
source_name=relation["source"],
target_name=relation["target"],
relation=relation["type"],
confidence=relation["confidence"],
)
print(
f" ✓ Added edge: {relation['source']} "
f"--[{relation['type']}]→ {relation['target']} "
f"(confidence: {relation['confidence']})"
)
except Exception as e:
errors.append(f"Failed to add edge {relation['source']} → {relation['target']}: {e}")
print(f" ✗ Error adding edge: {e}")
return {
"success": len(errors) == 0,
"nodes_added": len(added_nodes),
"edges_added": len(extraction_result["relations"]) - len(
[e for e in errors if "skip edge" in e.lower()]
),
"errors": errors,
}
def query_graph(skill: ContextGraphSkill, query: str) -> dict:
"""
Query the graph for context to answer the user's question.
Step 1: Read retrieval.md
Step 2: Call skill.query() which internally handles BFS + subgraph extraction
Step 3: Return structured context
"""
print(f"\n[QUERY] {query}\n")
try:
subgraph = skill.query(query)
if not subgraph["nodes"]:
print(" ℹ No relevant entities found in graph.")
return {
"success": True,
"query": query,
"subgraph": subgraph,
"nodes_found": 0,
"edges_found": 0,
}
print(f" ✓ Retrieved subgraph with {len(subgraph['nodes'])} nodes, {len(subgraph['edges'])} edges")
print(f"\n Nodes:")
for node_id, node in subgraph["nodes"].items():
print(f" - {node['name']} (type: {node['type']}, id: {node_id})")
print(f"\n Edges:")
for edge in subgraph["edges"]:
source_name = subgraph["nodes"][edge["source"]]["name"]
target_name = subgraph["nodes"][edge["target"]]["name"]
print(
f" - {source_name} --[{edge['type']}]→ {target_name} "
f"(confidence: {edge['confidence']})"
)
return {
"success": True,
"query": query,
"subgraph": subgraph,
"nodes_found": len(subgraph["nodes"]),
"edges_found": len(subgraph["edges"]),
}
except Exception as e:
error_msg = f"Query failed: {e}"
print(f" ✗ {error_msg}")
return {"success": False, "query": query, "error": error_msg}
def main():
"""Demo: ingest a document, then query the graph."""
skill = ContextGraphSkill()
# ===== INGESTION =====
document = """
System crashes due to memory leaks.
Memory leaks occur when objects are not released.
"""
result = ingest_document(skill, document)
print(f"\n[INGEST RESULT] Nodes added: {result['nodes_added']}, " f"Edges added: {result['edges_added']}")
if result["errors"]:
print(f"Errors: {result['errors']}")
# ===== RETRIEVAL =====
queries = [
"Why does the system crash?",
"What causes memory leaks?",
]
for query in queries:
result = query_graph(skill, query)
if result["success"]:
print(f" Nodes found: {result['nodes_found']}, Edges found: {result['edges_found']}")
else:
print(f" Error: {result['error']}")
if __name__ == "__main__":
main()