-
Notifications
You must be signed in to change notification settings - Fork 5.1k
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
[Question]: How to add a new relationship to a persisted knowledge graph? #9336
Comments
🤖 Hello @JinSeoung-Oh! Good to see you again. I appreciate your continued interest in LlamaIndex. Sure, you can add a new relationship to a persisted KnowledgeGraph using the graph_store = Neo4jGraphStore(username="your_username", password="your_password", url="your_neo4j_url")
graph_store.upsert_triplet("subject_node_id", "relationship_type", "object_node_id") In this example, replace The Here's the code that defines the def upsert_triplet(self, subj: str, rel: str, obj: str) -> None:
"""Add triplet."""
query = """
MERGE (n1:`%s` {id:$subj})
MERGE (n2:`%s` {id:$obj})
MERGE (n1)-[:`%s`]->(n2)
"""
prepared_statement = query % (
self.node_label,
self.node_label,
rel.replace(" ", "_").upper(),
)
with self._driver.session(database=self._database) as session:
session.run(prepared_statement, {"subj": subj, "obj": obj}) You can find this code in the llama_index/graph_stores/neo4j.py file. I hope this helps! If you have any more questions, feel free to ask. Best, Sources
This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research. Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant. If you want to continue the conversation, start your reply with @dosu-bot. |
@dosu-bot I have the other question.
This code is right? |
🤖 Based on the provided context, your code for initializing a KnowledgeGraphRAGRetriever in the LlamaIndex repository appears to be correct. The KnowledgeGraphRAGRetriever class is defined with the necessary arguments and methods, and it inherits from the BaseRetriever class, which is a common practice in object-oriented programming to extend the functionality of a base class. Here is the initialization code for the KnowledgeGraphRAGRetriever: class KnowledgeGraphRAGRetriever(BaseRetriever):
"""
Knowledge Graph RAG retriever.
Retriever that perform SubGraph RAG towards knowledge graph.
Args:
service_context (Optional[ServiceContext]): A service context to use.
storage_context (Optional[StorageContext]): A storage context to use.
entity_extract_fn (Optional[Callable]): A function to extract entities.
entity_extract_template Optional[BasePromptTemplate]): A Query Key Entity
Extraction Prompt (see :ref:`Prompt-Templates`).
entity_extract_policy (Optional[str]): The entity extraction policy to use.
default: "union"
possible values: "union", "intersection"
synonym_expand_fn (Optional[Callable]): A function to expand synonyms.
synonym_expand_template (Optional[QueryKeywordExpandPrompt]): A Query Key Entity
Expansion Prompt (see :ref:`Prompt-Templates`).
synonym_expand_policy (Optional[str]): The synonym expansion policy to use.
default: "union"
possible values: "union", "intersection"
max_entities (int): The maximum number of entities to extract.
default: 5
max_synonyms (int): The maximum number of synonyms to expand per entity.
default: 5
retriever_mode (Optional[str]): The retriever mode to use.
default: "keyword"
possible values: "keyword", "embedding", "keyword_embedding"
with_nl2graphquery (bool): Whether to combine NL2GraphQuery in context.
default: False
graph_traversal_depth (int): The depth of graph traversal.
default: 2
max_knowledge_sequence (int): The maximum number of knowledge sequence to
include in the response. By default, it's 30.
verbose (bool): Whether to print out debug info.
""" This class is designed to perform SubGraph RAG (Retrieval-Augmented Generation) towards a knowledge graph. It takes several parameters including service_context, storage_context, entity_extract_fn, entity_extract_template, entity_extract_policy, synonym_expand_fn, synonym_expand_template, synonym_expand_policy, max_entities, max_synonyms, retriever_mode, with_nl2graphquery, graph_traversal_depth, max_knowledge_sequence, and verbose. As for the You can find more details about these functions in the llama_index/indices/loading.py file in the LlamaIndex repository. Please let me know if you have any other questions or need further clarification. SourcesThis response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research. Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant. If you want to continue the conversation, start your reply with @dosu-bot. |
@dosu-bot In this, use case, I think I have to define storagecontext twice, maybe.. |
🤖 Based on the context provided, you can load a persisted Nebula Graph, add a new relationship to it, and then build a KnowledgeGraphRAGRetriever using the updated graph in the LlamaIndex data framework by following these steps:
from llama_index import (
KnowledgeGraphIndex,
LLMPredictor,
ServiceContext,
SimpleDirectoryReader,
)
from llama_index.storage.storage_context import StorageContext
from llama_index.graph_stores import NebulaGraphStore
from llama_index.llms import OpenAI
# define LLM
llm = OpenAI(temperature=0, model="text-davinci-002")
service_context = ServiceContext.from_defaults(llm=llm, chunk_size_limit=512)
# Define NebulaGraphStore
graph_store = NebulaGraphStore(
space_name=space_name,
edge_types=edge_types,
rel_prop_names=rel_prop_names,
tags=tags,
)
# Create StorageContext
storage_context = StorageContext.from_defaults(graph_store=graph_store)
from llama_index.query_engine import RetrieverQueryEngine
from llama_index.retrievers import KnowledgeGraphRAGRetriever
# Define KnowledgeGraphRAGRetriever
graph_rag_retriever = KnowledgeGraphRAGRetriever(
storage_context=storage_context,
service_context=service_context,
llm=llm,
verbose=True,
)
# Define RetrieverQueryEngine
query_engine = RetrieverQueryEngine.from_args(
graph_rag_retriever, service_context=service_context
) You do not need to define the storage context twice in this process. The storage context is defined once after setting up the NebulaGraphStore, and it is then passed to the KnowledgeGraphRAGRetriever. For more details, you can refer to the knowledge_graph_rag_query_engine.ipynb example in the LlamaIndex repository. Regarding the specific methods provided by the NebulaGraphStore class in the LlamaIndex framework to load a persisted graph, you can refer to the nebulagraph.py file in the LlamaIndex repository. The methods include Sources
This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research. Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant. If you want to continue the conversation, start your reply with @dosu-bot. |
@dosu-bot |
🤖 Based on the information provided, the current status of the LlamaIndex repository is active with the latest version being v0.9.12. The latest commit includes various bug fixes and improvements, such as fixing a bug with Unfortunately, I couldn't find the information about the latest commit and the author of it within the provided context. If you need more specific details about the latest commit or the author, you might want to check the commit history directly on the LlamaIndex repository. For more details about the changes made in the latest commit, you can refer to the CHANGELOG.md file in the repository. If you have any more questions or need further clarification, feel free to ask. SourcesThis response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research. Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant. If you want to continue the conversation, start your reply with @dosu-bot. |
@dosu-bot In this use case, I think we have to define Storage_context after add new triplet on NebularGraph |
🤖 Yes, your understanding is correct. In the LlamaIndex data framework, it is necessary to define the StorageContext after adding a new triplet to the NebulaGraph for the purpose of building an updated KnowledgeGraphRAGRetriever. The StorageContext is a utility container for storing nodes, indices, and vectors. It contains a document store, index store, vector stores, and a graph store. When you add a new triplet to the NebulaGraph, you are essentially adding new data to the graph store. To ensure that this new data is included in the KnowledgeGraphRAGRetriever, you need to update the StorageContext. The StorageContext has a method called Here is the code snippet for persisting the StorageContext: storage_context.persist(persist_dir=<your_directory>) Replace As for adding a new triplet to the NebulaGraph, there is a method named I hope this helps! If you have any more questions, feel free to ask. SourcesThis response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research. Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant. If you want to continue the conversation, start your reply with @dosu-bot. |
@dosu-bot |
@dosu-bot |
🤖 No problem at all! Misunderstandings happen, and I'm here to help. If you have any questions or need assistance with anything else, feel free to ask. This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research. Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant. If you want to continue the conversation, start your reply with @dosu-bot. |
@dosu-bot |
🤖 Based on the information from the LlamaIndex repository, the As for the The main difference between the two functions is where they add the new triplet. The You can find more details in the following sources: I hope this clarifies your question. If you have any more questions, feel free to ask. Sources
This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research. Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant. If you want to continue the conversation, start your reply with @dosu-bot. |
@dosu-bot |
🤖 Yes, you are correct. To add a new triplet to both the NebulaGraph and the KnowledgeGraphIndex, you would need to use the The def upsert_triplet(self, triplet: Tuple[str, str, str]) -> None:
"""Insert triplets.
Used for manual insertion of KG triplets (in the form
of (subject, relationship, object)).
Args:
triplet (str): Knowledge triplet
"""
self._graph_store.upsert_triplet(*triplet) (Source: llama_index/indices/knowledge_graph/base.py) As for the If you have the I hope this helps! If you have any more questions, feel free to ask. Sources
This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research. Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant. If you want to continue the conversation, start your reply with @dosu-bot. |
@dosu-bot |
🤖 Yes, you are correct. To add a new triplet to both the Nebula Graph and the Knowledge Index, you need to use the The def upsert_triplet(self, triplet: Tuple[str, str, str]) -> None:
"""Insert triplets.
Used for manual insertion of KG triplets (in the form
of (subject, relationship, object)).
Args:
triplet (str): Knowledge triplet
"""
self._graph_store.upsert_triplet(*triplet) Here, The
Here is the refined function definition based on the context shared: def upsert_triplet(self, subj: str, rel: str, obj: str) -> None:
"""Insert or update a triplet in the Nebula Graph database."""
# Escape the input strings
subj = escape_str(subj)
rel = escape_str(rel)
obj = escape_str(obj)
# Format subject and object fields based on the vertex ID type
if self._vid_type == "INT64":
assert all([subj.isdigit(), obj.isdigit()]), "Subject and object should be digit strings in current graph store."
subj_field = subj
obj_field = obj
else:
subj_field = f"{QUOTE}{subj}{QUOTE}"
obj_field = f"{QUOTE}{obj}{QUOTE}"
# Construct the DML query
edge_field = f"{subj_field}->{obj_field}"
edge_type = self._edge_types[0]
rel_prop_name = self._rel_prop_names[0]
entity_type = self._tags[0]
rel_hash = hash_string_to_rank(rel)
dml_query = (
f"INSERT VERTEX `{entity_type}`(name) VALUES {subj_field}:({QUOTE}{subj}{QUOTE});"
f"INSERT VERTEX `{entity_type}`(name) VALUES {obj_field}:({QUOTE}{obj}{QUOTE});"
f"INSERT EDGE `{edge_type}`(`{rel_prop_name}`) VALUES {edge_field}@{rel_hash}:({QUOTE}{rel}{QUOTE});"
)
# Execute the query and assert success
logger.debug(f"upsert_triplet()\nDML query: {dml_query}")
result = self.execute(dml_query)
assert result and result.is_succeeded(), f"Failed to upsert triplet: {subj} {rel} {obj}, query: {dml_query}" You can find more details in the llama_index/indices/knowledge_graph/base.py and llama_index/graph_stores/nebulagraph.py files. I hope this clarifies your question. If you have any more questions, feel free to ask. Sources
This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research. Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant. If you want to continue the conversation, start your reply with @dosu-bot. |
@dosu-bot |
🤖 You're welcome! If you have any more questions or need further assistance while modifying your code, feel free to ask. Good luck with your modifications! This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research. Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant. If you want to continue the conversation, start your reply with @dosu-bot. |
Question Validation
Question
Hi, on this time, I want to add new relationship to a persisted KnowledgeGraph
It is easy to find about VectorDB, but about KnowledgeGraph is so hard.
I already know how to add new relationship to knowledgeGraphIndex class, but I cannot find
how to add new relationship to a persisted KG
To load persisted KG, I used this code
storage_context = StorageContext.from_defaults(
docstore = SimpleDocumentStore.from_persist_dir(persist_dir=os.getcwd() + "/kg/graph/test"),
vector_store = SimpleVectorStore.from_persist_dir(persist_dir=os.getcwd() + "/kg/graph/test"),
index_store = SimpleIndexStore.from_persist_dir(persist_dir=os.getcwd() + "/kg/graph/test"))
from llama_index import load_index_from_storage, load_indices_from_storage, load_graph_from_storage
index = load_index_from_storage(storage_context)
indices = load_indices_from_storage(storage_context)
graph = load_graph_from_storage(storage_context)
Can I apply upsert method on index? or graph?
Thanks!
The text was updated successfully, but these errors were encountered: