Git Product home page Git Product logo

Comments (1)

dosubot avatar dosubot commented on June 29, 2024 3

To resolve the RuntimeError related to a duplicated primary key value "Summer founders program" when creating a KnowledgeGraphIndex from documents using the llama_index package, you need to handle duplicates in the document store. You can use the upsert_triplet_and_node method from the KnowledgeGraphIndex class to handle duplicates when adding nodes and triplets.

Here is an example of how to use this method:

from llama_index.legacy.indices.knowledge_graph.base import KnowledgeGraphIndex
from llama_index.legacy.schema import TextNode
from llama_index.legacy.service_context import ServiceContext

# Create an instance of KnowledgeGraphIndex
index = KnowledgeGraphIndex([], service_context=ServiceContext())

# Define your triplets and nodes
triplets = [
    ("foo", "is", "bar"),
    ("hello", "is not", "world"),
    ("Jane", "is mother of", "Bob"),
]
nodes = [TextNode(text=str(tup)) for tup in triplets]

# Add triplets and nodes to the index
for triplet, node in zip(triplets, nodes):
    index.upsert_triplet_and_node(triplet, node)

This method ensures that duplicates are handled appropriately when adding triplets and nodes to the KnowledgeGraphIndex.

In the provided test cases, the upsert_triplet_and_node method is used to handle duplicates effectively. For example:

from unittest.mock import patch
from llama_index.legacy.indices.knowledge_graph.base import KnowledgeGraphIndex
from llama_index.legacy.schema import TextNode
from llama_index.legacy.service_context import ServiceContext

@patch.object(KnowledgeGraphIndex, "_extract_triplets", side_effect=mock_extract_triplets)
def test_build_kg_manual(_patch_extract_triplets, mock_service_context):
    index = KnowledgeGraphIndex([], service_context=mock_service_context)
    tuples = [
        ("foo", "is", "bar"),
        ("hello", "is not", "world"),
        ("Jane", "is mother of", "Bob"),
    ]
    nodes = [TextNode(text=str(tup)) for tup in tuples]
    for tup, node in zip(tuples, nodes):
        index.upsert_triplet_and_node(tup, node)

    # Verify the nodes and triplets
    docstore_nodes = index.docstore.get_nodes(list(index.index_struct.node_ids))
    table_chunks = {n.get_content() for n in docstore_nodes}
    assert len(table_chunks) == 3
    assert "('foo', 'is', 'bar')" in table_chunks
    assert "('hello', 'is not', 'world')" in table_chunks
    assert "('Jane', 'is mother of', 'Bob')" in table_chunks

    # Test inserting the same node twice
    node = TextNode(text=str(("foo", "is", "bar")), id_="test_node")
    index.upsert_triplet_and_node(tup, node)
    index.upsert_triplet_and_node(tup, node)

This test case demonstrates how the upsert_triplet_and_node method can be used to handle duplicates effectively, ensuring that the RuntimeError related to duplicated primary key values is resolved [1][2][3][4].

Details

About Dosu
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.

To continue the conversation, mention @dosu.

from llama_index.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.