CloseVector
兼容性
可在浏览器和 Node.js 上使用
CloseVector 是一款跨平台向量数据库,可以在浏览器和 Node.js 中运行。例如,您可以在 Node.js 上创建索引,然后在浏览器上加载/查询它。有关更多信息,请访问 CloseVector 文档。
设置
CloseVector Web
- npm
- Yarn
- pnpm
npm install -S closevector-web
yarn add closevector-web
pnpm add closevector-web
CloseVector Node
- npm
- Yarn
- pnpm
npm install -S closevector-node
yarn add closevector-node
pnpm add closevector-node
提示
请参阅 本节了解有关安装集成包的常规说明。
- npm
- Yarn
- pnpm
npm install @langchain/openai @langchain/community
yarn add @langchain/openai @langchain/community
pnpm add @langchain/openai @langchain/community
用法
从文本创建新索引
// If you want to import the browser version, use the following line instead:
// import { CloseVectorWeb } from "@langchain/community/vectorstores/closevector/web";
import { CloseVectorNode } from "@langchain/community/vectorstores/closevector/node";
import { OpenAIEmbeddings } from "@langchain/openai";
export const run = async () => {
// If you want to import the browser version, use the following line instead:
// const vectorStore = await CloseVectorWeb.fromTexts(
const vectorStore = await CloseVectorNode.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 参考
- CloseVectorNode 来自
@langchain/community/vectorstores/closevector/node
- OpenAIEmbeddings 来自
@langchain/openai
从加载器创建新索引
// If you want to import the browser version, use the following line instead:
// import { CloseVectorWeb } from "@langchain/community/vectorstores/closevector/web";
import { CloseVectorNode } from "@langchain/community/vectorstores/closevector/node";
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
// If you want to import the browser version, use the following line instead:
// const vectorStore = await CloseVectorWeb.fromDocuments(
const vectorStore = await CloseVectorNode.fromDocuments(
docs,
new OpenAIEmbeddings()
);
// Search for the most similar document
const resultOne = await vectorStore.similaritySearch("hello world", 1);
console.log(resultOne);
API 参考
- CloseVectorNode 来自
@langchain/community/vectorstores/closevector/node
- OpenAIEmbeddings 来自
@langchain/openai
- TextLoader 来自
langchain/document_loaders/fs/text
将索引保存到 CloseVector CDN 并重新加载
CloseVector 支持将索引保存/加载到/从云中保存/加载。要使用此功能,您需要在 CloseVector 上创建一个帐户。请阅读 CloseVector 文档 并通过 登录 首先生成您的 API 密钥。
// If you want to import the browser version, use the following line instead:
// import { CloseVectorWeb } from "@langchain/community/vectorstores/closevector/web";
import { CloseVectorNode } from "@langchain/community/vectorstores/closevector/node";
import { CloseVectorWeb } from "@langchain/community/vectorstores/closevector/web";
import { OpenAIEmbeddings } from "@langchain/openai";
// Create a vector store through any method, here from texts as an example
// If you want to import the browser version, use the following line instead:
// const vectorStore = await CloseVectorWeb.fromTexts(
const vectorStore = await CloseVectorNode.fromTexts(
["Hello world", "Bye bye", "hello nice world"],
[{ id: 2 }, { id: 1 }, { id: 3 }],
new OpenAIEmbeddings(),
undefined,
{
key: "your access key",
secret: "your secret",
}
);
// Save the vector store to cloud
await vectorStore.saveToCloud({
description: "example",
public: true,
});
const { uuid } = vectorStore.instance;
// Load the vector store from cloud
// const loadedVectorStore = await CloseVectorWeb.load(
const loadedVectorStore = await CloseVectorNode.loadFromCloud({
uuid,
embeddings: new OpenAIEmbeddings(),
credentials: {
key: "your access key",
secret: "your secret",
},
});
// If you want to import the node version, use the following lines instead:
// const loadedVectorStoreOnNode = await CloseVectorNode.loadFromCloud({
// uuid,
// embeddings: new OpenAIEmbeddings(),
// credentials: {
// key: "your access key",
// secret: "your secret"
// }
// });
const loadedVectorStoreOnBrowser = await CloseVectorWeb.loadFromCloud({
uuid,
embeddings: new OpenAIEmbeddings(),
credentials: {
key: "your access key",
secret: "your secret",
},
});
// vectorStore and loadedVectorStore are identical
const result = await loadedVectorStore.similaritySearch("hello world", 1);
console.log(result);
// or
const resultOnBrowser = await loadedVectorStoreOnBrowser.similaritySearch(
"hello world",
1
);
console.log(resultOnBrowser);
API 参考
- CloseVectorNode 来自
@langchain/community/vectorstores/closevector/node
- CloseVectorWeb 来自
@langchain/community/vectorstores/closevector/web
- OpenAIEmbeddings 来自
@langchain/openai
将索引保存到文件并重新加载
// If you want to import the browser version, use the following line instead:
// import { CloseVectorWeb } from "@langchain/community/vectorstores/closevector/web";
import { CloseVectorNode } from "@langchain/community/vectorstores/closevector/node";
import { OpenAIEmbeddings } from "@langchain/openai";
// Create a vector store through any method, here from texts as an example
// If you want to import the browser version, use the following line instead:
// const vectorStore = await CloseVectorWeb.fromTexts(
const vectorStore = await CloseVectorNode.fromTexts(
["Hello world", "Bye bye", "hello nice world"],
[{ id: 2 }, { id: 1 }, { id: 3 }],
new OpenAIEmbeddings()
);
// Save the vector store to a directory
const directory = "your/directory/here";
await vectorStore.save(directory);
// Load the vector store from the same directory
// If you want to import the browser version, use the following line instead:
// const loadedVectorStore = await CloseVectorWeb.load(
const loadedVectorStore = await CloseVectorNode.load(
directory,
new OpenAIEmbeddings()
);
// vectorStore and loadedVectorStore are identical
const result = await loadedVectorStore.similaritySearch("hello world", 1);
console.log(result);
API 参考
- CloseVectorNode 来自
@langchain/community/vectorstores/closevector/node
- OpenAIEmbeddings 来自
@langchain/openai