成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

如何讓AI Agent在多輪對話中保持長期記憶?7種關鍵優化方法解析 原創

發布于 2025-6-20 06:40
瀏覽
0收藏

在基于大模型的 Agent 中,長期記憶的狀態維護至關重要,在 OpenAIAI 應用研究主管 Lilian Weng 的 博客《基于大模型的 Agent 構成》中,將記憶視為關鍵的組件之一,下面我將結合 LangChain 中的代碼,分享7 種不同的Agent記憶維護方式在不同場景中的應用。

獲取全量歷史對話

在電信公司的客服聊天機器人場景中,如果用戶在對話中先是詢問了賬單問題,接著又談到了網絡連接問 題,ConversationBufferMemory 可以用來記住整個與用戶的對話歷史,可以幫助 AI 在回答網絡問題時還 記得賬單問題的相關細節,從而提供更連貫的服務。

from langchain.memory import ConversationBufferMemory
memory = ConversationBufferMemory()
memory.save_context({"input": "你好"}, {"output": "怎么了"})
print(memory.load_memory_variables({}))

滑動窗口獲取最近部分對話內容

在一個電商平臺上,如果用戶詢問關于特定產品的問題(如手機的電池續航時間),然后又問到了配送方 式,ConversationBufferWindowMemory 可以幫助AI 只專注于最近的一兩個問題(如配送方式),而不是整個對話歷史,以提供更快速和專注的答復。

from langchain.memory import ConversationBufferWindowMemory
memory = ConversationBufferWindowMemory(k=1)
memory.save_context({"input": "iphone15續航"}, {"output": "續航一般"})
memory.save_context({"input": "配送"}, {"output": "很快"})
# {'history': 'Human: 配送\nAI: 很快'}
print(memory.load_memory_variables({}))

ConversationBufferWindowMemory 這個類在存儲message還是全量存儲的,只是在讀數據的時候只讀k個窗口。

獲取歷史對話中實體信息

在法律咨詢的場景中,客戶可能會提到特定的案件名稱、相關法律條款或個人信息(如“我在去年的交通 事故中受了傷,想了解關于賠償的法律建議”)。 ConversationEntityMemory可以幫助 AI 記住這些關鍵 實體和實體關系細節,從而在整個對話過程中提供更準確、更個性化的法律建議。

llm = ChatOpenAI(temperature=0, model="gpt-4o")


memory = ConversationEntityMemory(
    llm=llm,
    return_messages=True,
)
print(memory.load_memory_variables(inputs={"input": "good!  busy working on Langchain.  lots to do."}))
memory.save_context({"input": "good!  busy working on Langchain.  lots to do."}, {"output": "That sounds like a lot of work!  What kind of things are you doing to make Langchain better?"})
print(memory.load_memory_variables(inputs={"input": "i'm trying to improve Langchain's interfaces, the UX, its integrations with various products the user might want ...  a lot of stuff"}))
memory.save_context(inputs={"input": "i'm trying to improve Langchain's interfaces, the UX, its integrations with various products the user might want ...  a lot of stuff"}, outputs={"output": "that sounds great job"})
print(memory.load_memory_variables(inputs={"input": "what is langchain"}))

在會話過程中,需要從memory load 變量時:

  1. 根據history和用戶的提問(也就是最新一句話)提取實體,注意這里提取的是用戶最新提問的query的實體
  2. 從entity_store這個大字典查詢之前是否存在對應實體的描述信息,如果有對應的描述信息,則把對應的實體和描述信息作為entities字段返回
  3. 如果之前提取了實體,但是最新一句話

當一次會話結束之后,需要save_contexts:

  1. 保存human message和ai message到 messages列表
  2. 因為AI message 可能補充了human 提到的實體信息,所以使用LLM更新當前query提到的實體的描述信息
  3. 如果在當前會話之前提取了實體,但是當前會話只是簡單的問候,那么就不會更新實體的描述信息,本質還是因為實體信息是綁定在當前的query的

利用知識圖譜獲取歷史對話中的實體及其聯系

在醫療咨詢中, 一個病人可能會描述多個癥狀和過去的醫療歷史(如“我有糖尿病史,最近覺得經常口渴 和疲勞”)。 ConversationKGMemory 可以構建一個包含病人癥狀、疾病歷史和可能的健康關聯的知識圖譜,從而幫助 AI 提供更全面和深入的醫療建議。

from langchain_community.memory.kg import ConversationKGMemory

llm = ChatOpenAI(temperature=0, model="gpt-4o")

memory = ConversationKGMemory(llm=llm)
memory.save_context({"input": "say hi to sam"}, {"output": "who is sam"})
memory.save_context({"input": "sam is a friend"}, {"output": "okay"})
print(memory.load_memory_variables({"input": "who is sam"}))  # {'history': 'On Sam: Sam is a friend.'}
print(memory.get_current_entities("what's Sams favorite color?"))  # ['Sam']

當每次會話結束的時候,會利用LLM從history中抽取知識的三元組,并存儲到NetworkxEntityGraph圖對象中。

當新的會話開始需要從memory load數據的時候,從當前Query中利用LLM抽取實體,并從NetworkxEntityGraph圖對象中獲取這個實體的knowledge, 把所有實體的知識信息返回。

對歷史對話進行階段性總結摘要

在一系列的教育輔導對話中,學生可能會提出不同的數學問題或理解難題(如“我不太理解二次方程的求解方法”)。 ConversationSummaryMemory 可以幫助 AI 總結之前的輔導內容和學生的疑問點,以便在隨后的輔導中提供更針對性的解釋和練習.

llm = ChatOpenAI(temperature=0, model="gpt-4o")
memory = ConversationSummaryMemory(llm=llm)
memory.save_context({"input": "hi"}, {"output": "whats up"})
print(memory.load_memory_variables({}))  # {'history': 'The human greets the AI with "hi," and the AI responds with "what\'s up."'}

ConversationSummaryMemory 有個buffer的屬性,存放summary信息。每次會話結束的時候,用新生成的會話和之前的summary生成新的summary存儲在buffer屬性中。

ConversationSummaryMemory 特點:

  1. 只存儲摘要,不存儲原始對話
  2. 每次對話后都會更新摘要
  3. 適合長期對話,節省 token
  4. 可能丟失細節信息

需要獲取最新對話,又要兼顧較早歷史對話

在處理一個長期的技術問題時(如軟件故障排查),用戶可能會在多次對話中提供不同的錯誤信息和反 饋。ConversationSummaryBufferMemory 可以幫助 AI 保留最近幾次交互的詳細信息,同時提供歷史問 題處理的摘要,以便于更有效地識別和解決問題。

llm = ChatOpenAI(temperature=0, model="gpt-4o")
memory = ConversationSummaryBufferMemory(llm=llm, max_token_limit=10)
memory.save_context({"input": "hi"}, {"output": "whats up"})
memory.save_context({"input": "not much you"}, {"output": "not much"})
# {'history': 'System: The human greets with "hi." The AI responds with "what\'s up," and the human replies with "not much, you?"\nAI: not much'}
print(memory.load_memory_variables({}))

ConversationSummaryBufferMemory 會暫存不會超過max_token_limit的會話歷史,當歷史長度超過這個大小的時候,會截斷之前的會話歷史以使得會話現存的會話長度不超過max_token_limit,并把截斷的之前的會話歷史和之前的moving_summary_buffer更新moving_summary_buffer信息。

ConversationSummaryBufferMemory 特點:

  1. 存儲最近的對話 + 早期對話的摘要
  2. 結合了完整對話和摘要的優勢
  3. 保持最近對話的細節,壓縮早期對話
  4. 適合中等長度的對話

基于向量檢索對話信息

用戶可能會對特定新聞事件提出問題,如“最近的經濟峰會有什么重要決策?? VectorStoreRetrieverMemory 能夠快速從大量歷史新聞數據中檢索出與當前問題最相關的信息,即使這些信息在整個對話歷史中不是最新的,也能提供及時準確的背景信息和詳細報道。

import faiss

from langchain.docstore import InMemoryDocstore
from langchain.vectorstores import FAISS


embedding_size = 1536 # Dimensions of the OpenAIEmbeddings
index = faiss.IndexFlatL2(embedding_size)
embedding_fn = OpenAIEmbeddings().embed_query
vectorstore = FAISS(embedding_fn, index, InMemoryDocstore({}), {})


# the vector lookup still returns the semantically relevant information
retriever = vectorstore.as_retriever(search_kwargs=dict(k=1))
memory = VectorStoreRetrieverMemory(retriever=retriever)

# When added to an agent, the memory object can save pertinent information from conversations or used tools
memory.save_context({"input": "My favorite food is pizza"}, {"output": "thats good to know"})
memory.save_context({"input": "My favorite sport is soccer"}, {"output": "..."})
memory.save_context({"input": "I don't the Celtics"}, {"output": "ok"})

總結

在實際項目中,記憶方案的選擇需要綜合考量以下因素:

  1. 業務場景的信息生命周期要求
  2. 對話復雜度和上下文依賴程度
  3. 系統資源與響應延遲限制

在實際項目里,我常跟團隊說:"別一上來就整最復雜的,先想清楚你的AI到底需要記住什么。"有時候簡單的滑動窗口就夠用,非得加個知識圖譜反而把簡單問題復雜化。最近我在做一個客服系統,就用了混合記憶的方案,效果還不錯。


本文轉載自???AI 博物院??? 作者:longyunfeigu

?著作權歸作者所有,如需轉載,請注明出處,否則將追究法律責任
收藏
回復
舉報
回復
相關推薦
主站蜘蛛池模板: 国产精品18hdxxxⅹ在线 | 超碰成人免费 | 五月花丁香婷婷 | 美女爽到呻吟久久久久 | 亚洲成人av| 欧美激情va永久在线播放 | 亚洲一区二区三区在线免费 | 中文字幕第一页在线 | 国产婷婷综合 | 91精品国产日韩91久久久久久 | 中文字幕日韩欧美 | 伊人看片| 91久久国产| 中文字幕在线观看精品 | 一区二区三区四区视频 | 91精品在线播放 | 午夜爽爽男女免费观看hd | 一区二区三区在线免费观看 | 国产精品国产三级国产aⅴ无密码 | av入口| 国产成人免费 | 在线区| 九九热精品在线 | 中文字幕伊人 | 国产精品一二三区 | japanhd成人| 欧洲一区二区三区 | 91精品国产综合久久国产大片 | 性生生活大片免费看视频 | 粉嫩一区二区三区性色av | 丁香婷婷在线视频 | 国产成人免费视频网站高清观看视频 | 一区二区三区成人 | 精精国产xxxx视频在线播放 | 特黄特黄a级毛片免费专区 av网站免费在线观看 | 免费一级毛片 | 国产精品一卡二卡三卡 | 日本免费一区二区三区 | 精品国产一区二区三区久久久久久 | 桃色五月 | 午夜无码国产理论在线 |