向量嵌入管理Skill embeddings

这个技能提供了向量嵌入的完整API参考,用于配置嵌入提供商、管理向量存储、执行语义搜索和文本相似度比较。它支持多种AI模型和缓存管理,适用于人工智能应用、自然语言处理和数据检索任务。关键词:向量嵌入、语义搜索、API配置、缓存管理、文本相似度、人工智能、NLP、数据检索、AI模型、开发工具。

NLP 0 次安装 0 次浏览 更新于 3/9/2026

名称: 向量嵌入 描述: “向量嵌入配置和语义搜索” 表情: “🧬”

向量嵌入 - 完整API参考

配置嵌入提供商、管理向量存储和执行语义搜索。


聊天命令

查看配置

/embeddings                                 显示当前设置
/embeddings status                          提供商状态
/embeddings stats                           缓存统计

配置提供商

/embeddings provider openai                 使用OpenAI嵌入
/embeddings provider voyage                 使用Voyage AI
/embeddings provider local                  使用本地模型
/embeddings model text-embedding-3-small    设置模型

缓存管理

/embeddings cache stats                     查看缓存统计
/embeddings cache clear                     清除缓存
/embeddings cache size                      总缓存大小

测试

/embeddings test "示例文本"                  生成测试嵌入
/embeddings similarity "文本1" "文本2"      比较相似度

TypeScript API参考

创建嵌入服务

import { createEmbeddingsService } from 'clodds/embeddings';

const embeddings = createEmbeddingsService({
  // 提供商
  provider: 'openai',  // 'openai' | 'voyage' | 'local' | 'cohere'
  apiKey: process.env.OPENAI_API_KEY,

  // 模型
  model: 'text-embedding-3-small',
  dimensions: 1536,

  // 缓存
  cache: true,
  cacheBackend: 'sqlite',
  cachePath: './embeddings-cache.db',

  // 批处理
  batchSize: 100,
  maxConcurrent: 5,
});

生成嵌入

// 单个文本
const embedding = await embeddings.embed('你好世界');
console.log(`维度: ${embedding.length}`);

// 多个文本(批处理)
const vectors = await embeddings.embedBatch([
  '第一个文档',
  '第二个文档',
  '第三个文档',
]);

语义搜索

// 搜索存储的向量
const results = await embeddings.search({
  query: '交易策略',
  collection: '文档',
  limit: 10,
  threshold: 0.7,
});

for (const result of results) {
  console.log(`${result.text} (分数: ${result.score})`);
}

相似度

// 比较两个文本
const score = await embeddings.similarity(
  '猫坐在垫子上',
  '一只猫在毯子上休息'
);

console.log(`相似度: ${score}`);  // 0.0 - 1.0

存储向量

// 存储嵌入与元数据
await embeddings.store({
  collection: '文档',
  id: 'doc-1',
  text: '原始文本',
  embedding: vector,
  metadata: {
    source: '维基',
    date: '2024-01-01',
  },
});

// 批量存储
await embeddings.storeBatch({
  collection: '文档',
  items: [
    { id: 'doc-1', text: '第一个文档' },
    { id: 'doc-2', text: '第二个文档' },
  ],
});

缓存管理

// 获取缓存统计
const stats = await embeddings.getCacheStats();
console.log(`已缓存: ${stats.count} 个嵌入`);
console.log(`大小: ${stats.sizeMB} MB`);
console.log(`命中率: ${stats.hitRate}%`);

// 清除缓存
await embeddings.clearCache();

// 清除特定条目
await embeddings.clearCache({ olderThan: '7d' });

提供商配置

// 切换提供商
embeddings.setProvider('voyage', {
  apiKey: process.env.VOYAGE_API_KEY,
  model: 'voyage-large-2',
});

// 使用本地模型(Transformers.js)
// 无需API密钥 - 通过 @xenova/transformers 本地运行
embeddings.setProvider('local', {
  model: 'Xenova/all-MiniLM-L6-v2',  // 384 维度
});

提供商

提供商 模型 质量 速度 成本
OpenAI text-embedding-3-small/large 优秀 $0.02/1M
Voyage voyage-large-2 优秀 $0.02/1M
Cohere embed-english-v3 良好 $0.10/1M
本地 (Transformers.js) Xenova/all-MiniLM-L6-v2 良好 中等 免费

模型

OpenAI

模型 维度 最佳用途
text-embedding-3-small 1536 通用用途
text-embedding-3-large 3072 高精度

Voyage

模型 维度 最佳用途
voyage-large-2 1024 通用用途
voyage-code-2 1536 代码搜索

使用案例

语义记忆搜索

// 存储用户记忆
await embeddings.store({
  collection: '记忆',
  id: 'mem-1',
  text: '用户偏好保守交易',
});

// 搜索记忆
const relevant = await embeddings.search({
  query: '用户风险偏好是什么',
  collection: '记忆',
  limit: 5,
});

文档相似度

// 查找相似文档
const similar = await embeddings.findSimilar({
  text: '如何交易期权',
  collection: '文档',
  limit: 5,
});

最佳实践

  1. 使用缓存 — 避免冗余API调用
  2. 批处理请求 — 比单个调用更高效
  3. 明智选择维度 — 平衡质量与存储
  4. 监控成本 — 嵌入可能累积费用
  5. 开发时使用本地 — 使用本地模型节省成本