Skip to main content

TDAILongTermMemory

使用TDAI服务实现的企业级长期记忆,提供基于云的持久化存储和基本语义搜索功能。

概述

TDAILongTermMemory提供可靠的长期记忆功能,包括:
  • 基于策略的组织:按类型和用途对记忆进行分类
  • 云端持久化:使用TDAI基础设施的可靠存储
  • 基本语义搜索:基于内容的检索功能
  • 批量操作:高效的批量记忆操作
  • 企业集成:与现有TDAI部署兼容

配置

import { TDAILongTermMemory, MemoryClient } from '@ag-kit/agents/storage';

// 使用TDAI客户端初始化
const client = new MemoryClient({
  apiKey: 'your-tdai-api-key',
  baseUrl: 'https://api.tdai.com'
});

const longTermMemory = new TDAILongTermMemory({
  client: client,
  sessionId: 'user-session-123'
});

配置参数

config
TDAILongTermMemoryConfig
required
TDAI配置对象

核心接口方法

TDAILongTermMemory实现了标准的长期记忆接口方法。有关完整的API文档,包括record()recordBatch()retrieve()delete()update()clear()和其他方法,请参阅Memory Service API参考

使用示例

基本记忆操作

// 使用策略分类记录记忆
await longTermMemory.record({
  id: 'mem-1',
  content: 'User is interested in machine learning and AI',
  strategy: 'interests',
  metadata: {
    confidence: 0.8,
    source: 'conversation',
    tags: ['ml', 'ai', 'technology']
  },
  createdAt: new Date()
});

// 批量记录多个记忆
const memories = [
  { 
    id: 'mem-2',
    content: 'User prefers email notifications', 
    strategy: 'preferences', 
    metadata: { source: 'settings' },
    createdAt: new Date()
  },
  { 
    id: 'mem-3',
    content: 'User works in healthcare industry', 
    strategy: 'profile', 
    metadata: { confidence: 0.9 },
    createdAt: new Date()
  }
];
await longTermMemory.recordBatch(memories);

记忆检索

// 使用过滤器检索记忆
const profileMemories = await longTermMemory.retrieve({
  strategy: 'profile',
  limit: 10,
  orderBy: { createdAt: 'desc' }
});

// 使用TDAI的搜索功能进行语义搜索
const searchResults = await longTermMemory.semanticSearch('user preferences', {
  limit: 5,
  strategy: ['preferences', 'settings'],
  threshold: 0.6
});

// 复杂过滤
const recentMemories = await longTermMemory.retrieve({
  strategy: ['interests', 'preferences'],
  limit: 20,
  filters: { 
    confidence: { $gte: 0.7 },
    createdAt: { $gte: new Date('2024-01-01') }
  }
});

记忆管理

// 更新记忆内容
await longTermMemory.update('mem-1', {
  content: 'User is very interested in machine learning and deep learning',
  metadata: { 
    confidence: 0.9, 
    updated_reason: 'reinforced_interest',
    updatedAt: new Date()
  }
});

// 获取相关记忆(基于基本相似度)
const relatedMemories = await longTermMemory.getRelatedMemories('mem-1', 2);

// 按策略清除记忆
await longTermMemory.clear('temporary');

// 删除特定记忆
await longTermMemory.delete('mem-2');

// 按查询删除
await longTermMemory.delete({
  strategy: 'session_temp',
  filters: { createdAt: { $lt: new Date('2024-01-01') } }
});

功能特性

基于策略的组织

  • 按类型对记忆进行分类(preferencesfactsinterests等)
  • 按策略过滤和检索
  • 为不同用例组织记忆

云端持久化

  • 使用TDAI基础设施的可靠存储
  • 自动备份和恢复
  • 跨会话持久化
  • 企业级安全性

基本搜索功能

  • 基于内容的相似度搜索
  • 策略过滤
  • 基于元数据的查询
  • 排序和分页

批量操作

  • 高效的批量记录
  • 批量更新和删除
  • 针对大型数据集优化

限制

手动记忆提取

与Mem0LongTermMemory不同,TDAI实现需要手动提取记忆:
// 手动提取示例
function extractMemoriesFromConversation(messages: Message[]): MemoryEntity[] {
  const memories: MemoryEntity[] = [];
  
  for (const message of messages) {
    if (message.role === 'user') {
      // 自定义逻辑以识别重要信息
      if (message.content.includes('prefer') || message.content.includes('like')) {
        memories.push({
          id: `mem-${Date.now()}`,
          strategy: 'preferences',
          content: message.content,
          metadata: { source: 'conversation', confidence: 0.7 },
          createdAt: new Date()
        });
      }
    }
  }
  
  return memories;
}

// 使用手动提取
const messages = await shortTermMemory.list();
const extractedMemories = extractMemoriesFromConversation(messages);
await longTermMemory.recordBatch(extractedMemories);

基本整合

  • 无自动记忆整合
  • 需要手动去重
  • 有限的关系映射

搜索限制

  • 基本语义搜索功能
  • 无高级向量相似度
  • 有限的图关系支持

最佳实践

  1. 定义清晰的策略:在应用程序中使用一致的策略名称
  2. 包含元数据:添加置信度分数、来源和标签以便更好地组织
  3. 定期清理:实施定期清理过时记忆
  4. 手动提取:开发强大的逻辑来提取重要信息
  5. 批量操作:使用批量方法以在大型数据集上获得更好的性能
  6. 错误处理:为网络操作实施适当的错误处理

与短期记忆集成

import { InMemoryMemory, TDAILongTermMemory } from '@ag-kit/agents/storage';

class MemoryManager {
  private shortTerm: InMemoryMemory;
  private longTerm: TDAILongTermMemory;

  constructor() {
    this.shortTerm = new InMemoryMemory();
    this.longTerm = new TDAILongTermMemory({ /* config */ });
  }

  async processConversation(sessionId: string) {
    // 获取最近的对话
    const recentMessages = await this.shortTerm.list({ 
      sessionId, 
      limit: 50 
    });

    // 手动提取重要记忆
    const importantMemories = this.extractImportantInfo(recentMessages);

    // 存储到长期记忆
    if (importantMemories.length > 0) {
      await this.longTerm.recordBatch(importantMemories);
    }

    // 为下次对话检索相关上下文
    const context = await this.longTerm.semanticSearch('user context', {
      limit: 10,
      threshold: 0.6
    });

    return context;
  }

  private extractImportantInfo(messages: any[]): MemoryEntity[] {
    // 在此实现您的提取逻辑
    return [];
  }
}