AI创想
标题:
LangGraph实践:文本摘要智能体
[打印本页]
作者:
创想小编
时间:
5 小时前
标题:
LangGraph实践:文本摘要智能体
作者:CSDN博客
个人使用LangGraph框架实现的一个多角色的文本摘要智能体示例。
1、流程设计
(, 下载次数: 0)
上传
点击文件名下载附件
根据原文生成初步的摘要。
进入文本润色节点对摘要进行润色,增强摘要的可读性和流畅性。
进入文本审查节点对摘要与原文的一致性进行审查
摘要中的信息符合原文,输出结果
摘要中的信息不符合原文,将原文、摘要和摘要中存在的问题都交给文本修改节点进行修改。修改结束以后再进入文本审查节点审查摘要与原文的一致性
2、状态
状态是状态图中节点之间传递消息的数据结构
from typing import TypedDict
# 定义状态类型
class AgentState(TypedDict):
task_id: str # 任务ID
original_text: str # 原始文本
abstract_text: str # 摘要文本
polish_text: str # 修改文本
check_result: int # 审查结果
check_reason: str # 审查理由
复制代码
3、节点
图中主要包含四个节点,每个节点代表一个角色,各自负责完成对应的工作,共同组成了一个多角色的智能体系统。
3.1、abstractNode
from messageState.agentState import AgentState
from config.promptConfig import abstract_prompt
from util.llm import call_llm
def abstract_node(state: AgentState) -> AgentState:
task_id = state['task_id']
print(f"任务ID为{task_id},进入摘要节点")
original_text = state['original_text']
if original_text is None:
print("原文为空")
else:
system_prompt = abstract_prompt
user_prompt = f"需要生成摘要的原文为{original_text}"
abstract_text = call_llm(user_prompt,system_prompt)
state['abstract_text'] = abstract_text
return state
复制代码
3.2、polishNode
from messageState.agentState import AgentState
from config.promptConfig import polish_prompt
from util.llm import call_llm
def polish_node(state: AgentState) -> AgentState:
task_id = state['task_id']
print(f"任务ID为{task_id},进入润色节点")
abstract_text = state['abstract_text']
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