
转变 AI 开发:我在 14 岁时与 Langgraph 的旅程,构建一个反思型 AI 代理
起点:LangChain Agents
最初,我爸爸给了我一个项目,其中涉及使用 LangChain 加载和检索数据。我们使用 LangChain 加载器从 PDF 和 GitHub 存储库中导入内容。然后,使用 LangChain agent,我们将这些加载的内容连接到一个 AI 模型,该模型可以根据检索到的信息动态地回答问题。
以下是我爸爸给我的一个基本的 LangChain agent 设置片段:
## Load PDF documents
pdf_files = glob("./pdfs/*.pdf")
pdf_documents = []
for pdf_file in pdf_files:
loader = PyPDFLoader(pdf_file)
pdf_documents.extend(loader.load())
## Load GitHub repository documents
github_loader = GithubFileLoader(
repo="langchain-ai/langgraph",
branch="main",
github_api_url="https://api.github.com",
access_token="<YOUR_ACCESS_TOKEN>",
file_filter=lambda file_path: file_path.startswith("examples/multi_agent") and file_path.endswith(".ipynb")
)
github_documents = github_loader.load()
## Initialize LangChain agent
tools = [
Tool(
name="pdf_search",
func=lambda q: pdf_retriever.invoke(q),
description="Searches PDF documents for answers."
),
Tool(
name="github_search",
func=lambda q: github_retriever.invoke(q),
description="Searches GitHub repo examples."
)
]
agent = initialize_agent(
tools,
llm,
agent=AgentType.OPENAI_FUNCTIONS,
verbose=True,
)
这个基本的 agent 运行良好,但它有局限性——它并没有真正地反映或改进其答案。
选择 LangGraph 架构
为了升级我的聊天机器人,我选择了 LangGraph 的高级架构,称为 “具有反射和批判的多 agent 系统”。这个系统更智能,因为它包含自我改进和反射能力。这意味着我的聊天机器人现在可以检查和增强自己的答案。
1. 构建更智能的 Supervisor
第一步是创建一个更智能的 supervisor,根据问题类型选择最佳 agent(PDF 或 GitHub)。
这是我改进后的 supervisor 函数:
## Supervisor decides the best agent based on the question
def supervisor(state: AgentState) -> AgentState:
prompt = ChatPromptTemplate.from_template("""
Choose the agent for answering this question:
- pdf: theory notes or PDF documents
- github: practical code examples
Question: {question}
Answer exactly one word (pdf/github):
""")
decision = llm.invoke(prompt.format(question=state["question"]))
next_agent = decision.content.strip().lower()
return {"next": next_agent if next_agent in ["pdf", "github"] else "pdf"}
2. 反射和批判
接下来,我实现了反射和批判功能,允许聊天机器人评估和改进自己的答案。在最初的回复之后,聊天机器人会自我批判,以确保答案清晰、准确和实用。
以下是我的反射函数的样子:
## Reflection to critique and improve chatbot answers
def reflect(state: AgentState) -> AgentState:
iteration = state.get("iteration", 0) + 1
state["iteration"] = iteration
last_response = state["messages"][-1].content
critique_prompt = f"""
Critique this response:
{last_response}
Provide exactly three improvements:
1. Factual correction or clarification
2. Structure or readability improvement
3. Practical applicability suggestion
"""
critique = llm.invoke(critique_prompt)
refine_prompt = f"""
Original answer:
{last_response}
Critique points:
{critique.content}
Incorporate these improvements into a refined answer.
"""
refined = llm.invoke(refine_prompt)
if refined.content.strip() == last_response.strip() or iteration >= 2:
next_step = "finalize"
else:
next_step = "reflect"
return {"messages": state["messages"] + [AIMessage(refined.content)], "next": next_step}
3. 使用 LangSmith 进行可观察性
然后,我将我的聊天机器人连接到 LangSmith,这是一个强大的工具,可以跟踪聊天机器人的每个动作。使用 LangSmith,我可以实时观察聊天机器人的整个思维过程——从最初的答案到详细的反射。
我学到的
在此过程中,我学到了:
- 语义分块:将大文档拆分成更小、可搜索的部分。
- 向量数据库:有效地存储和检索信息块。
- 反射和批判:自动改进其答案的 AI agent。
- 使用 Docker 进行容器化:使用 Docker 允许我和我爸爸轻松协作并一致地运行我们的聊天机器人,确保我们都在使用相同的库和软件设置,没有任何兼容性问题。
LangSmith 可观察性
通过 LangSmith 观察我的聊天机器人令人兴奋。我可以清楚地看到聊天机器人采取的每个步骤,更好地理解 AI agent 如何思考、反射和改进。
Chatbot Response- Github Agent
LangSmith Traceability with tracking of each Steps
[## GitHub - ishveen-ai/learnaiagents
Contribute to ishveen-ai/learnaiagents development by creating an account on GitHub.
github.com](https://github.com/ishveen-ai/learnaiagents)