从runtime获取信息

from langchain.agents import create_agent, AgentState

from langchain.agents.middleware import dynamic_prompt, ModelRequest

from langchain_core.tools import BaseTool, tool

from typing import TypedDict

import json

from langgraph.checkpoint.memory import InMemorySaver

from langgraph.prebuilt import ToolRuntime

from init_llm import deepseek_llm

class CustomAgentState(AgentState):

user_id:str

hobby:list

other_info:dict

@tool

def get_user_info(runtime:ToolRuntime) -> str:

"""获取用户信息"""

print("runtime",runtime)

return runtime.state"user_id",runtime.state"hobby",runtime.state"other_info"

agent=create_agent(

model=deepseek_llm,

tools=get_user_info,

checkpointer=InMemorySaver(),

state_schema=CustomAgentState

)

config = {"configurable": {"thread_id": "1"}}

resp=agent.invoke({"messages":{"role":"user","content":"我叫张三,你是谁"}},config=config)

print(resp)

print(resp"messages"-1.content)

config = {"configurable": {"thread_id": "2"}}

resp=agent.invoke({"messages":{"role":"user","content":"我叫张三,你是谁?"}

,

"user_id":"user02",

"hobby":"篮球","足球",

"other_info":{"age":25,"gender":"男"}},config=config)

print(resp)

print(resp"messages"-1.content)

resp=agent.invoke({"messages":{"role":"user","content":"给我查询我的信息"}},config=config)

print(resp)

print(resp"messages"-1.content)

print(agent.get_state(config=config))

我了解到你的信息了!以下是你的一些基本情况:

  • **用户ID**:user02

  • **年龄**:25岁

  • **性别**:男

  • **兴趣爱好**:篮球 🏀、足球 ⚽

Process finished with exit code 0

相关推荐
Setsuna_F_Seiei7 小时前
前端转型 Agent 开发 05 之 Agent Hooks 与 Checkpointer(让 Agent 从全自动转变人为可掌控)
前端·agent·ai编程
步行cgn7 小时前
Spring c 命名空间注入详解
java·后端·spring
明月_清风7 小时前
Maven 到底是什么?一篇文章搞懂 Java 项目构建与依赖管理
java·后端·maven
比兔代理8 小时前
正向代理与反向代理技术辨析,为什么代理 IP 属于正向代理
服务器·网络·http·ip
aramae8 小时前
MySQL复合查询(8)
java·c语言·开发语言·后端·算法
Rain的Java大神之路8 小时前
如何快速上传10G文件
java·spring boot·redis·后端·mysql·spring cloud·面试
百万蹄蹄向前冲9 小时前
风扇转了一晚上MVP专家团翻车事故
前端·人工智能
默_笙9 小时前
🏛 给 AI 配一间办公室:Harness Engineering 六大模块与它的实现
前端·javascript
Wang's Blog9 小时前
Java 接入Redis: 通用命令与键管理
java·服务器·redis
Wang's Blog9 小时前
Java 接入Redis: 列表集合与有序集合操作命令
java·服务器·redis