conversation.predict(input="Hi, my name is XiaoMing") print(memory.load_memory_variables({})) conversation.predict(input="What is 1+1?") print(memory.load_memory_variables({}))
{'history': "Human: Hi, my name is XiaoMing\nAI: Hello XiaoMing! It's nice to meet you. How can I assist you today?"} {'history': "Human: Hi, my name is XiaoMing\nAI: Hello XiaoMing! It's nice to meet you. How can I assist you today?\nHuman: What is 1+1?\nAI: 1+1 equals 2. Is there anything else you would like to know?"} ===== {'history': "Human: Hi, my name is XiaoMing\nAI: Hello XiaoMing! It's nice to meet you. How can I assist you today?\nHuman: What is 1+1?\nAI: 1+1 equals 2. Is there anything else you would like to know?\nHuman: Hi\nAI: What's up"}
from langchain.memory import ConversationTokenBufferMemory from langchain.memory import ConversationSummaryBufferMemory
# 创建一个长字符串 schedule = "There is a meeting at 8am with your product team. \ You will need your powerpoint presentation prepared. \ 9am-12pm have time to work on your LangChain \ project which will go quickly because Langchain is such a powerful tool. \ At Noon, lunch at the italian resturant with a customer who is driving \ from over an hour away to meet you to understand the latest in AI. \ Be sure to bring your laptop to show the latest LLM demo."
# 使用对话摘要缓存记忆 llm = ChatOpenAI(temperature=0.0) memory = ConversationSummaryBufferMemory(llm=llm, max_token_limit=100) memory.save_context({"input": "Hello"}, {"output": "What's up"}) memory.save_context({"input": "Not much, just hanging"}, {"output": "Cool"}) memory.save_context( {"input": "What is on the schedule today?"}, {"output": f"{schedule}"} ) print(memory.load_memory_variables({})['history'])
输出
1
System: The human and AI exchange greetings and discuss the day's schedule. The AI informs the human of a morning meeting with the product team, work on the LangChain project, and a lunch meeting with a customer interested in AI. The AI emphasizes the importance of being prepared for the day's events.