顺序链

使用 LangChain 开发 LLM 应用

Jonathan Bennion

AI Engineer & LangChain Contributor

 

一个 AI 旅行指南。

使用 LangChain 开发 LLM 应用

 

有人向 AI 旅行指南询问巴黎的活动。

使用 LangChain 开发 LLM 应用

 

AI 为此人提供多项巴黎活动建议。

使用 LangChain 开发 LLM 应用

 

此人告知 AI 想去卢浮宫与凯旋门。

使用 LangChain 开发 LLM 应用

 

AI 旅行指南为所选活动生成行程。

使用 LangChain 开发 LLM 应用

 

此旅行行程工作流是顺序问题的示例。

使用 LangChain 开发 LLM 应用

顺序链

  • 输出 → 输入
destination_prompt = PromptTemplate(
    input_variables=["destination"],
    template="I am planning a trip to {destination}. Can you suggest some activities to do there?"
)

activities_prompt = PromptTemplate( input_variables=["activities"], template="I only have one day, so can you create an itinerary from your top three activities: {activities}." )
llm = ChatOpenAI(model="gpt-4o-mini", api_key=openai_api_key)
seq_chain = ({"activities": destination_prompt | llm | StrOutputParser()}
| activities_prompt | llm | StrOutputParser())
使用 LangChain 开发 LLM 应用
print(seq_chain.invoke({"destination": "Rome"}))
- 上午:
1. 早起参观斗兽场,可参加导览了解其历史与意义。
2. 之后前往罗马广场与帕拉蒂诺山,继续探索古罗马遗迹。

- 午餐:
3. 在历史中心附近的餐馆享用意式午餐。

- 下午:
4. 参观梵蒂冈城:圣彼得大教堂、梵蒂冈博物馆与西斯廷礼拜堂。
5. 漫步罗马街头,打卡万神殿、特雷维喷泉与纳沃纳广场等地标。
- 晚上:

6. 在公园放松,如波各赛庄园或橙园,远离喧嚣。
7. 在本地餐馆悠闲用晚餐,继续品尝意式美食与冰淇淋。
使用 LangChain 开发 LLM 应用

Passons à la pratique !

使用 LangChain 开发 LLM 应用

Preparing Video For Download...