from langchain_core.messages.utils import trim_messages, count_tokens_approximately from langchain.chat_models import init_chat_model from langgraph.checkpoint.memory import InMemorySaver from langgraph.graph import StateGraph, START, MessagesState
config = {"configurable": {"thread_id": "1"}} graph.invoke({"messages": "hi, my name is bob"}, config) graph.invoke({"messages": "write a short poem about cats"}, config) graph.invoke({"messages": "now do the same but for dogs"}, config) final_response = graph.invoke({"messages": "what's my name?"}, config)
print(final_response["messages"][-1].content) # 官方示例输出:Your name is Bob, as you mentioned when you first introduced yourself.
import math import types import uuid from langchain.chat_models import init_chat_model from langchain.embeddings import init_embeddings from langgraph.store.memory import InMemoryStore from langgraph_bigtool import create_agent as create_bigtool_agent from langgraph_bigtool.utils import convert_positional_only_function_to_tool
# 把 Python math 库函数批量转成工具 all_tools = [] for function_name indir(math): function = getattr(math, function_name) ifnotisinstance(function, types.BuiltinFunctionType): continue tool = convert_positional_only_function_to_tool(function)//内置函数转化为agent可调用的工具描述 if tool: all_tools.append(tool)
tool_registry = {str(uuid.uuid4()): tool for tool in all_tools}
# 把“工具描述”写入带向量索引的 Store embeddings = init_embeddings("openai:text-embedding-3-small") store = InMemoryStore( index={ "embed": embeddings, "dims": 1536,//向量维度 "fields": ["description"], } ) for tool_id, tool in tool_registry.items(): store.put( ("tools",), tool_id, {"description": f"{tool.name}: {tool.description}"}, )