Skip to content

索引存储#

索引存储包含轻量级的索引元数据(即在构建索引时创建的附加状态信息)。

更多详情请参阅API参考

简单索引存储#

默认情况下,LlamaIndex使用基于内存键值存储的简单索引存储。通过调用index_store.persist()(以及对应的SimpleIndexStore.from_persist_path(...))可以将索引持久化到磁盘(或从磁盘加载)。

MongoDB索引存储#

与文档存储类似,我们也可以使用MongoDB作为索引存储的后端存储。

from llama_index.storage.index_store.mongodb import MongoIndexStore
from llama_index.core import VectorStoreIndex

# 创建(或加载)索引存储
index_store = MongoIndexStore.from_uri(uri="<mongodb+srv://...>")

# 创建存储上下文
storage_context = StorageContext.from_defaults(index_store=index_store)

# 构建索引
index = VectorStoreIndex(nodes, storage_context=storage_context)

# 或者也可以加载索引
from llama_index.core import load_index_from_storage

index = load_index_from_storage(storage_context)

在底层,MongoIndexStore会连接到一个固定的MongoDB数据库,并为您的索引元数据初始化新的集合(或加载现有集合)。

注意:在实例化MongoIndexStore时可以配置db_namenamespace,否则它们默认为db_name="db_docstore"namespace="docstore"

请注意,使用MongoIndexStore时不需要调用storage_context.persist()(或index_store.persist()),因为数据默认就是持久化的。

您可以通过使用现有的db_namecollection_name重新初始化MongoIndexStore,轻松重新连接到MongoDB集合并重新加载索引。

更完整的示例可以在这里找到。

Redis索引存储#

我们支持Redis作为替代的文档存储后端,在摄取Node对象时持久化数据。

from llama_index.storage.index_store.redis import RedisIndexStore
from llama_index.core import VectorStoreIndex

# 创建(或加载)文档存储并添加节点
index_store = RedisIndexStore.from_host_and_port(
    host="127.0.0.1", port="6379", namespace="llama_index"
)

# 创建存储上下文
storage_context = StorageContext.from_defaults(index_store=index_store)

# 构建索引
index = VectorStoreIndex(nodes, storage_context=storage_context)

# 或者也可以加载索引
from llama_index.core import load_index_from_storage

index = load_index_from_storage(storage_context)

在底层,RedisIndexStore会连接到Redis数据库,并将您的节点添加到存储在{namespace}/index下的命名空间中。

注意:在实例化RedisIndexStore时可以配置namespace,否则默认为namespace="index_store"

您可以通过使用现有的hostportnamespace重新初始化RedisIndexStore,轻松重新连接到Redis客户端并重新加载索引。

更完整的示例可以在这里找到。

Couchbase索引存储#

Couchbase可以用作索引存储的后端存储。

from llama_index.storage.index_store.couchbase import CouchbaseIndexStore
from llama_index.core import VectorStoreIndex

from couchbase.cluster import Cluster
from couchbase.auth import PasswordAuthenticator
from couchbase.options import ClusterOptions
from datetime import timedelta

# 创建couchbase客户端
auth = PasswordAuthenticator("DB_USERNAME", "DB_PASSWORD")
options = ClusterOptions(authenticator=auth)

cluster = Cluster("couchbase://localhost", options)

# 等待集群准备就绪
cluster.wait_until_ready(timedelta(seconds=5))

# 创建(或加载)文档存储并添加节点
index_store = CouchbaseIndexStore.from_couchbase_client(
    client=cluster,
    bucket_name="llama-index",
    scope_name="_default",
    namespace="default",
)

# 创建存储上下文
storage_context = StorageContext.from_defaults(index_store=index_store)

# 构建索引
index = VectorStoreIndex(nodes, storage_context=storage_context)

# 或者也可以加载索引
from llama_index.core import load_index_from_storage

index = load_index_from_storage(storage_context)

在底层,CouchbaseIndexStore会连接到一个Couchbase操作数据库,并将您的节点添加到指定{bucket_name}{scope_name}中名为{namespace}_index的集合中。

注意:在实例化CouchbaseIndexStore时可以配置namespacebucketscope。默认情况下,使用的集合是index_store_data。除了字母数字字符外,集合名称中只允许使用-_%。存储会自动将其他特殊字符转换为_

您可以通过使用现有的clientbucket_namescope_namenamespace重新初始化CouchbaseIndexStore,轻松重新连接到Couchbase客户端并重新加载索引。

表格存储索引存储#

与文档存储类似,我们也可以使用Tablestore作为索引存储的后端存储。

from llama_index.storage.index_store.tablestore import TablestoreIndexStore
from llama_index.core import StorageContext, VectorStoreIndex

# 创建(或加载)索引存储
index_store = TablestoreIndexStore.from_config(
    endpoint="<tablestore_end_point>",
    instance_name="<tablestore_instance_name>",
    access_key_id="<tablestore_access_key_id>",
    access_key_secret="<tablestore_access_key_secret>",
)

# 创建存储上下文
storage_context = StorageContext.from_defaults(index_store=index_store)

# 构建索引
index = VectorStoreIndex(nodes, storage_context=storage_context)

# 或者也可以加载索引
from llama_index.core import load_index_from_storage

index = load_index_from_storage(storage_context)

在底层,TablestoreIndexStore会连接到一个表格存储数据库,并将您的节点添加到名为{namespace}_data的表下。

注意:在实例化TablestoreIndexStore时可以配置namespace

您可以通过使用现有的endpointinstance_nameaccess_key_idaccess_key_secret重新初始化TablestoreIndexStore,轻松重新连接到表格存储数据库并重新加载索引。

更完整的示例可以在这里找到。

Google AlloyDB索引存储#

与文档存储类似,我们也可以使用AlloyDB作为索引存储的后端存储。本教程演示同步接口。所有同步方法都有对应的异步方法。

pip install llama-index
pip install llama-index-alloydb-pg
pip install llama-index-llms-vertex
from llama_index_alloydb_pg import AlloyDBEngine, AlloyDBIndexStore
from llama_index.core import StorageContext, VectorStoreIndex

# 为连接池创建AlloyDB引擎
engine = AlloyDBEngine.from_instance(
    project_id=PROJECT_ID,
    region=REGION,
    cluster=CLUSTER,
    instance=INSTANCE,
    database=DATABASE,
    user=USER,
    password=PASSWORD,
)

# 在AlloyDB中初始化新表
engine.init_index_store_table(
    table_name=TABLE_NAME,
)

index_store = AlloyDBIndexStore.create_sync(
    engine=engine,
    table_name=TABLE_NAME,
)

# 创建存储上下文
storage_context = StorageContext.from_defaults(index_store=index_store)

# 构建索引
index = VectorStoreIndex(nodes, storage_context=storage_context)

# 或者也可以加载索引
from llama_index.core import load_index_from_storage

index = load_index_from_storage(storage_context)

注意:在初始化新表和实例化AlloyDBIndexStore时,可以配置schema_nametable_name。默认情况下schema_namepublic

在底层,AlloyDBIndexStore会连接到Google Cloud中的alloydb数据库,并将您的节点添加到schema_name下的表中。

您可以通过使用AlloyDBEngine重新初始化AlloyDBIndexStore而不初始化新表,轻松重新连接到AlloyDB数据库并重新加载索引。

更详细的指南可以在这里找到。

Google Cloud SQL for PostgreSQL索引存储#

与文档存储类似,我们也可以使用Cloud SQL for PostgreSQL作为索引存储的后端存储。本教程演示同步接口。所有同步方法都有对应的异步方法。

pip install llama-index
pip install llama-index-cloud-sql-pg
from llama_index_cloud_sql_pg import PostgresEngine, PostgresIndexStore
from llama_index.core import StorageContext, VectorStoreIndex

# 为连接池创建Postgres引擎
engine = PostgresEngine.from_instance(
    project_id=PROJECT_ID,
    region=REGION,
    instance=INSTANCE,
    database=DATABASE,
    user=USER,
    password=PASSWORD,
)

# 在cloud sql postgres中初始化新表
engine.init_index_store_table(
    table_name=TABLE_NAME,
)

index_store = PostgresIndexStore.create_sync(
    engine=engine,
    table_name=TABLE_NAME,
)

# 创建存储上下文
storage_context = StorageContext.from_defaults(index_store=index_store)

# 构建索引
index = VectorStoreIndex(nodes, storage_context=storage_context)

# 或者也可以加载索引
from llama_index.core import load_index_from_storage

index = load_index_from_storage(storage_context)

注意:在初始化新表和实例化PostgresIndexStore时,可以配置schema_nametable_name。默认情况下schema_namepublic

在底层,PostgresIndexStore会连接到Google Cloud中的cloud sql postgres数据库,并将您的节点添加到schema_name下的表中。

您可以通过使用PostgresEngine重新初始化PostgresIndexStore而不初始化新表,轻松重新连接到cloud sql postgres数据库并重新加载索引。

更详细的指南可以在这里找到。