从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

相关推荐
PBitW几秒前
git 中容易遗忘的点 (三) 🚀🚀🚀
前端·git·面试
名字还没想好☜2 分钟前
React setState 连续调用「丢更新」排查:批量更新与函数式更新
前端·javascript·react.js·react·usestate
tachibana24 分钟前
hot100 数组中的第K个最大元素(215)
java·数据结构·算法·leetcode
bitbrowser6 分钟前
Facebook 验证反复回到原页面,应该从哪里排查
运维·服务器·facebook
our_times9 分钟前
2026年Java开发者破局指南:Spring AI 2.0 与 Agent 开发实战
java·人工智能·spring
come1123417 分钟前
Vue 2 与 Vue 3 语法区别及使用技巧
前端·javascript·vue.js
腻害兔29 分钟前
【若依项目-产品经理视角】RuoYi-Vue-Pro 源码拆解:ERP 企业资源模块,一个轻量级进销存的完整实现?
前端·javascript·vue.js·人工智能·前端框架·产品经理·ai编程
虹科网络安全41 分钟前
艾体宝新闻|从 SQL 注入到服务器接管:CVE-2026-57517 暴露 Web 管理面板的供应链与安全编码风险
服务器·前端·sql
糖果店的幽灵1 小时前
langgraph的四种state解析
java·前端·javascript·langgraph
杨充1 小时前
4.接口而非实现编程
java·后端·架构