# Insert New Knowledge to Main Knowledge Graph

- Worked in Neo4j Aura DB
- Initiated a graph with chapter 1 and 2 of Harry Potter.
- Update the initiated graph with chapter 3.

```
documents = SimpleDirectoryReader(
    input_files=["./data/chapter/1.txt", "./data/chapter/2.txt", "./data/chapter/3.txt"]
).load_data()

index = KnowledgeGraphIndex.from_documents(
    documents,
    storage_context=storage_context,
    max_triplets_per_chunk=2,
)
```
I found there is a `insert_nodes` method to inject the processed graph to pre-existed one.  
We can create our own Knowledge managing class to inject knowledge graph or remove or update it.

<img src="src/0225_chapter1and2.png" width=40%>
<img src="src/0225_update_with_chapter3.png" width=40%>

# Memory Stream

## Memory Stream Class

[memory_stream.py](../src/memory/memory_stream.py)
```
# Example usage
# Create a MemoryStream object
memory_stream = MemoryStream(file_name="memory.json")

# Add memory
memory_stream.add_memory([MemoryItem("example_entity1", datetime.now()), MemoryItem("example_entity2", datetime.now())])

# Get all memory
print(memory_stream.get_memory())

# Remove memories older than 30 days
memory_stream.remove_old_memory(30)

# Save memory to file
memory_stream.save_memory()

# Get memory by index
print(memory_stream.get_memory_by_index(0))

# Remove memory by index
print(memory_stream.remove_memory_by_index(0))
```

### Test for test_memory_stream.py

```
python -m pytest tests/
```

# TODO
- [ ] Create MemoryEntityStorage class
- [ ] Preprocessing of sub-graph's output to store them in MemoryStream