提示樣板與串接
在這個練習中,你會開始使用 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": ____}))