跳转到内容

索引存储

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

查看 API 参考文档 获取更多详情。

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

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

from llama_index.storage.index_store.mongodb import MongoIndexStore
from llama_index.core import VectorStoreIndex
# create (or load) index store
index_store = MongoIndexStore.from_uri(uri="<mongodb+srv://...>")
# create storage context
storage_context = StorageContext.from_defaults(index_store=index_store)
# build index
index = VectorStoreIndex(nodes, storage_context=storage_context)
# or alternatively, load index
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作为替代文档存储后端,在Node对象被摄取时持久化数据。

from llama_index.storage.index_store.redis import RedisIndexStore
from llama_index.core import VectorStoreIndex
# create (or load) docstore and add nodes
index_store = RedisIndexStore.from_host_and_port(
host="127.0.0.1", port="6379", namespace="llama_index"
)
# create storage context
storage_context = StorageContext.from_defaults(index_store=index_store)
# build index
index = VectorStoreIndex(nodes, storage_context=storage_context)
# or alternatively, load index
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 可用作索引存储的存储后端。

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
# create couchbase client
auth = PasswordAuthenticator("DB_USERNAME", "DB_PASSWORD")
options = ClusterOptions(authenticator=auth)
cluster = Cluster("couchbase://localhost", options)
# Wait until the cluster is ready for use.
cluster.wait_until_ready(timedelta(seconds=5))
# create (or load) docstore and add nodes
index_store = CouchbaseIndexStore.from_couchbase_client(
client=cluster,
bucket_name="llama-index",
scope_name="_default",
namespace="default",
)
# create storage context
storage_context = StorageContext.from_defaults(index_store=index_store)
# build index
index = VectorStoreIndex(nodes, storage_context=storage_context)
# or alternatively, load index
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
# create (or load) index store
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>",
)
# create storage context
storage_context = StorageContext.from_defaults(index_store=index_store)
# build index
index = VectorStoreIndex(nodes, storage_context=storage_context)
# or alternatively, load index
from llama_index.core import load_index_from_storage
index = load_index_from_storage(storage_context)

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

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

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

更完整的示例可在此处找到

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

Terminal window
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
# create an AlloyDB Engine for connection pool
engine = AlloyDBEngine.from_instance(
project_id=PROJECT_ID,
region=REGION,
cluster=CLUSTER,
instance=INSTANCE,
database=DATABASE,
user=USER,
password=PASSWORD,
)
# initialize a new table in AlloyDB
engine.init_index_store_table(
table_name=TABLE_NAME,
)
index_store = AlloyDBIndexStore.create_sync(
engine=engine,
table_name=TABLE_NAME,
)
# create storage context
storage_context = StorageContext.from_defaults(index_store=index_store)
# build index
index = VectorStoreIndex(nodes, storage_context=storage_context)
# or alternatively, load index
from llama_index.core import load_index_from_storage
index = load_index_from_storage(storage_context)

注意:在初始化新表格并实例化 AlloyDBIndexStore 时,您可以配置 schema_name 以及 table_name。默认情况下,schema_namepublic

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

您可以通过重新初始化一个带有AlloyDBEngineAlloyDBIndexStore来轻松重新连接到您的AlloyDB数据库并重新加载索引,而无需初始化新表。

更详细的指南可在此处找到

Google Cloud SQL for PostgreSQL 索引存储

Section titled “Google Cloud SQL for PostgreSQL Index Store”

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

Terminal window
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
# create an Postgres Engine for connection pool
engine = PostgresEngine.from_instance(
project_id=PROJECT_ID,
region=REGION,
instance=INSTANCE,
database=DATABASE,
user=USER,
password=PASSWORD,
)
# initialize a new table in cloud sql postgres
engine.init_index_store_table(
table_name=TABLE_NAME,
)
index_store = PostgresIndexStore.create_sync(
engine=engine,
table_name=TABLE_NAME,
)
# create storage context
storage_context = StorageContext.from_defaults(index_store=index_store)
# build index
index = VectorStoreIndex(nodes, storage_context=storage_context)
# or alternatively, load index
from llama_index.core import load_index_from_storage
index = load_index_from_storage(storage_context)

注意:在初始化新表格并实例化 PostgresIndexStore 时,您可以配置 schema_name 以及 table_name。默认情况下,schema_namepublic

在底层,PostgresIndexStore 连接到 Google Cloud 中的云 SQL PostgreSQL 数据库,并将您的节点添加到 schema_name 下的一个表中。

你可以轻松地重新连接到你的云SQL PostgreSQL数据库,并通过重新初始化一个带有PostgresEnginePostgresIndexStore来重新加载索引,而无需初始化新表。

更详细的指南可在此处找到