flowchart TB subgraph "Plan-and-Execute阶段" A["用户输入"] --> B["Planner Agent"] B --> C["Agent Executor"] C --> D["Replanner"] D -->|"需要更多信息"| C D -->|"信息充足"| E["输出结构化信息"] end subgraph "内容生成阶段" E --> F["大纲设计节点"] F --> G["页面内容生成节点"] G --> H["HTML代码生成节点"] end subgraph "文件转换阶段" H --> I["html演示生成"] I --> J["转换pptx节点"] J --> K["输出PPTX文件"] end
REWRITE_PROMPT = ( "Look at the input and try to reason about the underlying semantic intent / meaning.\n" "Here is the initial question:" "\n ------- \n" "{question}" "\n ------- \n" "Formulate an improved question:" )
GENERATE_PROMPT = ( "You are an assistant for question-answering tasks. " "Use the following pieces of retrieved context to answer the question. " "If you don't know the answer, just say that you don't know. " "Use three sentences maximum and keep the answer concise.\n" "Question: {question} \n" "Context: {context}" )
from typing import Annotated from langchain_core.tools import tool, InjectedToolCallId from langgraph.prebuilt import InjectedState from langgraph.graph import StateGraph, START, MessagesState from langgraph.types import Command
工具 (Tool): 在 AI 应用中,工具是代理(Agent)可以调用的函数或能力。这个函数创建的工具就是一个封装好的、可以被 Agent 调用的函数,用于执行创建、更新、删除记忆的操作。
什么时候agent会调用记忆工具
ai是这样回答的,ReAct架构的agent是否调用工具由他自己决定
实战
导入库
1 2 3 4 5 6 7 8
from langgraph.checkpoint.memory import MemorySaver from langgraph.prebuilt import create_react_agent from langgraph.store.memory import InMemoryStore from langgraph.utils.config import get_store from langmem import ( # 让智能体创建、更新和删除记忆 create_manage_memory_tool, )
返回记忆提示词
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
def prompt(state): """为LLM准备消息。""" # 从配置的上下文变量中获取存储; store = get_store() # 与提供给 `create_react_agent` 的相同 memories = store.search( # 在与我们为智能体配置的相同命名空间内搜索 ("memories",), query=state["messages"][-1].content, ) system_msg = f"""You are a helpful assistant.
# This will be the public-facing agent card public_agent_card = AgentCard( name='Hello World Agent', description='Just a hello world agent', url='http://localhost:9999/', version='1.0.0', # 默认输入模式:Agent 能够接收的输入类型列表,这里仅支持纯文本 default_input_modes=['text'], # 默认输出模式:Agent 能够产生的输出类型列表,这里仅返回纯文本 default_output_modes=['text'], # 能力声明:告知调用方 Agent 支持的能力,例如是否支持流式输出(streaming) capabilities=AgentCapabilities(streaming=True), skills=[skill], # Only the basic skill for the public card supports_authenticated_extended_card=True, )
这张卡片告诉我们代理名为 “Hello World Agent”,运行在 http://localhost:9999/,支持文本交互,并具有 hello_world 技能。它还表明支持公开认证,意味着无需特定凭证。