AI创想

标题: LangGraph实践:文本摘要智能体 [打印本页]

作者: 创想小编    时间: 5 小时前
标题: LangGraph实践:文本摘要智能体
作者:CSDN博客
个人使用LangGraph框架实现的一个多角色的文本摘要智能体示例。
1、流程设计

(, 下载次数: 0)


2、状态

状态是状态图中节点之间传递消息的数据结构
  1. from typing import TypedDict
  2. # 定义状态类型
  3. class AgentState(TypedDict):
  4.     task_id: str         # 任务ID
  5.     original_text: str   # 原始文本
  6.     abstract_text: str   # 摘要文本
  7.     polish_text: str     # 修改文本
  8.     check_result: int    # 审查结果
  9.     check_reason: str    # 审查理由
复制代码
3、节点

图中主要包含四个节点,每个节点代表一个角色,各自负责完成对应的工作,共同组成了一个多角色的智能体系统。
3.1、abstractNode
  1. from messageState.agentState import AgentState
  2. from config.promptConfig import abstract_prompt
  3. from util.llm import call_llm
  4. def abstract_node(state: AgentState) -> AgentState:
  5.     task_id = state['task_id']
  6.     print(f"任务ID为{task_id},进入摘要节点")
  7.     original_text = state['original_text']
  8.     if original_text is None:
  9.         print("原文为空")
  10.     else:
  11.         system_prompt = abstract_prompt
  12.         user_prompt = f"需要生成摘要的原文为{original_text}"
  13.         abstract_text = call_llm(user_prompt,system_prompt)
  14.         state['abstract_text'] = abstract_text
  15.     return state
复制代码
3.2、polishNode
  1. from messageState.agentState import AgentState
  2. from config.promptConfig import polish_prompt
  3. from util.llm import call_llm
  4. def polish_node(state: AgentState) -> AgentState:
  5.     task_id = state['task_id']
  6.     print(f"任务ID为{task_id},进入润色节点")
  7.     abstract_text = state['abstract_text']
  8.     if abstract_text is None:
复制代码
原文地址:https://blog.csdn.net/weixin_44907479/article/details/147097227




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