Prepare for the presentation, add info

This commit is contained in:
Vasilije 2024-03-08 12:39:05 +01:00
parent fa6f50a03d
commit 5e526c0e06
7 changed files with 714 additions and 291 deletions

File diff suppressed because one or more lines are too long

View file

@ -3,43 +3,36 @@ from abc import abstractmethod
from typing import Protocol
class GraphDBInterface(Protocol):
""" Graphs """
@abstractmethod
async def create_graph(
self,
graph_name: str,
graph_config: object
): raise NotImplementedError
@abstractmethod
async def update_graph(
self,
graph_name: str,
graph_config: object
): raise NotImplementedError
""" Save and Load Graphs """
@abstractmethod
async def save_graph(
self,
graph_name: str
path: str
): raise NotImplementedError
@abstractmethod
async def load_graph(
self,
graph_name: str
path: str
): raise NotImplementedError
""" Collections """
@abstractmethod
async def delete_graph(
self,
path: str
): raise NotImplementedError
# @abstractmethod
# async def delete_collection(
# self,
# collection_name: str
# ): raise NotImplementedError
""" CRUD operations on graph nodes """
@abstractmethod
async def create(self,
user_id:str,
custom_user_properties:str,
required_layers:list,
default_fields:dict
): raise NotImplementedError
# @abstractmethod
# async def create_vector_index(

View file

@ -2,8 +2,8 @@ import pickle
from datetime import datetime
import aiofiles
import networkx as nx
from databases.graph.graph_db_interface import GraphDBInterface
from cognitive_architecture.infrastructure.databases.graph.graph_db_interface import GraphDBInterface
import logging
class NetworXDB(GraphDBInterface):
def __init__(self, filename="cognee_graph.pkl"):
@ -11,24 +11,39 @@ class NetworXDB(GraphDBInterface):
self.graph = nx.MultiDiGraph()
async def save_graph(self):
async def save_graph(self, path: str):
"""Asynchronously save the graph to a file."""
if path is not None:
path = self.filename
try:
async with aiofiles.open(self.filename, "wb") as f:
async with aiofiles.open(path, "wb") as f:
await f.write(pickle.dumps(self.graph))
self.logger.info("Graph saved successfully.")
logging.info("Graph saved successfully.")
except Exception as e:
self.logger.error(f"Failed to save graph: {e}")
logging.error(f"Failed to save graph: {e}")
async def load_graph(self):
async def load_graph(self, path: str):
if path is not None:
path = self.filename
try:
async with aiofiles.open(self.filename, "rb") as f:
async with aiofiles.open(path, "rb") as f:
data = await f.read()
self.graph = pickle.loads(data)
self.logger.info("Graph loaded successfully.")
logging.info("Graph loaded successfully.")
except Exception as e:
self.logger.error(f"Failed to load graph: {e}")
logging.error(f"Failed to load graph: {e}")
async def create_graph(self, user_id, custom_user_properties=None, required_layers=None, default_fields=None):
async def delete_graph(self, path: str):
if path is not None:
path = self.filename
try:
async with aiofiles.open(path, "wb") as f:
await f.write(pickle.dumps(self.graph))
logging.info("Graph deleted successfully.")
except Exception as e:
logging.error(f"Failed to delete graph: {e}")
async def create(self, user_id, custom_user_properties=None, required_layers=None, default_fields=None):
"""Asynchronously create or update a user content graph based on given parameters."""
# Assume required_layers is a dictionary-like object; use more robust validation in production
category_name = required_layers['name']

View file

@ -4,7 +4,7 @@ designed for extracting information in structured formats to build a knowledge g
- **Edges** represent relationships between concepts. They're akin to Wikipedia links.
- The aim is to achieve simplicity and clarity in the
knowledge graph, making it accessible for a vast audience.
YOU ARE ONLY EXTRACTING DATA FOR COGNITIVE LAYER `{{layer}}`
YOU ARE ONLY EXTRACTING DATA FOR COGNITIVE LAYER `{{ layer }}`
## 2. Labeling Nodes
- **Consistency**: Ensure you use basic or elementary types for node labels.
- For example, when you identify an entity representing a person,

View file

@ -5,17 +5,17 @@ from cognitive_architecture.infrastructure.llm.get_llm_client import get_llm_cli
from cognitive_architecture.shared.data_models import KnowledgeGraph
from cognitive_architecture.utils import async_render_template
async def generate_graph(filename: str,context, response_model: Type[BaseModel]):
async def generate_graph(text_input:str, filename: str,context, response_model: Type[BaseModel]):
llm_client = get_llm_client()
formatted_text_input = await async_render_template(filename, context)
return await llm_client.acreate_structured_output(formatted_text_input,formatted_text_input, response_model)
return await llm_client.acreate_structured_output(text_input,formatted_text_input, response_model)
if __name__ == "__main__":
import asyncio
asyncio.run(generate_graph("generate_graph_prompt.txt", {
'layer': 'text'
asyncio.run(generate_graph(text_input="bla", filename = "generate_graph_prompt.txt",context= {
'layer': 'BLA'
}, response_model=KnowledgeGraph))

15
poetry.lock generated
View file

@ -1,5 +1,16 @@
# This file is automatically @generated by Poetry 1.7.1 and should not be changed by hand.
[[package]]
name = "aiofiles"
version = "23.2.1"
description = "File support for asyncio."
optional = false
python-versions = ">=3.7"
files = [
{file = "aiofiles-23.2.1-py3-none-any.whl", hash = "sha256:19297512c647d4b27a2cf7c34caa7e405c0d60b5560618a29a9fe027b18b0107"},
{file = "aiofiles-23.2.1.tar.gz", hash = "sha256:84ec2218d8419404abcb9f0c02df3f34c6e0a68ed41072acfb1cef5cbc29051a"},
]
[[package]]
name = "aiohttp"
version = "3.9.3"
@ -4231,7 +4242,7 @@ lancedb = []
motherduck = ["pyarrow"]
mssql = []
neo4j = []
notebook = []
notebook = ["overrides"]
parquet = ["pyarrow"]
pinecone = []
postgres = []
@ -4245,4 +4256,4 @@ weaviate = []
[metadata]
lock-version = "2.0"
python-versions = "^3.10"
content-hash = "39962c40053f064940d94532130d182d5a0a0526c915c39a0c48f98f21321630"
content-hash = "313beec7482cf4513abd8521d453d2e8513c231b7b8d1f89df26f3dc095cf943"

View file

@ -41,6 +41,7 @@ pandas = "^2.2.1"
greenlet = "^3.0.3"
ruff = "^0.2.2"
overrides = "^7.7.0"
aiofiles = "^23.2.1"
[tool.poetry.extras]
dbt = ["dbt-core", "dbt-redshift", "dbt-bigquery", "dbt-duckdb", "dbt-snowflake", "dbt-athena-community", "dbt-databricks"]