:pencil: LLMs (大型語言模型) 會在龐大的數據庫上進行訓練,但它們並未針對私人數據進行訓練。檢索增強生成 (Retrieval-Augmented Generation, RAG) 透過將私有數據加入到LLMs已有的數據庫中,解決了這個問題。
RAG的概念及其重要性
建置高品質RAG系統的挑戰
進階檢索方法
課程涵蓋兩種進階檢索方法,這些方法提供比簡單方法更好的LLM上下文語境
評估LLM問答系統的指標
使用RAG三元組 (RAG triad) 進行評估:這三個指標是專為評估RAG系統設計的,涵蓋了RAG執行的三個主要步驟:
這三個指標共同作用,為LLM問答系統的評估提供了一個全面的視角,幫助開發者和研究者理解和改進RAG系統的性能。
實作練習和評估方法的應用
–
這一小節是快速概覽,後面三小節會有較為深入的說明
如何建立基礎與進階的RAG(Retrieval Augmented Generation,檢索增強生成)管道的全面概覽,主要使用Llama Index並結合TruLens進行效能評估,包括:
句子窗口檢索(Sentence Window retrieval):
自動合併檢索(Auto-merging retrieval):
如下圖所示(來源),基礎方法(Basic RAG pipeline)使用相同的文本塊進行索引/嵌入以及輸出合成。
優點:
缺點:
import utils
import os
import openai
openai.api_key = utils.get_openai_api_key()
from llama_index import SimpleDirectoryReader
documents = SimpleDirectoryReader(
input_files=["./eBook-How-to-Build-a-Career-in-AI.pdf"]
).load_data
print(type(documents), "\n")
print(len(documents), "\n")
print(type(documents[0]))
print(documents[0])
<class 'list'>
41
<class 'llama_index.schema.Document'>
Doc ID: dd31cb7a-d550-48a5-8af6-254a05b9c5a9
Text: PAGE 1Founder, DeepLearning.AICollected Insights from Andrew Ng
How to Build Your Career in AIA Simple Guide
from llama_index import VectorStoreIndex
from llama_index import ServiceContext
from llama_index.llms import OpenAI
llm = OpenAI(model="gpt-3.5-turbo", temperature=0.1)
service_context = ServiceContext.from_defaults(
llm=llm, embed_model="local:BAAI/bge-small-en-v1.5"
)
文件合併:將多個文檔的文本合併為一個長文本
from llama_index import Document
document = Document(text="\n\n".join([doc.text for doc in documents]))
建立索引
index = VectorStoreIndex.from_documents([document],
service_context=service_context)
# <llama_index.indices.vector_store.base.VectorStoreIndex at 0x7fdcf6f6d450>
建立檢索引擎
query_engine = index.as_query_engine()
使用者查詢
response = query_engine.query(
"What are steps to take when finding projects to build your experience?"
)
print(str(response))
:bookmark_tabs:詳細流程、實作與實驗見Sentence-window retrieval
句子窗口(Sentence Window retrieval)方法將文件分解為更小的單位,例如句子或小組句子。它解耦了用於檢索任務的嵌入(這些較小的塊存儲在Vector DB中),但在合成(生成階段),重新添加了檢索塊(chunk)周圍的上下文
在檢索期間,通過相似性搜索檢索與查詢最相關的句子,並用完整的周圍上下文替換該句子(使用圍繞上下文的靜態句子窗口,通過檢索原始檢索句子周圍的句子來實現),如下圖所示
:bookmark_tabs:詳細流程、實作與實驗見Auto-merging retrieval
下圖展示了自動合併檢索的工作方式,不會像基礎方法那樣檢索出一堆過於破碎的文本塊。當基礎方法把文本切割得太小時,會得到一堆訊息不完整的破碎文件塊(text chunk)
Sentence Window retrieval¶
from llama_index.llms import OpenAI
llm = OpenAI(model="gpt-3.5-turbo", temperature=0.1)
build_sentence_window_index()
VectorStoreIndex
建立Sentence Window 索引
from utils import build_sentence_window_index
sentence_index = build_sentence_window_index(
document,
llm,
embed_model="local:BAAI/bge-small-en-v1.5",
save_dir="sentence_index"
)
from utils import get_sentence_window_query_engine
sentence_window_engine = get_sentence_window_query_engine(sentence_index)
window_response = sentence_window_engine.query(
"how do I get started on a personal project in AI?")
print(str(window_response))
To get started on a personal project in AI, it is important to first identify and scope the project. Consider your career goals and choose a project that complements them. Ensure that the project is responsible, ethical, and beneficial to people. As you progress in your career, aim for projects that grow in scope, complexity, and impact over time. Building a portfolio of projects that shows skill progression can also be helpful. Additionally, there are resources available in the book that provide guidance on starting your AI job search and finding the right AI job for you.
Auto-merging retrieval
from utils import build_automerging_index
automerging_index = build_automerging_index(
documents,
llm,
embed_model="local:BAAI/bge-small-en-v1.5",
save_dir="merging_index"
)
from utils import get_automerging_query_engine
automerging_query_engine = get_automerging_query_engine(
automerging_index,
)
auto_merging_response = automerging_query_engine.query(
"How do I build a portfolio of AI projects?"
)
print(str(auto_merging_response))
Merging 1 nodes into parent node.
> Parent node id: 40e80a95-972f-484b-ab69-f4309b819c7b.
> Parent node text: PAGE 21Building a Portfolio of
Projects that Shows
Skill Progression CHAPTER 6
PROJECTS
> Merging 1 nodes into parent node.
> Parent node id: f83e0a3d-87d4-49da-935f-97cfcef45c6f.
> Parent node text: PAGE 21Building a Portfolio of
Projects that Shows
Skill Progression CHAPTER 6
PROJECTS
To build a portfolio of AI projects, it is important to start with simple undertakings and gradually progress to more complex ones. This progression over time will demonstrate your growth and development in the field. Additionally, effective communication is crucial. You should be able to explain your thought process and the value of your work to others. This will help others see the potential in your projects and trust you with resources for larger endeavors.
TruLens
評估。 Evaluation setup using TruLens建立用來評估RAG Pipeline表現的問題集
eval_questions = []
with open('eval_questions.txt', 'r') as file:
for line in file:
# Remove newline character and convert to integer
item = line.strip()
print(item)
eval_questions.append(item)
# What are the keys to building a career in AI?
# How can teamwork contribute to success in AI?
# What is the importance of networking in AI?
# What are some good habits to develop for a successful career?
# How can altruism be beneficial in building a career?
# What is imposter syndrome and how does it relate to AI?
# Who are some accomplished individuals who have experienced imposter syndrome?
# What is the first step to becoming good at AI?
# What are some common challenges in AI?
# Is it normal to find parts of AI challenging?
初始化TrueLens、重置資料庫
from trulens_eval import Tru
tru = Tru()
tru.reset_database()
For the classroom, we've written some of the code in helper functions inside a utils.py file.
- You can view the utils.py file in the file directory by clicking on the "Jupyter" logo at the top of the notebook.
- In later lessons, you'll get to work directly with the code that's currently wrapped inside these helper functions, to give you more options to customize your RAG pipeline.
🦑 Tru initialized with db url sqlite:///default.sqlite .
🛑 Secret keys may be written to the database. See the `database_redact_keys` option of `Tru` to prevent this.
from utils import get_prebuilt_trulens_recorder
tru_recorder = get_prebuilt_trulens_recorder(query_engine,
app_id="Direct Query Engine")
檢視評估結果
with tru_recorder as recording:
for question in eval_questions:
response = query_engine.query(question)
records, feedback = tru.get_records_and_feedback(app_ids=[])
records.head()
啟動TruLens dashboard
# launches on http://localhost:8501/
tru.run_dashboard()
兩種進階RAG與Base方法必較
Difference between Naive and Advanced RAG (Image by the author, inspired by [1])
文章做了檢索三階段優化的範例實作
:pencil2: 此外,檢索前技術不僅限於資料索引,還可以涵蓋推理階段的查詢(Query)技術優化,例如查詢路由、查詢重寫和查詢擴展。
指的是通過不同的方法和技術來改進信息檢索的效率和準確性。這包括調整檢索模型、優化查詢處理方式以及改進檢索過程中的各種步驟。
Sentence window retrieval
進一步處理檢索到的上下文可以幫助解決,例如超出上下文窗口限制或雜訊等問題,以幫助對關鍵信息的聚焦
提示壓縮(Prompt Compression)
重新排序(Re-ranking)
通俗的進階檢索技巧概論
[source: Advanced RAG Techniques: an Illustrated Overview]
滿完整的筆記,推薦一讀