Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

serialization and deserialization of graph documents #34

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions libs/experimental/langchain_experimental/graph_transformers/llm.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import asyncio
import json
import os
import pickle
from typing import Any, Dict, List, Optional, Sequence, Tuple, Type, Union, cast

from langchain_community.graphs.graph_document import GraphDocument, Node, Relationship
Expand Down Expand Up @@ -931,6 +933,41 @@ def convert_to_graph_documents(
"""
return [self.process_response(document, config) for document in documents]

def save_graph_documents(
self,
graph_documents: List[GraphDocument],
file_name: str = "graph_document.pkl",
) -> None:
"""
Serializing the graph documents to a file
"""
# get the current working directory
project_dir = os.getcwd()
intermediate_file_path = os.path.join(project_dir, file_name)
# open the file in write binary mode
with open(intermediate_file_path, "wb") as db_file:
pickle.dump(graph_documents, db_file)
file_path = os.path.abspath(db_file.name)
print(f"Graph documents saved to {file_path}") # noqa: T201

def load_graph_documents(
self, file_name: str = "graph_document.pkl"
) -> List[GraphDocument]:
"""
Deserializing the graph documents from a file
"""
# get the current working directory
project_dir = os.getcwd()
intermediate_file_path = os.path.join(project_dir, file_name)
# handling if user provides the full path
if not os.path.exists(file_name):
intermediate_file_path = file_name
# open the file in read binary mode
graph_documents = []
with open(intermediate_file_path, "rb") as db_file:
graph_documents = pickle.load(db_file)
return graph_documents

async def aprocess_response(
self, document: Document, config: Optional[RunnableConfig] = None
) -> GraphDocument:
Expand Down
9 changes: 9 additions & 0 deletions libs/experimental/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading