-
Checked other resources
Commit to Help
Example Codefrom dotenv import load_dotenv
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
load_dotenv()
os.environ["NEO4J_URI"] = "bolt://localhost:7687"
os.environ["NEO4J_USERNAME"] = "neo4j"
os.environ["NEO4J_PASSWORD"] = getpass.getpass()
graph = Neo4jGraph()
llm=Ollama(temperature=0,model="llama3",base_url="http://localhost:11434")
llm_transformer = LLMGraphTransformer(llm=llm)
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) DescriptionI follow the "How to construct knowledge graphs" tutorial , instead of chatgpt llm I m using ollama + llama3. But I dont have the luck when I run to convert_to_graph_documents it broke and give error message AttributeError Traceback (most recent call last) File /.../python3.12/site-packages/langchain_experimental/graph_transformers/llm.py:646, in LLMGraphTransformer.convert_to_graph_documents(self, documents) File /.../python3.12/site-packages/langchain_experimental/graph_transformers/llm.py:595, in LLMGraphTransformer.process_response(self, document) AttributeError: 'str' object has no attribute 'content' System Infolangchain==0.1.16 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Hey there, @Jeffhaha! I'm here to help with any bugs, questions, or contributions you might have. Let's see what's going on with the issue you've encountered. To resolve the Here is the relevant part of the 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 # This line should be corrected
parsed_json = self.json_repair.loads(raw_schema)
for rel in parsed_json:
# Nodes need to be deduplicated using a set
nodes_set.add((rel["head"], rel["head_type"]))
nodes_set.add((rel["tail"], rel["tail_type"])) The corrected line should be: raw_schema = raw_schema.content This ensures that |
Beta Was this translation helpful? Give feedback.
Hey there, @Jeffhaha! I'm here to help with any bugs, questions, or contributions you might have. Let's see what's going on with the issue you've encountered.
To resolve the
AttributeError: 'str' object has no attribute 'content'
when using theconvert_to_graph_documents
method from theLLMGraphTransformer
class, ensure that theraw_schema
is correctly cast to a dictionary before accessing its attributes. Specifically, in theprocess_response
method, theraw_schema
should be cast to a dictionary if it is not already one.Here is the relevant part of the
process_response
method with the necessary correction: