更新时间:2024-10-16 GMT+08:00
分享

History

History缓存,用于存储历史对话信息,辅助模型理解上下文信息,历史消息对有固定窗口、消息摘要等策略。

  • 初始化:消息记录支持不同的存储方式, 如内存、DCS(Redis)和RDS(Sql)。
    from pangukitsappdev.memory.sql_message_history import SQLMessageHistory
    from pangukitsappdev.api.memory.cache.cache_config import ServerInfoSql, ServerInfoRedis
    from pangukitsappdev.api.memory.message_history_config import MessageHistoryConfig
    from pangukitsappdev.memory.redis_message_history import RedisMessageHistory
    from langchain.memory import ChatMessageHistory
    # 内存型:
    chat_message = ChatMessageHistory()
    
    # Redis:
    redis_chat_message = RedisMessageHistory(msg_history_config=MessageHistoryConfig(store_name="redis",
                                                                                     server_info=ServerInfoRedis(env_prefix="sdk.memory.dcs"),
                                                                                     session_tag="test-memory-0624"))
    # Sql:
    sql_chat_message = SQLMessageHistory(msg_history_config=MessageHistoryConfig(store_name="sql",
                                                                                 server_info=ServerInfoSql(env_prefix="sdk.memory.rds"),
                                                                                 session_tag="test-memory-0624"))
  • 添加、查找、删除数据。
    # 更新数据
    chat_message.add_ai_message("i am ai.")
    chat_message.add_user_message("i am tester.")
    # 查找数据
    contents = [msg.content for msg in chat_message.messages]
    # 清理
    chat_message.clear()
  • 消息策略(windows-size):将固定轮次历史对话信息,作为历史上下文信息。
    from langchain.memory import ChatMessageHistory, ConversationBufferWindowMemory
    from pangukitsappdev.api.memory.cache.cache_config import ServerInfoRedis
    # 历史消息存储
    redis_chat_message = RedisMessageHistory(msg_history_config=MessageHistoryConfig(store_name="redis",
                                                                                     server_info=ServerInfoRedis(env_prefix="sdk.memory.dcs"),
                                                                                     session_tag="test-memory"))
    # 固定窗口策略: langchain.memory.buffer_window.ConversationBufferWindowMemory
    memory = ConversationBufferWindowMemory(k=3, chat_memory=redis_chat_message)
    
    memory.chat_memory.add_user_message("This is me, the human")
    memory.chat_memory.add_ai_message("This is me, the AI")
    print(memory.chat_memory.messages)
    memory.chat_memory.clear()
  • 消息策略(信息摘要)
    将历史消息进行摘要后,作为历史上下文信息。
    from pangukitsappdev.memory.conversation_summory_memory import ConversationSummaryBufferMemory
    from pangukitsappdev.api.skill.base import SimpleSkill
    from pangukitsappdev.prompt.prompt_tmpl import PromptTemplates
    # 历史消息存储
    redis_chat_message = RedisMessageHistory(msg_history_config=MessageHistoryConfig(store_name="redis",
                                                                                     server_info=ServerInfoRedis(env_prefix="sdk.memory.dcs"),
                                                                                     session_tag="test-memory"))
    # 摘要策略
    memory = ConversationSummaryBufferMemory(summary_skill=SimpleSkill(PromptTemplates.get("memory_summary"), LLMs.of("pangu")),
                                             chat_memory=redis_chat_message)
    
    memory.chat_memory.add_user_message("This is me, the human")
    memory.chat_memory.add_ai_message("This is me, the AI")
    print(memory.chat_memory.messages)
    memory.chat_memory.clear()

相关文档