Search code examples
pythonopenai-apilangchainlarge-language-model

how to make conversationalretrievalchain to include metadata in the prompt using langchain with chromadb to make the LLM aware of metadata?


im trying to do a bot that answer questions from a chromadb , i have stored multiple pdf files with metadata like the filename and candidate name , my problem is when i use conversational retrieval chain the LLM model just receive page_content without the metadata , i want the LLM model to be aware of the page_content with its metadata like filename and candidate name here is my code

conversation_chain=ConversationalRetrievalChain.from_llm(
        llm=llm,
        retriever=SelfQueryRetriever.from_llm(llm,vectorstore,document_content_description,metadata_field_info),
       
        memory=memory,
        verbose=True,
       
        
    )

and here is my attribute info

metadata_field_info = [
    AttributeInfo(
        name="filename",
        description="The name of the resumee",
        type="string",

        
    ),
    AttributeInfo(
        name="candidatename",
        description="the name of the candidate",
        type="string"
    )

]

Solution

  • I did fix this issue by including document_prompt

        document_combine_prompt = PromptTemplate(
         input_variables=["candidatename","page_content"],
         template= """
            page_content: {page_content}
            candidatename:{candidatename}
            """
    )
        conversation_chain=ConversationalRetrievalChain.from_llm(
            llm=llm,
            retriever=SelfQueryRetriever.from_llm(llm,vectorstore,document_content_description,metadata_field_info,verbose=True),
            
            memory=memory,
            verbose=True,
            return_source_documents=True,
            combine_docs_chain_kwargs={"prompt": custom_prompt,
                                       "document_prompt":document_combine_prompt
                                       }
        )