从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

相关推荐
白露与泡影2 小时前
Java 8老系统旁路接入AI Gateway:不升级JDK也能用AI
java·人工智能·gateway
Misnearch2 小时前
Java中创建Map的做法
java·hashmap
心之伊始2 小时前
Spring Cloud Gateway RequestRateLimiter 实战:Redis 令牌桶限流从配置到本地压测验证
java·架构·源码分析·csdn
团象科技2 小时前
从出海业务落地视角观察 海外服务器跑开源软件的实操逻辑演变
运维·服务器·开源软件
木头羊oll2 小时前
Uniapp 与 H5 在 App 端的交互
前端·javascript·html
可别3902 小时前
Vue 极简实现语音实时转写(录音转文字,低网络依赖、开箱即用)
前端·javascript·vue.js
snow@li2 小时前
nginx:详解与速查表 / Nginx = 反向代理 + 负载均衡 + 静态服务器 + HTTP 缓存 / 请求分发、静态加速、上线不中断
linux·服务器·nginx
AI人工智能+电脑小能手2 小时前
【大白话说Java面试题 第105题】【并发篇】第5题:说一下 synchronized 关键字的底层原理?
java·开发语言·面试
yueping22 小时前
【无标题】
java·开发语言