langchain-ai / langchain

🦜🔗 Build context-aware reasoning applications
https://python.langchain.com
MIT License
92.5k stars 14.8k forks source link

How to use RetrievalQAWithSourcesChain with a custom prompt #16356

Closed levalencia closed 4 months ago

levalencia commented 8 months ago

Checked other resources

Example Code

        prompt_template = """Use the following pieces of context to answer the question at the end.  Try to answer in a structured way. Write your answer in HTML format but do not include ```html ```. Put words in bold that directly answer your question.
        If you don't know the answer, just say 'I am sorry I dont know the answer to this question or you dont have access to the files needed to answer the question.' Don't try to make up an answer.

        {summaries}

        Question: {question}.
        """

        PROMPT = PromptTemplate(
            template=prompt_template, input_variables=["summaries", "question"]
        )

        memory = ConversationBufferWindowMemory(
            k=5, memory_key="chat_history", return_messages=True, output_key="answer"
        )

        for i in range(0, int(len(chat_history) / 2)):
            memory.save_context(
                {"input": chat_history[i * 2]}, {"answer": chat_history[(i * 2) + 1]}
            )

        chain = RetrievalQAWithSourcesChain.from_chain_type(llm=llm,
                                                            chain_type="stuff",
                                                            retriever=retriever,
                                                            memory= memory,
                                                            chain_type_kwargs={
                                                                "prompt": PromptTemplate(
                                                                    template=PROMPT,
                                                                    input_variables=["summaries", "question"],
                                                                ),
                                                            },
                                                        )

Description

I want to use RetrievalQAWithSourcesChain to generate an answer and the relevant sources from the retriever. However with this code I am getting this error:

. System.Private.CoreLib: Result: Failure Exception: KeyError: 'template'

prompt.py", line 146, in template_is_valid values["template"], values["template_format"]

System Info

langchain==0.1.0 langsmith==0.0.80 langchainhub==0.1.14 langchain-community==0.0.12 openai==1.7.2 azure-identity == 1.13.0 azure-core ==1.28.0

Related Components

dosubot[bot] commented 8 months ago

🤖

Hello again @levalencia! Nice to see you back in the LangChain repo. I appreciate your thorough detailing of the issue, it really helps to understand the context. I'm diving into the problem you've described and will be back with a comprehensive response soon.

dosubot[bot] commented 8 months ago

🤖

It seems like you're encountering a KeyError for 'template' because the PromptTemplate object is not being initialized correctly. In your code, you're passing the PromptTemplate object itself as the template, instead of a string.

Here's the corrected part of your code:

PROMPT = PromptTemplate(
    template=prompt_template, input_variables=["summaries", "question"]
)

chain = RetrievalQAWithSourcesChain.from_chain_type(llm=llm,
                                                    chain_type="stuff",
                                                    retriever=retriever,
                                                    memory= memory,
                                                    chain_type_kwargs={
                                                        "prompt": PROMPT,
                                                    },
                                                )

In this corrected code, PROMPT is a PromptTemplate object that is initialized with prompt_template (a string) as the template and ["summaries", "question"] as the input variables. This PROMPT object is then passed as the prompt argument when initializing RetrievalQAWithSourcesChain.

Please note that the PromptTemplate object expects a string as the template, and this string should contain placeholders for the input variables. In your case, the placeholders are {summaries} and {question}.

For more information about the PromptTemplate object and how it works, you can refer to the PromptTemplate documentation.

If you're still encountering issues after making these changes, please provide more details about the error and I'll be glad to help further.

Sources


This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

If you want to continue the conversation, start your reply with @dosu-bot.