Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Big refactor to make PG modular #25

Merged
merged 169 commits into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from 148 commits
Commits
Show all changes
169 commits
Select commit Hold shift + click to select a range
93cd967
initial commit
anubhuti24 Apr 26, 2024
15335db
refactor: embeddings to use liteLLM
anubhuti24 Apr 26, 2024
514b6cc
refactor: instructor through liteLLM
anubhuti24 Apr 26, 2024
1fa22bf
refactor: set default api_key
anubhuti24 Apr 26, 2024
b044782
updated natural.py
anubhuti24 Apr 26, 2024
364b1bc
feature: added groq dependency
anubhuti24 Apr 26, 2024
7819790
bugfix: resolved dependencies issue
anubhuti24 Apr 26, 2024
c1a4295
refactor: move to groq instead of openai
anubhuti24 Apr 26, 2024
c2c0b96
refactor: examples
anubhuti24 Apr 27, 2024
0e4bca9
refactor: low level apis to use arbitrary LM and embeddings
anubhuti24 Apr 27, 2024
93cdf4c
refactor: low level apis unit tests
anubhuti24 Apr 27, 2024
e88e7a0
refactor: Graph constructor
anubhuti24 Apr 27, 2024
933ff3d
refactor: graph.py
anubhuti24 Apr 27, 2024
34614e9
refactor: natural lang apis to use arbitrary LM and embeddings
anubhuti24 Apr 27, 2024
5002183
refactor: examples/nl_interface.py
anubhuti24 Apr 27, 2024
b11fbb9
refactor: updated unit tests
anubhuti24 Apr 27, 2024
72d7e56
refactor: high level apis
anubhuti24 Apr 27, 2024
b35659b
refactor: removed code duplication
anubhuti24 Apr 27, 2024
0c0eecd
refactor: pass embedding object
anubhuti24 Apr 27, 2024
191d8b4
refactor: nl apis to pass embed object
anubhuti24 Apr 27, 2024
74c0c54
refactor: ruff formatting and checking
anubhuti24 Apr 27, 2024
e84fc40
refactor: unit tests
anubhuti24 Apr 27, 2024
fa576c4
bugfix: test_graph.py
anubhuti24 Apr 29, 2024
511267d
refactor: db_url and token as kwargs
anubhuti24 Apr 29, 2024
8e91db6
refactor: types
anubhuti24 Apr 29, 2024
b697b80
refactor: embedding dimension as argument
anubhuti24 Apr 29, 2024
d21e216
refactor: examples, conftest.py
anubhuti24 Apr 29, 2024
8d66917
refactor: ruff format
anubhuti24 Apr 29, 2024
74540e4
refactor: added fallbacks
anubhuti24 Apr 29, 2024
a90231b
refactor: removed relative imports
anubhuti24 Apr 29, 2024
1234f47
refactor: renamed to db_auth_token
anubhuti24 Apr 29, 2024
28fa987
refactor: use high level apis
anubhuti24 Apr 29, 2024
961a327
refactor: updated examples scripts
anubhuti24 Apr 29, 2024
55c1f60
bugfix: versioning resolved
anubhuti24 Apr 30, 2024
ecb2eb0
refactor: kgchat service
anubhuti24 Apr 30, 2024
2311875
refactor: graph.py
anubhuti24 Apr 30, 2024
0439482
refactor: examples
anubhuti24 Apr 30, 2024
ef05e85
refactor: unit tests
anubhuti24 Apr 30, 2024
7ee1c8c
refactor: moved low level apis to graph.py
anubhuti24 May 2, 2024
d0d3666
bugfix: unit tests
anubhuti24 May 2, 2024
d9910cf
refactor : retriever example
anubhuti24 May 2, 2024
72bca0a
refactor: updated methods
anubhuti24 May 3, 2024
f0bc38b
feature: added a method to insert nl node
anubhuti24 May 3, 2024
a1ae872
refactor: updated examples
anubhuti24 May 3, 2024
37b4cb3
bugfix: unit tests and vector methods
anubhuti24 May 3, 2024
327dfd2
refactor: updated insert method
anubhuti24 May 3, 2024
eabfe56
refactor: graph.py
anubhuti24 May 3, 2024
a6faf18
refactor: search method to use descending and limit params
anubhuti24 May 3, 2024
694b648
feature: keyword arg sort_by support
anubhuti24 May 4, 2024
c44364e
refactor: optimized SQL query and search() method
anubhuti24 May 4, 2024
bf66431
refactor: updated examples
anubhuti24 May 4, 2024
109f283
refactor: updated README
anubhuti24 May 4, 2024
99895e0
refactor: unit tests
anubhuti24 May 4, 2024
1ba1956
refactor: kgchat.py
anubhuti24 May 4, 2024
7162701
refactor: updated dspy example in README.md
anubhuti24 May 4, 2024
bd16821
refactor: examples
anubhuti24 May 6, 2024
0d574e5
refactor: updated README and examples
anubhuti24 May 6, 2024
fb5f020
refactor: updated examples
anubhuti24 May 6, 2024
d24e23a
bugfix: same connection shared
anubhuti24 May 6, 2024
c4ad060
refactor: updated README.md
anubhuti24 May 7, 2024
08591cd
refactor: import statements
anubhuti24 May 7, 2024
7a021f0
refactor: dbclient dataclass
anubhuti24 May 7, 2024
c566998
refactor: updated unit tests
anubhuti24 May 7, 2024
be49dd2
refactor: file type as Path
anubhuti24 May 7, 2024
f418326
refactor: updated README.md
anubhuti24 May 7, 2024
436945d
refactor: sqlite3 for in-memory db
anubhuti24 May 8, 2024
216a908
refactor: updated graph.py
anubhuti24 May 8, 2024
43092f8
refactor: removed dataclasses
anubhuti24 May 8, 2024
d492c96
refactor: updated client.py
anubhuti24 May 8, 2024
0b54e15
refactor: added dbclient class
anubhuti24 May 9, 2024
0064038
refactor: graph
anubhuti24 May 9, 2024
0d20444
feature: added VectorDatabase abstract class
anubhuti24 May 10, 2024
de75ff8
feature: sqlitevss concrete class
anubhuti24 May 10, 2024
29d4da4
refactor: graph.py
anubhuti24 May 10, 2024
15210d4
refactor: updated examples
anubhuti24 May 10, 2024
dc9016a
refactor: formatted examples
anubhuti24 May 10, 2024
c791014
bugfix: graph.py and mypy errors
anubhuti24 May 11, 2024
c4f402b
refactor: added vlite
anubhuti24 May 11, 2024
4ce1d84
feature: implemented VLite concrete class
anubhuti24 May 11, 2024
1b59d9a
refacor: updated graph.py
anubhuti24 May 11, 2024
18985cd
refactor: updated examples
anubhuti24 May 11, 2024
2cd8fce
feature: added db init.py
anubhuti24 May 13, 2024
074d20a
refactor: updated dataclass, renamed basevectorstore.py
anubhuti24 May 13, 2024
8a44c3d
bugfix: updated vlite search_node and label
anubhuti24 May 13, 2024
5429d94
refactor: removed DBClient class
anubhuti24 May 13, 2024
5e440dd
bugfix: graph.py
anubhuti24 May 13, 2024
5780762
refactor: added missing method to vectorstore
anubhuti24 May 13, 2024
84ac853
refactor: added remaining methods to concrete classes
anubhuti24 May 13, 2024
0f9381f
feature: moved networkx methods to ml.py
anubhuti24 May 13, 2024
544e963
bugfix: updated sqlitevss.py
anubhuti24 May 14, 2024
88b7c16
feature: added find_nodes_like method to vlite
anubhuti24 May 14, 2024
6a43bc3
feature: added pruning method to vlite
anubhuti24 May 14, 2024
df486ec
refactor: added logic for sort_by and desc
anubhuti24 May 14, 2024
d4573a5
refactor: formatted examples
anubhuti24 May 14, 2024
4bbdefb
feature: added traverse method to vlite
anubhuti24 May 14, 2024
935b087
refactor: unit tests
anubhuti24 May 15, 2024
dd542a8
feature: added support for multiple db
anubhuti24 May 15, 2024
ff813b9
refactor: updated examples
anubhuti24 May 15, 2024
b4ac417
refactor: updated unit tests
anubhuti24 May 15, 2024
63ae887
bugfix: fixed eq method
anubhuti24 May 15, 2024
4e156ca
refactor: updated type check
anubhuti24 May 15, 2024
f2db73b
feature: added graph generator
anubhuti24 May 15, 2024
4d61a5d
refactor: updated examples
anubhuti24 May 15, 2024
7fbdaa0
refactor: updated import statements
anubhuti24 May 15, 2024
b1b6cf2
refactor: updated unit tests
anubhuti24 May 15, 2024
b060ef7
refactor: updated unit tests
anubhuti24 May 15, 2024
2ec9632
refactor: formatted
anubhuti24 May 15, 2024
fad870b
bugfix: fixed is_unique_prompt
anubhuti24 May 16, 2024
bcd5a3e
refactor: updated kgchat.py
anubhuti24 May 16, 2024
7056f8b
refactor: updated Readme.md
anubhuti24 May 16, 2024
2f97b3c
refactor: added embedding model to vss concrete class
anubhuti24 May 16, 2024
65bf9e4
refactor: removed unnecessary usage of isinstance
anubhuti24 May 16, 2024
1abe54b
refctor: updated import statements
anubhuti24 May 16, 2024
a4a5b66
refactor: removed DBClient
anubhuti24 May 16, 2024
cd37b13
refactor: removed limit parameter
anubhuti24 May 16, 2024
f7f7d33
refactor: updated graph object
anubhuti24 May 16, 2024
8392449
refactor: formattef files
anubhuti24 May 16, 2024
0d17222
feature: added database store
anubhuti24 May 20, 2024
4d85c8c
feature: added vector store
anubhuti24 May 20, 2024
d32de60
refactor: updated high level apis
anubhuti24 May 20, 2024
ef5da9d
feature: added new methods to db store
anubhuti24 May 21, 2024
ffaaac1
refactor: updated tursodb class
anubhuti24 May 21, 2024
878f11a
feature: added new vector search method to vector store
anubhuti24 May 21, 2024
c809ce0
refactor: updated vector search logic
anubhuti24 May 21, 2024
0e251f1
refactor: updated pythonic examples
anubhuti24 May 21, 2024
808e140
bugfix: fixed in-memory logic
anubhuti24 May 21, 2024
7e84c5f
refactor: sqlitevss concrete class
anubhuti24 May 21, 2024
81ea402
refactor: updated directories and file names
anubhuti24 May 21, 2024
bec54e6
refactor: moved raw queries
anubhuti24 May 21, 2024
6075f5c
refactor: updated import statements
anubhuti24 May 21, 2024
144f64a
refactor: moved visualize_graph to visualizers.py
anubhuti24 May 21, 2024
0af861b
refactor: updated path type
anubhuti24 May 21, 2024
ee3a201
chore: [WIP] many demo changes, breaks
sutyum May 21, 2024
8caec49
refactor: fixed default values
anubhuti24 May 22, 2024
147c18f
refactor: updated graph generator
anubhuti24 May 22, 2024
2bde3de
refactor: updated llm and embedding clients
anubhuti24 May 22, 2024
fd0713e
feature: added text_to_graph method
anubhuti24 May 22, 2024
7633a43
refactor: updated similarity search methods
anubhuti24 May 22, 2024
ce9a960
feature: added __repr__ method
anubhuti24 May 22, 2024
b3a425e
refactor: pythonic examples
anubhuti24 May 22, 2024
dedc527
refactor: added graphdb default value
anubhuti24 May 22, 2024
f047105
refactor: removed vlite database class
anubhuti24 May 23, 2024
e448a08
refactor: updated database layer
anubhuti24 May 23, 2024
c3f4739
refactor: updated vector store
anubhuti24 May 23, 2024
7fe6a6d
feature: added vlitevss class
anubhuti24 May 23, 2024
f628fcb
refactor: updated high level apis
anubhuti24 May 23, 2024
ab08c7b
refactor: updated examples
anubhuti24 May 23, 2024
d83b81b
refactor: updated vss
anubhuti24 May 23, 2024
9ab6539
refactor: updated embedding model client name
anubhuti24 May 23, 2024
9d1aad7
refactor: renamed DatabaseStore to DB
anubhuti24 May 23, 2024
ba6b281
refactor: added docstrings
anubhuti24 May 23, 2024
1931d72
refactor: updated types
anubhuti24 May 23, 2024
40e46f5
refactor: updated script and examples
anubhuti24 May 23, 2024
4abf995
refactor: updated dspy_program examples
anubhuti24 May 23, 2024
75da5bc
refactor: updated unit tests
anubhuti24 May 25, 2024
4329bbc
refactor: updated scripts and examples
anubhuti24 May 25, 2024
81583f1
refactor: updated SELECT queries
anubhuti24 May 25, 2024
a828aaa
refactor: renamed Graph to GraphDB
anubhuti24 May 25, 2024
26b3216
refactor: updated README.md
anubhuti24 May 25, 2024
77daaec
bugfix: updated graph.py
anubhuti24 May 25, 2024
ae49985
refactor: unit tests
anubhuti24 May 25, 2024
944a722
refactor: resolved mypy errors
anubhuti24 May 25, 2024
99e9df0
feature: added setup.py
anubhuti24 May 25, 2024
95c8538
refactor: setup.py
anubhuti24 May 25, 2024
d230501
feature: added py.typed
anubhuti24 May 25, 2024
8ab9669
refactor: updated README.md
anubhuti24 May 27, 2024
6301054
feature: added stub files
anubhuti24 May 27, 2024
e6feea7
refactor: library versioning
anubhuti24 May 27, 2024
7438b78
refactor: updated setup.py
anubhuti24 May 27, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 98 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ Personal-Graph is a Python library for creating, managing, and querying knowledg
- 💬 **Natural Language Interfaces**: Natural language queries powered by Sqlite-vss and Instructor
- 🤖 **ML-Ready**: Export data for machine learning libraries like Networkx and PyG

- ✅ Supports local execution with Ollama LLMs and Embedding Models
- ✅ Supports local db for both storing graph and embeddings

## Installation

Install Personal-Graph using pip:
Expand All @@ -20,26 +23,45 @@ pip install personal-graph

## Usage

### Usage of Vector store classes
There are two vector store classes SQLiteVSS and VLiteDatabase. Let's dive down into the implementation of both the vector store classes.


```
from personal_graph.database import SQLiteVSS, DBClient, TursoDB
from personal_graph import EmbeddingClient

vector_store = VliteVSS()
vector_store = VLiteDatabase(collection="memories", model_name="mxbai")
```

### Building a Working Memory for an AI

```python
from personal_graph import Graph
from personal_graph import Graph, LLMClient
from personal_graph.database import DBClient, TursoDB
from personal_graph.vector_store import SQLiteVSS
from personal_graph.graph_generator import InstructorGraphGenerator
from personal_graph.text import text_to_graph

graph = Graph("your_db_url", "your_auth_token")
graph = Graph(vector_store=vector_store)

# Insert information into the graph
graph.insert(text="Alice is Bob's sister. Bob works at Google.")
g = text_to_graph("Alice is Bob's sister. Bob works at Google.")
graph.insert_graph(g)

# Retrieve relevant information from the graph
query = "Who is Alice?"
results = graph.search(query)
print(results["body"])
sutyum marked this conversation as resolved.
Show resolved Hide resolved

# Use the retrieved information to answer questions
print(f"Question: {query}")
print(f"Answer: Alice is Bob's sister.")

query = "Where does Bob work?"
results = graph.search(query)
print(results["body"])
print(f"Question: {query}")
print(f"Answer: Bob works at Google.")
```
Expand All @@ -48,9 +70,9 @@ In this example, we insert information about Alice and Bob into the knowledge gr

### Building Long-Term Memory
```python
from personal_graph import Graph
from personal_graph import Graph, LLMClient

graph = Graph("your_db_url", "your_auth_token")
graph = Graph(vector_store=vector_store)

# Insert information about conversations with the user over time
graph.insert(
Expand Down Expand Up @@ -88,10 +110,9 @@ graph.insert(
# User queries about the deepest conversation
query = "What was the deepest conversation we've ever had?"

results = graph.search(query, sort_by="depth_score", descending=True, limit=1)
deepest_conversation = graph.search(query, sort_by="depth_score", descending=True, limit=1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should return a list by default, imagine for instance if i had chosen a limit of 10


if results:
deepest_conversation = results[0]
if deepest_conversation:
print(f"Question: {query}")
print(f"Answer: Our deepest conversation was on {deepest_conversation['date']} when we discussed {deepest_conversation['topic']}.")
else:
Expand All @@ -108,27 +129,31 @@ This example demonstrates how Personal-Graph can be used to build long-term memo

### Creating and Querying a Knowledge Graph
```py
from personal_graph import Graph, natural
from personal_graph import GraphDB, LLMClient
from personal_graph.graph_generator import InstructorGraphGenerator
from personal_graph.text import text_to_graph

graph = Graph("your_db_url", "your_auth_token")
graphdb = GraphDB(vector_store=vector_store)

nl_query = "Increased thirst, weight loss, increased hunger, and frequent urination are all symptoms of diabetes."
graph = natural.insert(text=nl_query)
graphdb = graph.insert_into_graph(text=nl_query)

search_query = "I am losing weight too frequently."
knowledge_graph = natural.search_from_graph(search_query)
#knowledge_graph = graph.search_from_graph(search_query)
g = text_to_graph(search_query)
print(g)

print(knowledge_graph)
graphdb.insert_graph(g)
```

### Retrieval and Question-Answering
```py
import dspy
from personal_graph.graph import Graph
from personal_graph.retriever import PersonalRM
from personal_graph.graph_generator import InstructorGraphGenerator
from personal_graph import Graph, LLMClient, PersonalRM

graph = Graph("your_db_url", "your_auth_token")
retriever = PersonalRM(graph=graph, k=2)
db = GraphDB() # storage_db is in-memory sqlite, vector_db is in vlite
retriever = PersonalRM(db=db, k=2)

class RAG(dspy.Module):
def __init__(self, depth=3):
Expand All @@ -142,8 +167,62 @@ class RAG(dspy.Module):
return dspy.Prediction(context=context, answer=prediction.answer)

rag = RAG(depth=2)
answer = rag("How is Jack related to James?")
print(answer)
response = rag("How is Jack related to James?")
print(response.answer)
```

### Local Usage

```py
from personal_graph.graph import GraphDB, OllamaClient, OllamaEmbeddingClient
from personal_graph.graph_generator import InstructorGraphGenerator
from personal_graph.database import DBClient, Sqlite
from personal_graph.vector_store import VliteVectorDB

phi3 = OllamaClient(model_name="phi3")
nomic_embed = OllamaEmbeddingClient(model_name="nomic-embed-text")

storage_db = Sqlite(path="./local.db")
vector_store = VliteVectorDB(path="./vectors")

graph_generator=InstructorGraphGenerator(llm_client=phi3)
print(graph_generator) # Should print the InstructorGraphGenerator

with GraphDB(
db=db,
vector_store=vector_store,
graph_generator=graph_generator
) as db:
print(db)

# Graph(
# db = Sqlite()
# vector_store = SQLiteVSS(
# persistence_layer=SQLite(
# db_client=DBClient(
# use_in_memory=True,
# vector0_so_path="path/to/vector0.so",
# vss0_so_path="path/to/vss0.so"
# ),
# embedding_model_client=EmbeddingClient(),
# )
# )
# )
```

### PersonalGraph to PyG, then back to PersonalGraph
The following is just a sketch of the planned flow. WIP.

```py
graphdb = GraphDB(storage=db, vector_store=vector_store, graph_generator=graph_generator)

graphdb.load_dataset("KarateClub")

pyg_graph = graphdb.to_pyg()

updated_graph = model(pyg_graph) # Run Neural Network algorithms here using PyG

graphdb.from_pyg(updated_graph)
```

For more details and API documentation, see the Personal-Graph Documentation.
Expand Down
22 changes: 17 additions & 5 deletions examples/dspy_program.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
import os
import dspy # type: ignore
from personal_graph.graph import Graph
from personal_graph.retriever import PersonalRM

graph = Graph(os.getenv("LIBSQL_URL"), os.getenv("LIBSQL_AUTH_TOKEN"))
from personal_graph import Graph, LLMClient, PersonalRM, EmbeddingClient
from personal_graph.database import TursoDB, SQLiteVSS
from personal_graph.graph_generator import InstructorGraphGenerator

vector_store = SQLiteVSS(
persistence_layer=TursoDB(
url=os.getenv("LIBSQL_URL"), auth_token=os.getenv("LIBSQL_AUTH_TOKEN")
),
embedding_model_client=EmbeddingClient(),
)

graph = Graph(
vector_store=vector_store,
graph_generator=InstructorGraphGenerator(llm_client=LLMClient()),
)
turbo = dspy.OpenAI(model="gpt-3.5-turbo", api_key=os.getenv("OPEN_API_KEY"))
retriever = PersonalRM(graph=graph, k=2)
dspy.settings.configure(lm=turbo, rm=retriever)
Expand Down Expand Up @@ -33,4 +44,5 @@ def forward(self, question):

rag = RAG(depth=2)

rag("How is Jack related to James?")
response = rag("How is Jack related to James?")
print(response.answer)
174 changes: 0 additions & 174 deletions examples/low_level_api.py

This file was deleted.

Loading
Loading