索引存储
索引存储包含轻量级索引元数据(即在构建索引时创建的附加状态信息)。
查看 API 参考文档 获取更多详情。
默认情况下,LlamaIndex使用基于内存键值存储的简单索引存储。
通过调用index_store.persist()(以及相应的SimpleIndexStore.from_persist_path(...))可以将它们持久化到磁盘(或从磁盘加载)。
MongoDB 索引存储
Section titled “MongoDB Index Store”与文档存储类似,我们也可以使用 MongoDB 作为索引存储的后端存储。
from llama_index.storage.index_store.mongodb import MongoIndexStorefrom llama_index.core import VectorStoreIndex
# create (or load) index storeindex_store = MongoIndexStore.from_uri(uri="<mongodb+srv://...>")
# create storage contextstorage_context = StorageContext.from_defaults(index_store=index_store)
# build indexindex = VectorStoreIndex(nodes, storage_context=storage_context)
# or alternatively, load indexfrom llama_index.core import load_index_from_storage
index = load_index_from_storage(storage_context)在底层,MongoIndexStore 连接到一个固定的MongoDB数据库,并为您的索引元数据初始化新集合(或加载现有集合)。
注意:您可以在实例化
MongoIndexStore时配置db_name和namespace,否则它们默认为db_name="db_docstore"和namespace="docstore"。
请注意,在使用 MongoIndexStore 时无需调用 storage_context.persist()(或 index_store.persist()),因为数据默认会被持久化。
你可以通过使用现有的 db_name 和 collection_name 重新初始化 MongoIndexStore,轻松重新连接到你的MongoDB集合并重新加载索引。
更完整的示例可在此处找到
Redis 索引存储
Section titled “Redis Index Store”我们支持将Redis作为替代文档存储后端,在Node对象被摄取时持久化数据。
from llama_index.storage.index_store.redis import RedisIndexStorefrom llama_index.core import VectorStoreIndex
# create (or load) docstore and add nodesindex_store = RedisIndexStore.from_host_and_port( host="127.0.0.1", port="6379", namespace="llama_index")
# create storage contextstorage_context = StorageContext.from_defaults(index_store=index_store)
# build indexindex = VectorStoreIndex(nodes, storage_context=storage_context)
# or alternatively, load indexfrom llama_index.core import load_index_from_storage
index = load_index_from_storage(storage_context)在底层,RedisIndexStore 连接到 redis 数据库,并将您的节点添加到存储在 {namespace}/index 下的命名空间中。
注意:您可以在实例化
RedisIndexStore时配置namespace,否则默认使用namespace="index_store"。
您可以通过使用现有的 host、port 和 namespace 重新初始化 RedisIndexStore,轻松重新连接到您的 Redis 客户端并重新加载索引。
更完整的示例可在此处找到
Couchbase 索引存储
Section titled “Couchbase Index Store”Couchbase 可用作索引存储的存储后端。
from llama_index.storage.index_store.couchbase import CouchbaseIndexStorefrom llama_index.core import VectorStoreIndex
from couchbase.cluster import Clusterfrom couchbase.auth import PasswordAuthenticatorfrom couchbase.options import ClusterOptionsfrom datetime import timedelta
# create couchbase clientauth = 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 nodesindex_store = CouchbaseIndexStore.from_couchbase_client( client=cluster, bucket_name="llama-index", scope_name="_default", namespace="default",)
# create storage contextstorage_context = StorageContext.from_defaults(index_store=index_store)
# build indexindex = VectorStoreIndex(nodes, storage_context=storage_context)
# or alternatively, load indexfrom llama_index.core import load_index_from_storage
index = load_index_from_storage(storage_context)在底层,CouchbaseIndexStore 连接到 Couchbase 操作数据库,并将您的节点添加到指定 {bucket_name} 和 {scope_name} 中名为 {namespace}_index 的集合中。
注意:您可以在实例化
CouchbaseIndexStore时配置namespace、bucket和scope。默认情况下,使用的集合是index_store_data。除了字母数字字符外,仅允许将-、_和%作为集合名称的一部分。存储将自动将其他特殊字符转换为_。
您可以通过使用现有的 client、bucket_name、scope_name 和 namespace 重新初始化 CouchbaseIndexStore,轻松重新连接到您的 Couchbase 客户端并重新加载索引。
Tablestore 索引存储
Section titled “Tablestore Index Store”与文档存储类似,我们也可以使用 Tablestore 作为索引存储的后端存储。
from llama_index.storage.index_store.tablestore import TablestoreIndexStorefrom llama_index.core import StorageContext, VectorStoreIndex
# create (or load) index storeindex_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 contextstorage_context = StorageContext.from_defaults(index_store=index_store)
# build indexindex = VectorStoreIndex(nodes, storage_context=storage_context)
# or alternatively, load indexfrom llama_index.core import load_index_from_storage
index = load_index_from_storage(storage_context)在底层,TablestoreIndexStore 连接到 Tablestore 数据库,并将您的节点添加到名为 {namespace}_data 的表下。
注意:您可以在实例化
TablestoreIndexStore时配置namespace。
您可以通过使用现有的 endpoint、instance_name、access_key_id 和 access_key_secret 重新初始化 TablestoreIndexStore,轻松重新连接到您的表格存储数据库并重新加载索引。
更完整的示例可在此处找到
Google AlloyDB 索引存储
Section titled “Google AlloyDB Index Store”与文档存储类似,我们也可以使用 AlloyDB 作为索引存储的后端存储。
本教程演示同步接口。所有同步方法都有对应的异步方法。
pip install llama-indexpip install llama-index-alloydb-pgpip install llama-index-llms-vertexfrom llama_index_alloydb_pg import AlloyDBEngine, AlloyDBIndexStorefrom llama_index.core import StorageContext, VectorStoreIndex
# create an AlloyDB Engine for connection poolengine = AlloyDBEngine.from_instance( project_id=PROJECT_ID, region=REGION, cluster=CLUSTER, instance=INSTANCE, database=DATABASE, user=USER, password=PASSWORD,)
# initialize a new table in AlloyDBengine.init_index_store_table( table_name=TABLE_NAME,)
index_store = AlloyDBIndexStore.create_sync( engine=engine, table_name=TABLE_NAME,)
# create storage contextstorage_context = StorageContext.from_defaults(index_store=index_store)
# build indexindex = VectorStoreIndex(nodes, storage_context=storage_context)
# or alternatively, load indexfrom llama_index.core import load_index_from_storage
index = load_index_from_storage(storage_context)注意:在初始化新表格并实例化
AlloyDBIndexStore时,您可以配置schema_name以及table_name。默认情况下,schema_name为public。
在底层,AlloyDBIndexStore 连接到 Google Cloud 中的 alloydb 数据库,并将您的节点添加到 schema_name 下的一个表中。
您可以通过重新初始化一个带有AlloyDBEngine的AlloyDBIndexStore来轻松重新连接到您的AlloyDB数据库并重新加载索引,而无需初始化新表。
更详细的指南可在此处找到
Google Cloud SQL for PostgreSQL 索引存储
Section titled “Google Cloud SQL for PostgreSQL Index Store”与文档存储类似,我们也可以使用 Cloud SQL for PostgreSQL 作为索引存储的后端存储。
本教程演示同步接口。所有同步方法都有对应的异步方法。
pip install llama-indexpip install llama-index-cloud-sql-pgfrom llama_index_cloud_sql_pg import PostgresEngine, PostgresIndexStorefrom llama_index.core import StorageContext, VectorStoreIndex
# create an Postgres Engine for connection poolengine = PostgresEngine.from_instance( project_id=PROJECT_ID, region=REGION, instance=INSTANCE, database=DATABASE, user=USER, password=PASSWORD,)
# initialize a new table in cloud sql postgresengine.init_index_store_table( table_name=TABLE_NAME,)
index_store = PostgresIndexStore.create_sync( engine=engine, table_name=TABLE_NAME,)
# create storage contextstorage_context = StorageContext.from_defaults(index_store=index_store)
# build indexindex = VectorStoreIndex(nodes, storage_context=storage_context)
# or alternatively, load indexfrom llama_index.core import load_index_from_storage
index = load_index_from_storage(storage_context)注意:在初始化新表格并实例化
PostgresIndexStore时,您可以配置schema_name以及table_name。默认情况下,schema_name为public。
在底层,PostgresIndexStore 连接到 Google Cloud 中的云 SQL PostgreSQL 数据库,并将您的节点添加到 schema_name 下的一个表中。
你可以轻松地重新连接到你的云SQL PostgreSQL数据库,并通过重新初始化一个带有PostgresEngine的PostgresIndexStore来重新加载索引,而无需初始化新表。
更详细的指南可在此处找到