PromptTemplate 如何与 RetrievalQA 交互?

wha*_*gon 6 langchain py-langchain

我是 LangChain 的新手,我正在尝试创建一个简单的问答机器人(通过文档)。按照他们网站上的文档和指南,我创建了一个简单的工作机器人,但我很难理解代码的某些部分。

template = """Use the following pieces of context to answer the question at the end. 
If you don't know the answer, just say that you don't know, don't try to make up an answer. 
Use three sentences maximum and keep the answer as concise as possible. 
Always say "thanks for asking!" at the end of the answer. 
{context}
Question: {question}
Helpful Answer:"""

QA_CHAIN_PROMPT = PromptTemplate(input_variables=["context", "question"], template=template)

llm = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0)
qa = RetrievalQA.from_chain_type(llm,
                                chain_type='stuff',
                                retriever=vectorstore.as_retriever(),
                                chain_type_kwargs={"prompt": QA_CHAIN_PROMPT})

query = "some query"
print(qa.run(query))
Run Code Online (Sandbox Code Playgroud)

鉴于上面的示例代码,我有一些问题。

  1. 当内部没有传递任何参数时,在提示模板中包含 {context} 和 {question} 有何意义?

  2. chain_type_kwargs={"prompt": QA_CHAIN_PROMPT}实际实现了什么?

  3. 如果我要在提示中包含一个新参数(例如 {name}),我应该在哪里实际传递该参数的值?

ZKS*_*ZKS 1

当内部没有传递任何参数时,在提示模板中包含 {context} 和 {question} 有何意义?

Answer - The context and question placeholders inside the prompt template are meant to be filled in with actual values when you generate a prompt using the template. 
Run Code Online (Sandbox Code Playgroud)

chain_type_kwargs={"prompt": QA_CHAIN_PROMPT} 实际上完成了什么?

Answer - chain_type_kwargs is used to pass additional keyword argument to RetrievalQA. Here you are passing your prompt (QA_CHAIN_PROMPT) as an argument
Run Code Online (Sandbox Code Playgroud)

如果我要在提示中包含一个新参数(例如 {name}),我应该在哪里实际传递该参数的值?

Answer - You can do this by passing placeholder in your prompt template. Your code will look like below, please find my comment inline

        template = """Use the following pieces of context to answer the question at the end. 
        If you don't know the answer, just say that you don't know, don't try to make up an answer. 
        Use three sentences maximum and keep the answer as concise as possible. 
        Always say "thanks for asking!" at the end of the answer. 
        {context}
        Question: {question}
        Name: {name} #This is your additional parameter
        Helpful Answer: "{answer} Thanks for asking!" """
        
        #Adding new parameter to prompt
        QA_CHAIN_PROMPT = PromptTemplate(input_variables=["context", "question", "name", "answer"], template=template)

        #Passing values along with value for new parameter
        prompt = QA_CHAIN_PROMPT(context="Some context here", question="What is the purpose of life?", name="AIBot", answer="The purpose of life is...")

        result = qa.run(prompt)
        print(result)
Run Code Online (Sandbox Code Playgroud)