提示模板与链式调用
在本练习中,您将开始使用 LangChain 的两个核心组件:提示模板 和 链!
完成本练习所需的类(包括 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": ____}))