run-llama/llama_index
GitHub: run-llama/llama_index
LlamaIndex 是一个开源数据框架,帮助开发者将私有数据与大语言模型连接,构建检索增强生成(RAG)和文档智能体应用。
Stars: 50371 | Forks: 7632
# 🗂️ LlamaIndex 🦙
[](https://pypi.org/project/llama-index/)
[](https://github.com/run-llama/llama_index/actions/workflows/build_package.yml)
[](https://github.com/jerryjliu/llama_index/graphs/contributors)
[](https://discord.gg/dGcwcsnxhU)
[](https://x.com/llama_index)
[](https://www.reddit.com/r/LlamaIndex/)
[](https://www.phorm.ai/query?projectId=c5863b56-6703-4a5d-87b6-7e6031bf16b6)
LlamaIndex OSS(由 [LlamaIndex](https://llamaindex.ai?utm_medium=li_github&utm_source=github&utm_campaign=2026--) 提供)是一个用于构建 agentic 应用的开源框架。**[Parse](https://cloud.llamaindex.ai?utm_medium=li_github&utm_source=github&utm_campaign=2026--)** 是我们的企业级平台,提供 agentic OCR、解析、提取、索引等功能。你可以将 LlamaParse 与此框架结合使用,也可以单独使用它;有关注册和产品链接,请参阅下方的 [LlamaParse](#llamacloud-document-agent-platform)。
使用 LlamaIndex 进行构建通常涉及使用 LlamaIndex core 以及一组选定的集成(或插件)。在以下环境中开始使用 LlamaIndex 构建有两种方法
Python:
1. **入门版**:[`llama-index`](https://pypi.org/project/llama-index/)。一个入门级的 Python 包,包含核心 LlamaIndex 以及一系列精选的集成。
2. **自定义版**:[`llama-index-core`](https://pypi.org/project/llama-index-core/)。安装核心 LlamaIndex,并在 [LlamaHub](https://llamahub.ai/) 上添加你的应用所需的
LlamaIndex 集成包。有超过 300 个 LlamaIndex 集成
包可以与核心组件无缝协作,让你能够使用首选的
LLM、embedding 和 vector store 提供商进行构建。
LlamaIndex Python 库采用了命名空间设计,因此
包含 `core` 的导入语句意味着正在使用核心包。相反,那些
不包含 `core` 的语句意味着正在使用某个集成包。
```
# 典型模式
from llama_index.core.xxx import ClassABC # core submodule xxx
from llama_index.xxx.yyy import (
SubclassABC,
) # integration yyy for submodule xxx
# 具体示例
from llama_index.core.llms import LLM
from llama_index.llms.openai import OpenAI
```
### LlamaParse(文档 agent 平台)
**LlamaParse** 是一个独立的平台——专注于文档 agent 和 agentic OCR。它包括 **Parse**(解析)、**LlamaAgents**(已部署的文档 agent)、**Extract**(结构化提取)和 **Index**(摄取与 RAG)。你可以将其与 LlamaIndex 框架结合使用,也可以单独使用。
- **[注册 LlamaParse](https://cloud.llamaindex.ai?utm_medium=li_github&utm_source=github&utm_campaign=2026--)** — 创建账号并获取你的 API key。
- **Parse** — Agentic OCR 和文档解析(支持 130 多种格式)。[文档](https://developers.llamaindex.ai/python/cloud/llamaparse/?utm_medium=li_github&utm_source=github&utm_campaign=2026--)
- **Extract** — 从文档中提取结构化数据。[文档](https://developers.llamaindex.ai/python/cloud/llamaextract/?utm_medium=li_github&utm_source=github&utm_campaign=2026--)
- **Index** — 摄取、索引和 RAG pipeline。[文档](https://developers.llamaindex.ai/python/cloud/llamacloud/?utm_medium=li_github&utm_source=github&utm_campaign=2026--)
- **Split** — 将大型文档拆分为子类别。[文档](https://developers.llamaindex.ai/python/cloud/split/getting_started/?utm_medium=li_github&utm_source=github&utm_campaign=2026--)
- **Agents** — 使用 `Workflows` 和 Agent Builder 构建端到端的文档 agent。[文档](https://developers.llamaindex.ai/python/llamaagents/overview/?utm_medium=li_github&utm_source=github&utm_campaign=2026--)
### 重要链接
[文档](https://developers.llamaindex.ai/python/framework/?utm_medium=li_github&utm_source=github&utm_campaign=2026--)
[X (原 Twitter)](https://x.com/llama_index)
[LinkedIn](https://www.linkedin.com/company/llamaindex/)
[Reddit](https://www.reddit.com/r/LlamaIndex/)
[Discord](https://discord.gg/dGcwcsnxhU)
## 🚀 概览
**注意**:本 README 的更新频率不如文档。请查看上方文档以获取最新更新!
### 背景
- LLM 是一项在知识生成和推理方面极其出色的技术。它们是在大量公开可用数据上进行预训练的。
- 我们如何才能最好地用我们自己的私有数据来增强 LLM?
我们需要一个全面的工具包来帮助执行这种针对 LLM 的数据增强。
### 提议的解决方案
这正是 **LlamaIndex** 发挥作用的地方。LlamaIndex 是一个“数据框架”,可帮助你构建 LLM 应用。它提供了以下工具:
- 提供**数据连接器**,用于摄取现有的数据源和数据格式(API、PDF、文档、SQL 等)。
- 提供各种方法来**结构化你的数据**(indices、graphs),以便这些数据可以轻松地与 LLM 一起使用。
- 提供**基于数据的先进检索/查询接口**:输入任何 LLM 提示词,获取检索到的上下文和经过知识增强的输出。
- 允许与你外部的应用框架(例如 LangChain、Flask、Docker、ChatGPT 或任何其他框架)进行轻松集成。
LlamaIndex 为初级用户和高级用户都提供了相应的工具。我们的高级 API 允许初级用户在
5 行代码内使用 LlamaIndex 摄取和查询他们的数据。我们的低级 API 允许高级用户自定义和扩展任何模块(数据连接器、indices、retrievers、query engines、reranking 模块),
以满足他们的需求。
## 📄 文档
完整文档可以在[这里](https://developers.llamaindex.ai/python/framework/?utm_medium=li_github&utm_source=github&utm_campaign=2026--)找到
请查看文档以获取最新的教程、操作指南、参考文档和其他资源!
## 💻 使用示例
```
# 自定义选择与 core 配合使用的 integrations
pip install llama-index-core
pip install llama-index-llms-openai
pip install llama-index-llms-ollama
pip install llama-index-embeddings-huggingface
```
示例位于 `docs/examples` 文件夹中。Indices 位于 `indices` 文件夹中(请参阅下方的 indices 列表)。
使用 OpenAI 构建一个简单的 vector store index:
```
import os
os.environ["OPENAI_API_KEY"] = "YOUR_OPENAI_API_KEY"
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
documents = SimpleDirectoryReader("YOUR_DATA_DIRECTORY").load_data()
index = VectorStoreIndex.from_documents(documents)
```
使用非 OpenAI 的 LLM(例如通过 Ollama 托管的 LLM)构建一个简单的 vector store index:
```
from llama_index.core import Settings, VectorStoreIndex, SimpleDirectoryReader
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
from llama_index.llms.ollama import Ollama
from transformers import AutoTokenizer
# 设置 LLM
Settings.llm = Ollama(
model="llama-3.1:latest",
request_timeout=360.0,
)
# 设置 tokenizer 以匹配 LLM
Settings.tokenizer = AutoTokenizer.from_pretrained(
"meta-llama/Llama-3.1-8B-Instruct"
)
# 设置 embed model
Settings.embed_model = HuggingFaceEmbedding(
model_name="BAAI/bge-small-en-v1.5"
)
documents = SimpleDirectoryReader("YOUR_DATA_DIRECTORY").load_data()
index = VectorStoreIndex.from_documents(
documents,
)
```
查询:
```
query_engine = index.as_query_engine()
query_engine.query("YOUR_QUESTION")
```
默认情况下,数据存储在内存中。
持久化到磁盘(在 `./storage` 目录下):
```
index.storage_context.persist()
```
从磁盘重新加载:
```
from llama_index.core import StorageContext, load_index_from_storage
# 重建 storage context
storage_context = StorageContext.from_defaults(persist_dir="./storage")
# 加载 index
index = load_index_from_storage(storage_context)
```
## 关于构建产物验证的说明
默认情况下,`llama-index-core` 包含一个 `_static` 文件夹,其中包含随包安装一起提供的 nltk 和 tiktoken 缓存。这确保了你能够在运行时磁盘访问权限受限的环境中轻松运行 `llama-index`。
为了验证这些文件安全且有效,我们使用了 github 的 `attest-build-provenance` action。此 action 将验证 `_static` 文件夹中的文件是否与 `llama-index-core/llama_index/core/_static` 文件夹中的文件相同。
要验证这一点,你可以运行以下脚本(指向你已安装的包):
```
#!/bin/bash
STATIC_DIR="venv/lib/python3.13/site-packages/llama_index/core/_static"
REPO="run-llama/llama_index"
find "$STATIC_DIR" -type f | while read -r file; do
echo "Verifying: $file"
gh attestation verify "$file" -R "$REPO" || echo "Failed to verify: $file"
done
```
## 📖 引用
如果你在论文中使用 LlamaIndex,请引用以下内容:
```
@software{Liu_LlamaIndex_2022,
author = {Liu, Jerry},
doi = {10.5281/zenodo.1234},
month = {11},
title = {{LlamaIndex}},
url = {https://github.com/jerryjliu/llama_index},
year = {2022}
}
```
标签:AI风险缓解, C2, DLL 劫持, OCR, Python, RAG, 人工智能, 大语言模型, 文档智能, 无后门, 用户模式Hook绕过, 逆向工具