输入“/”快速插入内容

用llama3打造光速响应的RAG Chatbot

2024年8月26日修改
作者:lucas大叔
本示例不仅展示如何构建高级的RAG 索引/查询 pipeline,而且还传授如何将其转化为具有快速响应的 全栈 应用。
1.
通过句子拆分、标题提取和Gemini embedding定义索引 pipeline
2.
定义以Groq为LLM和聊天引擎查询的 pipeline 维护对话历史
3.
使用chainlit构建整洁的全栈应用
准备工作
现在我们开始研究如何基于LlamaIndex实现RAG,首先安装 llama-index
代码块
pip install llama-index
安装完成后导入必要的包
代码块
from llama_index.core import (
VectorStoreIndex,
SimpleDirectoryReader,
StorageContext,
ServiceContext,
load_index_from_storage
)
from llama_index.core.node_parser import SemanticSplitterNodeParser
from llama_index.embeddings.gemini import GeminiEmbedding
from llama_index.llms.groq import Groq
数据摄入
构建RAG pipeline的第一步是加载数据,可以是PDF、文本、代码或Markdown文件,SQL数据库,或者我们希望加载的任何其他格式。LlamaIndex提供了一系列的数据加载器,无论是本地文件存储还是Google Drive或MongoDB等外部存储。
代码块
reader = SimpleDirectoryReader(input_dir="path/to/directory")
documents = reader.load_data()
SimpleDirectoryReader 模块允许从本地系统加载跨格式的文件/文件夹。
参数
input_dir — 定义包含待加载文件的本地 目录路径 。如果要从子目录中读取文件,请设置 recursive=True。
input_files — 定义待加载的文件列表。
代码块
SimpleDirectoryReader(input_files=["path/to/file1", "path/to/file2"])
exclude — 定义从目录中排除的文件列表。
代码块
SimpleDirectoryReader(
input_dir="path/to/directory", exclude=["path/to/file1", "path/to/file2"]
)
required_exts — 定义指定目录中包含的文件格式扩展名列表。
代码块
SimpleDirectoryReader(
input_dir="path/to/directory", required_exts=[".pdf", ".docx"]
)
show_progress=True 显示在运行序列中正在处理的文件数量。
对于多进程:
代码块
documents = reader.load_data(num_workers=4)
向量嵌入