RAG 系统快速入门指南
用通俗方式讲解 RAG 的核心概念和实现步骤,适合初学者快速掌握 RAG 应用的基础。
用通俗方式讲解 RAG 的核心概念和实现步骤,适合初学者快速掌握 RAG 应用的基础。
你是否也被 AI 和 AI 搞得有点蒙?我也是!但我们得顺势而为,不然无法坚持做出影响!
这篇博文讲的正是这样一个 AI 的东西,它在科技世界中产生了很大的潜力。不管你是初学者还是专家,不管你从事科技工作还是对科技有兴趣,你都应该了解这个。
在这篇博文中,我会深入讲解检索增强生成(RAG),并使用一个出色的框架 LLMWARE 创建一个快速的 prompt 模型。
让我们开始...3️⃣...2️⃣...1️⃣...🤓
让我们从基础开始,这样你能轻松理解 RAG。
首先,什么是 AI?AI 或人工智能就是制造智能机器的科学和工程学。
在 AI 中,有许多子集。看看下面的图表:
现在让我们讨论另一个庞杂的领域——机器学习(ML)。从上面的图表可以看出,ML 是 AI 的一个子集。ML 专注于构建从数据中学习的计算机系统。因此,ML 是 AI 的一部分,它处理并训练一种称为模型的软件,以对数据做出有用的预测或生成内容。
有趣的事实:LLM 是人工智能(AI)程序的一种,基于机器学习构建。因此,LLM 在海量数据集上训练——这就是"大型"这个名称的由来。
AI
├── ML (Machine Learning)
│ ├── LLM (Large Language Models)
│ └── RAG (Retrieval-Augmented Generation)
RAG 或检索增强生成是一个突破性的 AI 框架(就像 NextJs 是 Js 的框架一样),用于通过让模型基于外部知识来源来改进 LLM 生成响应的质量。
RAG 将 LLM 已有的强大能力扩展到特定领域或组织的内部知识库,完全无需重新训练模型。这是一种经济高效的方法,能改进 LLM 的输出,使其在各种情境中保持相关、准确和有用。
我希望你现在对 RAG 的概念有所了解。为了让概念更清晰,让我们跳到示例部分,在那里我们将创建一个简单的项目,使用 LLMWARE 框架来测试基于 Prompt 的 RAG 模型。
如果你不了解 LLMWARE,请阅读下面的文章。这是构建项目的唯一前置条件!😝
LLMware.ai 🤖: An Ultimate Python Toolkit for Building LLM Apps
Rohan Sharma ・ Aug 29 '24
llmware 为构建基于 LLM 的应用程序(如 RAG、Agents)提供了统一的框架,使用小型、专用的模型,可以私下部署,与企业知识来源安全集成,并成本有效地为任何业务流程调整和适配。
在这个例子中,我们将说明:
探索 - 如何在 llmware ModelCatalog 中探索模型。
加载模型 - 如何从目录中加载选定的模型。
Prompt - 如何创建基本 prompt 并运行模型推理。
1️⃣ 安装 llmware(如上所述)。或者在终端中运行这个代码:
pip3 install llmware
**2️⃣ 考虑到你没有任何测试问题来测试这个项目。因此,你可以使用下面这个:
def hello_world_questions():
""" This is a set of useful test questions to do a 'hello world' but there is nothing special about the
questions - please feel free to edit and ask your own queries with your own context passages.
--if you are using one of the llmware models, please take note that the models have been trained to answer
based on the information provided, so if you ask a question without passing any context passage, then
don't be surprised if the model responds with 'Not Found.' """
test_list = [
{"query": "What is the total amount of the invoice?",
"answer": "$22,500.00",
"context": "Services Vendor Inc. \n100 Elm Street Pleasantville, NY \nTO Alpha Inc. 5900 1st Street "
"Los Angeles, CA \nDescription Front End Engineering Service $5000.00 \n Back End Engineering"
" Service $7500.00 \n Quality Assurance Manager $10,000.00 \n Total Amount $22,500.00 \n"
"Make all checks payable to Services Vendor Inc. Payment is due within 30 days."
"If you have any questions concerning this invoice, contact Bia Hermes. "
"THANK YOU FOR YOUR BUSINESS! INVOICE INVOICE # 0001 DATE 01/01/2022 FOR Alpha Project P.O. # 1000"},
{"query": "What was the amount of the trade surplus?",
"answer": "62.4 billion yen ($416.6 million)",
"context": "Japan's September trade balance swings into surplus, surprising expectations"
"Japan recorded a trade surplus of 62.4 billion yen ($416.6 million) for September, "
"beating expectations from economists polled by Reuters for a trade deficit of 42.5 "
"billion yen. Data from Japan's customs agency revealed that exports in September "
"increased 4.3% year on year, while imports slid 16.3% compared to the same period "
"last year. According to FactSet, exports to Asia fell for the ninth straight month, "
"which reflected ongoing China weakness. Exports were supported by shipments to "
"Western markets, FactSet added. — Lim Hui Jie"}
]
return test_list
**3️⃣ 创建一个 Python 文件,比如 fast_start_rag.py,并粘贴下面的代码:
import time
from llmware.prompts import Prompt
from llmware.models import ModelCatalog
def fast_start_prompting (model_name):
""" This is the main example script - it loads the question list, loads the model, and executes the prompts. """
t0 = time.time()
# load in the 'hello world' test questions above
test_list = hello_world_questions()
print(f"\n > Loading Model: {model_name}...")
prompter = Prompt().load_model(model_name)
t1 = time.time()
print(f"\n > Model {model_name} load time: {t1-t0} seconds")
for i, entries in enumerate(test_list):
print(f"\n{i+1}. Query: {entries['query']}")
# run the prompt
output = prompter.prompt_main(entries["query"],
context=entries["context"],
prompt_name="default_with_context",
temperature=0.30)
# 'output' is a dictionary with two keys - 'llm_response' and 'usage'
# --'llm_response' is the output from the model
# --'usage' is a dictionary with the usage stats
llm_response = output["llm_response"].strip("\n")
print(f"LLM Response: {llm_response}")
# note: the 'gold answer' is the answer we provided above in the hello_world question list
print(f"Gold Answer: {entries['answer']}")
print(f"LLM Usage: {output['usage']}")
t2 = time.time()
print(f"\nTotal processing time: {t2-t1} seconds")
return 0
if __name__ == "__main__":
# Step 1 - we will pick a model from the ModelCatalog
# A few useful methods to discover and display a list of available models...
# all generative models
llm_models = ModelCatalog().list_generative_models()
# if you only want to see the local models
llm_local_models = ModelCatalog().list_generative_local_models()
# to see only the open source models
llm_open_source_models = ModelCatalog().list_open_source_models()
# we will print out the local models
for i, models in enumerate(llm_local_models):
print("models: ", i, models["model_name"], models["model_family"])
# for purposes of demo, try a few selected models from the list
# each of these pytorch models are ~1b parameters and will run reasonably fast and accurate on CPU
# --per note above, may require separate pip3 install of: torch and transformers
pytorch_generative_models = ["llmware/bling-1b-0.1", "llmware/bling-tiny-llama-v0", "llmware/bling-falcon-1b-0.1"]
# bling-answer-tool is 1b parameters quantized
# bling-phi-3-gguf is 3.8b parameters quantized
# dragon-yi-6b-gguf is 6b parameters quantized
gguf_generative_models = ["bling-answer-tool", "bling-phi-3-gguf","llmware/dragon-yi-6b-gguf"]
# by default, we will select a gguf model requiring no additional imports
model_name = gguf_generative_models[0]
# to swap in a GPT-4 openai model - uncomment these two lines
# model_name = "gpt-4"
# os.environ["USER_MANAGED_OPENAI_API_KEY"] = "<insert-your-openai-key>"
fast_start_prompting(model_name)
**4️⃣ 回到终端,运行下面的代码来运行应用程序:
python fast_start_rag.py
虽然代码自我解释(检查注释),但你可能想知道,刚才发生了什么!你可能有很多问题。但等等!我有专门的解释部分,特别是给视觉学习者。请查看这个链接,Prompt Models (Ex. 3): Fast Start to RAG (2024)。如果你想了解更多,那就看一下这个播放列表:
Fast Start to RAG (2024 updates) - YouTube
学习如何在这个易于跟随的分步教程系列中掌握 RAG 的基础知识
检索增强生成(RAG)是优化大语言模型输出的过程,使其在生成响应之前引用其训练数据来源之外的权威知识库。
如果你仍然有任何问题,请在评论区留言。或者,你可以通过以下链接加入 LLMWare 官方 Discord 频道:https://discord.com/invite/fCztJQeV7J
感谢!你是最美的人!继续学习,继续努力。祝你有美好的一天!!💝
一些评论可能仅对已登录的访问者可见。登录以查看所有评论。
如需进一步操作,你可以考虑屏蔽此人和/或举报滥用。