Repository navigation
Expand file tree
/
Copy pathcustom_graph_model_entity_schema_definition.py
More file actions
82 lines (56 loc) · 2.38 KB
/
Copy pathcustom_graph_model_entity_schema_definition.py
File metadata and controls
82 lines (56 loc) · 2.38 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
import asyncio
import os
from cognee import SearchType, config, forget, recall, remember, visualize_graph
from cognee.low_level import DataPoint
# Define a custom graph model for programming languages.
class FieldType(DataPoint):
name: str = "Field"
class Field(DataPoint):
name: str
is_type: FieldType
metadata: dict = {"index_fields": ["name"]}
class ProgrammingLanguageType(DataPoint):
name: str = "Programming Language"
class ProgrammingLanguage(DataPoint):
name: str
used_in: list[Field] = []
is_type: ProgrammingLanguageType
metadata: dict = {"index_fields": ["name"]}
def set_up_config():
data_directory_path = os.path.join(os.path.dirname(__file__), ".data_storage")
# Set up the data directory. Cognee will store files here.
config.data_root_directory(data_directory_path)
cognee_directory_path = os.path.join(os.path.dirname(__file__), ".cognee_system")
# Set up the Cognee system directory. Cognee will store system files and databases here.
config.system_root_directory(cognee_directory_path)
async def visualize_data():
graph_file_path = os.path.join(
os.path.dirname(__file__), ".artifacts", "graph_visualization.html"
)
await visualize_graph(graph_file_path)
async def main():
set_up_config()
# Prune data and system metadata before running, only if we want "fresh" state.
await forget(everything=True)
text = "The Python programming language is widely used in data analysis, web development, and machine learning."
await remember(text, graph_model=ProgrammingLanguage, self_improvement=False)
await visualize_data()
# Completion query that uses graph data to form context.
graph_completion = await recall(
query_text="What is Python?", query_type=SearchType.GRAPH_COMPLETION
)
print(graph_completion)
# Completion query that uses document chunks to form context.
rag_completion = await recall(
query_text="What is Python?", query_type=SearchType.RAG_COMPLETION
)
print(rag_completion)
# Query all summaries related to query.
summaries = await recall(query_text="Python", query_type=SearchType.SUMMARIES)
for summary in summaries:
print(summary)
chunks = await recall(query_text="Python", query_type=SearchType.CHUNKS)
for chunk in chunks:
print(chunk)
if __name__ == "__main__":
asyncio.run(main())