USearch
兼容性
仅在 Node.js 上可用。
USearch 是一个用于高效相似性搜索和密集向量聚类的库。
设置
安装 usearch 包,它是 USearch 的 Node.js 绑定。
- npm
- Yarn
- pnpm
npm install -S usearch
yarn add usearch
pnpm add usearch
提示
有关安装集成包的常规说明,请参阅此部分。
- npm
- Yarn
- pnpm
npm install @langchain/openai @langchain/community @langchain/core
yarn add @langchain/openai @langchain/community @langchain/core
pnpm add @langchain/openai @langchain/community @langchain/core
用法
从文本创建新索引
import { USearch } from "@langchain/community/vectorstores/usearch";
import { OpenAIEmbeddings } from "@langchain/openai";
const vectorStore = await USearch.fromTexts(
["Hello world", "Bye bye", "hello nice world"],
[{ id: 2 }, { id: 1 }, { id: 3 }],
new OpenAIEmbeddings()
);
const resultOne = await vectorStore.similaritySearch("hello world", 1);
console.log(resultOne);
API 参考
- USearch 来自
@langchain/community/vectorstores/usearch
- OpenAIEmbeddings 来自
@langchain/openai
从加载器创建新索引
import { USearch } from "@langchain/community/vectorstores/usearch";
import { OpenAIEmbeddings } from "@langchain/openai";
import { TextLoader } from "langchain/document_loaders/fs/text";
// Create docs with a loader
const loader = new TextLoader("src/document_loaders/example_data/example.txt");
const docs = await loader.load();
// Load the docs into the vector store
const vectorStore = await USearch.fromDocuments(docs, new OpenAIEmbeddings());
// Search for the most similar document
const resultOne = await vectorStore.similaritySearch("hello world", 1);
console.log(resultOne);
API 参考
- USearch 来自
@langchain/community/vectorstores/usearch
- OpenAIEmbeddings 来自
@langchain/openai
- TextLoader 来自
langchain/document_loaders/fs/text