12分钟阅读Evergreen Team

AI代理记忆管理与上下文持久化2026:构建有记忆的代理

掌握AI代理记忆管理和上下文持久化。学习如何构建跨会话保持上下文、从交互中学习并持续改进的代理。

AI代理记忆管理与上下文持久化2026

记忆管理和上下文持久化已成为2026年构建有效AI代理的关键挑战。虽然大型语言模型提供了强大的推理能力,但它们默认是无状态的——每次对话都从头开始,没有之前交互的记忆。这种限制严重制约了AI代理在现实应用中的有用性,因为在这些应用中,连续性、个性化和学习至关重要。

AI代理的记忆架构

有效的AI代理记忆系统通常在多个层次上运行:工作记忆用于当前对话或任务的短期上下文;情景记忆记录特定的过去交互、对话和事件;语义记忆存储从交互中学到的通用知识和事实;程序记忆存储学到的技能、工作流和程序;用户记忆存储关于特定用户的个性化信息。

代码示例 1:多层记忆系统

// Multi-layered memory system for AI agents
import { MemoryManager } from '@ai-agents/memory';
import { VectorStore } from '@ai-agents/vector';

class AgentMemorySystem {
  constructor() {
    this.workingMemory = new WorkingMemory({
      maxSize: 100, // tokens
      strategy: 'sliding-window'
    });
    
    this.episodicMemory = new EpisodicMemory({
      vectorStore: new VectorStore({
        model: 'text-embedding-3-large',
        dimensions: 1536
      }),
      retention: '90d'
    });
    
    this.semanticMemory = new SemanticMemory({
      vectorStore: new VectorStore({
        model: 'text-embedding-3-large',
        dimensions: 1536
      }),
      consolidation: 'daily'
    });
    
    this.userMemory = new UserMemory({
      storage: 'persistent',
      encryption: true
    });
  }

  async addToMemory(interaction, userId) {
    // Add to working memory
    await this.workingMemory.add(interaction);
    
    // Store in episodic memory
    await this.episodicMemory.store({
      timestamp: new Date(),
      userId,
      content: interaction,
      context: await this.getCurrentContext()
    });
    
    // Extract and store semantic knowledge
    const knowledge = await this.extractKnowledge(interaction);
    if (knowledge) {
      await this.semanticMemory.store({
        content: knowledge,
        source: interaction,
        timestamp: new Date()
      });
    }
    
    // Update user-specific memory
    await this.userMemory.update(userId, interaction);
  }

  async retrieveContext(query, userId) {
    // Get working memory
    const working = await this.workingMemory.getRecent(10);
    
    // Retrieve relevant episodic memories
    const episodic = await this.episodicMemory.search(query, {
      limit: 5,
      userId,
      timeRange: '30d'
    });
    
    // Retrieve relevant semantic knowledge
    const semantic = await this.semanticMemory.search(query, {
      limit: 10,
      minRelevance: 0.8
    });
    
    // Get user-specific context
    const userContext = await this.userMemory.get(userId, query);
    
    return {
      working,
      episodic,
      semantic,
      userContext
    };
  }

  async extractKnowledge(interaction) {
    // Use AI to extract generalizable knowledge
    const prompt = `
      Extract generalizable knowledge from this interaction:
      
      ${interaction}
      
      If there are general facts, preferences, or patterns that would be
      useful in future interactions, extract them. Otherwise return null.
    `;
    
    const result = await this.model.complete(prompt);
    return result.knowledge;
  }
}

// Usage
const memory = new AgentMemorySystem();

// During conversation
await memory.addToMemory(
  "User prefers concise responses and is working on a React project",
  "user_123"
);

// Retrieve context for new query
const context = await memory.retrieveContext(
  "How do I optimize this component?",
  "user_123"
);

console.log('Retrieved context:', context);

代码示例 2:会话连续性

// Conversation continuity across sessions
import { SessionManager } from '@ai-agents/sessions';
import { MemorySystem } from '@ai-agents/memory';

class ConversationContinuity {
  constructor() {
    this.sessionManager = new SessionManager({
      storage: 'redis',
      ttl: '30d'
    });
    
    this.memory = new MemorySystem();
  }

  async startSession(userId, sessionId) {
    // Load previous session if exists
    const previousSession = await this.sessionManager.getLastSession(userId);
    
    if (previousSession) {
      // Restore context from previous session
      const context = await this.memory.retrieveContext(
        previousSession.lastQuery,
        userId
      );
      
      // Initialize new session with context
      const newSession = await this.sessionManager.create({
        userId,
        sessionId,
        context,
        continuedFrom: previousSession.sessionId
      });
      
      return {
        session: newSession,
        continuity: {
          previousSession: previousSession.sessionId,
          context: context,
          summary: await this.generateSummary(previousSession)
        }
      };
    }
    
    // New user or no previous session
    return {
      session: await this.sessionManager.create({ userId, sessionId }),
      continuity: null
    };
  }

  async continueConversation(sessionId, message) {
    const session = await this.sessionManager.get(sessionId);
    
    // Add to working memory
    await this.memory.workingMemory.add(message);
    
    // Retrieve relevant context
    const context = await this.memory.retrieveContext(message, session.userId);
    
    // Generate response with full context
    const response = await this.generateResponse(message, context);
    
    // Update session
    await this.sessionManager.update(sessionId, {
      lastMessage: message,
      lastResponse: response,
      lastQuery: message,
      messageCount: session.messageCount + 1
    });
    
    // Store in long-term memory
    await this.memory.addToMemory(
      { user: message, assistant: response },
      session.userId
    );
    
    return response;
  }

  async generateSummary(session) {
    // Use AI to summarize previous session
    const prompt = `
      Summarize this previous conversation in 2-3 sentences:
      
      ${JSON.stringify(session.messages)}
      
      Focus on:
      1. Main topics discussed
      2. Key decisions or outcomes
      3. Unresolved questions
    `;
    
    return await this.model.complete(prompt);
  }
}

// Usage
const continuity = new ConversationContinuity();

// User returns after some time
const result = await continuity.startSession('user_123', 'session_456');

if (result.continuity) {
  console.log('Welcome back! Last time we discussed:', result.continuity.summary);
}

// Continue conversation
const response = await continuity.continueConversation(
  'session_456',
  "Let's continue working on that React component"
);

配置示例

# Memory System Configuration
# agent-memory-config.yml
memory:
  working:
    max_tokens: 4000
    strategy: sliding-window
    compression: true
    
  episodic:
    enabled: true
    vector_store:
      type: pinecone
      index: agent-episodes
      dimensions: 1536
      model: text-embedding-3-large
    retention: 90d
    consolidation: weekly
    
  semantic:
    enabled: true
    vector_store:
      type: pinecone
      index: agent-knowledge
      dimensions: 1536
      model: text-embedding-3-large
    consolidation: daily
    min_relevance: 0.8
    
  user:
    enabled: true
    storage: postgresql
    encryption: true
    fields:
      - preferences
      - history
      - communication_style
      - technical_level

retrieval:
  strategy: hybrid
  weights:
    working: 0.3
    episodic: 0.3
    semantic: 0.3
    user: 0.1
  max_tokens: 8000
  
learning:
  enabled: true
  knowledge_extraction: true
  pattern_recognition: true
  feedback_integration: true

代码示例 3:从交互学习

// Learning from interactions
import { LearningEngine } from '@ai-agents/learning';
import { MemorySystem } from '@ai-agents/memory';

class AgentLearningSystem {
  constructor() {
    this.learning = new LearningEngine({
      model: 'gpt-4-turbo',
      strategies: ['pattern-recognition', 'feedback-integration']
    });
    
    this.memory = new MemorySystem();
  }

  async learnFromInteraction(interaction, feedback) {
    // Analyze interaction for learnable patterns
    const analysis = await this.learning.analyze({
      interaction,
      feedback,
      context: await this.memory.getCurrentContext()
    });
    
    // Extract lessons
    const lessons = [];
    
    // Learn from positive feedback
    if (feedback.rating >= 4) {
      const patterns = await this.learning.extractSuccessPatterns(analysis);
      lessons.push(...patterns);
    }
    
    // Learn from negative feedback
    if (feedback.rating <= 2) {
      const improvements = await this.learning.extractImprovements(analysis);
      lessons.push(...improvements);
    }
    
    // Store lessons in semantic memory
    for (const lesson of lessons) {
      await this.memory.semanticMemory.store({
        type: 'learned_lesson',
        content: lesson,
        source: interaction.id,
        confidence: feedback.rating / 5,
        timestamp: new Date()
      });
    }
    
    // Update user model
    if (feedback.preferences) {
      await this.memory.userMemory.updatePreferences(
        interaction.userId,
        feedback.preferences
      );
    }
    
    return {
      lessonsLearned: lessons.length,
      patterns: lessons,
      updatedUserModel: true
    };
  }

  async applyLearnings(query, userId) {
    // Retrieve relevant learnings
    const learnings = await this.memory.semanticMemory.search(query, {
      type: 'learned_lesson',
      limit: 10,
      minConfidence: 0.7
    });
    
    // Get user preferences
    const preferences = await this.memory.userMemory.getPreferences(userId);
    
    // Apply learnings to response generation
    return {
      learnings,
      preferences,
      applyToPrompt: (prompt) => {
        let enhancedPrompt = prompt;
        
        // Add relevant learnings
        if (learnings.length > 0) {
          enhancedPrompt += '\n\nApply these learnings:\n';
          learnings.forEach(l => {
            enhancedPrompt += `- ${l.content}\n`;
          });
        }
        
        // Add user preferences
        if (preferences) {
          enhancedPrompt += '\n\nUser preferences:\n';
          Object.entries(preferences).forEach(([key, value]) => {
            enhancedPrompt += `- ${key}: ${value}\n`;
          });
        }
        
        return enhancedPrompt;
      }
    };
  }
}

// Usage
const learning = new AgentLearningSystem();

// After interaction with feedback
await learning.learnFromInteraction(
  {
    id: 'interaction_123',
    userId: 'user_456',
    query: 'Explain async/await',
    response: 'Async/await is...',
    context: { /* ... */ }
  },
  {
    rating: 5,
    comment: 'Great explanation, very clear!',
    preferences: {
      explanation_style: 'concise',
      technical_level: 'intermediate'
    }
  }
);

// Apply learnings to future interactions
const context = await learning.applyLearnings(
  'How do promises work?',
  'user_456'
);

console.log('Applied learnings:', context.learnings.length);

代码示例 4:记忆整合

// Memory consolidation and optimization
import { MemoryConsolidator } from '@ai-agents/consolidation';

class MemoryConsolidationSystem {
  constructor() {
    this.consolidator = new MemoryConsolidator({
      model: 'gpt-4-turbo',
      strategies: ['summarization', 'deduplication', 'abstraction']
    });
  }

  async consolidateMemories() {
    // Consolidate episodic memories
    const episodes = await this.memory.episodicMemory.getOlderThan('7d');
    
    const consolidated = [];
    for (const episode of episodes) {
      // Summarize old episodes
      const summary = await this.consolidator.summarize(episode);
      consolidated.push({
        type: 'consolidated_episode',
        content: summary,
        sourceEpisodes: [episode.id],
        timestamp: new Date()
      });
    }
    
    // Store consolidated memories
    await this.memory.semanticMemory.storeBatch(consolidated);
    
    // Remove old episodic memories
    await this.memory.episodicMemory.removeOlderThan('7d');
    
    // Deduplicate semantic memories
    const duplicates = await this.consolidator.findDuplicates(
      await this.memory.semanticMemory.getAll()
    );
    
    for (const dupGroup of duplicates) {
      // Merge duplicates
      const merged = await this.consolidator.merge(dupGroup);
      
      // Remove originals
      await this.memory.semanticMemory.removeBatch(dupGroup.map(d => d.id));
      
      // Store merged version
      await this.memory.semanticMemory.store(merged);
    }
    
    return {
      episodesConsolidated: consolidated.length,
      duplicatesRemoved: duplicates.length,
      memorySaved: this.calculateMemorySaved(consolidated, duplicates)
    };
  }

  async optimizeRetrieval() {
    // Analyze retrieval patterns
    const patterns = await this.analyzeRetrievalPatterns();
    
    // Optimize vector store based on usage
    await this.memory.semanticMemory.optimize({
      hotTopics: patterns.frequentTopics,
      coldTopics: patterns.rareTopics,
      rebalance: true
    });
    
    // Update retrieval weights based on success
    await this.updateRetrievalWeights(patterns.successRates);
    
    return {
      optimized: true,
      patterns: patterns
    };
  }
}

// Run consolidation daily
async function dailyConsolidation() {
  const system = new MemoryConsolidationSystem();
  
  const result = await system.consolidateMemories();
  console.log(`Consolidated ${result.episodesConsolidated} episodes`);
  console.log(`Removed ${result.duplicatesRemoved} duplicates`);
  console.log(`Memory saved: ${result.memorySaved}MB`);
  
  await system.optimizeRetrieval();
  console.log('Retrieval optimized');
}

// Schedule daily at 2 AM
cron.schedule('0 2 * * *', dailyConsolidation);

总结

记忆管理和上下文持久化对于构建能够保持连续性、从交互中学习并提供个性化体验的有效AI代理至关重要。通过实现复杂的多层记忆系统,开发者可以创建能够记住过去交互、从反馈中学习并持续改进性能的代理。关键组件包括多层架构、会话连续性、从交互学习、记忆整合和个性化。要实施有效的记忆系统,从清晰的架构开始,实现强大的检索机制,并建立持续学习和优化的流程。

相关工具推荐

常见问题

什么是AI代理记忆管理?

AI代理记忆管理是指为AI代理实现记忆系统,使其能够跨会话保持上下文、存储和检索信息、从交互中学习。

AI代理需要哪些类型的记忆?

AI代理通常需要工作记忆、情景记忆、语义记忆、程序记忆和用户记忆。

如何实现上下文持久化?

通过向量数据库、会话管理系统、记忆整合机制和智能检索系统来实现。

记忆系统如何学习?

通过分析用户反馈、识别交互模式、提取可泛化知识,并定期整合和优化记忆来持续学习。

最佳实践是什么?

分层记忆架构、合理的保留策略、记忆整合、保护用户隐私,并持续监控和改进记忆质量。