Find answers from the community

Updated 3 months ago

is there a way to update a knowledge graph without remaking it completely?

is there a way to update a knowledge graph without remaking it completely?
k
C
5 comments
Please stand by @Cukon as I search the knowledge base. I normally take about 20 seconds or so. :wave:
----------------------
Yes, there is a way to update a Knowledge Graph in LlamaIndex without remaking it completely. You can add new nodes and relationships to the graph manually using the upsert_triplet_and_node method. This method allows you to add new triplets (subject, relationship, object) and associate them with a specific node.

Here is an example of how you can do this:

Plain Text
# for node 0
node_0_tups = [
    ("author", "worked on", "writing"),
    ("author", "worked on", "programming"),
]
for tup in node_0_tups:
    index.upsert_triplet_and_node(tup, nodes[0])

# for node 1
node_1_tups = [
    ("Interleaf", "made software for", "creating documents"),
    ("Interleaf", "added", "scripting language"),
    ("software", "generate", "web sites"),
]
for tup in node_1_tups:
    index.upsert_triplet_and_node(tup, nodes[1])


In this example, new triplets are being added to nodes 0 and 1. The upsert_triplet_and_node method updates the existing node if it already exists, or inserts a new node if it doesn't.

You can find more details in the LlamaIndex documentation ().
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
@kapa.ai how would do this from new files?
Let me check the knowledge base for you. One moment! :books:
----------------------
If you have new data in files that you want to add to your existing Knowledge Graph in LlamaIndex, you can use the refresh() function. This function allows you to index only the new documents without rebuilding the entire index from scratch.

Here is an example of how you can do this:

Plain Text
# Assuming you have a list of new documents
new_documents = [...]

# Refresh the index with the new documents
refreshed_or_inserted = index.refresh(new_documents)


In this example, new_documents is a list of your new documents. The refresh() function will return a boolean array, indicating which documents in the input were refreshed or inserted.

If a document's content has changed, the update() function is called, which removes and re-inserts the document from the index.

You can find more details in the LlamaIndex documentation ().
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
Add a reply
Sign up and join the conversation on Discord