# 使用 LangChain 建立 RAG 向量資料庫
最近因應建置系統的需求,想使用 RAG([Retrieval-Augmented Generation](https://en.wikipedia.org/wiki/Retrieval-augmented_generation))來增強 LLM 知識庫。想要先建立文本的向量資料庫並儲存起來,之後想使用時可以直接載入而無需重新轉換向量。
## 安裝相關套件
```
pip install langchain_community
pip install langchain_openai
pip install langchain_text_splitters
pip install chromadb
```
## 建立向量資料庫並儲存
可以使用 FAISS 或 Chroma 處理向量資料,我這裡採用 Chroma。
```
from langchain_community.document_loaders import TextLoader
from langchain_community.vectorstores import FAISS
from langchain_openai.embeddings import OpenAIEmbeddings
from langchain_text_splitters import RecursiveCharacterTextSplitter
# OpenAI API Key
API_KEY = "Your API Key"
PERSIST_DIR = "./vectordb" # 設置儲存向量資料庫的目錄
# 載入文本
loader = TextLoader("./Text.txt", encoding="utf-8")
docs = loader.load()
# 切割文本
text_splitter = RecursiveCharacterTextSplitter(
chunk_size=1000,
chunk_overlap=50,
separators=["\n\n", "\n", ".", "。", "!", "?", ",", ",", " ", ""]
)
texts = text_splitter.split_documents(docs)
# 嵌入向量資料庫
embeddings_model = OpenAIEmbeddings(openai_api_key=API_KEY, model="text-embedding-3-large")
# 建立向量資料庫
vectordb = Chroma.from_documents(
documents=texts,
embedding=embeddings_model,
persist_directory=PERSIST_DIR # 設置持久化目錄
)
# 儲存向量資料庫
vectordb.persist() # 使用 persist 方法將向量資料庫寫入目錄
```
## 載入向量資料庫
之後想使用時直接載入向量資料庫檢索
```
from langchain_community.vectorstores import Chroma
from langchain_openai.embeddings import OpenAIEmbeddings
API_KEY = "Your API Key"
PERSIST_DIR = "./vectordb"
# 加載嵌入模型
embeddings_model = OpenAIEmbeddings(openai_api_key=API_KEY, model="text-embedding-3-large")
# 載入向量資料庫
vectordb = Chroma(persist_directory=PERSIST_DIR, embedding_function=embeddings_model)
# 建立文檔檢索器
retriever = vectordb.as_retriever()
retrieved_docs = retriever.invoke("檢索文字")
print(retrieved_docs[0].page_content)
```
## References
* [AI大模型應用開發(7):讓語言模型訪問外部資料庫](https://blog.raven.tw/AI-7-11a021a1715c8083814aee3b2560a9a6)
* [[LangChain-01] RAG實戰第一站](https://medium.com/jimmy-wang/langchain-rag%E5%AF%A6%E6%88%B0%E7%AC%AC%E4%B8%80%E7%AB%99-efe975f4c3bd)