Skip to main content
LibreChat is joining ClickHouse to power the open-source Agentic Data Stack 🎉 Learn more
LibreChat

内存配置

概述

memory 对象允许您为应用程序配置对话记忆和个性化功能。此配置控制系统如何记忆和个性化对话,包括令牌限制、消息上下文窗口以及基于代理的记忆处理。

示例

memory:
  disabled: false
  validKeys: ['user_preferences', 'conversation_context', 'personal_info']
  tokenLimit: 2000
  charLimit: 10000
  maxInputTokens: 12000
  personalize: true
  messageWindowSize: 5
  agent:
    provider: 'openAI'
    model: 'gpt-4'
    instructions: 'You are a helpful assistant that remembers user preferences and context.'
    model_parameters:
      temperature: 0.7
      max_tokens: 1000

disabled

KeyTypeDescriptionExample
disabledBoolean设置为 true 时禁用记忆功能。禁用后,系统将不会存储或使用对话记忆。disabled: false

默认值: false

memory:
  disabled: true

validKeys

KeyTypeDescriptionExample
validKeysArray of Strings指定哪些键对于内存存储是有效的。这有助于控制哪些类型的信息可以存储在内存中。validKeys: ["user_name", "preferences", "上下文"]

默认: 无限制(所有密钥均有效)

memory:
  validKeys:
    - 'user_preferences'
    - 'conversation_context'
    - 'personal_information'
    - 'learned_facts'

tokenLimit

KeyTypeDescriptionExample
tokenLimitNumber设置可用于内存存储和处理的最大令牌数。tokenLimit: 2000

默认: 无限制

memory:
  tokenLimit: 2000

charLimit

KeyTypeDescriptionExample
charLimitNumber设置单个记忆条目允许的最大字符数。这可以防止过大的记忆负载影响性能或超出 API 限制。charLimit: 10000

默认值: 10000

memory:
  charLimit: 10000

maxInputTokens

KeyTypeDescriptionExample
maxInputTokensNumber设置在进行记忆提取前发送给自动记忆代理的最近聊天 token 的最大数量。长输入会从开头处截断,以保留最新的上下文。maxInputTokens: 12000

默认值: 12000

memory:
  maxInputTokens: 12000

personalize

KeyTypeDescriptionExample
personalizeBoolean当设置为 true 时,允许用户选择开启或关闭记忆功能。用户可以在聊天界面中切换记忆功能的开关。当设置为 false 时,记忆功能将被完全禁用。personalize: true

默认值: true

memory:
  personalize: false

messageWindowSize

KeyTypeDescriptionExample
messageWindowSizeNumber指定包含在记忆上下文窗口中的最近消息数量。messageWindowSize: 5

默认值: 5

memory:
  messageWindowSize: 10

agent

KeyTypeDescriptionExample
agentObject | Union配置负责内存处理的 agent。可以是现有 agent 的 ID 引用,也可以是完整的 agent 配置。agent: { provider: "openAI", model: "gpt-4" }

agent 字段支持两种不同的配置格式:

按 ID 获取 Agent

当你拥有一个预配置的 agent 时,你可以通过其 ID 来引用它:

memory:
  agent:
    id: 'memory-agent-001'

自定义 Agent 配置

若需更多控制权,您可以定义完整的智能体配置:

memory:
  agent:
    provider: 'openAI'
    model: 'gpt-4'
    instructions: 'You are a memory assistant that helps maintain conversation context and user preferences.'
    model_parameters:
      temperature: 0.3
      max_tokens: 1500
      top_p: 0.9

Agent 配置字段

当使用自定义智能体配置时,可以使用以下字段:

provider (必填)

KeyTypeDescriptionExample
providerString指定用于记忆代理的 AI 提供商。可以是内置提供商(例如 "openAI"、"anthropic"、"google")或自定义 endpoint 名称。provider: "openAI"

model (必填)

KeyTypeDescriptionExample
modelString指定用于记忆处理的模型。model: "gpt-4"

instructions (可选)

KeyTypeDescriptionExample
instructionsString用于替换设置和/或删除记忆的默认指令的自定义指令。主要应在需要特定信息处理的 validKeys 时使用。instructions: "Only store user preferences and facts when explicitly mentioned."

model_parameters (可选)

KeyTypeDescriptionExample
model_parametersObject传递给模型以微调其行为的附加参数。值必须是字符串、数字或布尔值。model_parameters: { temperature: 0.7 }

完整配置示例

这是一个展示所有内存配置选项的综合示例:

version: 1.3.10
cache: true
 
memory:
  disabled: false
  validKeys:
    - 'user_preferences'
    - 'conversation_context'
    - 'learned_facts'
    - 'personal_information'
  tokenLimit: 3000
  charLimit: 10000
  maxInputTokens: 12000
  personalize: true
  messageWindowSize: 8
  agent:
    provider: 'openAI'
    model: 'gpt-4'
    instructions: |
      Store memory using only the specified validKeys. For user_preferences: save 
      explicitly stated preferences about communication style, topics of interest, 
      or workflow preferences. For conversation_context: save important facts or 
      ongoing projects mentioned. For learned_facts: save objective information 
      about the user. For personal_information: save only what the user explicitly 
      shares about themselves. Delete outdated or incorrect information promptly.
    model_parameters:
      temperature: 0.2
      max_tokens: 2000
      top_p: 0.8
      frequency_penalty: 0.1

使用自定义 endpoint

Memory 功能支持自定义 endpoint。使用自定义 endpoint 时,provider 字段必须与自定义 endpoint 的 name 完全匹配。包含环境变量和用户占位符的自定义请求头会被正确解析。

 
endpoints:
    custom:
        - name: 'Custom Memory Endpoint'
           apiKey: 'dummy'
           baseURL: 'https://api.gateway.ai/v1'
           headers:
             x-gateway-api-key: '${GATEWAY_API_KEY}'
             x-gateway-virtual-key: '${GATEWAY_OPENAI_VIRTUAL_KEY}'
             X-User-Identifier: '{{LIBRECHAT_USER_EMAIL}}'
             X-Application-Identifier: 'LibreChat - Test'
             api-key: '${TEST_CUSTOM_API_KEY}'
           models:
             default:
               - 'gpt-4o-mini'
               - 'gpt-4o'
             fetch: false
 
memory:
  disabled: false
  tokenLimit: 3000
  maxInputTokens: 12000
  personalize: true
  messageWindowSize: 10
  agent:
    provider: 'Custom Memory Endpoint'
    model: 'gpt-4o-mini'

注意事项

  • Memory 功能增强了对话的连续性和个性化。
  • personalize 为 true 时,用户会在聊天界面中获得一个开关,用于控制内存使用。
  • Token limits 有助于控制内存使用和处理成本
  • maxInputTokens 限制了发送给自动记忆代理的聊天上下文,而 tokenLimit 则控制已存储记忆的使用量
  • 有效的密钥提供了对可存储信息的细粒度控制
  • 自定义 instructions 会替换默认的内存处理指令,并应与 validKeys 一起使用。
  • Agent 配置允许自定义记忆处理行为
  • 当禁用时,无论其他设置如何,所有记忆功能都将关闭。
  • 消息窗口大小会影响内存更新时所考虑的近期上下文数量

这篇指南怎么样?