Tạo và quản lí Vector Stores (tiếp tục)
Các ví dụ về vector stores bao gồm FAISS, Pinecone, Weaviate, Chroma, pgVector.
LangChain cung cấp tích hợp mượt mà với các vector stores khác nhau, cả địa phương (FAISS, Chroma) và
có quản lí (Pinecone, Weaviate).
Sử dụng các vector stores để xây dựng tìm kiếm ngữ nghĩa, retrieval-augmented generation, và các hệ
thống AI nhận biết bối cảnh hiệu quả.
Lựa chọn vector store phụ thuộc vào quy mô, phí tổn, và ưa thích cơ sở hạ tầng ứng dụng của bạn.
Các bước tạo và quản lí các vector stores trong LangChain
1. Cài đặt các thư viện yêu cầu cho Faiss
!pip install -qU faiss-cpu
Một vài vector stores yêu cầu các thư viện hay APIs thêm. Cài đặt chúng khi cần
2. Tạo một Vector Store (Vector Store địa phương)
from langchain_openai import AzureOpenAIEmbeddings
from langchain.vectorstores import FAISS
# Initialize Azure OpenAI Embeddings
embedding_model = AzureOpenAIEmbeddings(azure_deployment = “<Embedding Deployment Name>”,
openai_api_version = “2024-05-01-preview”)
# Create texts and vector store
texts = [“LangChain is great for AI.”, “Vector databases are powerful.”]
vector_store = FAISS.from_texts(texts, embedding_model)
# Save the vector store locally
vector_store.save_local(“/content/faiss_store/”)
# Load the vector store
loaded_vector_store = FAISS.load_local(“/content/faiss_store/”,
embedding_model,
allow_dangerous_deserialization=True)
# Step 2: Create a list of texts and their embeddings
texts = [“LangChain is great for AI.”, “Vector databases are powerful.”]
vector_store = FAISS.from_texts(texts, embedding_model)
# Step 3: Save the vector store locally
vector_store.save_local(“faiss_store”)
# Step 4: Load the vector store
loaded_vector_store = FAISS.load_local(“faiss_store”, embedding_model)
Output:
Code này sử dụng Azure OpenAI embeddings với LangChain để chuyển đổi các texts thành các trình bày vector
và lưu giữ chúng trong một CSDL vector FAISS. Nó minh họa tạo một vector store từ các text mẫu, lưu nó
địa phương, và load lại nó sau này cho sử dụng. Thiết lập này làm có thể tìm kiếm ngữ nghĩa và giành hiệu
quả trong các ứng dụng hỗ trợ bởi AI sử dụng các models chứa trên Azure.

