Commit
·
3de8751
1
Parent(s):
9c83ba9
use logging
Browse files
kg_builder/src/knowledge_graph_builder.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
|
| 2 |
from api_connections import get_graph_connection
|
| 3 |
|
|
@@ -13,15 +14,25 @@ from models import Node, Relationship, KnowledgeGraph
|
|
| 13 |
from utils import map_to_base_node, map_to_base_relationship
|
| 14 |
from api_connections import get_extraction_chain
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
def extract_and_store_graph(
|
| 17 |
document: Document,
|
| 18 |
-
|
| 19 |
nodes:Optional[List[str]] = None,
|
| 20 |
rels:Optional[List[str]]=None) -> None:
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
data = extract_chain.invoke(document.page_content)['function']
|
| 26 |
# Construct a graph document
|
| 27 |
graph_document = GraphDocument(
|
|
@@ -29,6 +40,8 @@ def extract_and_store_graph(
|
|
| 29 |
relationships = [map_to_base_relationship(rel) for rel in data.rels],
|
| 30 |
source = document
|
| 31 |
)
|
|
|
|
| 32 |
# Store information into a graph
|
|
|
|
| 33 |
graph.add_graph_documents([graph_document])
|
| 34 |
|
|
|
|
| 1 |
+
import logging
|
| 2 |
|
| 3 |
from api_connections import get_graph_connection
|
| 4 |
|
|
|
|
| 14 |
from utils import map_to_base_node, map_to_base_relationship
|
| 15 |
from api_connections import get_extraction_chain
|
| 16 |
|
| 17 |
+
logging.basicConfig(format='%(name)s - %(levelname)s - %(message)s', level=logging.INFO)
|
| 18 |
+
logger = logging.getLogger(__name__)
|
| 19 |
+
logger.setLevel(logging.INFO)
|
| 20 |
+
|
| 21 |
def extract_and_store_graph(
|
| 22 |
document: Document,
|
| 23 |
+
data_source_name: str,
|
| 24 |
nodes:Optional[List[str]] = None,
|
| 25 |
rels:Optional[List[str]]=None) -> None:
|
| 26 |
+
"""
|
| 27 |
+
Extract data from text document and add nodes and relationships to knowledge graph
|
| 28 |
+
:param document: Text document
|
| 29 |
+
:param data_source_Name: Data source name, e.g. "Traffic Law"
|
| 30 |
+
:param nodes: TODO
|
| 31 |
+
:param rels: TODO
|
| 32 |
+
"""
|
| 33 |
+
|
| 34 |
+
logger.info("Extract graph data using OpenAI functions ...")
|
| 35 |
+
extract_chain = get_extraction_chain(data_source_name, nodes, rels)
|
| 36 |
data = extract_chain.invoke(document.page_content)['function']
|
| 37 |
# Construct a graph document
|
| 38 |
graph_document = GraphDocument(
|
|
|
|
| 40 |
relationships = [map_to_base_relationship(rel) for rel in data.rels],
|
| 41 |
source = document
|
| 42 |
)
|
| 43 |
+
|
| 44 |
# Store information into a graph
|
| 45 |
+
graph = get_graph_connection(data_source_name)
|
| 46 |
graph.add_graph_documents([graph_document])
|
| 47 |
|