llama_index 如何限制VectorStoreIndex的from_documents资源使用?

uemypmqf  于 2个月前  发布在  其他
关注(0)|答案(2)|浏览(23)

问题验证

  • 我已在文档和Discord中寻找答案。

问题

hgqdbh6s

hgqdbh6s1#

你好@atlas-comstock!我是一个机器人,可以帮助你解决任何问题或疑问。如果你需要帮助或有任何问题,请随时提问!
为了限制VectorStoreIndexfrom_documents资源使用,你可以调整向量生成和插入的批处理大小。默认情况下,VectorStoreIndex以2048个节点的批次生成和插入向量。如果你的内存受限,可以通过传递insert_batch_size参数来降低此批处理大小。
以下是一个示例:

from llama_index.core import VectorStoreIndex, SimpleDirectoryReader

# Load documents
documents = SimpleDirectoryReader("../../examples/data/paul_graham").load_data()

# Build index with a smaller batch size to limit resource usage
index = VectorStoreIndex.from_documents(documents, insert_batch_size=512)

此外,如果你使用的是Vectra后端,可以使用parallelize_ingest参数来控制摄取过程是否应该并行化。这有助于更有效地管理资源使用。
以下是Vectra的示例:

from llama_index.legacy.indices.managed.vectara.base import VectaraIndex
from llama_index.legacy.schema import Document

# Load documents
documents = [Document(text="Sample text", metadata={"author": "Author"})]

# Build Vectara index with parallel ingestion to limit resource usage
index = VectaraIndex.from_documents(documents, parallelize_ingest=True)

要更精细地控制资源使用,可以使用低级API自定义存储上下文。这允许你管理导入的文档、嵌入向量和索引元数据的存储位置,从而有助于优化资源使用。
以下是使用低级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

# Load documents
documents = SimpleDirectoryReader("../../examples/data/paul_graham").load_data()

# 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(
    docstore=SimpleDocumentStore(),
    vector_store=SimpleVectorStore(),
    index_store=SimpleIndexStore(),
)

# Add nodes to docstore
storage_context.docstore.add_documents(nodes)

# Build index
index = VectorStoreIndex(nodes, storage_context=storage_context)

# Save index
index.storage_context.persist(persist_dir="<persist_dir>")

这些调整有助于更有效地管理内存和处理资源,特别是在处理大型数据集或有限系统资源时。此外,VectorStoreIndex类提供了异步方法用于嵌入和添加节点,这可以利用异步处理1(https://github.com/run-llama/llama_index/blob/main/docs/docs/module_guides/indexing/vector_store_index.md)2(https://github.com/run-llama/llama_index/blob/main/llama-index-legacy/llama_index/legacy/indices/managed/vectara/base.py)3(https://github.com/run-llama/llama_index/blob/main/docs/docs/module_guides/storing/customization.md)4(https://github.com/run-llama/llama_index/blob/main/llama-index-legacy/llama_index/legacy/indices/vector_store/base.py)进一步优化资源使用。

  • 要继续与Dosu交谈,请提及@dosu。*
wi3ka0sx

wi3ka0sx2#

很难说,除非看到你的代码。
如果你使用本地嵌入,你可能需要设置批处理大小。例如:
HuggingFaceEmbedding(..., embed_batch_size=2)
如果你索引大量数据,你可能需要使用专用的向量存储集成,而不是使用默认的内存选项。

相关问题