哪个网站做黄金交易最好,深圳积分商城网站建设,网络优化是做什么的,WordPress tag 分类文章目录 一、储存概览1、概念2、使用模式3、模块 二、Vector Stores1、简单向量存储2、矢量存储选项和功能支持3、Example Notebooks 三、文件存储1、简单文档存储2、MongoDB 文档存储3、Redis 文档存储4、Firestore 文档存储 四、索引存储1、简单索引存储2、MongoDB 索引存储… 文章目录 一、储存概览1、概念2、使用模式3、模块 二、Vector Stores1、简单向量存储2、矢量存储选项和功能支持3、Example Notebooks 三、文件存储1、简单文档存储2、MongoDB 文档存储3、Redis 文档存储4、Firestore 文档存储 四、索引存储1、简单索引存储2、MongoDB 索引存储3、Redis索引存储 五、Chat Stores1、简单聊天商店2、Redis聊天商店 六、键值存储七、保存和加载数据1、持久化数据2、加载数据中3、使用远程后端 八、自定义存储低级API矢量存储集成和存储 本文转载改编自 https://docs.llamaindex.ai/en/stable/module_guides/storing/ 一、储存概览 1、概念
LlamaIndex 提供了一个用于摄取、索引和查询外部数据的高级接口。
在底层LlamaIndex 还支持可交换存储组件允许您自定义
文档存储Node存储摄取的文档即对象的地方索引存储存储索引元数据的位置向量存储存储嵌入向量的位置。图存储存储知识图的位置即 for KnowledgeGraphIndex。聊天存储存储和组织聊天消息的地方。
文档/索引存储依赖于通用的键值存储抽象这也在下面详细介绍。 LlamaIndex 支持将数据持久保存到fsspec支持的任何存储后端。我们已确认支持以下存储后端
本地文件系统AWS S3Cloudflare R2 2、使用模式
许多向量存储FAISS 除外将存储数据和索引嵌入。
这意味着您不需要使用单独的文档存储或索引存储。
这也意味着您不需要显式地保留这些数据——这会自动发生。用法如下所示来构建新索引/重新加载现有索引。
## build a new index
from llama_index.core import VectorStoreIndex, StorageContext
from llama_index.vector_stores.deeplake import DeepLakeVectorStore# construct vector store and customize storage context
vector_store DeepLakeVectorStore(dataset_pathdataset_path)
storage_context StorageContext.from_defaults(vector_storevector_store)
# Load documents and build index
index VectorStoreIndex.from_documents(documents, storage_contextstorage_context
)***
## reload an existing one
index VectorStoreIndex.from_vector_store(vector_storevector_store)有关更多详细信息请参阅下面的矢量存储模块指南。
请注意通常要使用存储抽象您需要定义一个StorageContext对象
from llama_index.core.storage.docstore import SimpleDocumentStore
from llama_index.core.storage.index_store import SimpleIndexStore
from llama_index.core.vector_stores import SimpleVectorStore
from llama_index.core import StorageContext# create storage context using default stores
storage_context StorageContext.from_defaults(docstoreSimpleDocumentStore(),vector_storeSimpleVectorStore(),index_storeSimpleIndexStore(),
)有关自定义/持久性的更多详细信息可以在下面的指南中找到。
定制化保存/加载 3、模块
我们提供有关不同存储组件的深入指南。
Vector StoresDocstoresIndex StoresKey-Val StoresGraph StoresChatStores 二、Vector Stores
向量存储包含所摄取文档块有时也包含文档块的嵌入向量。 1、简单向量存储
默认情况下LlamaIndex 使用简单的内存向量存储非常适合快速实验。它们可以通过调用vector_store.persist()分别保存到磁盘并从磁盘加载SimpleVectorStore.from_persist_path(...)。 2、矢量存储选项和功能支持
LlamaIndex 支持 20 多种不同的矢量存储选项。我们正在积极添加更多集成并提高每个集成的功能覆盖范围。
Vector StoreTypeMetadata FilteringHybrid SearchDeleteStore DocumentsAsyncApache Cassandra®self-hosted / cloud✓✓✓Astra DBcloud✓✓✓Azure AI Searchcloud✓✓✓✓Azure CosmosDB MongoDBcloud✓✓BaiduVectorDBcloud✓✓✓ChatGPT Retrieval Pluginaggregator✓✓Chromaself-hosted✓✓✓DashVectorcloud✓✓✓✓Databrickscloud✓✓✓Deeplakeself-hosted / cloud✓✓✓DocArrayaggregator✓✓✓DuckDBin-memory / self-hosted✓✓✓DynamoDBcloud✓Elasticsearchself-hosted / cloud✓✓✓✓✓FAISSin-memorytxtaiin-memoryJaguarself-hosted / cloud✓✓✓✓LanceDBcloud✓✓✓Lanternself-hosted / cloud✓✓✓✓✓Metalcloud✓✓✓MongoDB Atlasself-hosted / cloud✓✓✓MyScalecloud✓✓✓✓Milvus / Zillizself-hosted / cloud✓✓✓Neo4jVectorself-hosted / cloud✓✓OpenSearchself-hosted / cloud✓✓✓✓✓Pineconecloud✓✓✓✓Postgresself-hosted / cloud✓✓✓✓✓pgvecto.rsself-hosted / cloud✓✓✓✓Qdrantself-hosted / cloud✓✓✓✓✓Redisself-hosted / cloud✓✓✓Simplein-memory✓✓SingleStoreself-hosted / cloud✓✓✓Supabaseself-hosted / cloud✓✓✓Taircloud✓✓✓TiDBcloud✓✓✓TencentVectorDBcloud✓✓✓✓Timescale✓✓✓✓Typesenseself-hosted / cloud✓✓✓Upstashcloud✓Weaviateself-hosted / cloud✓✓✓✓
有关更多详细信息请参阅矢量存储集成。 3、Example Notebooks
Astra DBAsync Index CreationAzure AI SearchAzure Cosmos DBBaiduCaasandraChromadbDashDatabricksDeeplakeDocArray HNSWDocArray in-MemoryDuckDBEspillaJaguarLanceDBLanternMetalMilvusMyScaleElsaticSearchFAISSMongoDB AtlasNeo4jOpenSearchPineconePinecone Hybrid SearchPGvectoRSPostgresRedisQdrantQdrant Hybrid SearchRocksetSimpleSupabaseTairTiDBTencentTimesacleUpstashWeaviateWeaviate Hybrid SearchZep 三、文件存储
文档存储包含摄取的文档块我们将其称为Node对象。
有关更多详细信息请参阅API 参考。 1、简单文档存储
默认情况下将对象SimpleDocumentStore存储Node在内存中。它们可以通过调用docstore.persist()分别保存到磁盘并从磁盘加载SimpleDocumentStore.from_persist_path(...)。
可以在这里找到更完整的示例 2、MongoDB 文档存储
我们支持 MongoDB 作为替代文档存储后端在Node摄取对象时保留数据。
from llama_index.storage.docstore.mongodb import MongoDocumentStore
from llama_index.core.node_parser import SentenceSplitter# create parser and parse document into nodes
parser SentenceSplitter()
nodes parser.get_nodes_from_documents(documents)# create (or load) docstore and add nodes
docstore MongoDocumentStore.from_uri(urimongodbsrv://...)
docstore.add_documents(nodes)# create storage context
storage_context StorageContext.from_defaults(docstoredocstore)# build index
index VectorStoreIndex(nodes, storage_contextstorage_context)在后台MongoDocumentStore连接到固定的 MongoDB 数据库并为节点初始化新集合或加载现有集合。 注意您可以在实例化时配置db_name和否则它们默认为和。namespaceMongoDocumentStoredb_namedb_docstorenamespacedocstore 请注意使用 an 时无需调用storage_context.persist()(或) 因为默认情况下数据是持久保存的。docstore.persist()MongoDocumentStore
MongoDocumentStore您可以轻松地重新连接到 MongoDB 集合并通过使用现有的db_name和重新初始化 来重新加载索引collection_name。
可以在这里找到更完整的示例 3、Redis 文档存储
我们支持 Redis 作为替代文档存储后端在Node摄取对象时保留数据。
from llama_index.storage.docstore.redis import RedisDocumentStore
from llama_index.core.node_parser import SentenceSplitter# create parser and parse document into nodes
parser SentenceSplitter()
nodes parser.get_nodes_from_documents(documents)# create (or load) docstore and add nodes
docstore RedisDocumentStore.from_host_and_port(host127.0.0.1, port6379, namespacellama_index
)
docstore.add_documents(nodes)# create storage context
storage_context StorageContext.from_defaults(docstoredocstore)# build index
index VectorStoreIndex(nodes, storage_contextstorage_context)在后台RedisDocumentStore连接到 redis 数据库并将节点添加到存储在{namespace}/docs. namespace注意实例化时可以配置RedisDocumentStore否则默认namespacedocstore。 RedisDocumentStore您可以轻松地重新连接到 Redis 客户端并通过使用现有的host、port和重新初始化 来重新加载索引namespace。
可以在这里找到更完整的示例 4、Firestore 文档存储
我们支持 Firestore 作为替代文档存储后端在Node摄取对象时保留数据。
from llama_index.storage.docstore.firestore import FirestoreDocumentStore
from llama_index.core.node_parser import SentenceSplitter# create parser and parse document into nodes
parser SentenceSplitter()
nodes parser.get_nodes_from_documents(documents)# create (or load) docstore and add nodes
docstore FirestoreDocumentStore.from_database(projectproject-id,database(default),
)
docstore.add_documents(nodes)# create storage context
storage_context StorageContext.from_defaults(docstoredocstore)# build index
index VectorStoreIndex(nodes, storage_contextstorage_context)在后台FirestoreDocumentStore连接到 Google Cloud 中的 firestore 数据库并将您的节点添加到存储在{namespace}/docs. namespace注意实例化时可以配置FirestoreDocumentStore否则默认namespacedocstore。 FirestoreDocumentStore您可以轻松地重新连接到 Firestore 数据库并通过使用现有的project、database和重新初始化 来重新加载索引namespace。
可以在这里找到更完整的示例 四、索引存储
索引存储包含轻量级索引元数据即构建索引时创建的附加状态信息。
有关更多详细信息请参阅API 参考。 1、简单索引存储
默认情况下LlamaIndex 使用由内存中键值存储支持的简单索引存储。它们可以通过调用index_store.persist()分别保存到磁盘并从磁盘加载SimpleIndexStore.from_persist_path(...)。 2、MongoDB 索引存储
与文档存储类似我们也可以将其用作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(urimongodbsrv://...)# create storage context
storage_context StorageContext.from_defaults(index_storeindex_store)# build index
index VectorStoreIndex(nodes, storage_contextstorage_context)# or alternatively, load index
from llama_index.core import load_index_from_storageindex load_index_from_storage(storage_context)在后台MongoIndexStore连接到固定的 MongoDB 数据库并为索引元数据初始化新集合或加载现有集合。 注意您可以在实例化时配置db_name和否则它们默认为和。namespaceMongoIndexStoredb_namedb_docstorenamespacedocstore 请注意使用 an 时无需调用storage_context.persist()(或) 因为默认情况下数据是持久保存的。index_store.persist()MongoIndexStore
MongoIndexStore您可以轻松地重新连接到 MongoDB 集合并通过使用现有的db_name和重新初始化 来重新加载索引collection_name。
可以在这里找到更完整的示例 3、Redis索引存储
我们支持 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(host127.0.0.1, port6379, namespacellama_index
)# create storage context
storage_context StorageContext.from_defaults(index_storeindex_store)# build index
index VectorStoreIndex(nodes, storage_contextstorage_context)# or alternatively, load index
from llama_index.core import load_index_from_storageindex load_index_from_storage(storage_context)在后台RedisIndexStore连接到 redis 数据库并将节点添加到存储在{namespace}/index. namespace注意实例化时可以配置RedisIndexStore否则默认namespaceindex_store。 RedisIndexStore您可以轻松地重新连接到 Redis 客户端并通过使用现有的host、port和重新初始化 来重新加载索引namespace。
可以在这里找到更完整的示例 五、Chat Stores
聊天存储充当存储您的聊天历史记录的集中式界面。与其他存储格式相比聊天历史记录是独一无二的因为消息的顺序对于维持整体对话非常重要。
user_ids聊天存储可以通过键如或其他唯一可识别字符串组织聊天消息序列并处理delete、insert、 和get操作。 1、简单聊天商店
最基本的聊天存储是SimpleChatStore它将消息存储在内存中并且可以保存到磁盘或从磁盘保存也可以序列化并存储在其他地方。
通常您将实例化一个聊天存储并将其提供给内存模块。SimpleChatStore如果未提供则使用聊天存储的内存模块将默认使用。
from llama_index.core.storage.chat_store import SimpleChatStore
from llama_index.core.memory import ChatMemoryBufferchat_store SimpleChatStore()chat_memory ChatMemoryBuffer.from_defaults(token_limit3000,chat_storechat_store,chat_store_keyuser1,
)创建内存后您可以将其包含在代理或聊天引擎中
agent OpenAIAgent.from_tools(tools, memorymemory)
# OR
chat_engine index.as_chat_engine(memorymemory)要保存聊天存储供以后使用您可以从磁盘保存/加载
chat_store.persist(persist_pathchat_store.json)
loaded_chat_store SimpleChatStore.from_persist_path(persist_pathchat_store.json
)或者您可以与字符串进行转换同时将字符串保存在其他位置
chat_store_string chat_store.json()
loaded_chat_store SimpleChatStore.parse_raw(chat_store_string)2、Redis聊天商店
使用RedisChatStore您可以远程存储聊天记录而不必担心手动保存和加载聊天记录。
from llama_index.storage.chat_store.redis import RedisChatStore
from llama_index.core.memory import ChatMemoryBufferchat_store RedisChatStore(redis_urlredis://localhost:6379, ttl300)chat_memory ChatMemoryBuffer.from_defaults(token_limit3000,chat_storechat_store,chat_store_keyuser1,
)六、键值存储
键值存储是为我们的文档存储和索引存储提供支持的底层存储抽象。
我们提供以下键值存储
简单键值存储内存中的 KV 存储。用户可以选择调用persist这个kv存储来将数据保存到磁盘。MongoDB 键值存储MongoDB KV 存储。
有关更多详细信息请参阅API 参考。
注意目前这些存储抽象不面向外部。 七、保存和加载数据
1、持久化数据
默认情况下LlamaIndex 将数据存储在内存中如果需要可以显式保留该数据
storage_context.persist(persist_dirpersist_dir)这将根据指定persist_dir或./storage默认将数据保存到磁盘。
假设您跟踪要加载的索引 ID则可以保留多个索引并从同一目录加载多个索引。
用户还可以配置MongoDB默认保存数据的替代存储后端例如。在这种情况下调用storage_context.persist()将不会执行任何操作。 2、加载数据中
要加载数据用户只需使用相同的配置重新创建存储上下文例如传入相同的persist_dir或向量存储客户端。
storage_context StorageContext.from_defaults(docstoreSimpleDocumentStore.from_persist_dir(persist_dirpersist_dir),vector_storeSimpleVectorStore.from_persist_dir(persist_dirpersist_dir),index_storeSimpleIndexStore.from_persist_dir(persist_dirpersist_dir),
)StorageContext然后我们可以通过下面的一些便利函数加载特定的索引。
from llama_index.core import (load_index_from_storage,load_indices_from_storage,load_graph_from_storage,
)# load a single index
# need to specify index_id if multiple indexes are persisted to the same directory
index load_index_from_storage(storage_context, index_idindex_id)# dont need to specify index_id if theres only one index in storage context
index load_index_from_storage(storage_context)# load multiple indices
indices load_indices_from_storage(storage_context) # loads all indices
indices load_indices_from_storage(storage_context, index_ids[index_id1, ...]
) # loads specific indices# load composable graph
graph load_graph_from_storage(storage_context, root_idroot_id
) # loads graph with the specified root_id3、使用远程后端
默认情况下LlamaIndex 使用本地文件系统来加载和保存文件。但是您可以通过传递对象来覆盖它fsspec.AbstractFileSystem。
这是一个简单的例子实例化一个向量存储
import dotenv
import s3fs
import osdotenv.load_dotenv(../../../.env)# load documents
documents SimpleDirectoryReader(../../../examples/paul_graham_essay/data/
).load_data()
print(len(documents))
index VectorStoreIndex.from_documents(documents)至此一切都已经是一样了。现在 - 让我们实例化一个 S3 文件系统并从那里保存/加载。
# set up s3fs
AWS_KEY os.environ[AWS_ACCESS_KEY_ID]
AWS_SECRET os.environ[AWS_SECRET_ACCESS_KEY]
R2_ACCOUNT_ID os.environ[R2_ACCOUNT_ID]assert AWS_KEY is not None and AWS_KEY ! s3 s3fs.S3FileSystem(keyAWS_KEY,secretAWS_SECRET,endpoint_urlfhttps://{R2_ACCOUNT_ID}.r2.cloudflarestorage.com,s3_additional_kwargs{ACL: public-read},
)# If youre using 2 indexes with the same StorageContext,
# run this to save the index to remote blob storage
index.set_index_id(vector_index)# persist index to s3
s3_bucket_name llama-index/storage_demo # {bucket_name}/{index_name}
index.storage_context.persist(persist_dirs3_bucket_name, fss3)# load index from s3
index_from_s3 load_index_from_storage(StorageContext.from_defaults(persist_dirs3_bucket_name, fss3),index_idvector_index,
)默认情况下如果您不传递文件系统我们将假定本地文件系统。 八、自定义存储
默认情况下LlamaIndex 隐藏了复杂性让您可以用不到 5 行代码查询数据
from llama_index.core import VectorStoreIndex, SimpleDirectoryReaderdocuments SimpleDirectoryReader(data).load_data()
index VectorStoreIndex.from_documents(documents)
query_engine index.as_query_engine()
response query_engine.query(Summarize the documents.)在底层LlamaIndex 还支持可交换的存储层允许您自定义提取的文档即Node对象、嵌入向量和索引元数据的存储位置。 低级API
为此无需使用高级 API
index VectorStoreIndex.from_documents(documents)我们使用较低级别的 API 来提供更精细的控制
from llama_index.core.storage.docstore import SimpleDocumentStore
from llama_index.core.storage.index_store import SimpleIndexStore
from llama_index.core.vector_stores import SimpleVectorStore
from llama_index.core.node_parser import SentenceSplitter# create parser and parse document into nodes
parser SentenceSplitter()
nodes parser.get_nodes_from_documents(documents)# create storage context using default stores
storage_context StorageContext.from_defaults(docstoreSimpleDocumentStore(),vector_storeSimpleVectorStore(),index_storeSimpleIndexStore(),
)# create (or load) docstore and add nodes
storage_context.docstore.add_documents(nodes)# build index
index VectorStoreIndex(nodes, storage_contextstorage_context)# save index
index.storage_context.persist(persist_dirpersist_dir)# can also set index_id to save multiple indexes to the same folder
index.set_index_id(index_id)
index.storage_context.persist(persist_dirpersist_dir)# to load index later, make sure you setup the storage context
# this will loaded the persisted stores from persist_dir
storage_context StorageContext.from_defaults(persist_dirpersist_dir)# then load the index object
from llama_index.core import load_index_from_storageloaded_index load_index_from_storage(storage_context)# if loading an index from a persist_dir containing multiple indexes
loaded_index load_index_from_storage(storage_context, index_idindex_id)# if loading multiple indexes from a persist dir
loaded_indicies load_index_from_storage(storage_context, index_ids[index_id, ...]
)您可以通过一行更改来自定义底层存储以实例化不同的文档存储、索引存储和向量存储。有关更多详细信息请参阅文档存储、向量存储、索引存储指南。 矢量存储集成和存储
我们的大多数矢量存储集成将整个索引矢量文本存储在矢量存储本身中。这样做的主要好处是不必显式地持久化索引如上所示因为矢量存储已经托管并将数据持久化在我们的索引中。
支持这种做法的矢量存储是
AzureAISearchVectorStoreChatGPTRetrievalPluginClientCassandraVectorStoreChromaVectorStoreEpsillaVectorStoreDocArrayHnswVectorStoreDocArrayInMemoryVectorStoreJaguarVectorStoreLanceDBVectorStoreMetalVectorStoreMilvusVectorStoreMyScaleVectorStoreOpensearchVectorStorePineconeVectorStoreQdrantVectorStoreRedisVectorStoreUpstashVectorStoreWeaviateVectorStore 下面是一个使用 Pinecone 的小例子
import pinecone
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
from llama_index.vector_stores.pinecone import PineconeVectorStore# Creating a Pinecone index
api_key api_key
pinecone.init(api_keyapi_key, environmentus-west1-gcp)
pinecone.create_index(quickstart, dimension1536, metriceuclidean, pod_typep1
)
index pinecone.Index(quickstart)# construct vector store
vector_store PineconeVectorStore(pinecone_indexindex)# create storage context
storage_context StorageContext.from_defaults(vector_storevector_store)# load documents
documents SimpleDirectoryReader(./data).load_data()# create index, which will insert documents/vectors to pinecone
index VectorStoreIndex.from_documents(documents, storage_contextstorage_context
)如果您有一个已加载数据的现有矢量存储您可以连接到它并直接创建一个VectorStoreIndex如下所示
index pinecone.Index(quickstart)
vector_store PineconeVectorStore(pinecone_indexindex)
loaded_index VectorStoreIndex.from_vector_store(vector_storevector_store)2014-04-15一