Replies: 1 comment 2 replies
-
Hey @gedman4b! I'm here to help you with your coding issues. I'm a bot designed to assist with bug fixes, answer questions, and guide you towards becoming a contributor. Let's tackle this challenge together! To load a serialized JSON file into a LangChain InstallationFirst, install the necessary packages: %pip install -qU langchain_community langchain_experimental jq Loading JSON FileNow, you can use the from langchain_experimental.graph_transformers import LLMGraphTransformer
from langchain_core.documents import Document
from langchain_openai import ChatOpenAI
# Initialize the language model
llm = ChatOpenAI(temperature=0)
# Create the transformer
transformer = LLMGraphTransformer(
llm=llm,
allowed_nodes=["Person", "Organization"],
allowed_relationships=["WORKS_FOR", "HAS_AWARD", "PRODUCED_BY", "HAS_CHARACTERISTIC"]
)
# Load the JSON file into a Document
with open("/path/to/your/serialized_file.json", "r") as file:
json_content = file.read()
doc = Document(page_content=json_content)
# Convert to graph documents
graph_documents = transformer.convert_to_graph_documents([doc])
print(graph_documents[0]) This code snippet reads the JSON file from disk and loads it into a LangChain object using the |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Checked other resources
Commit to Help
Example Code
Description
I have serialized a LangChain GraphDocument and saved it as a json file.
I now wish to load that file back into a LangChain Graph Document. (see my code above)
When I try to dot his I am getting a NotImplementedError:
NotImplementedError: Trying to load an object that doesn't implement serialization: {'lc': 1, 'type': 'not_implemented', 'id': ['langchain_community', 'graphs', 'graph_document', 'GraphDocument'], ........
I then tried to use your ChatLangChain and it responded with a suggestion the same as the code I came up with. The Chat response:
**To load a serialized JSON file into a LangChain GraphDocument, you can follow these steps:
• Use the loads function from the langchain_core.load module to deserialize the JSON string representation.
• Specify a secrets_map if your serialized object contains sensitive information, such as API keys.
Example code snippet:
from langchain_core.load import loads
with open("/path/to/your/serialized_file.json", "r") as fp:
serialized_data = json.load(fp)
graph_document = loads(serialized_data, secrets_map={"API_KEY": "your-api-key"})
This will instantiate the GraphDocument from the serialized JSON data.**
How can I get this code to work?
System Info
pip freeze | grep langchain
langchain==0.2.12
langchain-chroma==0.1.3
langchain-community==0.2.11
langchain-core==0.2.28
langchain-experimental==0.0.64
langchain-openai==0.1.20
langchain-text-splitters==0.2.2
Platform windows
Python 3.11.2
Beta Was this translation helpful? Give feedback.
All reactions