4

Langchain have added this function ConversationalRetrievalChain which is used to chat over docs with history. According to their documentation here ConversationalRetrievalChain I need to pass prompts which are instructions to the function. How can i achieve that with this function call?

here is the code

qa = ConversationalRetrievalChain.from_llm(OpenAI(temperature=0), vectorstore.as_retriever(), memory=memory)
jeeves
  • 1,871
  • 9
  • 25

2 Answers2

5

You can pass your prompt in ConversationalRetrievalChain.from_llm() method with the combine_docs_chain_kwargs param. See the below example with ref to your provided sample code:

qa = ConversationalRetrievalChain.from_llm(
    llm=OpenAI(temperature=0),
    retriever=vectorstore.as_retriever(),
    combine_docs_chain_kwargs={"prompt": prompt}
)

If you see the source, the combine_docs_chain_kwargs then pass through the load_qa_chain() with your provided prompt.

0

this code worked for me (Thanks to DennisPeeters) :

general_system_template = r""" 
Given a specific context, please give a short answer to the question, covering the required advices in general and then provide the names all of relevant(even if it relates a bit) products. 
 ----
{context}
----
"""
general_user_template = "Question:```{question}```"
messages = [
            SystemMessagePromptTemplate.from_template(general_system_template),
            HumanMessagePromptTemplate.from_template(general_user_template)
]
qa_prompt = ChatPromptTemplate.from_messages( messages )

return ConversationalRetrievalChain.from_llm(
            llm=ChatOpenAI(
                model_name=self.model_name,
                temperature=self.temperature,
                max_tokens=self.max_tokens,
            ),
            retriever=self.retriever,
            chain_type="stuff",
            verbose=self.verbose,
            , combine_docs_chain_kwargs={'prompt': qa_prompt}
        ) 
smbanaei
  • 1,123
  • 8
  • 14