Langchain: how to get complete prompt `RetrievalQA.from_chain_type`

5.4k views Asked by At

How do I see the complete prompt (retrieved_relevant_context + question) after qa_chain is run?

qa_chain = RetrievalQA.from_chain_type(
    llm,
    retriever=vectordb.as_retriever(),
)
result = qa_chain({"query": question})

I am referring to the course from deeplearning.ai enter image description here

2

There are 2 answers

0
Sergei G. On

You can see your complete prompt by setting the verbose parameter to TRUE as mentionned below.

qa_chain = RetrievalQA.from_chain_type(
    llm=llm_model,
    chain_type='stuff',
    retriever=vectorsdb.as_retriever(search_kwargs={"k":6}),
    verbose=True,
    chain_type_kwargs={
        "verbose": True,
        "prompt": prompt
    }

)
0
j3ffyang On

Another 2 options to print out the full chain, including prompt

  1. Enable verbose and debug
from langchain.globals import set_verbose, set_debug
set_debug(True)
set_verbose(True)
  1. Use StdOutCallbackHandler
from langchain.callbacks import StdOutCallbackHandler
handler = StdOutCallbackHandler()

qa_with_sources_chain = RetrievalQA.from_chain_type(
    llm=llm,
    chain_type="stuff",
    chain_type_kwargs={"prompt": PROMPT},
    retriever=vectorstore.as_retriever(
        # search_type="similarity_score_threshold",
        search_kwargs={'score_threshold': 0.01, "k": 8},
        # search_kwargs={"k": 8}
    ),
    callbacks=[handler],
    # return_source_documents=True
)