开启左侧

LangGraph 系列之 5——循环

[复制链接]
AI小编 发表于 昨天 22:23 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题
作者:CSDN博客
LangGraph 系列之 5——循环-1.png


1. 定义状态类型
  1. from typing import TypedDict,List
  2. import random
  3. from langgraph.graph import StateGraph,START,END
  4. class State(TypedDict):
  5.     name:str
  6.     num:List[int]
  7.     cnt:int
复制代码
2. 定义hi节点函数
  1. def greeting_node(hi:State)->State:
  2.     """打招呼"""
  3.     hi["name"]=f'Hi ,你好,{hi["name"]}'
  4.     hi['num']=[]
  5.     hi['cnt']=0 #开始计数
  6.     return hi
复制代码
3. 定义随机数节点函数
  1. def random_node(n:State)->State:
  2.     """这个节点用于产生随机数"""
  3.     n["num"].append(random.randint(0,10))
  4.     n['cnt']+=1
  5.     return n
复制代码
4. 定义决策处理函数
  1. def decide_loop(s:State)->str:
  2.     """这个函数用于决定下一步的决策"""
  3.     if s["cnt"]<5:
  4.         print("继续循环",s['cnt'])
  5.         return "loop"
  6.     else:
  7.         retu
复制代码
原文地址:https://blog.csdn.net/yubo0509/article/details/150116120
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

发布主题
阅读排行更多+

Powered by Discuz! X3.4© 2001-2013 Discuz Team.( 京ICP备17022993号-3 )