Prepare for the presentation, add info
This commit is contained in:
parent
fa6f50a03d
commit
5e526c0e06
7 changed files with 714 additions and 291 deletions
903
Demo_graph.ipynb
903
Demo_graph.ipynb
File diff suppressed because one or more lines are too long
|
|
@ -3,43 +3,36 @@ from abc import abstractmethod
|
||||||
from typing import Protocol
|
from typing import Protocol
|
||||||
|
|
||||||
class GraphDBInterface(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 """
|
""" Save and Load Graphs """
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
async def save_graph(
|
async def save_graph(
|
||||||
self,
|
self,
|
||||||
graph_name: str
|
path: str
|
||||||
): raise NotImplementedError
|
): raise NotImplementedError
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
async def load_graph(
|
async def load_graph(
|
||||||
self,
|
self,
|
||||||
graph_name: str
|
path: str
|
||||||
): raise NotImplementedError
|
): raise NotImplementedError
|
||||||
|
|
||||||
""" Collections """
|
@abstractmethod
|
||||||
|
async def delete_graph(
|
||||||
|
self,
|
||||||
|
path: str
|
||||||
|
): raise NotImplementedError
|
||||||
|
|
||||||
# @abstractmethod
|
""" CRUD operations on graph nodes """
|
||||||
# async def delete_collection(
|
|
||||||
# self,
|
@abstractmethod
|
||||||
# collection_name: str
|
async def create(self,
|
||||||
# ): raise NotImplementedError
|
user_id:str,
|
||||||
|
custom_user_properties:str,
|
||||||
|
required_layers:list,
|
||||||
|
default_fields:dict
|
||||||
|
): raise NotImplementedError
|
||||||
|
|
||||||
# @abstractmethod
|
# @abstractmethod
|
||||||
# async def create_vector_index(
|
# async def create_vector_index(
|
||||||
|
|
|
||||||
|
|
@ -2,8 +2,8 @@ import pickle
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
import aiofiles
|
import aiofiles
|
||||||
import networkx as nx
|
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):
|
class NetworXDB(GraphDBInterface):
|
||||||
def __init__(self, filename="cognee_graph.pkl"):
|
def __init__(self, filename="cognee_graph.pkl"):
|
||||||
|
|
@ -11,24 +11,39 @@ class NetworXDB(GraphDBInterface):
|
||||||
self.graph = nx.MultiDiGraph()
|
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:
|
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))
|
await f.write(pickle.dumps(self.graph))
|
||||||
self.logger.info("Graph saved successfully.")
|
logging.info("Graph saved successfully.")
|
||||||
except Exception as e:
|
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:
|
try:
|
||||||
async with aiofiles.open(self.filename, "rb") as f:
|
async with aiofiles.open(path, "rb") as f:
|
||||||
data = await f.read()
|
data = await f.read()
|
||||||
self.graph = pickle.loads(data)
|
self.graph = pickle.loads(data)
|
||||||
self.logger.info("Graph loaded successfully.")
|
logging.info("Graph loaded successfully.")
|
||||||
except Exception as e:
|
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."""
|
"""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
|
# Assume required_layers is a dictionary-like object; use more robust validation in production
|
||||||
category_name = required_layers['name']
|
category_name = required_layers['name']
|
||||||
|
|
|
||||||
|
|
@ -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.
|
- **Edges** represent relationships between concepts. They're akin to Wikipedia links.
|
||||||
- The aim is to achieve simplicity and clarity in the
|
- The aim is to achieve simplicity and clarity in the
|
||||||
knowledge graph, making it accessible for a vast audience.
|
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
|
## 2. Labeling Nodes
|
||||||
- **Consistency**: Ensure you use basic or elementary types for node labels.
|
- **Consistency**: Ensure you use basic or elementary types for node labels.
|
||||||
- For example, when you identify an entity representing a person,
|
- For example, when you identify an entity representing a person,
|
||||||
|
|
|
||||||
|
|
@ -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.shared.data_models import KnowledgeGraph
|
||||||
from cognitive_architecture.utils import async_render_template
|
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()
|
llm_client = get_llm_client()
|
||||||
|
|
||||||
formatted_text_input = await async_render_template(filename, context)
|
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__":
|
if __name__ == "__main__":
|
||||||
import asyncio
|
import asyncio
|
||||||
asyncio.run(generate_graph("generate_graph_prompt.txt", {
|
asyncio.run(generate_graph(text_input="bla", filename = "generate_graph_prompt.txt",context= {
|
||||||
'layer': 'text'
|
'layer': 'BLA'
|
||||||
}, response_model=KnowledgeGraph))
|
}, response_model=KnowledgeGraph))
|
||||||
|
|
||||||
|
|
|
||||||
15
poetry.lock
generated
15
poetry.lock
generated
|
|
@ -1,5 +1,16 @@
|
||||||
# This file is automatically @generated by Poetry 1.7.1 and should not be changed by hand.
|
# 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]]
|
[[package]]
|
||||||
name = "aiohttp"
|
name = "aiohttp"
|
||||||
version = "3.9.3"
|
version = "3.9.3"
|
||||||
|
|
@ -4231,7 +4242,7 @@ lancedb = []
|
||||||
motherduck = ["pyarrow"]
|
motherduck = ["pyarrow"]
|
||||||
mssql = []
|
mssql = []
|
||||||
neo4j = []
|
neo4j = []
|
||||||
notebook = []
|
notebook = ["overrides"]
|
||||||
parquet = ["pyarrow"]
|
parquet = ["pyarrow"]
|
||||||
pinecone = []
|
pinecone = []
|
||||||
postgres = []
|
postgres = []
|
||||||
|
|
@ -4245,4 +4256,4 @@ weaviate = []
|
||||||
[metadata]
|
[metadata]
|
||||||
lock-version = "2.0"
|
lock-version = "2.0"
|
||||||
python-versions = "^3.10"
|
python-versions = "^3.10"
|
||||||
content-hash = "39962c40053f064940d94532130d182d5a0a0526c915c39a0c48f98f21321630"
|
content-hash = "313beec7482cf4513abd8521d453d2e8513c231b7b8d1f89df26f3dc095cf943"
|
||||||
|
|
|
||||||
|
|
@ -41,6 +41,7 @@ pandas = "^2.2.1"
|
||||||
greenlet = "^3.0.3"
|
greenlet = "^3.0.3"
|
||||||
ruff = "^0.2.2"
|
ruff = "^0.2.2"
|
||||||
overrides = "^7.7.0"
|
overrides = "^7.7.0"
|
||||||
|
aiofiles = "^23.2.1"
|
||||||
|
|
||||||
[tool.poetry.extras]
|
[tool.poetry.extras]
|
||||||
dbt = ["dbt-core", "dbt-redshift", "dbt-bigquery", "dbt-duckdb", "dbt-snowflake", "dbt-athena-community", "dbt-databricks"]
|
dbt = ["dbt-core", "dbt-redshift", "dbt-bigquery", "dbt-duckdb", "dbt-snowflake", "dbt-athena-community", "dbt-databricks"]
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue