Giter Club home page Giter Club logo

Comments (1)

dosubot avatar dosubot commented on June 26, 2024 2

To resolve the KeyError: '166001' encountered when using faiss.IndexIVFPQ, you need to ensure that the key '166001' exists in the nodes_dict of your IndexDict. The error suggests that the key is missing when trying to retrieve node IDs from the nodes_dict.

Here's the relevant part of the code where the error might be occurring:

def _build_node_list_from_query_result(
        self, query_result: VectorStoreQueryResult
    ) -> List[NodeWithScore]:
    if query_result.nodes is None:
        if query_result.ids is None:
            raise ValueError(
                "Vector store query result should return at "
                "least one of nodes or ids."
            )
        assert isinstance(self._index.index_struct, IndexDict)
        node_ids = [
            self._index.index_struct.nodes_dict[idx] for idx in query_result.ids
        ]
        nodes = self._docstore.get_nodes(node_ids)
        query_result.nodes = nodes
    else:
        for i in range(len(query_result.nodes)):
            source_node = query_result.nodes[i].source_node
            if (not self._vector_store.stores_text) or (
                source_node is not None and source_node.node_type != ObjectType.TEXT
            ):
                node_id = query_result.nodes[i].node_id
                if self._docstore.document_exists(node_id):
                    query_result.nodes[i] = self._docstore.get_node(
                        node_id
                    )  # type: ignore[index]

    log_vector_store_query_result(query_result)

    node_with_scores: List[NodeWithScore] = []
    for ind, node in enumerate(query_result.nodes):
        score: Optional[float] = None
        if query_result.similarities is not None:
            score = query_result.similarities[ind]
        node_with_scores.append(NodeWithScore(node=node, score=score))

    return node_with_scores

To fix this issue, you should:

  1. Verify that the key '166001' exists in self._index.index_struct.nodes_dict.
  2. Ensure that the nodes_dict is correctly populated with all necessary keys before querying.

Here is a potential check you can add before accessing the nodes_dict:

node_ids = []
for idx in query_result.ids:
    if idx in self._index.index_struct.nodes_dict:
        node_ids.append(self._index.index_struct.nodes_dict[idx])
    else:
        raise KeyError(f"Key '{idx}' not found in nodes_dict")

This will help you identify missing keys and handle the error more gracefully [1].

Additionally, ensure that the nodes_dict is populated correctly during the construction of the vector store. The nodes_dict in the IndexDict is populated during the _add_nodes_to_index method:

def _add_nodes_to_index(
        self,
        index_struct: IndexDict,
        nodes: Sequence[BaseNode],
        show_progress: bool = False,
        **insert_kwargs: Any,
    ) -> None:
    if not nodes:
        return

    for nodes_batch in iter_batch(nodes, self._insert_batch_size):
        nodes_batch = self._get_node_with_embedding(nodes_batch, show_progress)
        new_ids = self._vector_store.add(nodes_batch, **insert_kwargs)

        if not self._vector_store.stores_text or self._store_nodes_override:
            for node, new_id in zip(nodes_batch, new_ids):
                node_without_embedding = node.copy()
                node_without_embedding.embedding = None

                index_struct.add_node(node_without_embedding, text_id=new_id)
                self._docstore.add_documents(
                    [node_without_embedding], allow_update=True
                )
        else:
            for node, new_id in zip(nodes_batch, new_ids):
                if isinstance(node, (ImageNode, IndexNode)):
                    node_without_embedding = node.copy()
                    node_without_embedding.embedding = None

                    index_struct.add_node(node_without_embedding, text_id=new_id)
                    self._docstore.add_documents(
                        [node_without_embedding], allow_update=True
                    )

This method ensures that the nodes_dict is populated correctly during the index construction process [2][3].

To continue talking to Dosu, 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.