创建检索提示
在任何 RAG 实现中,检索提示都是关键组成部分。本练习中,您将为检索链创建一个聊天提示模板,并测试 LLM 是否只能使用提供的上下文来回应。
已为您定义好一个可用的 llm。
本练习是课程的一部分
使用 LangChain 的 Retrieval Augmented Generation (RAG)
练习说明
- 将字符串
prompt转换为可复用的聊天提示模板。 - 创建一个 LCEL 链,将提示模板与提供的
llm集成。 - 对给定输入调用
chain,检查模型是否仅依据提供的上下文进行回应。
交互式实操练习
通过完成这段示例代码来试试这个练习。
prompt = """
Use the only the context provided to answer the following question. If you don't know the answer, reply that you are unsure.
Context: {context}
Question: {question}
"""
# Convert the string into a chat prompt template
prompt_template = ____
# Create an LCEL chain to test the prompt
chain = ____ | ____
# Invoke the chain on the inputs provided
print(chain.____({"context": "DataCamp's RAG course was created by Meri Nova and James Chapman!", "question": "Who created DataCamp's RAG course?"}))