-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_packer_rag.py
More file actions
executable file
·118 lines (95 loc) · 3.43 KB
/
test_packer_rag.py
File metadata and controls
executable file
·118 lines (95 loc) · 3.43 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
#!/usr/bin/env python3
"""
Cognee RAG workflow - Retrieve relevant chunks
This retrieves context that Claude Code can then read and use to answer questions
"""
import asyncio
import sys
from pathlib import Path
from client import CogneeClient, SearchType
async def main():
COGNEE_URL = "http://eva_cognee:8181"
PACKER_DOC_PATH = "/home/steve/DevOps/workspaces/DesconSimGui/docs/packer.md"
DATASET_NAME = "DesconSim_Packer"
print("=" * 80)
print("Cognee RAG Test - Retrieve Context for Claude Code")
print("=" * 80)
print()
# Read document
print("[1/4] Reading packer.md...")
packer_content = Path(PACKER_DOC_PATH).read_text()
print(f"✓ Loaded ({len(packer_content)} characters)")
print()
# Initialize Cognee client
print("[2/4] Connecting to Cognee...")
cognee_client = CogneeClient(base_url=COGNEE_URL, timeout=1800.0)
print("✓ Connected")
print()
try:
# Reset and add document
print("[3/4] Resetting and adding document...")
await cognee_client.prune()
print(" ✓ Memory cleared")
await cognee_client.add(text=packer_content, dataset_name=DATASET_NAME)
print(" ✓ Document added")
await cognee_client.cognify(datasets=[DATASET_NAME])
print(" ✓ Knowledge graph built")
print()
# Perform retrieval queries
print("[4/4] Retrieving context for questions...")
print()
queries = [
"What is priming and downstream control in the packer?",
"Explain the speed control hierarchy",
"How does the virtual conveyor system work?"
]
for query_num, question in enumerate(queries, 1):
print("=" * 80)
print(f"QUERY {query_num}/{len(queries)}: {question}")
print("=" * 80)
print()
# Retrieve context from Cognee
search_result = await cognee_client.search(
query_text=question,
query_type=SearchType.CHUNKS
)
if not search_result.get("success"):
print(f"ERROR: Search failed: {search_result}")
continue
# Extract text from chunks
chunks = search_result.get("data", [])
context_parts = []
for chunk in chunks[:2]: # Use top 2 chunks
if isinstance(chunk, dict) and 'text' in chunk:
context_parts.append(chunk['text'])
if not context_parts:
print("No context retrieved")
continue
# Display retrieved context
print(f"Retrieved {len(context_parts)} relevant chunk(s):")
print()
print("-" * 80)
print("RETRIEVED CONTEXT:")
print("-" * 80)
for i, context in enumerate(context_parts, 1):
print(f"\n[Chunk {i}]")
print(context)
print()
print("-" * 80)
print()
print("Claude Code: Please read the above context and answer the question.")
print()
print("=" * 80)
print("Retrieval complete!")
print("=" * 80)
except Exception as e:
print(f"ERROR: {e}")
import traceback
traceback.print_exc()
return 1
finally:
await cognee_client.close()
return 0
if __name__ == "__main__":
exit_code = asyncio.run(main())
sys.exit(exit_code)