从 LlamaIndex 官方 GitHub README 翻译整理的快速上手指南。
LlamaIndex 是一个开源的 AI 框架,专门用来构建智能体应用(Agentic Applications)。它由 LlamaIndex 团队维护,GitHub 地址:run-llama/llama_index。
简单说,LlamaIndex 是一个数据与 LLM 之间的桥梁——它能帮你把各种格式的数据(PDF、网页、数据库等)变成大模型能理解的结构,然后基于这些数据做问答、分析、自动化任务。
确保 Python 版本 >= 3.8,然后通过 pip 安装:
pip install llama-index
如果需要使用 LlamaParse(企业级文档解析),额外安装:
pip install llama-parse
注意:LlamaParse 是云端服务,国内访问可能受限,推荐先体验本地核心功能。
下面是一个最简单的 RAG 示例——从本地文档中读取内容,然后让 LLM 回答相关问题。
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
# 1. 加载本地文档(支持 .txt, .pdf, .docx 等)
documents = SimpleDirectoryReader("data").load_data()
# 2. 构建索引
index = VectorStoreIndex.from_documents(documents)
# 3. 创建问答引擎
query_engine = index.as_query_engine()
# 4. 提问
response = query_engine.query("这份文档的主要内容是什么?")
print(response)
运行前准备:
data/ 文件夹,放入你的文档翻译整理自 GitHub README,原文:https://github.com/run-llama/llama_index