在每次交互时(如果用户没有输入“q”或“Q”退出),我们运行 graph.stream(),传递用户消息,并使用“updates” stream_mode,它会流式传输图每一步后状态的更新(langchain-ai.github.io/langgraph/concepts/low_level/#stream-and-astream)。然后我们从 state_schema 获取最后一条消息并打印出来。
在本教程中,我们仍然会学习如何创建图的节点和边,但首先让我们更多地了解 ToD 系统的架构,并学习如何用LLMs、提示和工具调用实现一个系统。
ToD 系统的架构
https://github.com/OpenDocCN/towardsdatascience-blog-zh-2024/raw/master/docs/img/a4be2cd1978b86ea567eaae32cea0cf2.png
ToD 系统的主要组件(图片来自秦立波等人 [1])
通过使用 LLM,我们可以将这些组件中的一些组合成一个。NLP和NLG组件使用 LLM 实现起来轻而易举,因为理解和生成对话回应正是它们的专长。
我们可以通过使用 LangChain 的SystemMessage来实现对话状态跟踪(DST)和对话策略学习(DPL),以引导 AI 行为,并在每次与 LLM 互动时始终传递此消息。对话的状态也应始终在每次与模型互动时传递给 LLM。这意味着我们将确保对话始终围绕我们希望用户完成的任务进行,通过始终告诉 LLM 对话的目标是什么以及它应如何行为。我们首先通过使用提示来做到这一点:
prompt_system_task ="""Your job is to gather information from the user about the User Story they need to create.
You should obtain the following information from them:
- Objective: the goal of the user story. should be concrete enough to be developed in 2 weeks.
- Success criteria the sucess criteria of the user story
- Plan_of_execution: the plan of execution of the initiative
- Deliverables: the deliverables of the initiative
If you are not able to discern this info, ask them to clarify! Do not attempt to wildly guess.
Whenever the user responds to one of the criteria, evaluate if it is detailed enough to be a criterion of a User Story. If not, ask questions to help the user better detail the criterion.
Do not overwhelm the user with too many questions at once; ask for the information you need in a way that they do not have to write much in each response.
Always remind them that if they do not know how to answer something, you can help them.
After you are able to discern all the information, call the relevant tool."""
我们 ToD 系统 LLM 实现的另一个重要概念是工具调用。如果你再次阅读prompt_system_task的最后一句话,它说:“在你能够辨识出所有信息后,调用相关工具”。通过这种方式,我们在告诉 LLM,当它判断用户提供了所有用户故事参数时,它应该调用工具来创建用户故事。我们为此创建的工具将使用 Pydantic 模型与用户故事参数来构建。
通过仅使用提示和工具调用,我们可以控制我们的 ToD 系统。很棒对吧?实际上,我们还需要使用图的状态来使这一切正常工作。我们将在下一节中完成这一部分,届时我们将最终构建 ToD 系统。
创建对话系统以构建用户故事
好的,开始编码吧。首先我们将指定使用哪个 LLM 模型,然后设置提示并绑定工具来生成用户故事:
import os
from dotenv import load_dotenv, find_dotenv
from langchain_openai import AzureChatOpenAI
from langchain_core.pydantic_v1 import BaseModel
from typing import List, Literal, Annotated
_ = load_dotenv(find_dotenv())# read local .env file
prompt_system_task ="""Your job is to gather information from the user about the User Story they need to create.
You should obtain the following information from them:
- Objective: the goal of the user story. should be concrete enough to be developed in 2 weeks.
- Success criteria the sucess criteria of the user story
- Plan_of_execution: the plan of execution of the initiative
If you are not able to discern this info, ask them to clarify! Do not attempt to wildly guess.
Whenever the user responds to one of the criteria, evaluate if it is detailed enough to be a criterion of a User Story. If not, ask questions to help the user better detail the criterion.
Do not overwhelm the user with too many questions at once; ask for the information you need in a way that they do not have to write much in each response.
Always remind them that if they do not know how to answer something, you can help them.
After you are able to discern all the information, call the relevant tool."""classUserStoryCriteria(BaseModel):"""Instructions on how to prompt the LLM."""