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

AttributeError on calling LLMGraphTransformer.convert_to_graph_documents #21482

Open
5 tasks done
mintomohan opened this issue May 9, 2024 · 7 comments
Open
5 tasks done
Labels
🤖:bug Related to a bug, vulnerability, unexpected error with an existing feature 🔌: neo4j Primarily related to Neo4j integrations

Comments

@mintomohan
Copy link

mintomohan commented May 9, 2024

Checked other resources

  • I added a very descriptive title to this issue.
  • I searched the LangChain documentation with the integrated search.
  • I used the GitHub search to find a similar question and didn't find it.
  • I am sure that this is a bug in LangChain rather than my code.
  • The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package).

Example Code

from langchain_experimental.graph_transformers.llm import LLMGraphTransformer
from langchain.llms.bedrock import Bedrock
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_community.retrievers import WikipediaRetriever
from langchain_community.graphs.neo4j_graph import Neo4jGraph

bedrock=boto3.client(service_name='bedrock-runtime')

def prepare_graph(wiki_keyword):
    wiki_retriever = WikipediaRetriever(doc_content_chars_max=2000, top_k_results=1)
    docs = wiki_retriever.invoke(wiki_keyword)
    
    text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
    doc_chunks = text_splitter.split_documents(docs)
    
    llm=Bedrock(model_id='mistral.mistral-7b-instruct-v0:2', client=bedrock)
    llm_transformer = LLMGraphTransformer(llm=llm)    
    
    graph_documents = llm_transformer.convert_to_graph_documents(doc_chunks)
    graph = Neo4jGraph()
    graph.add_graph_documents(graph_documents)

Error Message and Stack Trace (if applicable)

Traceback (most recent call last):
  File "...\...\load_data_graph.py", line 46, in <module>
    prepare_graph('Paris')
  File "...\...\load_data_graph.py", line 38, in prepare_graph
    graph_documents = llm_transformer.convert_to_graph_documents(doc_chunks)
  File "...\...\venv\lib\site-packages\langchain_experimental\graph_transformers\llm.py", line 646, in convert_to_graph_documents   
    return [self.process_response(document) for document in documents]
  File "...\...\venv\lib\site-packages\langchain_experimental\graph_transformers\llm.py", line 646, in <listcomp>
    return [self.process_response(document) for document in documents]
  File "...\...\venv\lib\site-packages\langchain_experimental\graph_transformers\llm.py", line 595, in process_response
    parsed_json = self.json_repair.loads(raw_schema.content)
AttributeError: 'str' object has no attribute 'content'

Description

I am trying to load a page from Wikipedia, split it and load to Neo4j using langchain

Wikipedia --> WikipediaRetriever --> RecursiveCharacterTextSplitter --> LLMGraphTransformer --> Neo4jGraph

LLM used is Mistral 7B (using AWS Bedrock)

System Info

langchain==0.1.17
langchain-community==0.0.36
langchain-core==0.1.52
langchain-experimental==0.0.58
langchain-text-splitters==0.0.1
langsmith==0.1.52

platform : windows

Python 3.10.8

@dosubot dosubot bot added 🔌: neo4j Primarily related to Neo4j integrations 🤖:bug Related to a bug, vulnerability, unexpected error with an existing feature labels May 9, 2024
@liugddx
Copy link
Contributor

liugddx commented May 13, 2024

Let me see.

@UmutAlihan
Copy link

I am having the same issue here :'/

@somashekarbrdtscblr
Copy link

somashekarbrdtscblr commented May 17, 2024

its able to run llama2 server and model but having this same bug/error

CODE:
%pip install --upgrade --quiet langchain langchain-community langchain-openai langchain-experimental neo4j json_repair

import os
import json
import types
from langchain_community.graphs import Neo4jGraph
from langchain_experimental.graph_transformers import LLMGraphTransformer
from langchain_openai import ChatOpenAI
from langchain_core.documents import Document
from langchain_community.llms import Ollama

llm = Ollama(temperature=0, model="llama2")
llm_transformer = LLMGraphTransformer(llm=llm)

os.environ["NEO4J_URI"] = "neo4j://localhost:7687"
os.environ["NEO4J_USERNAME"] = "neo4j"
os.environ["NEO4J_PASSWORD"] = "neo4jadmin"

graph = Neo4jGraph()

text = """Marie Curie, born in 1867, was a Polish and naturalised-French physicist and chemist who conducted pioneering research on radioactivity.She was the first woman to win a Nobel Prize, the first person to win a Nobel Prize twice, and the only person to win a Nobel Prize in two scientific fields.Her husband, Pierre Curie, was a co-winner of her first Nobel Prize, making them the first-ever married couple to win the Nobel Prize and launching the Curie family legacy of five Nobel Prizes.She was, in 1906, the first woman to become a professor at the University of Paris."""

documents = [Document(page_content=text)]
graph_documents = llm_transformer.convert_to_graph_documents(documents)

ERROR

AttributeError Traceback (most recent call last)
Cell In[27], line 2
1 documents = [Document(page_content=text)]
----> 2 graph_documents = llm_transformer.convert_to_graph_documents(documents)

File C:\tools\Anaconda3\lib\site-packages\langchain_experimental\graph_transformers\llm.py:646, in LLMGraphTransformer.convert_to_graph_documents(self, documents)
634 def convert_to_graph_documents(
635 self, documents: Sequence[Document]
636 ) -> List[GraphDocument]:
637 """Convert a sequence of documents into graph documents.
638
639 Args:
(...)
644 Sequence[GraphDocument]: The transformed documents as graphs.
645 """
--> 646 return [self.process_response(document) for document in documents]

File C:\tools\Anaconda3\lib\site-packages\langchain_experimental\graph_transformers\llm.py:646, in (.0)
634 def convert_to_graph_documents(
635 self, documents: Sequence[Document]
636 ) -> List[GraphDocument]:
637 """Convert a sequence of documents into graph documents.
638
639 Args:
(...)
644 Sequence[GraphDocument]: The transformed documents as graphs.
645 """
--> 646 return [self.process_response(document) for document in documents]

File C:\tools\Anaconda3\lib\site-packages\langchain_experimental\graph_transformers\llm.py:595, in LLMGraphTransformer.process_response(self, document)
593 nodes_set = set()
594 relationships = []
--> 595 parsed_json = self.json_repair.loads(raw_schema.content)
596 for rel in parsed_json:
597 # Nodes need to be deduplicated using a set
598 nodes_set.add((rel["head"], rel["head_type"]))

AttributeError: 'str' object has no attribute 'content'

@Yweik
Copy link

Yweik commented May 23, 2024


AttributeError Traceback (most recent call last)
in <cell line: 5>()
3 text_splitter = TokenTextSplitter(chunk_size=512, chunk_overlap=24)
4 documents = text_splitter.split_documents(docs)
----> 5 graph_documents = llm_transformer.convert_to_graph_documents(documents)

2 frames
/usr/local/lib/python3.10/dist-packages/langchain_experimental/graph_transformers/llm.py in process_response(self, document)
593 nodes_set = set()
594 relationships = []
--> 595 parsed_json = self.json_repair.loads(raw_schema.content)
596 for rel in parsed_json:
597 # Nodes need to be deduplicated using a set

AttributeError: 'str' object has no attribute 'content'
I encountered the same problem

@love11mishra
Copy link

I'm also facing the same error.

@francruzdiconium
Copy link

Running the same "Marie Curie" use case (with AzureOpeAi) i always get empty nodes. On Langsmith I can that the model is able to extract the nodes, but i see on the PydanticToolsParser step:

[big json here] are not valid JSON. Received JSONDecodeError Expecting value: line 1 column 1 (char 0)

@mintomohan
Copy link
Author

Updates on the original issue:

(1) I switched from Mistral to OpenAI (GPT 3.5 Turbo) and encountered a different error message.

ValueError: Could not use APOC procedures.

(2) I referred to the following issue and adjusted the settings in Neo4j as suggested: #12901 (comment)

Currently, I am facing this error:

    graph_documents = llm_transformer.convert_to_graph_documents(doc_chunks)
  File "...\...\lib\site-packages\langchain_experimental\graph_transformers\llm.py", line 646, in convert_to_graph_documents
    return [self.process_response(document) for document in documents]
  File "...\...\lib\site-packages\langchain_experimental\graph_transformers\llm.py", line 646, in <listcomp>      
    return [self.process_response(document) for document in documents]
  File "...\...\lib\site-packages\langchain_experimental\graph_transformers\llm.py", line 598, in process_response    nodes_set.add((rel["head"], rel["head_type"]))
TypeError: string indices must be integers

Version Info:

langchain                                0.1.20
langchain-aws                            0.1.6
langchain-cohere                         0.1.5
langchain-community                      0.0.38
langchain-core                           0.1.52
langchain-experimental                   0.0.58
langchain-openai                         0.0.5
langchain-text-splitters                 0.0.1
neo4j                                    5.20.0

liugz18 added a commit to liugz18/langchain that referenced this issue Jun 3, 2024
…cuments langchain-ai#21482

Fix AttributeError on calling LLMGraphTransformer.convert_to_graph_documents langchain-ai#21482
since raw_schema is always a str
baskaryan pushed a commit that referenced this issue Jun 4, 2024
Fix AttributeError on calling
LLMGraphTransformer.convert_to_graph_documents #21482

 since raw_schema is always a str

@baskaryan
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🤖:bug Related to a bug, vulnerability, unexpected error with an existing feature 🔌: neo4j Primarily related to Neo4j integrations
Projects
None yet
Development

No branches or pull requests

7 participants