AI创想

标题: LangGraph 系列之 5——循环 [打印本页]

作者: AI小编    时间: 昨天 22:23
标题: LangGraph 系列之 5——循环
作者:CSDN博客
(, 下载次数: 0)


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




欢迎光临 AI创想 (https://www.llms-ai.com/) Powered by Discuz! X3.4