Aylin Naebzadeh
Aylin Naebzadeh

Reputation: 145

TypeError: unhashable type: ‘list’, When trying to create a knowledge graph from a list of documents using `convert_to_graph_documents`

I cannot figure out what does the below error mean when trying to create a knowledge graph from multiple documents using LLMGraphTransformer module from LangChain. I have retrieved several documents from Wikipedia, and applied some preprocessing steps. Here is my full code:

repo_id = "mistralai/Mistral-7B-Instruct-v0.2"

llm = HuggingFaceEndpoint(
    repo_id=repo_id,
    max_length=1000,
    temperature=0.001,
    huggingfacehub_api_token=HUGGINGFACEHUB_API_TOKEN,
)

llm_transformer = LLMGraphTransformer(llm=llm)

documents = [Document(page_content=doc.page_content) for doc in preprocessed_documents]
# Convert unique documents to graph documents
graph_documents = llm_transformer.convert_to_graph_documents(documents)

# Combine nodes and relationships from all graph documents
all_nodes = set()
all_relationships = set()
for graph_doc in graph_documents:
    all_nodes.update(graph_doc.nodes)
    all_relationships.update(graph_doc.relationships)

print(f"Nodes: {all_nodes}")
print(f"Relationships: {all_relationships}")

And this is the error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[54], line 3
      1 documents = [Document(page_content=doc.page_content) for doc in preprocessed_documents]
      2 # Convert unique documents to graph documents
----> 3 graph_documents = llm_transformer.convert_to_graph_documents(documents)
      5 # Combine nodes and relationships from all graph documents
      6 all_nodes = set()

File /opt/conda/lib/python3.10/site-packages/langchain_experimental/graph_transformers/llm.py:809, in LLMGraphTransformer.convert_to_graph_documents(self, documents, config)
    797 def convert_to_graph_documents(
    798     self, documents: Sequence[Document], config: Optional[RunnableConfig] = None
    799 ) -> List[GraphDocument]:
    800     """Convert a sequence of documents into graph documents.
    801 
    802     Args:
   (...)
    807         Sequence[GraphDocument]: The transformed documents as graphs.
    808     """
--> 809     return [self.process_response(document, config) for document in documents]

File /opt/conda/lib/python3.10/site-packages/langchain_experimental/graph_transformers/llm.py:809, in <listcomp>(.0)
    797 def convert_to_graph_documents(
    798     self, documents: Sequence[Document], config: Optional[RunnableConfig] = None
    799 ) -> List[GraphDocument]:
    800     """Convert a sequence of documents into graph documents.
    801 
    802     Args:
   (...)
    807         Sequence[GraphDocument]: The transformed documents as graphs.
    808     """
--> 809     return [self.process_response(document, config) for document in documents]

File /opt/conda/lib/python3.10/site-packages/langchain_experimental/graph_transformers/llm.py:762, in LLMGraphTransformer.process_response(self, document, config)
    759 # Nodes need to be deduplicated using a set
    760 # Use default Node label for nodes if missing
    761 nodes_set.add((rel["head"], rel.get("head_type", "Node")))
--> 762 nodes_set.add((rel["tail"], rel.get("tail_type", "Node")))
    764 source_node = Node(id=rel["head"], type=rel.get("head_type", "Node"))
    765 target_node = Node(id=rel["tail"], type=rel.get("tail_type", "Node"))

TypeError: unhashable type: 'list'

Upvotes: 1

Views: 70

Answers (0)

Related Questions