-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_setup.py
More file actions
166 lines (130 loc) · 4.64 KB
/
test_setup.py
File metadata and controls
166 lines (130 loc) · 4.64 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
#!/usr/bin/env python3
"""
Test script to verify the knowledge graph extraction setup.
"""
def test_basic_imports():
"""Test if basic imports work."""
print("🧪 Testing basic imports...")
try:
from kg_builder.workflow import EBWorkflow
from kg_builder.engine import GeminiEngine
from kg_builder.relations import RelationsData
from kg_builder.prompts.prompting import ExtractorPrompting, BuilderPrompting
print("✅ All kg_builder imports successful")
return True
except ImportError as e:
print(f"❌ Import error: {e}")
return False
def test_dependencies():
"""Test if external dependencies work."""
print("\n🧪 Testing external dependencies...")
missing = []
try:
import torch
print(f"✅ PyTorch: {torch.__version__}")
except ImportError:
missing.append("torch")
try:
import transformers
print(f"✅ Transformers: {transformers.__version__}")
except ImportError:
missing.append("transformers")
try:
from google import genai
print("✅ Google GenAI available")
except ImportError:
missing.append("google-genai")
try:
from langchain_huggingface import HuggingFaceEmbeddings
print("✅ LangChain HuggingFace available")
except ImportError:
missing.append("langchain-huggingface")
try:
import networkx as nx
print(f"✅ NetworkX: {nx.__version__}")
except ImportError:
missing.append("networkx")
try:
import matplotlib
print(f"✅ Matplotlib: {matplotlib.__version__}")
except ImportError:
missing.append("matplotlib")
if missing:
print(f"\n❌ Missing packages: {', '.join(missing)}")
print("Run: pip install -r requirements.txt")
return False
return True
def test_configuration():
"""Test configuration setup."""
print("\n🧪 Testing configuration...")
try:
from dotenv import load_dotenv
import os
load_dotenv()
api_key = os.getenv("GEMINI_API_KEY")
if not api_key:
print("⚠️ No GEMINI_API_KEY found in .env file")
return False
elif api_key == "your_gemini_api_key_here":
print("⚠️ Please replace placeholder API key in .env file")
return False
else:
print("✅ API key configured")
return True
except ImportError:
print("❌ python-dotenv not installed")
return False
def test_simple_workflow():
"""Test if workflow can be instantiated."""
print("\n🧪 Testing workflow instantiation...")
try:
from langchain_huggingface import HuggingFaceEmbeddings
from kg_builder.workflow import EBWorkflow
from kg_builder.relations import RelationsData
# This will download the model if not cached
print(" 📥 Loading embeddings model (may take a moment)...")
embeddings = HuggingFaceEmbeddings(
model_name="sentence-transformers/all-MiniLM-L6-v2"
)
workflow = EBWorkflow(embeddings=embeddings)
relations_data = RelationsData.empty(["PERSON", "COMPANY"])
print("✅ Workflow instantiation successful")
return True
except Exception as e:
print(f"❌ Workflow test failed: {e}")
return False
def main():
"""Run all tests."""
print("🧪 Knowledge Graph Extraction - System Test")
print("=" * 50)
tests = [
test_basic_imports,
test_dependencies,
test_configuration,
test_simple_workflow
]
results = []
for test in tests:
try:
result = test()
results.append(result)
except Exception as e:
print(f"❌ Test failed with exception: {e}")
results.append(False)
print("\n" + "=" * 50)
print("🏁 Test Summary:")
success_count = sum(results)
total_count = len(results)
if success_count == total_count:
print(f"✅ All {total_count} tests passed!")
print("\n🎉 System is ready!")
print("Run 'python run.py' to start extraction.")
else:
print(f"❌ {total_count - success_count} of {total_count} tests failed")
print("\n🔧 Please fix the issues above before running the system.")
print("\n📚 Quick start guide:")
print("1. Make sure all tests pass")
print("2. Add your Gemini API key to .env file")
print("3. Run: python run.py")
if __name__ == "__main__":
main()