synapse-pkg 0.1.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- synapse_pkg-0.1.1/LICENSE +21 -0
- synapse_pkg-0.1.1/PKG-INFO +222 -0
- synapse_pkg-0.1.1/README.md +211 -0
- synapse_pkg-0.1.1/pyproject.toml +18 -0
- synapse_pkg-0.1.1/setup.cfg +4 -0
- synapse_pkg-0.1.1/synapse/__init__.py +1 -0
- synapse_pkg-0.1.1/synapse/extractor.py +63 -0
- synapse_pkg-0.1.1/synapse/graph.py +19 -0
- synapse_pkg-0.1.1/synapse/persistence.py +22 -0
- synapse_pkg-0.1.1/synapse/processor.py +25 -0
- synapse_pkg-0.1.1/synapse/query.py +40 -0
- synapse_pkg-0.1.1/synapse/synapse.py +37 -0
- synapse_pkg-0.1.1/synapse/utils.py +2 -0
- synapse_pkg-0.1.1/synapse_pkg.egg-info/PKG-INFO +222 -0
- synapse_pkg-0.1.1/synapse_pkg.egg-info/SOURCES.txt +16 -0
- synapse_pkg-0.1.1/synapse_pkg.egg-info/dependency_links.txt +1 -0
- synapse_pkg-0.1.1/synapse_pkg.egg-info/requires.txt +1 -0
- synapse_pkg-0.1.1/synapse_pkg.egg-info/top_level.txt +1 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Piyush Garg
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: synapse-pkg
|
|
3
|
+
Version: 0.1.1
|
|
4
|
+
Summary: Knowledge graph extraction library powered by LLMs
|
|
5
|
+
Author: Piyush Garg
|
|
6
|
+
Requires-Python: >=3.10
|
|
7
|
+
Description-Content-Type: text/markdown
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Requires-Dist: google-generativeai
|
|
10
|
+
Dynamic: license-file
|
|
11
|
+
|
|
12
|
+
# π§ Synapse
|
|
13
|
+
|
|
14
|
+
> Transform natural language into a persistent knowledge graph.
|
|
15
|
+
|
|
16
|
+
Synapse is a Python library that converts unstructured text into a structured knowledge graph by extracting concepts and relationships from natural language.
|
|
17
|
+
|
|
18
|
+
Instead of storing information as raw text, Synapse organizes information into a graph structure that can be queried, traversed, and extended for building intelligent systems and memory systems.
|
|
19
|
+
|
|
20
|
+
---
|
|
21
|
+
|
|
22
|
+
## β¨ Features
|
|
23
|
+
|
|
24
|
+
- Natural language β structured relationships
|
|
25
|
+
- Persistent graph storage
|
|
26
|
+
- Duplicate prevention
|
|
27
|
+
- Case-insensitive querying
|
|
28
|
+
- Multi-hop relationship traversal
|
|
29
|
+
- Local-first architecture
|
|
30
|
+
- Provider-agnostic design
|
|
31
|
+
- Extensible memory engine
|
|
32
|
+
|
|
33
|
+
---
|
|
34
|
+
|
|
35
|
+
## π¦ Installation
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
pip install synapse-ai
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
---
|
|
42
|
+
|
|
43
|
+
## β‘ Quick Start
|
|
44
|
+
|
|
45
|
+
```python
|
|
46
|
+
from synapse import Synapse
|
|
47
|
+
|
|
48
|
+
brain = Synapse()
|
|
49
|
+
|
|
50
|
+
brain.add(
|
|
51
|
+
"CEO of Apple met Microsoft executives in Seattle."
|
|
52
|
+
)
|
|
53
|
+
|
|
54
|
+
brain.save()
|
|
55
|
+
|
|
56
|
+
print(
|
|
57
|
+
brain.get_relations("Apple")
|
|
58
|
+
)
|
|
59
|
+
|
|
60
|
+
print(
|
|
61
|
+
brain.get_indirect_relations("CEO of Apple")
|
|
62
|
+
)
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
---
|
|
66
|
+
|
|
67
|
+
## Example
|
|
68
|
+
|
|
69
|
+
### Input
|
|
70
|
+
|
|
71
|
+
```text
|
|
72
|
+
Python is used for AI and TensorFlow is used in AI.
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
### Extracted Graph
|
|
76
|
+
|
|
77
|
+
```python
|
|
78
|
+
{
|
|
79
|
+
"python": [
|
|
80
|
+
("used_for", "ai")
|
|
81
|
+
],
|
|
82
|
+
"tensorflow": [
|
|
83
|
+
("used_in", "ai")
|
|
84
|
+
]
|
|
85
|
+
}
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
---
|
|
89
|
+
|
|
90
|
+
## Project Structure
|
|
91
|
+
|
|
92
|
+
```text
|
|
93
|
+
synapse/
|
|
94
|
+
βββ __init__.py
|
|
95
|
+
βββ synapse.py
|
|
96
|
+
βββ graph.py
|
|
97
|
+
βββ query.py
|
|
98
|
+
βββ processor.py
|
|
99
|
+
βββ extractor.py
|
|
100
|
+
βββ persistence.py
|
|
101
|
+
βββ utils.py
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
---
|
|
105
|
+
|
|
106
|
+
## βοΈ Design Principles
|
|
107
|
+
|
|
108
|
+
- Simplicity over complexity
|
|
109
|
+
- Local-first architecture
|
|
110
|
+
- Incremental development
|
|
111
|
+
- Extensibility
|
|
112
|
+
- No overengineering
|
|
113
|
+
|
|
114
|
+
---
|
|
115
|
+
|
|
116
|
+
## π Current Status
|
|
117
|
+
|
|
118
|
+
### β
V1 Complete
|
|
119
|
+
|
|
120
|
+
Implemented features:
|
|
121
|
+
|
|
122
|
+
- Text ingestion
|
|
123
|
+
- Relationship extraction
|
|
124
|
+
- Graph construction
|
|
125
|
+
- Graph persistence
|
|
126
|
+
- Query system
|
|
127
|
+
- Multi-hop traversal
|
|
128
|
+
- Python package support
|
|
129
|
+
- Provider-agnostic extractor architecture
|
|
130
|
+
|
|
131
|
+
---
|
|
132
|
+
|
|
133
|
+
# πΊοΈ Roadmap
|
|
134
|
+
|
|
135
|
+
## V2 β Conversational Memory Layer
|
|
136
|
+
|
|
137
|
+
Build a conversational interface on top of the graph.
|
|
138
|
+
|
|
139
|
+
Planned capabilities:
|
|
140
|
+
|
|
141
|
+
- Memory-backed conversations
|
|
142
|
+
- Graph-aware retrieval
|
|
143
|
+
- Contextual question answering
|
|
144
|
+
- Better reasoning over stored knowledge
|
|
145
|
+
- Persistent long-term memory
|
|
146
|
+
|
|
147
|
+
---
|
|
148
|
+
|
|
149
|
+
## V3 β Agentic Knowledge Graph
|
|
150
|
+
|
|
151
|
+
Integrate coding agents and sandboxed environments.
|
|
152
|
+
|
|
153
|
+
Planned capabilities:
|
|
154
|
+
|
|
155
|
+
- Claude Code integration
|
|
156
|
+
- Codex integration
|
|
157
|
+
- Live graph construction during conversations
|
|
158
|
+
- Automatic graph updates
|
|
159
|
+
- Multi-session memory
|
|
160
|
+
- Knowledge graph visualization
|
|
161
|
+
|
|
162
|
+
---
|
|
163
|
+
|
|
164
|
+
## V4 β Portable Intelligence
|
|
165
|
+
|
|
166
|
+
Turn Synapse into a transferable memory engine.
|
|
167
|
+
|
|
168
|
+
Planned capabilities:
|
|
169
|
+
|
|
170
|
+
- Import knowledge graphs
|
|
171
|
+
- Export knowledge graphs
|
|
172
|
+
- Transfer memory between LLMs
|
|
173
|
+
- Local execution
|
|
174
|
+
- Offline mode
|
|
175
|
+
- Personal AI memory system
|
|
176
|
+
|
|
177
|
+
Example:
|
|
178
|
+
|
|
179
|
+
```text
|
|
180
|
+
Claude
|
|
181
|
+
β
|
|
182
|
+
Knowledge Graph
|
|
183
|
+
β
|
|
184
|
+
Export
|
|
185
|
+
β
|
|
186
|
+
GPT
|
|
187
|
+
β
|
|
188
|
+
Continue with the same memory
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
---
|
|
192
|
+
|
|
193
|
+
## π Vision
|
|
194
|
+
|
|
195
|
+
Modern AI systems own the intelligence, but they also own the memory.
|
|
196
|
+
|
|
197
|
+
Synapse aims to separate the two.
|
|
198
|
+
|
|
199
|
+
The long-term goal is to make memory portable, persistent, and independent of any single model provider.
|
|
200
|
+
|
|
201
|
+
Memory should belong to the user, not the model.
|
|
202
|
+
|
|
203
|
+
---
|
|
204
|
+
|
|
205
|
+
## π§βπ» Author
|
|
206
|
+
|
|
207
|
+
### Piyush Garg
|
|
208
|
+
|
|
209
|
+
AI/ML enthusiast interested in:
|
|
210
|
+
|
|
211
|
+
- Knowledge Graphs
|
|
212
|
+
- LLM Systems
|
|
213
|
+
- Intelligent Agents
|
|
214
|
+
- Machine Learning Infrastructure
|
|
215
|
+
|
|
216
|
+
Building systems at the intersection of memory and intelligence.
|
|
217
|
+
|
|
218
|
+
---
|
|
219
|
+
|
|
220
|
+
## π License
|
|
221
|
+
|
|
222
|
+
MIT License
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
# π§ Synapse
|
|
2
|
+
|
|
3
|
+
> Transform natural language into a persistent knowledge graph.
|
|
4
|
+
|
|
5
|
+
Synapse is a Python library that converts unstructured text into a structured knowledge graph by extracting concepts and relationships from natural language.
|
|
6
|
+
|
|
7
|
+
Instead of storing information as raw text, Synapse organizes information into a graph structure that can be queried, traversed, and extended for building intelligent systems and memory systems.
|
|
8
|
+
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
## β¨ Features
|
|
12
|
+
|
|
13
|
+
- Natural language β structured relationships
|
|
14
|
+
- Persistent graph storage
|
|
15
|
+
- Duplicate prevention
|
|
16
|
+
- Case-insensitive querying
|
|
17
|
+
- Multi-hop relationship traversal
|
|
18
|
+
- Local-first architecture
|
|
19
|
+
- Provider-agnostic design
|
|
20
|
+
- Extensible memory engine
|
|
21
|
+
|
|
22
|
+
---
|
|
23
|
+
|
|
24
|
+
## π¦ Installation
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
pip install synapse-ai
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
---
|
|
31
|
+
|
|
32
|
+
## β‘ Quick Start
|
|
33
|
+
|
|
34
|
+
```python
|
|
35
|
+
from synapse import Synapse
|
|
36
|
+
|
|
37
|
+
brain = Synapse()
|
|
38
|
+
|
|
39
|
+
brain.add(
|
|
40
|
+
"CEO of Apple met Microsoft executives in Seattle."
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
brain.save()
|
|
44
|
+
|
|
45
|
+
print(
|
|
46
|
+
brain.get_relations("Apple")
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
print(
|
|
50
|
+
brain.get_indirect_relations("CEO of Apple")
|
|
51
|
+
)
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
---
|
|
55
|
+
|
|
56
|
+
## Example
|
|
57
|
+
|
|
58
|
+
### Input
|
|
59
|
+
|
|
60
|
+
```text
|
|
61
|
+
Python is used for AI and TensorFlow is used in AI.
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### Extracted Graph
|
|
65
|
+
|
|
66
|
+
```python
|
|
67
|
+
{
|
|
68
|
+
"python": [
|
|
69
|
+
("used_for", "ai")
|
|
70
|
+
],
|
|
71
|
+
"tensorflow": [
|
|
72
|
+
("used_in", "ai")
|
|
73
|
+
]
|
|
74
|
+
}
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
---
|
|
78
|
+
|
|
79
|
+
## Project Structure
|
|
80
|
+
|
|
81
|
+
```text
|
|
82
|
+
synapse/
|
|
83
|
+
βββ __init__.py
|
|
84
|
+
βββ synapse.py
|
|
85
|
+
βββ graph.py
|
|
86
|
+
βββ query.py
|
|
87
|
+
βββ processor.py
|
|
88
|
+
βββ extractor.py
|
|
89
|
+
βββ persistence.py
|
|
90
|
+
βββ utils.py
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
---
|
|
94
|
+
|
|
95
|
+
## βοΈ Design Principles
|
|
96
|
+
|
|
97
|
+
- Simplicity over complexity
|
|
98
|
+
- Local-first architecture
|
|
99
|
+
- Incremental development
|
|
100
|
+
- Extensibility
|
|
101
|
+
- No overengineering
|
|
102
|
+
|
|
103
|
+
---
|
|
104
|
+
|
|
105
|
+
## π Current Status
|
|
106
|
+
|
|
107
|
+
### β
V1 Complete
|
|
108
|
+
|
|
109
|
+
Implemented features:
|
|
110
|
+
|
|
111
|
+
- Text ingestion
|
|
112
|
+
- Relationship extraction
|
|
113
|
+
- Graph construction
|
|
114
|
+
- Graph persistence
|
|
115
|
+
- Query system
|
|
116
|
+
- Multi-hop traversal
|
|
117
|
+
- Python package support
|
|
118
|
+
- Provider-agnostic extractor architecture
|
|
119
|
+
|
|
120
|
+
---
|
|
121
|
+
|
|
122
|
+
# πΊοΈ Roadmap
|
|
123
|
+
|
|
124
|
+
## V2 β Conversational Memory Layer
|
|
125
|
+
|
|
126
|
+
Build a conversational interface on top of the graph.
|
|
127
|
+
|
|
128
|
+
Planned capabilities:
|
|
129
|
+
|
|
130
|
+
- Memory-backed conversations
|
|
131
|
+
- Graph-aware retrieval
|
|
132
|
+
- Contextual question answering
|
|
133
|
+
- Better reasoning over stored knowledge
|
|
134
|
+
- Persistent long-term memory
|
|
135
|
+
|
|
136
|
+
---
|
|
137
|
+
|
|
138
|
+
## V3 β Agentic Knowledge Graph
|
|
139
|
+
|
|
140
|
+
Integrate coding agents and sandboxed environments.
|
|
141
|
+
|
|
142
|
+
Planned capabilities:
|
|
143
|
+
|
|
144
|
+
- Claude Code integration
|
|
145
|
+
- Codex integration
|
|
146
|
+
- Live graph construction during conversations
|
|
147
|
+
- Automatic graph updates
|
|
148
|
+
- Multi-session memory
|
|
149
|
+
- Knowledge graph visualization
|
|
150
|
+
|
|
151
|
+
---
|
|
152
|
+
|
|
153
|
+
## V4 β Portable Intelligence
|
|
154
|
+
|
|
155
|
+
Turn Synapse into a transferable memory engine.
|
|
156
|
+
|
|
157
|
+
Planned capabilities:
|
|
158
|
+
|
|
159
|
+
- Import knowledge graphs
|
|
160
|
+
- Export knowledge graphs
|
|
161
|
+
- Transfer memory between LLMs
|
|
162
|
+
- Local execution
|
|
163
|
+
- Offline mode
|
|
164
|
+
- Personal AI memory system
|
|
165
|
+
|
|
166
|
+
Example:
|
|
167
|
+
|
|
168
|
+
```text
|
|
169
|
+
Claude
|
|
170
|
+
β
|
|
171
|
+
Knowledge Graph
|
|
172
|
+
β
|
|
173
|
+
Export
|
|
174
|
+
β
|
|
175
|
+
GPT
|
|
176
|
+
β
|
|
177
|
+
Continue with the same memory
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
---
|
|
181
|
+
|
|
182
|
+
## π Vision
|
|
183
|
+
|
|
184
|
+
Modern AI systems own the intelligence, but they also own the memory.
|
|
185
|
+
|
|
186
|
+
Synapse aims to separate the two.
|
|
187
|
+
|
|
188
|
+
The long-term goal is to make memory portable, persistent, and independent of any single model provider.
|
|
189
|
+
|
|
190
|
+
Memory should belong to the user, not the model.
|
|
191
|
+
|
|
192
|
+
---
|
|
193
|
+
|
|
194
|
+
## π§βπ» Author
|
|
195
|
+
|
|
196
|
+
### Piyush Garg
|
|
197
|
+
|
|
198
|
+
AI/ML enthusiast interested in:
|
|
199
|
+
|
|
200
|
+
- Knowledge Graphs
|
|
201
|
+
- LLM Systems
|
|
202
|
+
- Intelligent Agents
|
|
203
|
+
- Machine Learning Infrastructure
|
|
204
|
+
|
|
205
|
+
Building systems at the intersection of memory and intelligence.
|
|
206
|
+
|
|
207
|
+
---
|
|
208
|
+
|
|
209
|
+
## π License
|
|
210
|
+
|
|
211
|
+
MIT License
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61.0"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "synapse-pkg"
|
|
7
|
+
version = "0.1.1"
|
|
8
|
+
description = "Knowledge graph extraction library powered by LLMs"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.10"
|
|
11
|
+
|
|
12
|
+
authors = [
|
|
13
|
+
{ name = "Piyush Garg" }
|
|
14
|
+
]
|
|
15
|
+
|
|
16
|
+
dependencies = [
|
|
17
|
+
"google-generativeai",
|
|
18
|
+
]
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from .synapse import Synapse
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
from dotenv import load_dotenv
|
|
2
|
+
import os
|
|
3
|
+
from google import genai
|
|
4
|
+
|
|
5
|
+
load_dotenv()
|
|
6
|
+
client = genai.Client()
|
|
7
|
+
|
|
8
|
+
prompt_template = """you are an information extractor system whose work is to extract information in
|
|
9
|
+
the specified schema and return the output in the specified type only
|
|
10
|
+
RETURN ONLY THE VALID JSON
|
|
11
|
+
|
|
12
|
+
Extract:-
|
|
13
|
+
1. Concepts
|
|
14
|
+
2. Relationships
|
|
15
|
+
|
|
16
|
+
RETURN OUTPUT USING THIS SCHEMA ONLY
|
|
17
|
+
{{
|
|
18
|
+
"concepts": [],
|
|
19
|
+
"relationships": [
|
|
20
|
+
{{
|
|
21
|
+
"source": "",
|
|
22
|
+
"relation": "",
|
|
23
|
+
"target": ""
|
|
24
|
+
}}
|
|
25
|
+
]
|
|
26
|
+
}}
|
|
27
|
+
text: {input_text}
|
|
28
|
+
|
|
29
|
+
BELOW IS AN EXAMPLE ON HOW THE OUTPUT SHOULD LOOK LIKE
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
text: Python is used for AI and Tensorflow is used in AI
|
|
33
|
+
{{
|
|
34
|
+
"concepts": [
|
|
35
|
+
"Python",
|
|
36
|
+
"TensorFlow",
|
|
37
|
+
"AI"
|
|
38
|
+
],
|
|
39
|
+
"relationships": [
|
|
40
|
+
{{
|
|
41
|
+
"source": "Python",
|
|
42
|
+
"relation": "used_for",
|
|
43
|
+
"target": "AI"
|
|
44
|
+
}},
|
|
45
|
+
{{
|
|
46
|
+
"source": "TensorFlow",
|
|
47
|
+
"relation": "used_in",
|
|
48
|
+
"target": "AI"
|
|
49
|
+
}}
|
|
50
|
+
]
|
|
51
|
+
}}
|
|
52
|
+
"""
|
|
53
|
+
|
|
54
|
+
def generate_prompt(user_text):
|
|
55
|
+
prompt = prompt_template.format(input_text=user_text)
|
|
56
|
+
return prompt
|
|
57
|
+
|
|
58
|
+
def extractor(prompt):
|
|
59
|
+
response = client.models.generate_content(
|
|
60
|
+
model="gemini-2.5-flash",
|
|
61
|
+
contents=prompt,
|
|
62
|
+
)
|
|
63
|
+
return response
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
from .utils import normalize
|
|
2
|
+
def add_relationship(
|
|
3
|
+
graph:dict,
|
|
4
|
+
source:str,
|
|
5
|
+
relation:str,
|
|
6
|
+
target:str
|
|
7
|
+
):
|
|
8
|
+
source = normalize(source)
|
|
9
|
+
relation = normalize(relation)
|
|
10
|
+
target = normalize(target)
|
|
11
|
+
tup = (relation,target)
|
|
12
|
+
|
|
13
|
+
graph.setdefault(source,[])
|
|
14
|
+
if tup in graph[source]:
|
|
15
|
+
return graph
|
|
16
|
+
else:
|
|
17
|
+
graph[source].append(tup)
|
|
18
|
+
return graph
|
|
19
|
+
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import json
|
|
2
|
+
GRAPH_FILE = 'graph.json'
|
|
3
|
+
|
|
4
|
+
def save(graph:dict):
|
|
5
|
+
json_graph = {source: [list(item) for item in relation]
|
|
6
|
+
for source,relation in graph.items()
|
|
7
|
+
}
|
|
8
|
+
with open(GRAPH_FILE,'w') as f:
|
|
9
|
+
json.dump(json_graph,f)
|
|
10
|
+
|
|
11
|
+
def load_graph():
|
|
12
|
+
try:
|
|
13
|
+
with open(GRAPH_FILE,'r') as f:
|
|
14
|
+
json_graph = json.load(f)
|
|
15
|
+
except FileNotFoundError:
|
|
16
|
+
return {}
|
|
17
|
+
|
|
18
|
+
loaded_graph = {
|
|
19
|
+
source: [tuple(item) for item in relation]
|
|
20
|
+
for source,relation in json_graph.items()
|
|
21
|
+
}
|
|
22
|
+
return loaded_graph
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import json
|
|
2
|
+
from .extractor import generate_prompt,extractor
|
|
3
|
+
|
|
4
|
+
def process_text(user_text:str,extractor):
|
|
5
|
+
|
|
6
|
+
"""
|
|
7
|
+
Extract structured relationships from text using an LLM.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
prompt = generate_prompt(user_text)
|
|
11
|
+
raw_response = extractor(prompt)
|
|
12
|
+
cleaned_text = (
|
|
13
|
+
raw_response.text
|
|
14
|
+
.replace("```json", "")
|
|
15
|
+
.replace("```", "")
|
|
16
|
+
.strip()
|
|
17
|
+
)
|
|
18
|
+
try:
|
|
19
|
+
parsed_response = json.loads(cleaned_text)
|
|
20
|
+
return parsed_response
|
|
21
|
+
except json.JSONDecodeError:
|
|
22
|
+
return {
|
|
23
|
+
"concepts":[],
|
|
24
|
+
"relationships":[]
|
|
25
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
from .utils import normalize
|
|
2
|
+
def get_relations(
|
|
3
|
+
graph:dict,
|
|
4
|
+
concept:str,
|
|
5
|
+
relation:str | None=None
|
|
6
|
+
):
|
|
7
|
+
|
|
8
|
+
"""
|
|
9
|
+
Return all relations for a concept.
|
|
10
|
+
|
|
11
|
+
Optionally filter by relation type.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
if not concept.strip() or not concept:
|
|
15
|
+
return []
|
|
16
|
+
concept = normalize(concept)
|
|
17
|
+
relations = graph.get(concept, [])
|
|
18
|
+
if relation is None:
|
|
19
|
+
return relations
|
|
20
|
+
return [
|
|
21
|
+
(relation_name,target)
|
|
22
|
+
for relation_name,target in relations
|
|
23
|
+
if relation_name.lower() == relation.lower()
|
|
24
|
+
]
|
|
25
|
+
|
|
26
|
+
def get_indirect_relations(
|
|
27
|
+
graph:dict,
|
|
28
|
+
source:str
|
|
29
|
+
):
|
|
30
|
+
if not source.strip() or not source:
|
|
31
|
+
return []
|
|
32
|
+
source = normalize(source)
|
|
33
|
+
res=[]
|
|
34
|
+
relations = get_relations(graph, source)
|
|
35
|
+
for relation, target in relations:
|
|
36
|
+
if target in graph:
|
|
37
|
+
secondary_relations = get_relations(graph, target)
|
|
38
|
+
for secondary_relation, secondary_target in secondary_relations:
|
|
39
|
+
res.append((relation, target, secondary_relation, secondary_target))
|
|
40
|
+
return res
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
from .persistence import save,load_graph
|
|
2
|
+
from .query import get_relations, get_indirect_relations
|
|
3
|
+
from .processor import process_text
|
|
4
|
+
from .graph import add_relationship
|
|
5
|
+
from .extractor import extractor
|
|
6
|
+
class Synapse:
|
|
7
|
+
def __init__(self):
|
|
8
|
+
self.graph={}
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def save(self):
|
|
12
|
+
save(self.graph)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def load_graph(self):
|
|
16
|
+
self.graph = load_graph()
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def get_relations(
|
|
20
|
+
self,
|
|
21
|
+
concept:str,
|
|
22
|
+
relation:str | None=None
|
|
23
|
+
):
|
|
24
|
+
return get_relations(self.graph,concept)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def get_indirect_relations(self,source:str):
|
|
28
|
+
return get_indirect_relations(self.graph,source)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def add(self,text:str):
|
|
32
|
+
if not text.strip():
|
|
33
|
+
return
|
|
34
|
+
parsed_response=process_text(text)
|
|
35
|
+
relationships = parsed_response['relationships']
|
|
36
|
+
for rel in relationships:
|
|
37
|
+
add_relationship(self.graph, rel['source'], rel['relation'], rel['target'])
|
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: synapse-pkg
|
|
3
|
+
Version: 0.1.1
|
|
4
|
+
Summary: Knowledge graph extraction library powered by LLMs
|
|
5
|
+
Author: Piyush Garg
|
|
6
|
+
Requires-Python: >=3.10
|
|
7
|
+
Description-Content-Type: text/markdown
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Requires-Dist: google-generativeai
|
|
10
|
+
Dynamic: license-file
|
|
11
|
+
|
|
12
|
+
# π§ Synapse
|
|
13
|
+
|
|
14
|
+
> Transform natural language into a persistent knowledge graph.
|
|
15
|
+
|
|
16
|
+
Synapse is a Python library that converts unstructured text into a structured knowledge graph by extracting concepts and relationships from natural language.
|
|
17
|
+
|
|
18
|
+
Instead of storing information as raw text, Synapse organizes information into a graph structure that can be queried, traversed, and extended for building intelligent systems and memory systems.
|
|
19
|
+
|
|
20
|
+
---
|
|
21
|
+
|
|
22
|
+
## β¨ Features
|
|
23
|
+
|
|
24
|
+
- Natural language β structured relationships
|
|
25
|
+
- Persistent graph storage
|
|
26
|
+
- Duplicate prevention
|
|
27
|
+
- Case-insensitive querying
|
|
28
|
+
- Multi-hop relationship traversal
|
|
29
|
+
- Local-first architecture
|
|
30
|
+
- Provider-agnostic design
|
|
31
|
+
- Extensible memory engine
|
|
32
|
+
|
|
33
|
+
---
|
|
34
|
+
|
|
35
|
+
## π¦ Installation
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
pip install synapse-ai
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
---
|
|
42
|
+
|
|
43
|
+
## β‘ Quick Start
|
|
44
|
+
|
|
45
|
+
```python
|
|
46
|
+
from synapse import Synapse
|
|
47
|
+
|
|
48
|
+
brain = Synapse()
|
|
49
|
+
|
|
50
|
+
brain.add(
|
|
51
|
+
"CEO of Apple met Microsoft executives in Seattle."
|
|
52
|
+
)
|
|
53
|
+
|
|
54
|
+
brain.save()
|
|
55
|
+
|
|
56
|
+
print(
|
|
57
|
+
brain.get_relations("Apple")
|
|
58
|
+
)
|
|
59
|
+
|
|
60
|
+
print(
|
|
61
|
+
brain.get_indirect_relations("CEO of Apple")
|
|
62
|
+
)
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
---
|
|
66
|
+
|
|
67
|
+
## Example
|
|
68
|
+
|
|
69
|
+
### Input
|
|
70
|
+
|
|
71
|
+
```text
|
|
72
|
+
Python is used for AI and TensorFlow is used in AI.
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
### Extracted Graph
|
|
76
|
+
|
|
77
|
+
```python
|
|
78
|
+
{
|
|
79
|
+
"python": [
|
|
80
|
+
("used_for", "ai")
|
|
81
|
+
],
|
|
82
|
+
"tensorflow": [
|
|
83
|
+
("used_in", "ai")
|
|
84
|
+
]
|
|
85
|
+
}
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
---
|
|
89
|
+
|
|
90
|
+
## Project Structure
|
|
91
|
+
|
|
92
|
+
```text
|
|
93
|
+
synapse/
|
|
94
|
+
βββ __init__.py
|
|
95
|
+
βββ synapse.py
|
|
96
|
+
βββ graph.py
|
|
97
|
+
βββ query.py
|
|
98
|
+
βββ processor.py
|
|
99
|
+
βββ extractor.py
|
|
100
|
+
βββ persistence.py
|
|
101
|
+
βββ utils.py
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
---
|
|
105
|
+
|
|
106
|
+
## βοΈ Design Principles
|
|
107
|
+
|
|
108
|
+
- Simplicity over complexity
|
|
109
|
+
- Local-first architecture
|
|
110
|
+
- Incremental development
|
|
111
|
+
- Extensibility
|
|
112
|
+
- No overengineering
|
|
113
|
+
|
|
114
|
+
---
|
|
115
|
+
|
|
116
|
+
## π Current Status
|
|
117
|
+
|
|
118
|
+
### β
V1 Complete
|
|
119
|
+
|
|
120
|
+
Implemented features:
|
|
121
|
+
|
|
122
|
+
- Text ingestion
|
|
123
|
+
- Relationship extraction
|
|
124
|
+
- Graph construction
|
|
125
|
+
- Graph persistence
|
|
126
|
+
- Query system
|
|
127
|
+
- Multi-hop traversal
|
|
128
|
+
- Python package support
|
|
129
|
+
- Provider-agnostic extractor architecture
|
|
130
|
+
|
|
131
|
+
---
|
|
132
|
+
|
|
133
|
+
# πΊοΈ Roadmap
|
|
134
|
+
|
|
135
|
+
## V2 β Conversational Memory Layer
|
|
136
|
+
|
|
137
|
+
Build a conversational interface on top of the graph.
|
|
138
|
+
|
|
139
|
+
Planned capabilities:
|
|
140
|
+
|
|
141
|
+
- Memory-backed conversations
|
|
142
|
+
- Graph-aware retrieval
|
|
143
|
+
- Contextual question answering
|
|
144
|
+
- Better reasoning over stored knowledge
|
|
145
|
+
- Persistent long-term memory
|
|
146
|
+
|
|
147
|
+
---
|
|
148
|
+
|
|
149
|
+
## V3 β Agentic Knowledge Graph
|
|
150
|
+
|
|
151
|
+
Integrate coding agents and sandboxed environments.
|
|
152
|
+
|
|
153
|
+
Planned capabilities:
|
|
154
|
+
|
|
155
|
+
- Claude Code integration
|
|
156
|
+
- Codex integration
|
|
157
|
+
- Live graph construction during conversations
|
|
158
|
+
- Automatic graph updates
|
|
159
|
+
- Multi-session memory
|
|
160
|
+
- Knowledge graph visualization
|
|
161
|
+
|
|
162
|
+
---
|
|
163
|
+
|
|
164
|
+
## V4 β Portable Intelligence
|
|
165
|
+
|
|
166
|
+
Turn Synapse into a transferable memory engine.
|
|
167
|
+
|
|
168
|
+
Planned capabilities:
|
|
169
|
+
|
|
170
|
+
- Import knowledge graphs
|
|
171
|
+
- Export knowledge graphs
|
|
172
|
+
- Transfer memory between LLMs
|
|
173
|
+
- Local execution
|
|
174
|
+
- Offline mode
|
|
175
|
+
- Personal AI memory system
|
|
176
|
+
|
|
177
|
+
Example:
|
|
178
|
+
|
|
179
|
+
```text
|
|
180
|
+
Claude
|
|
181
|
+
β
|
|
182
|
+
Knowledge Graph
|
|
183
|
+
β
|
|
184
|
+
Export
|
|
185
|
+
β
|
|
186
|
+
GPT
|
|
187
|
+
β
|
|
188
|
+
Continue with the same memory
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
---
|
|
192
|
+
|
|
193
|
+
## π Vision
|
|
194
|
+
|
|
195
|
+
Modern AI systems own the intelligence, but they also own the memory.
|
|
196
|
+
|
|
197
|
+
Synapse aims to separate the two.
|
|
198
|
+
|
|
199
|
+
The long-term goal is to make memory portable, persistent, and independent of any single model provider.
|
|
200
|
+
|
|
201
|
+
Memory should belong to the user, not the model.
|
|
202
|
+
|
|
203
|
+
---
|
|
204
|
+
|
|
205
|
+
## π§βπ» Author
|
|
206
|
+
|
|
207
|
+
### Piyush Garg
|
|
208
|
+
|
|
209
|
+
AI/ML enthusiast interested in:
|
|
210
|
+
|
|
211
|
+
- Knowledge Graphs
|
|
212
|
+
- LLM Systems
|
|
213
|
+
- Intelligent Agents
|
|
214
|
+
- Machine Learning Infrastructure
|
|
215
|
+
|
|
216
|
+
Building systems at the intersection of memory and intelligence.
|
|
217
|
+
|
|
218
|
+
---
|
|
219
|
+
|
|
220
|
+
## π License
|
|
221
|
+
|
|
222
|
+
MIT License
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
synapse/__init__.py
|
|
5
|
+
synapse/extractor.py
|
|
6
|
+
synapse/graph.py
|
|
7
|
+
synapse/persistence.py
|
|
8
|
+
synapse/processor.py
|
|
9
|
+
synapse/query.py
|
|
10
|
+
synapse/synapse.py
|
|
11
|
+
synapse/utils.py
|
|
12
|
+
synapse_pkg.egg-info/PKG-INFO
|
|
13
|
+
synapse_pkg.egg-info/SOURCES.txt
|
|
14
|
+
synapse_pkg.egg-info/dependency_links.txt
|
|
15
|
+
synapse_pkg.egg-info/requires.txt
|
|
16
|
+
synapse_pkg.egg-info/top_level.txt
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
google-generativeai
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
synapse
|