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

AI Agent开发学习系列 - LangGraph(4): 有多个输入的Graph(练习解答)

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

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

  1. 输入你的名字
  2. 输入一个数字列表,如[1,2,3,4]
  3. 输入一个运算符号
  4. 如果运算符号为+,则返回Hi there <名字>, your answer is <数字列表每项之号>
  5. 如果运算符号为*,则返回Hi there <名字>, your answer is <数字列表每项之积>
  6. 如果运算符号为以上其他,则返回Hi there <名字>! Your operation is not supported!

解答:

from typing import TypedDict, List
from langgraph.graph import StateGraph
import mathclass AgenState(TypedDict):name: strvalues: List[int]operation: strresult: strdef process_values(state: AgenState) -> AgenState:"""This function handles multiple different inputs"""if state["operation"] == "+":state["result"] = f"Hi there {state["name"]}, your answer is {sum(state["values"])}"elif state["operation"] == "*":state["result"] = f"Hi there {state["name"]}, your aswer is {math.prod(state["values"])}"else:state["result"] = f"Hi there {state["name"]}! Your operation is not supported!"return stategraph = StateGraph(AgenState)graph.add_node("processor", process_values)
graph.set_entry_point("processor")
graph.set_finish_point("processor")app = graph.compile()from IPython.display import Image, display
display(Image(app.get_graph().draw_mermaid_png()))answers = app.invoke({"values": [1, 2, 3, 4], "name": "Alex", "operation": "+"})
print(answers["result"])

运行结果:
在这里插入图片描述

Hi there Alex, your answer is 10
http://www.xdnf.cn/news/16784.html

相关文章:

  • 设计模式篇:在前端,我们如何“重构”观察者、策略和装饰器模式
  • Android 运行 deno 的新方法 (3): Termux 胖喵安初
  • vue3pinia
  • 深度学习核心:卷积神经网络 - 原理、实现及在医学影像领域的应用
  • vue3 新手学习入门
  • Elasticsearch 混合检索一句 `retriever.rrf`,把语义召回与关键词召回融合到极致
  • Agents-SDK智能体开发[5]之集成MCP进阶
  • 【vue】创建响应式数据ref和reactive的区别
  • Ⅹ—6.计算机二级综合题23---26套
  • 两个服务之间的大规模数据推送
  • TOGAF指南1
  • Thymeleaf 模板引擎原理
  • 网站QPS多少才算高并发
  • c++和python联合编程示例
  • 5-EP4CE10F17C8-引脚配置
  • MySQL 高并发下如何保证事务提交的绝对顺序?
  • 向量投影计算,举例说明
  • 幂等性校验(订单重复提交问题)
  • X2Doris是SelectDB可视化数据迁移工具,安装与部署使用手册,轻松进行大数据迁移
  • Spring AI MCP:解锁大模型应用开发新姿势
  • 关于继承的一些知识(C++)
  • 层次聚类:无需“猜”K值,如何让数据自己画出“家族图谱”?
  • 深度学习-梯度爆炸与梯度消失
  • 笔试——Day25
  • 深度解读 CSGHub:开源协议、核心功能与产品定位
  • Java:JWT 从原理到高频面试题解析
  • Agents-SDK智能体开发[2]之工具调用
  • Web开发-PHP应用TP框架MVC模型路由访问模版渲染安全写法版本漏洞
  • Mysql group by
  • 机器学习第二课之逻辑回归(二)LogisticRegression