libSQL
Turso 是一个与 SQLite 兼容的数据库,构建于 libSQL(SQLite 的开放贡献分支)。向量相似性搜索作为原生数据类型内置于 Turso 和 libSQL 中,使你能够直接在数据库中存储和查询向量。
¥Turso is a SQLite-compatible database built on libSQL, the Open Contribution fork of SQLite. Vector Similiarity Search is built into Turso and libSQL as a native datatype, enabling you to store and query vectors directly in the database.
LangChain.js 支持使用本地 libSQL 或远程 Turso 数据库作为向量存储,并提供简单的 API 与其交互。
¥LangChain.js supports using a local libSQL, or remote Turso database as a vector store, and provides a simple API to interact with it.
本指南提供了 libSQL 向量存储入门的快速概述。有关 libSQL 所有功能和配置的详细文档,请参阅 API 参考。
¥This guide provides a quick overview for getting started with libSQL vector stores. For detailed documentation of all libSQL features and configurations head to the API reference.
概述
¥Overview
集成详情
¥Integration details
Class | Package | PY support | Package latest |
---|---|---|---|
LibSQLVectorStore | @langchain/community | ❌ |
设置
¥Setup
要使用 libSQL 向量存储,你需要创建一个 Turso 账户或设置一个本地 SQLite 数据库,并安装 @langchain/community
集成软件包。
¥To use libSQL vector stores, you'll need to create a Turso account or set up a local SQLite database, and install the @langchain/community
integration package.
本指南还将使用 OpenAI 嵌入,这需要你安装 @langchain/openai
集成包。如果你愿意,还可以使用其他受支持的嵌入模型。
¥This guide will also use OpenAI embeddings, which require you to install the @langchain/openai
integration package. You can also use other supported embeddings models if you wish.
你可以在使用 libSQL 矢量存储时使用本地 SQLite,也可以使用托管的 Turso 数据库。
¥You can use local SQLite when working with the libSQL vector store, or use a hosted Turso Database.
- npm
- Yarn
- pnpm
npm install @libsql/client @langchain/openai @langchain/community
yarn add @libsql/client @langchain/openai @langchain/community
pnpm add @libsql/client @langchain/openai @langchain/community
现在是时候创建数据库了。你可以在本地创建一个,也可以使用托管的 Turso 数据库。
¥Now it's time to create a database. You can create one locally, or use a hosted Turso database.
本地 libSQL
¥Local libSQL
创建一个新的本地 SQLite 文件并连接到 shell:
¥Create a new local SQLite file and connect to the shell:
sqlite3 file.db
托管的 Turso
¥Hosted Turso
访问 sqlite.new 创建新数据库、为其命名并创建数据库身份验证令牌。
¥Visit sqlite.new to create a new database, give it a name, and create a database auth token.
确保复制数据库身份验证令牌和数据库 URL,其内容应类似于:
¥Make sure to copy the database auth token, and the database URL, it should look something like:
libsql://[database-name]-[your-username].turso.io
设置表和索引
¥Setup the table and index
执行以下 SQL 命令创建新表或将嵌入列添加到现有表。
¥Execute the following SQL command to create a new table or add the embedding column to an existing table.
确保修改 SQL 的以下部分:
¥Make sure to modify the following parts of the SQL:
TABLE_NAME
是你要创建的表的名称。¥
TABLE_NAME
is the name of the table you want to create.content
用于存储Document.pageContent
值。¥
content
is used to store theDocument.pageContent
values.metadata
用于存储Document.metadata
对象。¥
metadata
is used to store theDocument.metadata
object.EMBEDDING_COLUMN
用于存储向量值,请使用你计划使用的模型的维度大小(OpenAI 为 1536)。¥
EMBEDDING_COLUMN
is used to store the vector values, use the dimensions size used by the model you plan to use (1536 for OpenAI).
CREATE TABLE IF NOT EXISTS TABLE_NAME (
id INTEGER PRIMARY KEY AUTOINCREMENT,
content TEXT,
metadata TEXT,
EMBEDDING_COLUMN F32_BLOB(1536) -- 1536-dimensional f32 vector for OpenAI
);
现在在 EMBEDDING_COLUMN
列上创建索引 - 索引名称很重要!:
¥Now create an index on the EMBEDDING_COLUMN
column - the index name is important!:
CREATE INDEX IF NOT EXISTS idx_TABLE_NAME_EMBEDDING_COLUMN ON TABLE_NAME(libsql_vector_idx(EMBEDDING_COLUMN));
确保将 TABLE_NAME
和 EMBEDDING_COLUMN
替换为你在上一步中使用的值。
¥Make sure to replace the TABLE_NAME
and EMBEDDING_COLUMN
with the values you used in the previous step.
实例化
¥Instantiation
要初始化新的 LibSQL
矢量存储,你需要在远程工作时提供数据库 URL 和 Auth Token,或者通过传递本地 SQLite 的文件名来初始化。
¥To initialize a new LibSQL
vector store, you need to provide the database URL and Auth Token when working remotely, or by passing the filename for a local SQLite.
import { LibSQLVectorStore } from "@langchain/community/vectorstores/libsql";
import { OpenAIEmbeddings } from "@langchain/openai";
import { createClient } from "@libsql/client";
const embeddings = new OpenAIEmbeddings({
model: "text-embedding-3-small",
});
const libsqlClient = createClient({
url: "libsql://[database-name]-[your-username].turso.io",
authToken: "...",
});
// Local instantiation
// const libsqlClient = createClient({
// url: "file:./dev.db",
// });
const vectorStore = new LibSQLVectorStore(embeddings, {
db: libsqlClient,
table: "TABLE_NAME",
column: "EMBEDDING_COLUMN",
});
管理向量存储
¥Manage vector store
将项目添加到向量存储
¥Add items to vector store
import type { Document } from "@langchain/core/documents";
const documents: Document[] = [
{ pageContent: "Hello", metadata: { topic: "greeting" } },
{ pageContent: "Bye bye", metadata: { topic: "greeting" } },
];
await vectorStore.addDocuments(documents);
从向量存储中删除项目
¥Delete items from vector store
await vectorStore.deleteDocuments({ ids: [1, 2] });
查询向量存储
¥Query vector store
插入文档后,即可查询向量存储。
¥Once you have inserted the documents, you can query the vector store.
直接查询
¥Query directly
执行简单的相似性搜索可以按如下方式完成:
¥Performing a simple similarity search can be done as follows:
const resultOne = await vectorStore.similaritySearch("hola", 1);
for (const doc of similaritySearchResults) {
console.log(`${doc.pageContent} [${JSON.stringify(doc.metadata, null)}]`);
}
对于关系数据库和图数据库,字段特定语言 (DSL) 用于查询数据。
¥For similarity search with scores:
const similaritySearchWithScoreResults =
await vectorStore.similaritySearchWithScore("hola", 1);
for (const [doc, score] of similaritySearchWithScoreResults) {
console.log(
`${score.toFixed(3)} ${doc.pageContent} [${JSON.stringify(doc.metadata)}]`
);
}
API 参考
¥API reference
有关 LibSQLVectorStore
所有功能和配置的详细文档,请参阅 API 参考。
¥For detailed documentation of all LibSQLVectorStore
features and configurations head to the API reference.
相关
¥Related
向量存储 概念指南
¥Vector store conceptual guide
向量存储 操作指南
¥Vector store how-to guides