0

I am implementing this code

class ChromaProductRetriever(BaseRetriever, BaseModel):
    vectorstore: VectorStore
 
    class Config:
        arbitrary_types_allowed = True
 
    def combine_metadata(self, doc) -> str:
        metadata = doc.metadata
        return (
           "Item Name: " + metadata["item_name"] + ". " +
           "Item Description: " + metadata["bullet_point"] + ". " +
           "Item Keywords: " + metadata["item_keywords"] + "."
        )
 
    def get_relevant_documents(self, query):
        docs = []
        for doc in self.vectorstore.similarity_search(query):
            content = self.combine_metadata(doc)
            docs.append(Document(
                page_content=content,
                metadata=doc.metadata
            ))
 
        return docs

But when I instantiate the class

chroma_product_retriever = ChromaProductRetriever(vectorstore=vectorstore)

I get the following error

TypeError: Can't instantiate abstract class ChromaProductRetriever with abstract method aget_relevant_documents

For some reason, the code adds an "a" to the get_relevant_documents method. I'm not too clear on abstract classes or methods so i'm not sure whats going on. I'm using python version 3.7.9. Any help would be great.

user1753640
  • 193
  • 1
  • 2
  • 11
  • Without knowing what exactly `BaseRetriever` and `BaseModel` are, I can only guess that one of those implements the abstractmethod `aget_relevant_documents`, so when subclassing that abstract class you should use the exact same name for the method. – OrOrg Jul 06 '23 at 08:14
  • Thanks, the code for BaseRetriever is here https://api.python.langchain.com/en/latest/_modules/langchain/schema/retriever.html and this is where I confused as I am using the same exact name – user1753640 Jul 06 '23 at 08:24
  • It seems as if there are two abstract methods in that class: `_get_relevant_documents` and `_aget_relevant_documents`, so I suppose you should be implementing those instead. The `get_relevant_documents` and `aget_relevant_documents` methods are already implemented in `BaseRetriever`, and they already make a call to their 'underscored' counterparts. – OrOrg Jul 06 '23 at 08:29
  • OK I figured out what was going on, I just need to add this async def aget_relevant_documents(self, query: str): raise NotImplementedError and it now works, not sure why though – user1753640 Jul 06 '23 at 08:41

0 Answers0