LlamaIndex 基础 RAG
使用 LlamaIndex 实现基础 RAG
🌐 查看英文原文 | 源码 Notebook
使用 LlamaIndex 构建 RAG Pipeline
在本教程中,我们将学习如何使用 LlamaIndex 构建基础 RAG Pipeline。该流程包含以下步骤。
- 设置 LLM 和 Embedding 模型。
- 下载数据。
- 加载数据。
- 索引数据。
- 创建查询引擎。
- 查询。
安装
!pip install llama-index
!pip install llama-index-llms-anthropic
!pip install llama-index-embeddings-huggingface设置 API Keys
import os
os.environ["ANTHROPIC_API_KEY"] = "YOUR Claude API KEY"设置 LLM 和 Embedding 模型
我们将使用 Anthropic 最新发布的 Claude 3 Opus 模型
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
from llama_index.llms.anthropic import Anthropicllm = Anthropic(temperature=0.0, model="claude-opus-4-1")
embed_model = HuggingFaceEmbedding(model_name="BAAI/bge-base-en-v1.5")config.json: 0%| | 0.00/777 [00:00<?, ?B/s]
model.safetensors: 0%| | 0.00/438M [00:00<?, ?B/s]
tokenizer_config.json: 0%| | 0.00/366 [00:00<?, ?B/s]
vocab.txt: 0%| | 0.00/232k [00:00<?, ?B/s]
tokenizer.json: 0%| | 0.00/711k [00:00<?, ?B/s]
special_tokens_map.json: 0%| | 0.00/125 [00:00<?, ?B/s]
from llama_index.core import Settings
Settings.llm = llm
Settings.embed_model = embed_model
Settings.chunk_size = 512下载数据
!mkdir -p 'data/paul_graham/'
!wget 'https://raw.githubusercontent.com/run-llama/llama_index/main/docs/examples/data/paul_graham/paul_graham_essay.txt' -O 'data/paul_graham/paul_graham_essay.txt'--2024-03-08 06:51:30-- https://raw.githubusercontent.com/run-llama/llama_index/main/docs/examples/data/paul_graham/paul_graham_essay.txt
Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 185.199.109.133, 185.199.108.133, 185.199.110.133, ...
Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|185.199.109.133|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 75042 (73K) [text/plain]
Saving to: ‘data/paul_graham/paul_graham_essay.txt’
data/paul_graham/pa 100%[===================>] 73.28K --.-KB/s in 0.002s
2024-03-08 06:51:30 (34.6 MB/s) - ‘data/paul_graham/paul_graham_essay.txt’ saved [75042/75042]
from llama_index.core import (
SimpleDirectoryReader,
VectorStoreIndex,
)加载数据
documents = SimpleDirectoryReader("./data/paul_graham").load_data()索引数据
index = VectorStoreIndex.from_documents(
documents,
)创建查询引擎
query_engine = index.as_query_engine(similarity_top_k=3)测试查询
response = query_engine.query("What did author do growing up?")print(response)Based on the information provided, the author worked on two main things outside of school before college: writing and programming.
For writing, he wrote short stories as a beginning writer, though he felt they were awful, with hardly any plot and just characters with strong feelings.
In terms of programming, in 9th grade he tried writing his first programs on an IBM 1401 computer that his school district used. He and his friend got permission to use it, programming in an early version of Fortran using punch cards. However, he had difficulty figuring out what to actually do with the computer at that stage given the limited inputs available.
Tip
中文读者提示:本章节的代码和输出为英文原文。如需理解具体实现细节,可参考上方中文导读和代码注释。如有疑问,欢迎在 GitHub Issues 讨论。