Google Vertex AI Feature Store
Google Cloud Vertex Feature Store 透過讓您在 Google Cloud BigQuery 中以低延遲提供資料,包括執行嵌入的近似鄰近檢索能力,來簡化您的 ML 功能管理和線上服務流程
本教學課程示範如何輕鬆地直接從您的 BigQuery 資料執行低延遲向量搜尋和近似最近鄰檢索,從而以最少的設定啟用強大的 ML 應用程式。我們將使用 VertexFSVectorStore
類別來完成此操作。
此類別是一組 2 個類別的一部分,能夠在 Google Cloud 中提供統一的資料儲存和彈性的向量搜尋
- BigQuery 向量搜尋:使用
BigQueryVectorStore
類別,非常適合快速原型設計,無需基礎架構設定和批次檢索。 - Feature Store Online Store:使用
VertexFSVectorStore
類別,可實現低延遲檢索,並可手動或排程資料同步。非常適合生產就緒、面向使用者的 GenAI 應用程式。
開始使用
安裝程式庫
%pip install --upgrade --quiet langchain langchain-google-vertexai "langchain-google-community[featurestore]"
若要在這個 Jupyter 執行階段中使用新安裝的套件,您必須重新啟動執行階段。您可以執行以下儲存格來執行此操作,這會重新啟動目前的核心。
import IPython
app = IPython.Application.instance()
app.kernel.do_shutdown(True)
開始之前
設定您的專案 ID
如果您不知道您的專案 ID,請嘗試以下操作
- 執行
gcloud config list
。 - 執行
gcloud projects list
。 - 請參閱支援頁面:尋找專案 ID。
PROJECT_ID = "" # @param {type:"string"}
# Set the project id
! gcloud config set project {PROJECT_ID}
設定區域
您也可以變更 BigQuery 使用的 REGION
變數。深入瞭解 BigQuery 區域。
REGION = "us-central1" # @param {type: "string"}
設定資料集和表格名稱
它們將成為您的 BigQuery 向量儲存庫。
DATASET = "my_langchain_dataset" # @param {type: "string"}
TABLE = "doc_and_vectors" # @param {type: "string"}
驗證您的 Notebook 環境
- 如果您使用 Colab 執行此 Notebook,請取消註解以下儲存格並繼續。
- 如果您使用 Vertex AI Workbench,請查看此處的設定指示 here。
# from google.colab import auth as google_auth
# google_auth.authenticate_user()
Demo:VertexFSVectorStore
建立嵌入類別實例
您可能需要在您的專案中啟用 Vertex AI API,方法是執行 gcloud services enable aiplatform.googleapis.com --project {PROJECT_ID}
(將 {PROJECT_ID}
替換為您的專案名稱)。
您可以使用任何 LangChain 嵌入模型。
from langchain_google_vertexai import VertexAIEmbeddings
embedding = VertexAIEmbeddings(
model_name="textembedding-gecko@latest", project=PROJECT_ID
)
初始化 VertexFSVectorStore
如果 BigQuery 資料集和表格不存在,將會自動建立。請參閱 此處 的類別定義,以瞭解所有選用參數。
from langchain_google_community import VertexFSVectorStore
store = VertexFSVectorStore(
project_id=PROJECT_ID,
dataset_name=DATASET,
table_name=TABLE,
location=REGION,
embedding=embedding,
)
新增文字
注意:首次同步處理將耗時約 20 分鐘,因為需要建立 Feature Online Store。
all_texts = ["Apples and oranges", "Cars and airplanes", "Pineapple", "Train", "Banana"]
metadatas = [{"len": len(t)} for t in all_texts]
store.add_texts(all_texts, metadatas=metadatas)
您也可以透過執行 sync_data
方法,依需求啟動同步。
store.sync_data()
在生產環境中,您也可以使用 cron_schedule
類別參數來設定自動排程同步。例如
store = VertexFSVectorStore(cron_schedule="TZ=America/Los_Angeles 00 13 11 8 *", ...)
搜尋文件
query = "I'd like a fruit."
docs = store.similarity_search(query)
print(docs)
依向量搜尋文件
query_vector = embedding.embed_query(query)
docs = store.similarity_search_by_vector(query_vector, k=2)
print(docs)
使用中繼資料篩選器搜尋文件
# This should only return "Banana" document.
docs = store.similarity_search_by_vector(query_vector, filter={"len": 6})
print(docs)
新增具有嵌入的文字
您也可以使用 add_texts_with_embeddings
方法,帶入您自己的嵌入。這對於可能需要在嵌入產生之前進行自訂預處理的多模態資料特別有用。
items = ["some text"]
embs = embedding.embed(items)
ids = store.add_texts_with_embeddings(
texts=["some text"], embs=embs, metadatas=[{"len": 1}]
)
使用 BigQuery 進行批次服務
您可以直接使用 .to_bq_vector_store()
方法來取得 BigQueryVectorStore 物件,這為批次使用案例提供最佳化的效能。所有必要參數都會從現有類別自動轉移。請參閱 類別定義,以瞭解您可以使用的所有參數。
使用 .to_vertex_fs_vector_store()
方法,同樣可以輕鬆地移回 BigQueryVectorStore。
store.to_bq_vector_store() # pass optional VertexFSVectorStore parameters as arguments