memory node, neo4j grapph db

This commit is contained in:
Dobromir Popov
2024-03-20 08:54:14 +00:00
parent 97b0f9b64f
commit c9f77a6001
18 changed files with 625 additions and 0 deletions

3
memory1/.env Normal file
View File

@ -0,0 +1,3 @@
NEO4J_URI="bolt://192.168.0.10:7687"
NEO4J_USER="neo4j"
NEO4J_PASSWORD="lucas-bicycle-powder-stretch-ford-9492"

50
memory1/intint.neo.py Normal file
View File

@ -0,0 +1,50 @@
from decouple import config
from neo4j import GraphDatabase
class Neo4jConnection:
def __init__(self):
self.uri = config("NEO4J_URI")
self.user = config("NEO4J_USER")
self.password = config("NEO4J_PASSWORD")
self.driver = GraphDatabase.driver(self.uri, auth=(self.user, self.password))
# Create the schema
self.create_schema()
# Close the connection
self.close()
def close(self):
self.driver.close()
def create_schema(self):
with self.driver.session() as session:
session.write_transaction(self._create_constraints_and_indexes)
@staticmethod
def _create_constraints_and_indexes(tx):
# Constraints and indexes for Person
tx.run("CREATE CONSTRAINT ON (p:Person) ASSERT p.person_id IS UNIQUE;")
# Constraints and indexes for Memory
tx.run("CREATE CONSTRAINT ON (m:Memory) ASSERT m.memory_id IS UNIQUE;")
tx.run("CREATE INDEX ON :Memory(content);")
tx.run("CREATE INDEX ON :Memory(timestamp);")
# Constraints and indexes for Tag
tx.run("CREATE CONSTRAINT ON (t:Tag) ASSERT t.tag_id IS UNIQUE;")
tx.run("CREATE INDEX ON :Tag(tag_name);")
# Constraints and indexes for Category
tx.run("CREATE CONSTRAINT ON (c:Category) ASSERT c.category_id IS UNIQUE;")
# Constraints and indexes for Skill
tx.run("CREATE CONSTRAINT ON (s:Skill) ASSERT s.name IS UNIQUE;")
# Constraints and indexes for Fact
tx.run("CREATE CONSTRAINT ON (f:Fact) ASSERT f.certainty IS UNIQUE;")
# Additional schema definitions can be added here, such as relationship constraints or more complex indexing strategies.
if __name__ == "__main__":
conn = Neo4jConnection()

88
memory1/models.py Normal file
View File

@ -0,0 +1,88 @@
class Person:
def __init__(self, person_id, name, age):
self.person_id = person_id
self.name = name
self.age = age
class Memory:
def __init__(self, memory_id, content, timestamp, importance, relevance, associated_tags):
self.memory_id = memory_id
self.content = content
self.timestamp = timestamp
self.importance = importance
self.relevance = relevance
self.associated_tags = associated_tags
class Tag:
def __init__(self, tag_id, tag_name):
self.tag_id = tag_id
self.tag_name = tag_name
# // #
class Category:
def __init__(self, category_id, name, details):
self.category_id = category_id
self.name = name
self.details = details
self.embedding_ollama = embedding_ollama
class Skill: # or "hat". used to help with the context. more specific than category
def __init__(self, category_id, name):
self.category_id = category_id
self.name = name
class Fact:
def __init__(self, category_id, certainty, embedding_ollama):
self.category_id = category_id
self.certainty = certainty
self.embedding_ollama = embedding_ollama
class Context:
def __init__(self):
self.memories = []
self.facts = []
self.skills = []
self.tags = []
self.categories = []
def add_memory(self, memory):
self.memories.append(memory)
def add_fact(self, fact):
self.facts.append(fact)
def add_skill(self, skill):
self.skills.append(skill)
def add_tag(self, tag):
self.tags.append(tag)
def add_category(self, category):
self.categories.append(category)
def retrieve_memories(self, filter_tags=None):
if filter_tags:
return [memory for memory in self.memories if any(tag in memory.associated_tags for tag in filter_tags)]
return self.memories
def retrieve_facts(self, certainty_threshold):
return [fact for fact in self.facts if fact.certainty >= certainty_threshold]
def update_memory_relevance(self, memory_id, new_relevance):
for memory in self.memories:
if memory.memory_id == memory_id:
memory.relevance = new_relevance
break
def summarize_context(self):
# This method could be designed to summarize the current state of the context
# For simplicity, it just prints out the counts of different entities
print(f"Memories: {len(self.memories)}")
print(f"Facts: {len(self.facts)}")
print(f"Skills: {len(self.skills)}")
print(f"Tags: {len(self.tags)}")
print(f"Categories: {len(self.categories)}")