開始使用免費開始

建立檢索提示

在任何 RAG 實作中,檢索提示(retrieval prompt)都是關鍵的一環。這個練習中,你會為檢索鏈建立聊天提示範本,並測試 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?"}))
編輯並執行程式碼