使用 LCEL 的序列式鏈
你已建立好提示樣板,現在該把一切串起來,包括 LLM,並使用鏈與 LCEL。已為你定義好一個 llm,使用的是 OpenAI 的 gpt-4o-mini 模型。
在最後呼叫鏈的步驟中,你可以自由插入任何想要的活動!如果一時想不到點子,可以嘗試輸入 "play the harmonica"。
本練習屬於課程
使用 LangChain 開發 LLM 應用
練習說明
- 使用 LCEL 建立一個序列式鏈,先將
learning_prompt傳入llm,再把輸出餵給time_prompt後重新送至llm。 - 第一部分應建立一個字典,鍵為
"learning_plan",值為第一段鏈的結果。 - 使用
.invoke()方法呼叫該鏈,並帶入你選擇的活動!
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
learning_prompt = PromptTemplate(
input_variables=["activity"],
template="I want to learn how to {activity}. Can you suggest how I can learn this step-by-step?"
)
time_prompt = PromptTemplate(
input_variables=["learning_plan"],
template="I only have one week. Can you create a concise plan to help me hit this goal: {learning_plan}."
)
# Complete the sequential chain with LCEL
seq_chain = ({"learning_plan": ____ | ____ | ____}
| ____
| ____
| StrOutputParser())
# Call the chain
print(seq_chain.____({"____": "____"}))