当前位置: 首页 > news >正文

AI Agent开发学习系列 - LangGraph(6): 有多个节点的Sequential Graph(练习解答)

在AI Agent开发学习系列 - LangGraph(5): 有多个节点的Sequential Graph中,我们学习了如何创建有多个节点的Sequential Graph。为了巩固学习,我们来做一个练习。

用LangGraph创建如下图的一个Agent:
在这里插入图片描述
要求:

  1. 输入你的名字
  2. 输入你的年龄
  3. 输入你的技能列表,如[Java, Python, JS]
  4. 在第一个节点中,返回结果<名字> welcmoe to the system!
  5. 在第二个节点中,返回第一个节点的结果加上You are <年龄> years old!
  6. 第三个节点中中,返回第二个节点的结果加上You have skills in <技能>, <技能> and <技能>.
  7. 最终输出图和最终消息结果。

解答:

from typing import TypedDict
from langgraph.graph import StateGraphclass AgentState(TypedDict):name: strage: strskills: listmessage: strdef first_note(state: AgentState) -> AgentState:"""This is the first node of our sequence"""state["message"] = f"{state["name"]}, welcmoe to the system! "return statedef second_node(state: AgentState) -> AgentState:"""This is the second node of our sequence"""state["message"] = state["message"] + f"You are {state["age"]} years old! "return statedef third_node(state: AgentState) -> AgentState:"""This is the third node of our sequence"""state["message"] = state["message"] + f"You have skills in {", ".join(state["skills"][0:-1])} and {state["skills"][-1]}."return stategraph = StateGraph(AgentState)graph.add_node("first_note", first_note)
graph.add_node("second_node", second_node)
graph.add_node("third_node", third_node)graph.set_entry_point("first_note")
graph.add_edge("first_note", "second_node")
graph.add_edge("second_node", "third_node")
graph.set_finish_point("third_node")app = graph.compile()from IPython.display import Image, display
display(Image(app.get_graph().draw_mermaid_png()))result = app.invoke({"name": "Alex", "age": "41", "skills": ["Python", "AI Agent", "LangGraph"]})
print(result["message"])

输出结果:
在这里插入图片描述

Alex, welcmoe to the system! You are 41 years old! You have skills in Python, AI Agent and LangGraph.
http://www.lryc.cn/news/608175.html

相关文章:

  • 深入理解C++中的Lazy Evaluation:延迟计算的艺术
  • LangGraph认知篇-Command函数
  • UDP通信中BIND端口号的作用解析,LOCALPORT的关系解析
  • 搜索与图论(最小生成树 二分图)
  • 【Django】-5- ORM的其他用法
  • 企业级单点登录(SSO)技术详解:从原理到安全实践
  • 前端与后端部署大冒险:Java、Go、C++三剑客
  • ARM Cortex-M异常处理高级特性详解
  • 集成电路学习:什么是HAL硬件抽象层
  • 【DL学习笔记】计算图与自动求导
  • 紧急救援!Oracle RAC节点驱逐元凶:私网Packet Reassembles Failed“包重组失败”一招救命
  • linux ssh公钥移除办法
  • MySQL PostgreSQL JDBC URL 配置允许批量操作
  • SM2国密算法的大数运算原理详解
  • 牛客 - 旋转数组的最小数字
  • 【PCL点云库:下】
  • 详解Python标准库之互联网数据处理
  • 一个物理引擎仿真器(mujoco这种)的计算流程
  • 回溯 79 单词搜索一波三折想和
  • 中科院开源HYPIR图像复原大模型:1.7秒,老照片变8K画质
  • 深入剖析Nacos:云原生架构的基石
  • JVM 02 垃圾回收
  • 【LeetCode 热题 100】(三)滑动窗口
  • file命令libmagic、python的cchardet库使用、自定义magic文件的使用
  • 【Spring Boot 快速入门】五、文件上传
  • Python 入门指南:从零基础到环境搭建
  • Qt 信号和槽正常连接返回true,但发送信号后槽函数无响应问题【已解决】
  • AI原生数据库:告别SQL的新时代来了?
  • 飞书推送工具-自动化测试发送测试报告一种方式
  • Linux 动静态库的制作和使用