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