RAG 管道与 LlamaIndex
在本笔记本中,我们将研究如何构建一个基本的 RAG 管道与 LlamaIndex。该管道包含以下步骤。
- 设置 LLM 和嵌入模型。
- 下载数据。
- 加载数据。
- 索引数据。
- 创建查询引擎。
- 查询。
安装
!pip install llama-index
!pip install llama-index-llms-anthropic
!pip install llama-index-embeddings-huggingface
设置 API 密钥
import os
os.environ['ANTHROPIC_API_KEY'] = 'YOUR ANTHROPIC API KEY'
设置 LLM 和嵌入模型
我们将使用 Anthropic 最新发布的 Claude 3 Opus
模型。
from llama_index.llms.anthropic import Anthropic
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
llm = Anthropic(temperature=0.0, model='claude-3-opus-20240229')
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 (
VectorStoreIndex,
SimpleDirectoryReader,
)
加载数据
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)
根据提供的信息,作者在上大学之前,除了上学之外,主要做了两件事:写作和编程。
写作方面,他年轻时写过短篇故事,虽然他觉得这些故事很糟糕,几乎没有情节,只有一些感情强烈的角色。
编程方面,九年级时,他尝试在一台学校区使用的 IBM 1401 计算机上编写他的第一个程序。他和他的朋友获得了使用许可,使用一种早期版本的 Fortran 语言,通过打孔卡进行编程。然而,在那个阶段,由于可用的输入有限,他很难弄清楚计算机到底能做什么。