-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory.py
More file actions
74 lines (66 loc) · 3.14 KB
/
Copy pathmemory.py
File metadata and controls
74 lines (66 loc) · 3.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
from langchain.agents import create_agent
from langchain_community.chat_models.tongyi import ChatTongyi
import os
from langchain.tools import tool
from langchain.agents.middleware import SummarizationMiddleware
from langchain_core.runnables import RunnableConfig
from langgraph.checkpoint.memory import InMemorySaver # Checkpoint
# 工具函数,用于获取天气
@tool
def weather_tool(city: str) -> str:
"""Get the weather for a city."""
return f"it's sunny and 70 degrees in {city}"
# 使用 InMemorySaver,也就是在内存中来保存对话状态。如果你希望持久化状态,可以考虑使用数据库或文件存储。
checkpointer = InMemorySaver()
# 创建聊天模型
llm = ChatTongyi(model="qwen-plus", api_key=os.environ["DASHSCOPE_API_KEY"])
# 创建代理,使用state_schema
agent = create_agent(
model=llm,
tools=[weather_tool],
system_prompt="You are a helpful assistant",
checkpointer=checkpointer, # 使用自定义的 checkpoint
middleware=[SummarizationMiddleware(
model=llm,
trigger=("tokens", 4000), # 设置触发条件,当对话内容的 token 数达到 4000 时触发摘要
keep=("messages", 20) # 保留最后的20条消息
)]
)
config: RunnableConfig = {"configurable": {"thread_id": "1"}}
# 多轮对话示例,每一次invoke都会更新checkpointer状态
state = agent.invoke({"messages": [{"role": "user", "content": "我的名字是韩"}]}, config)
state = agent.invoke({"messages": [{"role": "user", "content": "北京天气怎么样"}]}, config)
state = agent.invoke({"messages": [{"role": "user", "content": "南京呢"}]}, config)
state = agent.invoke({"messages": [{"role": "user", "content": "什么是计算机网络"}]}, config)
state = agent.invoke({"messages": [{"role": "user", "content": "推荐几本计算机网络的课程"}]}, config)
state = agent.invoke({"messages": [{"role": "user", "content": "我应该如何学习计算机网络"}]}, config)
# 查看最后的总结结果
print(state["messages"])
# from langchain.agents import create_agent
# from langchain.agents.middleware import SummarizationMiddleware
# from langgraph.checkpoint.memory import InMemorySaver
# from langchain_core.runnables import RunnableConfig
# from langchain_community.chat_models.tongyi import ChatTongyi
# import os
# checkpointer = InMemorySaver()
# llm = ChatTongyi(model="qwen-plus", api_key=os.environ["DASHSCOPE_API_KEY"])
# agent = create_agent(
# model=llm,
# tools=[],
# middleware=[
# SummarizationMiddleware(
# model=llm,
# trigger=("tokens", 4000),
# keep=("messages", 20)
# )
# ],
# checkpointer=checkpointer,
# )
# config: RunnableConfig = {"configurable": {"thread_id": "1"}}
# agent.invoke({"messages": "hi, my name is bob"}, config)
# agent.invoke({"messages": "write a short poem about cats"}, config)
# agent.invoke({"messages": "now do the same but for dogs"}, config)
# agent.invoke({"messages": "I like dogs and cats"}, config)
# final_response = agent.invoke({"messages": "what's my name?"}, config)
# print(f"fianal response:{final_response}")
# final_response["messages"][-1].pretty_print()