開始使用免費開始

提示樣板與串接

在這個練習中,你會開始使用 LangChain 的兩個核心元件:提示樣板(prompt templates)鏈(chains)

完成本練習所需的類別(包含 ChatOpenAI)都已為你預先載入。

本練習屬於課程

使用 LangChain 開發 LLM 應用

檢視課程

練習說明

  • 將提供的 template 文字轉換為標準(非聊天式)提示樣板。
  • 建立一個鏈,將提示樣板傳入 LLM。
  • 對提供的 question 變數呼叫該鏈。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Create a prompt template from the template string
template = "You are an artificial intelligence assistant, answer the question. {question}"
prompt = PromptTemplate.____(
    template=____
)

llm = ChatOpenAI(model="gpt-4o-mini", api_key='')	

# Create a chain to integrate the prompt template and LLM
llm_chain = ____ | ____

# Invoke the chain on the question
question = "How does LangChain make LLM application development easier?"
print(llm_chain.invoke({"question": ____}))
編輯並執行程式碼