索引存储包含轻量级索引元数据(即构建索引时创建的附加状态信息)。
更多详情请参见 API 参考。
简单索引存储#
默认情况下,LlamaIndex 使用一个基于内存键值存储的简单索引存储。通过调用 index_store.persist()
(和 SimpleIndexStore.from_persist_path(...)
)可以将其持久化到磁盘(并从中加载)。
MongoDB 索引存储#
与文档存储类似,我们也可以使用 MongoDB
作为索引存储的存储后端。
在底层,MongoIndexStore
连接到一个固定的 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
时配置 db_name
和 namespace
,否则它们默认为 db_name="db_docstore"
和 namespace="docstore"
。
请注意,使用
MongoIndexStore
时无需调用storage_context.persist()
(或index_store.persist()
),因为数据默认是持久化的。
您可以通过使用现有的 db_name
和 collection_name
重新初始化 MongoIndexStore
来轻松地重新连接到您的 MongoDB 集合并重新加载索引。
更完整的示例可在此处找到。
Redis 索引存储#
我们支持 Redis 作为替代文档存储后端,它在摄取 Node
对象时持久化数据。
在底层,RedisIndexStore
连接到 Redis 数据库,并将您的节点添加到存储在 {namespace}/index
下的命名空间中。
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
时配置 namespace
,否则它默认为 namespace="index_store"
。
您可以通过使用现有的
host
、port
和namespace
重新初始化RedisIndexStore
来轻松地重新连接到您的 Redis 客户端并重新加载索引。
更完整的示例可在此处找到。
Couchbase 索引存储#
Couchbase 可以用作索引存储的存储后端。
在底层,CouchbaseIndexStore
连接到 Couchbase 操作数据库,并将您的节点添加到指定 {bucket_name}
和 {scope_name}
中名为 {namespace}_index
的集合中。
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
时配置 namespace
、bucket
和 scope
。默认情况下,使用的集合名为 index_store_data
。除了字母数字字符外,集合名称中只允许使用 -
、_
和 %
。存储会自动将其他特殊字符转换为 _
。
注意:您可以在实例化
CouchbaseIndexStore
时配置namespace
、bucket
和scope
。默认情况下,使用的集合是index_store_data
。除了字母数字字符外,集合名称中只允许使用-
、_
和%
。存储会自动将其他特殊字符转换为_
。
您可以通过使用现有的 client
、bucket_name
、scope_name
和 namespace
重新初始化 CouchbaseIndexStore
,轻松地重新连接到您的 Couchbase 客户端并重新加载索引。
Tablestore 索引存储#
与文档存储类似,我们也可以使用 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
。
您可以通过使用现有的 endpoint
、instance_name
、access_key_id
和 access_key_secret
重新初始化 TablestoreIndexStore
,轻松地重新连接到您的 Tablestore 数据库并重新加载索引。
更完整的示例可以在此处找到
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
# 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_name
是public
。
在底层,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
# 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_name
是public
。
在底层,PostgresIndexStore
连接到 Google Cloud 中的 Cloud SQL PostgreSQL 数据库,并将您的节点添加到 schema_name
下的表中。
您可以通过使用 PostgresEngine
重新初始化 PostgresIndexStore
,轻松地重新连接到您的 Cloud SQL PostgreSQL 数据库并重新加载索引,而无需初始化新表。
更详细的指南可以在此处找到