SingleStore
SingleStoreDB 是一个强大、高性能的分布式 SQL 数据库解决方案,旨在在 云 和本地环境中表现出色。它拥有多功能的功能集,提供无缝部署选项,同时提供无与伦比的性能。
SingleStoreDB 的一个突出特点是其对向量存储和操作的高级支持,使其成为需要复杂 AI 功能(如文本相似性匹配)的应用程序的理想选择。借助内置向量函数(如 dot_product 和 euclidean_distance),SingleStoreDB 使开发人员能够高效地实现复杂的算法。
对于渴望在 SingleStoreDB 中利用向量数据的开发人员,我们提供了一个全面的教程,指导他们了解 使用向量数据 的复杂性。本教程深入探讨了 SingleStoreDB 中的向量存储,展示了其促进基于向量相似性搜索的能力。利用向量索引,可以以惊人的速度执行查询,从而快速检索相关数据。
此外,SingleStoreDB 的向量存储与 基于 Lucene 的全文索引 无缝集成,从而实现强大的文本相似性搜索。用户可以根据文档元数据对象的选定字段过滤搜索结果,从而提高查询精度。
SingleStoreDB 的与众不同之处在于它能够以各种方式组合向量搜索和全文搜索,从而提供灵活性和多功能性。无论是通过文本或向量相似性进行预过滤并选择最相关的数据,还是采用加权和方法来计算最终相似度得分,开发人员都有多种选择可供使用。
本质上,SingleStoreDB 为管理和查询向量数据提供了全面的解决方案,为 AI 驱动的应用程序提供了无与伦比的性能和灵活性。
仅在 Node.js 上可用。
LangChain.js 需要 mysql2
库来创建与 SingleStoreDB 实例的连接。
设置
- npm
- Yarn
- pnpm
npm install -S mysql2
yarn add mysql2
pnpm add mysql2
用法
SingleStoreVectorStore
管理连接池。建议在终止应用程序之前调用 await store.end();
,以确保所有连接都已正确关闭并防止任何可能的资源泄漏。
标准用法
有关安装集成包的常规说明,请参阅此部分。
- 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
以下是一个简单的示例,展示了如何导入相关模块并使用 SingleStoreVectorStore
执行基本相似性搜索
import { SingleStoreVectorStore } from "@langchain/community/vectorstores/singlestore";
import { OpenAIEmbeddings } from "@langchain/openai";
export const run = async () => {
const vectorStore = await SingleStoreVectorStore.fromTexts(
["Hello world", "Bye bye", "hello nice world"],
[{ id: 2 }, { id: 1 }, { id: 3 }],
new OpenAIEmbeddings(),
{
connectionOptions: {
host: process.env.SINGLESTORE_HOST,
port: Number(process.env.SINGLESTORE_PORT),
user: process.env.SINGLESTORE_USERNAME,
password: process.env.SINGLESTORE_PASSWORD,
database: process.env.SINGLESTORE_DATABASE,
},
}
);
const resultOne = await vectorStore.similaritySearch("hello world", 1);
console.log(resultOne);
await vectorStore.end();
};
API 参考
- SingleStoreVectorStore 来自
@langchain/community/vectorstores/singlestore
- OpenAIEmbeddings 来自
@langchain/openai
元数据过滤
如果需要根据特定的元数据字段过滤结果,您可以传递一个过滤器参数,将搜索范围缩小到与过滤器对象中所有指定字段匹配的文档
import { SingleStoreVectorStore } from "@langchain/community/vectorstores/singlestore";
import { OpenAIEmbeddings } from "@langchain/openai";
export const run = async () => {
const vectorStore = await SingleStoreVectorStore.fromTexts(
["Good afternoon", "Bye bye", "Boa tarde!", "Até logo!"],
[
{ id: 1, language: "English" },
{ id: 2, language: "English" },
{ id: 3, language: "Portugese" },
{ id: 4, language: "Portugese" },
],
new OpenAIEmbeddings(),
{
connectionOptions: {
host: process.env.SINGLESTORE_HOST,
port: Number(process.env.SINGLESTORE_PORT),
user: process.env.SINGLESTORE_USERNAME,
password: process.env.SINGLESTORE_PASSWORD,
database: process.env.SINGLESTORE_DATABASE,
},
distanceMetric: "EUCLIDEAN_DISTANCE",
}
);
const resultOne = await vectorStore.similaritySearch("greetings", 1, {
language: "Portugese",
});
console.log(resultOne);
await vectorStore.end();
};
API 参考
- SingleStoreVectorStore 来自
@langchain/community/vectorstores/singlestore
- OpenAIEmbeddings 来自
@langchain/openai
向量索引
通过利用 ANN 向量索引,提高 SingleStore DB 8.5 或更高版本的搜索效率。通过在向量存储对象创建期间设置 useVectorIndex: true
,您可以激活此功能。此外,如果您的向量维度与默认 OpenAI 嵌入大小 1536 不同,请确保相应地指定 vectorSize
参数。
混合搜索
SingleStoreDB 提供了多种搜索策略,每种策略都经过精心设计,以满足特定的用例和用户偏好。默认的 VECTOR_ONLY
策略利用向量运算(如 DOT_PRODUCT
或 EUCLIDEAN_DISTANCE
)直接计算向量之间的相似度得分,而 TEXT_ONLY
则采用基于 Lucene 的全文搜索,这对于以文本为中心的应用程序尤其有利。对于寻求平衡方法的用户,FILTER_BY_TEXT
首先根据文本相似性优化结果,然后再进行向量比较,而 FILTER_BY_VECTOR
则优先考虑向量相似性,在评估文本相似性以获得最佳匹配之前过滤结果。值得注意的是,FILTER_BY_TEXT
和 FILTER_BY_VECTOR
都需要全文索引才能运行。此外,WEIGHTED_SUM
是一种复杂的策略,它通过权衡向量相似性和文本相似性来计算最终相似度得分,尽管它仅使用点积距离计算,并且也需要全文索引。这些多功能的策略使用户能够根据其独特的需求微调搜索,从而促进高效而精确的数据检索和分析。此外,SingleStoreDB 的混合方法(例如 FILTER_BY_TEXT
、FILTER_BY_VECTOR
和 WEIGHTED_SUM
策略)无缝地融合了向量搜索和基于文本的搜索,从而最大限度地提高了效率和准确性,确保用户可以为各种应用程序充分利用该平台的功能。
import { SingleStoreVectorStore } from "@langchain/community/vectorstores/singlestore";
import { OpenAIEmbeddings } from "@langchain/openai";
export const run = async () => {
const vectorStore = await SingleStoreVectorStore.fromTexts(
[
"In the parched desert, a sudden rainstorm brought relief, as the droplets danced upon the thirsty earth, rejuvenating the landscape with the sweet scent of petrichor.",
"Amidst the bustling cityscape, the rain fell relentlessly, creating a symphony of pitter-patter on the pavement, while umbrellas bloomed like colorful flowers in a sea of gray.",
"High in the mountains, the rain transformed into a delicate mist, enveloping the peaks in a mystical veil, where each droplet seemed to whisper secrets to the ancient rocks below.",
"Blanketing the countryside in a soft, pristine layer, the snowfall painted a serene tableau, muffling the world in a tranquil hush as delicate flakes settled upon the branches of trees like nature's own lacework.",
"In the urban landscape, snow descended, transforming bustling streets into a winter wonderland, where the laughter of children echoed amidst the flurry of snowballs and the twinkle of holiday lights.",
"Atop the rugged peaks, snow fell with an unyielding intensity, sculpting the landscape into a pristine alpine paradise, where the frozen crystals shimmered under the moonlight, casting a spell of enchantment over the wilderness below.",
],
[
{ category: "rain" },
{ category: "rain" },
{ category: "rain" },
{ category: "snow" },
{ category: "snow" },
{ category: "snow" },
],
new OpenAIEmbeddings(),
{
connectionOptions: {
host: process.env.SINGLESTORE_HOST,
port: Number(process.env.SINGLESTORE_PORT),
user: process.env.SINGLESTORE_USERNAME,
password: process.env.SINGLESTORE_PASSWORD,
database: process.env.SINGLESTORE_DATABASE,
},
distanceMetric: "DOT_PRODUCT",
useVectorIndex: true,
useFullTextIndex: true,
}
);
const resultOne = await vectorStore.similaritySearch(
"rainstorm in parched desert, rain",
1,
{ category: "rain" }
);
console.log(resultOne[0].pageContent);
await vectorStore.setSearchConfig({
searchStrategy: "TEXT_ONLY",
});
const resultTwo = await vectorStore.similaritySearch(
"rainstorm in parched desert, rain",
1
);
console.log(resultTwo[0].pageContent);
await vectorStore.setSearchConfig({
searchStrategy: "FILTER_BY_TEXT",
filterThreshold: 0.1,
});
const resultThree = await vectorStore.similaritySearch(
"rainstorm in parched desert, rain",
1
);
console.log(resultThree[0].pageContent);
await vectorStore.setSearchConfig({
searchStrategy: "FILTER_BY_VECTOR",
filterThreshold: 0.1,
});
const resultFour = await vectorStore.similaritySearch(
"rainstorm in parched desert, rain",
1
);
console.log(resultFour[0].pageContent);
await vectorStore.setSearchConfig({
searchStrategy: "WEIGHTED_SUM",
textWeight: 0.2,
vectorWeight: 0.8,
vectorselectCountMultiplier: 10,
});
const resultFive = await vectorStore.similaritySearch(
"rainstorm in parched desert, rain",
1
);
console.log(resultFive[0].pageContent);
await vectorStore.end();
};
API 参考
- SingleStoreVectorStore 来自
@langchain/community/vectorstores/singlestore
- OpenAIEmbeddings 来自
@langchain/openai