索引层级:向量、树、关键词
比较 VectorStoreIndex(默认)、TreeIndex(摘要)和 KeywordTableIndex(稀疏)。
索引层级:向量、树、关键词 是 CoddyKit 上的免费 AI Agents 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 AI Agents 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 AI Agents 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
LlamaIndex 中的索引
LlamaIndex 将其检索数据结构称为“索引”。索引有多种类型,每种类型都适合不同的任务。
VectorStoreIndex(默认)
基于嵌入的标准索引——您日常使用的首选:
from llama_index.core import VectorStoreIndex
index = VectorStoreIndex.from_documents(documents)
query_engine = index.as_query_engine()
print(query_engine.query('What is in the handbook?'))TreeIndex
构建分层摘要树——适用于摘要类查询:
from llama_index.core import TreeIndex
index = TreeIndex.from_documents(documents)
# Internal nodes are summaries of their children.
# Good for 'summarise the whole corpus' tasks.KeywordTableIndex
经典的基于关键词的索引——当基于嵌入的方法在罕见术语上失效时使用:
from llama_index.core import KeywordTableIndex
index = KeywordTableIndex.from_documents(documents)
# Falls back to exact-match for rare technical terms.SummaryIndex(原名为 ListIndex)
包含每个分块的扁平列表——当您希望 LLM 查看 ALL 数据时很有用:
from llama_index.core import SummaryIndex
index = SummaryIndex.from_documents(documents)
# Best for short corpora where you can stuff everything into context.KnowledgeGraphIndex
从文档中提取知识图谱(实体 + 关系):
from llama_index.core import KnowledgeGraphIndex
index = KnowledgeGraphIndex.from_documents(
documents,
max_triplets_per_chunk=10
)选择索引
| 任务 | 索引 |
|---|---|
| 按相似度问答 | VectorStoreIndex |
| 对整个语料库进行摘要 | TreeIndex / SummaryIndex |
| 罕见的精确术语 | KeywordTableIndex |
| 关系事实 | KnowledgeGraphIndex |
组合索引
您可以堆叠多个索引——例如,在每个文档的向量索引之上构建摘要树:
from llama_index.core import ComposableGraph
graph = ComposableGraph.from_indices(
TreeIndex,
[vector_index_doc_1, vector_index_doc_2, ...],
index_summaries=['Doc 1 summary', 'Doc 2 summary']
)Persisting an Index
index.storage_context.persist(persist_dir='./store')
# Reload later:
from llama_index.core import StorageContext, load_index_from_storage
ctx = StorageContext.from_defaults(persist_dir='./store')
index = load_index_from_storage(ctx)使用外部向量存储
将 LlamaIndex 与 Pinecone、Qdrant、Weaviate 等配合使用:
from llama_index.vector_stores.pinecone import PineconeVectorStore
from llama_index.core import StorageContext, VectorStoreIndex
vector_store = PineconeVectorStore(pinecone_index=pc_index)
storage = StorageContext.from_defaults(vector_store=vector_store)
index = VectorStoreIndex.from_documents(documents, storage_context=storage)混合检索
通过独立索引和融合结果的查询引擎,将向量检索与关键词检索结合起来。
刷新索引
对于文档更新,LlamaIndex 支持增量刷新:
index.refresh_ref_docs(updated_documents)用于摘要的索引
哪种 LlamaIndex 类型最适合“对整个语料库进行摘要”?
回顾
请选择与任务匹配的索引。默认使用 VectorStoreIndex;对于特定需求,再选择 TreeIndex、KeywordTableIndex 或 KnowledgeGraphIndex。
常见问题解答
「索引层级:向量、树、关键词」课时是免费的吗?
是的 — 「索引层级:向量、树、关键词」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 AI Agents 课程的其余内容,请升级到 CoddyKit PRO。 AI Agents 课程共包含 4 节课。
「索引层级:向量、树、关键词」这节课中我会学到什么?
比较 VectorStoreIndex(默认)、TreeIndex(摘要)和 KeywordTableIndex(稀疏)。 你通过在浏览器中直接运行的动手代码来练习 AI Agents,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 AI Agents 需要有经验吗?
无需任何先前经验。CoddyKit 上的 AI Agents 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。
「索引层级:向量、树、关键词」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 AI Agents 课中编写并运行代码吗?
能。每节 AI Agents 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。