File size: 1,004 Bytes
d1d1d6a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from langchain.schema.retriever import BaseRetriever
from langchain_core.callbacks import CallbackManagerForRetrieverRun
from langchain_pinecone.vectorstores import Pinecone
from langchain.schema import Document
from pydantic import PrivateAttr

class AskMeAboutRagRetriever(BaseRetriever):
    vectorstore: Pinecone = PrivateAttr()

    def __init__(self, vectorstore: Pinecone, **data):
        super().__init__(**data)
        self.vectorstore = vectorstore
        
    def _get_relevant_documents(self, query: str, *, run_manager: CallbackManagerForRetrieverRun):
        retrieved_docs = self.vectorstore.as_retriever().get_relevant_documents(query)
        docs = [
            Document(
                page_content= str(i+1) + ".)" + "Title = " + "(" + doc.metadata.get('title') + ")" + " " + "Content = " + "(" + doc.page_content + ")",
                metadata={"title": doc.metadata.get('title')}
            )
            for i, doc in enumerate(retrieved_docs)
        ]
        return docs