Replies: 1 comment 2 replies
-
Hey there, @ghinwa-mj! I'm here to help you with your LangChain issue. I'm a bot that can assist you with bug fixes, answer your questions, and guide you to become a contributor. Let's tackle this together! The To fix this issue, you can add a check to ensure that all required keys are present before attempting to access them. Here is the modified def process_response(self, document: Document) -> GraphDocument:
"""
Processes a single document, transforming it into a graph document using
an LLM based on the model's schema and constraints.
"""
text = document.page_content
raw_schema = self.chain.invoke({"input": text})
if self._function_call:
raw_schema = cast(Dict[Any, Any], raw_schema)
nodes, relationships = _convert_to_graph_document(raw_schema)
else:
nodes_set = set()
relationships = []
if not isinstance(raw_schema, str):
raw_schema = raw_schema.content
parsed_json = self.json_repair.loads(raw_schema)
for rel in parsed_json:
# Check if all required keys are present
if all(key in rel for key in ["head", "head_type", "relation", "tail", "tail_type"]):
# Nodes need to be deduplicated using a set
nodes_set.add((rel["head"], rel["head_type"]))
nodes_set.add((rel["tail"], rel["tail_type"]))
source_node = Node(id=rel["head"], type=rel["head_type"])
target_node = Node(id=rel["tail"], type=rel["tail_type"])
relationships.append(
Relationship(
source=source_node, target=target_node, type=rel["relation"]
)
)
else:
# Handle the case where keys are missing
print(f"Skipping relationship due to missing keys: {rel}")
# Create nodes list
nodes = [Node(id=el[0], type=el[1]) for el in list(nodes_set)]
# Strict mode filtering
if self.strict_mode and (self.allowed_nodes or self.allowed_relationships):
if self.allowed_nodes:
lower_allowed_nodes = [el.lower() for el in self.allowed_nodes]
nodes = [
node for node in nodes if node.type.lower() in lower_allowed_nodes
]
relationships = [
rel
for rel in relationships
if rel.source.type.lower() in lower_allowed_nodes
and rel.target.type.lower() in lower_allowed_nodes
]
if self.allowed_relationships:
relationships = [
rel
for rel in relationships
if rel.type.lower()
in [el.lower() for el in self.allowed_relationships]
]
return GraphDocument(nodes=nodes, relationships=relationships, source=document) This modification ensures that the code only processes relationships that contain all the required keys, thereby preventing the |
Beta Was this translation helpful? Give feedback.
-
Checked other resources
Commit to Help
Example Code
Description
I am using the function llm_transformer.convert_to_graph() to create graph documents from pdf_texts and upload them to my neo4j database.
Sometimes I get a KeyError: “KeyError: 'tail_type’” or “KeyError: 'tail’”.
This doesn’t happen always (typically with the longer pdfs). Any ideas where this could be coming from and what I can do to fix it?
System Info
Using GoogleColab
Beta Was this translation helpful? Give feedback.
All reactions