LlamaIndex 数据索引与 RAG
LlamaIndex 面向 RAG 和数据索引,提供文档加载、分块、索引、检索、查询引擎和 Agent。适合把私有数据接入大模型。
核心概念:Document、Node、Index、Retriever、QueryEngine、VectorStoreIndex。
安装:
pip install llama-index llama-index-vector-stores-qdrant
构建索引:
from llama_index.core import SimpleDirectoryReader, VectorStoreIndex
documents = SimpleDirectoryReader("./docs").load_data()
index = VectorStoreIndex.from_documents(documents)
query_engine = index.as_query_engine()
print(query_engine.query("如何排查磁盘满?"))
持久化:
index.storage_context.persist(persist_dir="./storage")
加载已有索引:
from llama_index.core import StorageContext, load_index_from_storage
storage_context = StorageContext.from_defaults(persist_dir="./storage")
index = load_index_from_storage(storage_context)
similarity_top_k 太小,答案缺少上下文。