Hindsight Node.js客户端:在JavaScript生态中使用AI记忆

【免费下载链接】hindsight Hindsight: Agent Memory That Learns 【免费下载链接】hindsight 项目地址: https://gitcode.com/GitHub_Trending/hindsight2/hindsight

Hindsight Node.js客户端是一个专为JavaScript开发者设计的TypeScript SDK,它提供了简洁的API接口,帮助开发者在应用中轻松集成Hindsight的AI记忆功能。通过这个客户端,你可以让AI应用拥有持久化的记忆能力,实现跨会话的上下文理解和知识积累。

快速开始:安装与配置

要开始使用Hindsight Node.js客户端,首先需要安装依赖包。在你的Node.js项目中,运行以下命令:

npm install @vectorize-io/hindsight-client

安装完成后,你需要创建一个Hindsight客户端实例。最简单的方式是:

import { HindsightClient } from '@vectorize-io/hindsight-client';

// 基本配置(无认证)
const client = new HindsightClient({ baseUrl: 'http://localhost:8888' });

如果你需要使用API密钥进行认证,可以这样配置:

// 使用API密钥认证
const client = new HindsightClient({
  baseUrl: 'http://localhost:8888',
  apiKey: 'your-api-key'
});

Hindsight与Node.js集成示意图

核心功能:记忆的存储与检索

Hindsight Node.js客户端提供了直观的API来管理AI记忆,主要包括记忆的存储(retain)、检索(recall)和反思(reflect)三个核心操作。

存储记忆(Retain)

要存储一条记忆,使用retain方法:

// 存储一条简单记忆
await client.retain('user123', 'Alice loves AI and machine learning');

你还可以添加更多上下文信息:

// 带上下文的记忆存储
await client.retain('user123', 'Alice is learning TypeScript', {
  context: 'Online course',
  timestamp: new Date(),
  tags: ['education', 'programming']
});

对于多条记忆,可以使用retainBatch方法批量存储:

// 批量存储记忆
await client.retainBatch('user123', [
  { content: 'Alice works at a tech company', tags: ['career'] },
  { content: 'Alice enjoys hiking', tags: ['hobbies'] }
]);

检索记忆(Recall)

要检索与特定查询相关的记忆,使用recall方法:

// 检索记忆
const results = await client.recall('user123', 'What are Alice\'s interests?');
console.log(results.results);

你可以通过标签过滤来精确检索特定类型的记忆:

// 带标签过滤的记忆检索
const results = await client.recall('user123', 'What does Alice like?', {
  tags: ['hobbies'],
  tagsMatch: 'any_strict'
});

反思与生成(Reflect)

reflect方法允许AI基于存储的记忆进行思考并生成回应:

// 基于记忆生成回应
const answer = await client.reflect('user123', 'Tell me about Alice');
console.log(answer.response);

你还可以提供额外的上下文来引导反思过程:

// 带上下文的反思
const answer = await client.reflect('user123', 'What should I talk about with Alice?', {
  context: 'We are at a tech conference'
});

高级功能:记忆银行管理

Hindsight使用"记忆银行"(bank)来组织不同用户或场景的记忆。你可以创建和管理多个记忆银行,以实现数据隔离和分类。

创建记忆银行

// 创建新的记忆银行
await client.createBank('user123', {
  name: 'Alice\'s Memory Bank',
  reflectMission: 'Help me understand Alice\'s preferences and background'
});

获取银行配置

// 获取银行配置
const config = await client.getBankConfig('user123');
console.log(config);

更新银行设置

// 更新银行配置
await client.updateBankConfig('user123', {
  dispositionEmpathy: 4,
  retainChunkSize: 1000
});

错误处理与最佳实践

在使用Hindsight客户端时,合理的错误处理非常重要:

try {
  const results = await client.recall('user123', 'What are Alice\'s interests?');
  console.log(results.results);
} catch (error) {
  if (error instanceof HindsightError) {
    console.error(`API Error: ${error.message} (Status: ${error.statusCode})`);
    console.error('Details:', error.details);
  } else {
    console.error('Unexpected error:', error);
  }
}

最佳实践建议:

  1. 为不同用户或场景使用不同的记忆银行ID
  2. 合理使用标签系统对记忆进行分类
  3. 在生产环境中始终使用API密钥认证
  4. 对于大量记忆,考虑使用批量操作API
  5. 根据应用需求调整记忆检索的预算参数

客户端API参考

Hindsight Node.js客户端提供了丰富的API来管理记忆和银行。以下是一些常用接口的简要说明:

  • retain(bankId, content, options): 存储单条记忆
  • retainBatch(bankId, items, options): 批量存储记忆
  • retainFiles(bankId, files, options): 上传文件并存储其内容
  • recall(bankId, query, options): 检索与查询相关的记忆
  • reflect(bankId, query, options): 基于记忆生成回应
  • listMemories(bankId, options): 列出记忆
  • createBank(bankId, options): 创建记忆银行
  • getBankProfile(bankId): 获取银行配置文件
  • updateBankConfig(bankId, options): 更新银行配置

完整的API文档可以在客户端源代码中找到,主要实现位于hindsight-clients/typescript/src/index.ts

结语

Hindsight Node.js客户端为JavaScript开发者提供了强大而直观的工具,让AI应用能够拥有持久化的记忆能力。通过简单的API调用,你可以轻松实现记忆的存储、检索和反思,为你的AI应用添加上下文理解和知识积累的能力。

无论是构建智能聊天机器人、个人助手,还是需要长期记忆的AI应用,Hindsight Node.js客户端都能为你提供可靠的记忆管理解决方案。开始探索吧,让你的AI应用不再健忘!

【免费下载链接】hindsight Hindsight: Agent Memory That Learns 【免费下载链接】hindsight 项目地址: https://gitcode.com/GitHub_Trending/hindsight2/hindsight

更多推荐