Find answers from the community

Updated 4 months ago

explain this error : nodes_retrieved = retriever.retrieve(AttributeError: type object 'r

At a glance
The post describes an error message, "AttributeError: type object 'retriever' has no attribute 'retrieve'", which typically means that the object "retriever" does not have a method called "retrieve". A community member provides an example of how to properly create and use a "retriever" object in the context of the LlamaIndex library. The example demonstrates how to define a custom retriever class that inherits from the BaseRetriever class and has a _retrieve method. The community member suggests creating an instance of the custom retriever class and then calling the retrieve method on that instance. Another community member asks how the "retriever" was created, indicating that the error seems unusual.
explain this error :
nodes_retrieved = retriever.retrieve(
AttributeError: type object 'retriever' has no attribute 'retrieve'
k
L
4 comments
Please stand by @tatanfort as I search the knowledge base. I normally take about 20 seconds or so. :wave:
----------------------
The error message you're seeing, AttributeError: type object 'retriever' has no attribute 'retrieve', typically means that the object retriever does not have a method called retrieve.

In the context of LlamaIndex, a retriever should be an instance of a class that inherits from BaseRetriever or one of its subclasses. These classes do have a retrieve method. For example, in the CustomRetriever class () example, the retrieve method is defined as _retrieve.

Here's an example of how a retriever should be defined and used:

```python
from llama_index import QueryBundle
from llama_index.schema import NodeWithScore
from llama_index.retrievers import BaseRetriever, VectorIndexRetriever, KeywordTableSimpleRetriever
from typing import List

class CustomRetriever(BaseRetriever):
"""Custom retriever that performs both semantic search and hybrid search."""

def init(
self,
vector_retriever: VectorIndexRetriever,
keyword_retriever: KeywordTableSimpleRetriever,
mode: str = "AND",
) -> None:
"""Init params."""

self._vector_retriever = vector_retriever
self._keyword_retriever = keyword_retriever
if mode not in ("AND", "OR"):
raise ValueError("Invalid mode.")
self._mode = mode
super().init()

def _retrieve(self, query_bundle: QueryBundle) -> List[NodeWithScore]:
"""Retrieve nodes given query."""

vector_nodes = self._vector_retriever.retrieve(query_bundle)
keyword_nodes = self._keyword_retriever.retrieve(query_bundle)

vector_ids = {n.node.node_id for n in vector_nodes}
keyword_ids = {n.node.node_id for n in keyword_nodes}
combined_dict = {n.node.node_id: n for n in vector_nodes}
combined_dict.update({n.node.node_id: n for n in keyword_nodes})

if self._mode == "AND":
retrieve_ids = vector_ids.intersection(keyword_ids)
else:
retrieve_ids = vector_ids.union(keyword_ids)

retrieve_nodes = [combined_dict[rid] for rid in retrieve_ids]
return retrieve_nodes

Create an instance of CustomRetriever

retriever = CustomRetriever(vector_retriever, keyword_retriever, mode="AND")

Now you can call the retrieve method

nodes_retrieved = retriever.retrieve(query_bundle)
`` Please ensure that your retriever object is an instance of a class that has a retrieve` method. If you're still having trouble, please refer to the LlamaIndex documentation () for more information.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
How did you create the retriever? Pretty weird error
Add a reply
Sign up and join the conversation on Discord