从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

相关推荐
贵州诚鑫致达科技有限公司3 分钟前
PVID 是什么?端口参数三身份
服务器·经验分享
骑着蜗牛撵大象3274 分钟前
豆包 Seed 驱动的记账智能体:从拍票抽取到月报生成的全链路落地记录
java·前端·javascript·结构化输出·豆包seed·多模态识别·记账系统
溪语流沙6 分钟前
【Web全栈进阶】前端世界观:现代工具链与框架为什么存在
前端·javascript·vue.js
影视飓风TIM9 分钟前
Linux 进程信号原理与剖析
linux·运维·服务器
憧憬成为java架构高手的小白14 分钟前
langchain4j手敲理解
java·langchain
kyriewen28 分钟前
我逐个测了 JSON.stringify 的 7 个坑:第 1 个就在丢数据
前端·javascript·程序员
iCxhust31 分钟前
先想好如何测试:让测试驱动你的程序设计
运维·服务器·软件工程·编程的艺术
10mAh1 小时前
【Java】ArrayList 遍历删除为什么报错?三种写法与漏删复现
java·开发语言·windows
积流成海1 小时前
用代码实现哈希计算器:Web Crypto API 实战
前端·算法·哈希算法·哈希函数·盘子工具站
Philtell1 小时前
突然连接不上内网服务器的问题
运维·服务器