RAG là gì ?
Retrieval-Augmented
Generation (RAG) là một mô hình máy học kết hợp giữa khả năng trích xuất thông
tin và khả năng tạo ra nội dung mới. Điều này thường được thực hiện thông qua
việc sử dụng một mô-đun trích xuất thông tin (retrieval model) để lấy thông tin
từ nguồn dữ liệu có sẵn và sau đó sử dụng một mô-đun sinh nội dung (generation
model) để tạo ra câu trả lời hoặc văn bản mới dựa trên thông tin đó. Mô hình
này thường được áp dụng trong các ứng dụng như tìm kiếm thông tin và tạo ra văn
bản phản hồi tự nhiên.
Sơ
đồ tổng quan RAG
- Load: First we need to load our
data. We’ll use DocumentLoaders for
this.
- Split: Text
splitters break large Documents into smaller chunks.
This is useful both for indexing data and for passing it in to a model,
since large chunks are harder to search over and won’t fit in a model’s
finite context window.
- Store: We need somewhere to store and
index our splits, so that they can later be Retrieve: Given a user
input, relevant splits are retrieved from storage using a Retriever.
- Generate: A ChatModel / LLM produces
an answer using a prompt that includes the question and the retrieved data
Sơ
đồ RAG sử dụng model của OpenAI (API model)
Code mẫu tham khảo (đã test) : https://colab.research.google.com/github/langchain-ai/langchain/blob/master/docs/docs/use_cases/question_answering/index.ipynb
Sơ
đồ quá trình tạo RAG từ framework langchain tích hợp model trên Hungingface
(local model)
Sơ đồ step-by-step:
STEP 0: Cài thư viện
Lưu ý một số thư viện có thể yêu cầu
khởi động lại môi trường ảo để cập nhập
Đầu tiên, cài đặt các gói cần thiết để nhúng cục bộ và lưu
trữ vectơ.
!pip install langchainhub
langchain
!pip install gpt4all
langchain
!pip install chromadb
!pip -q install langchain
huggingface_hub transformers sentence_transformers accelerate bitsandbytes
STEP 1. Tải tài liệu
Tại liệu hiện tại đang là trang web tuy nhiên nó có thể ở
nhiều dạng khác như txt, pdf, csv, json, …
from
langchain.document_loaders import WebBaseLoader
#
loader =
WebBaseLoader("https://lilianweng.github.io/posts/2023-06-23-agent/")
loader
= WebBaseLoader("https://e.vnexpress.net/news/culture/vietnamese-pop-stars-english-language-song-a-youtube-hit-4601722.html")
data =
loader.load()
STEP 2. Tách nội dung
Vì chiều dài của một đoạn text trên web site quá lớn nên ta
cần phải thực hiện phân tách thành các đoạn text nhỏ
from langchain.text_splitter
import RecursiveCharacterTextSplitter
text_splitter
= RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=0)
all_splits
= text_splitter.split_documents(data)
STEP 3. Store
Docs sẽ lưu những nội dung có độ tương quan vector với question
from langchain.embeddings import GPT4AllEmbeddings
from langchain.vectorstores import Chroma
vectorstore
= Chroma.from_documents(documents=all_splits, embedding=GPT4AllEmbeddings())
question
= "Who
is Son Tung MTP?"
docs =
vectorstore.similarity_search(question)
len(docs)
for doc in docs:
print(doc)
STEP 4. Retrieve
Cần một token của Hungingface để tải model và sử dụng (trước
tiên phải đăng kí và tạo tài khoản hungingface)
import os
os.environ['HUGGINGFACEHUB_API_TOKEN'] = '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'
Sau đây là một ví dụ về sử dụng model local không truy vấn và
có truy vấn
from langchain import PromptTemplate, HuggingFaceHub, LLMChain
template
= """Question:
{question}
Answer:
"""
#
Answer: Let's think step by step."""
prompt
= PromptTemplate(template=template, input_variables=["question"])
from langchain.llms import HuggingFacePipeline
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline, AutoModelForSeq2SeqLM
model_id
= 'google/flan-t5-small'
tokenizer
= AutoTokenizer.from_pretrained(model_id)
model
= AutoModelForSeq2SeqLM.from_pretrained(model_id, device_map='auto')
pipeline
= pipeline(
"text2text-generation",
model=model,
tokenizer=tokenizer,
max_length=128
)
local_llm
= HuggingFacePipeline(pipeline=pipeline)
Không truy vấn dữ liệu
#@title
Không truy vấn tài liệu
llm_chain
= LLMChain(prompt=prompt,
llm=local_llm
)
question
= "Who
is Son Tung MTP?"
print(llm_chain.run(question))
Có truy vấn dữ liệu
from langchain.chains import LLMChain
from langchain.prompts import PromptTemplate
#
Prompt
prompt
= PromptTemplate.from_template(
"Who is Son Tung MTP?: {docs}"
)
#
Chain
llm_chain
= LLMChain(llm=local_llm, prompt=prompt)
# Run
question
= "Who
is Son Tung MTP?"
docs =
vectorstore.similarity_search(question)
result
= llm_chain(docs)
#
Output
print(result["text"])
#################################################################
#################################################################
#################################################################
#################################################################
#
Prompt
prompt
= PromptTemplate.from_template(
"What is Making My Way?: {docs}"
)
#
Chain
llm_chain
= LLMChain(llm=local_llm, prompt=prompt)
# Run
question
= "What
is Making My Way?"
docs =
vectorstore.similarity_search(question)
result
= llm_chain(docs)
#
Output
print(result["text"])
Ví dụ có truy vấn dữ liệu sử dụng model openchat/openchat_3.5
import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)
from langchain.chains import LLMChain
from langchain.prompts import PromptTemplate
#
Prompt
prompt
= PromptTemplate.from_template(
"Who is Son Tung MTP?: {docs}"
)
#
Chain
llm_chain
= LLMChain(prompt=prompt,
llm=HuggingFaceHub(repo_id="openchat/openchat_3.5",
#
repo_id="mistralai/Mixtral-8x7B-Instruct-v0.1",
model_kwargs={"temperature":0.1,
"max_length":128}),)
# Run
question
= "Who
is Son Tung MTP?"
docs =
vectorstore.similarity_search(question)
result
= llm_chain(docs)
#
Output
print(result["text"])
################################################################################
################################################################################
################################################################################
#
Prompt
prompt
= PromptTemplate.from_template(
"What is Making My Way?: {docs}"
)
#
Chain
llm_chain
= LLMChain(prompt=prompt,
llm=HuggingFaceHub(repo_id="openchat/openchat_3.5",
#
repo_id="mistralai/Mixtral-8x7B-Instruct-v0.1",
model_kwargs={"temperature":0.1,
"max_length":128}),)
# Run
question
= "What
is Making My Way?"
docs =
vectorstore.similarity_search(question)
result
= llm_chain(docs)
#
Output
print(result["text"])
Cho kết quả khá tốt
Son
Tung M-TP is a Vietnamese singer, songwriter, and rapper. He is one of the most
popular pop stars in Vietnam. He has won an MTV Europe Music Award and was
included in the 2018 Forbes 30 Under 30 list for Vietnam.
He
recently released an English-language song called "Making My Way"
which has already reached 3.6 million listeners on YouTube. The music video
[Document(page_content='According
to the singer, the new song is a way for him to forget about things that didn’t
work out and a reminder to follow his heart."My fan support allows me to
keep creating, improving, and doing things that many wouldn’t dare to
try," Tung, 29, said."Making My Way" is a soliloquy of a
heartbroken man who was able to endure the pain of being
Ví dụ có truy vấn dữ liệu sử dụng mode 70 tỉ tham số mistralai/Mixtral-8x7B-Instruct-v0.1
import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)
from langchain.chains import LLMChain
from langchain.prompts import PromptTemplate
#
Prompt
prompt
= PromptTemplate.from_template(
"Who is Son Tung MTP?: {docs}"
)
#
Chain
llm_chain
= LLMChain(prompt=prompt,
llm=HuggingFaceHub(repo_id="mistralai/Mixtral-8x7B-Instruct-v0.1",
model_kwargs={"temperature":0.1,
"max_length":128}),)
# Run
question
= "Who
is Son Tung MTP?"
docs =
vectorstore.similarity_search(question)
result
= llm_chain(docs)
#
Output
print(result["text"])
#############################################################################
###############################################################################
#
Prompt
prompt
= PromptTemplate.from_template(
"What is Making My Way?: {docs}"
)
#
Chain
llm_chain
= LLMChain(prompt=prompt,
llm=HuggingFaceHub(repo_id="mistralai/Mixtral-8x7B-Instruct-v0.1",
model_kwargs={"temperature":0.1,
"max_length":128}),)
# Run
question
= "What
is Making My Way?"
docs =
vectorstore.similarity_search(question)
result
= llm_chain(docs)
#
Output
print(result["text"])
Cho kết quả
tốt nhất trong các model trên
Son
Tung M-TP, whose real name is Nguyen Thanh Tung, is one of Vietnam's biggest
pop stars. He is from the northern province of Thai Binh and has won an MTV
Europe Music Award and was included in the 2018 Forbes 30 Under 30 list for
Vietnam. His team is planning to release the accompanying music video for his
English song "Making My Way" shortly. The
The provided
document contains information about the English song "Making My Way"
by Vietnamese pop star Son Tung-MTP, which has reached 3.6 million listeners
after debuting on YouTube midnight Thursday. The song is described as a way for
the singer to forget about things that didn’t work out and a reminder to follow
his heart. The song is a soliloquy of a heartbroken man who was able to endure
the pain of being lied
No comments:
Post a Comment