从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

相关推荐
程序员黑豆6 小时前
Java 注释详解:单行、多行与文档注释的完整指南
java·前端·ai编程
剪刀石头布啊7 小时前
antd中可编辑表格中巧用useWatch实现联动高性能效果,以及定制延伸学习
前端
油丶酸萝卜别吃7 小时前
前端转全栈学习路线
前端·学习
浮生望8 小时前
前端路由进阶:History API原理与手写HistoryRouter
前端
hey_sml8 小时前
JAVA每日一学---CompletableFuture中thenApply与thenCompose区别详解
java·开发语言
Doraemomo8 小时前
数据结构-环形链表
java·数据结构·链表
陆枫Larry8 小时前
JavaScript 中的竞态是什么,为啥会有竟态?
前端
江畔柳前堤8 小时前
HBM:大语言模型时代的「算力血液」——从内存墙到带宽革命的深度拆解
服务器·人工智能·windows·目标检测·语言模型·自然语言处理·软件工程
萧瑟余晖9 小时前
Java深入解析篇十七之SpringCloud
java·开发语言
是未才10 小时前
从输入 URL 到页面返回:DNS、路由、TLS 与 HTTP 完整链路
java·后端·计算机网络