跳至主要內容

Google Vertex AI Feature Store(Google Vertex AI 特徵儲存)

Google Cloud Vertex Feature Store 透過讓您在 Google Cloud BigQuery 中以低延遲提供資料服務,簡化您的 ML 特徵管理和線上服務流程,包括執行嵌入的近似鄰近檢索的能力。

本教學課程將向您展示如何輕鬆地直接從 BigQuery 資料執行低延遲向量搜尋和近似最近鄰檢索,從而以最少的設置實現強大的 ML 應用程式。我們將使用 VertexFSVectorStore 類別來完成此操作。

此類別是一組 2 個類別的一部分,能夠在 Google Cloud 中提供統一的資料儲存和彈性的向量搜尋

  • BigQuery Vector Search:使用 BigQueryVectorStore 類別,非常適合快速原型設計,無需基礎架構設置和批次檢索。
  • Feature Store Online Store:使用 VertexFSVectorStore 類別,透過手動或排程的資料同步實現低延遲檢索。非常適合適用於生產環境且面向使用者的 GenAI 應用程式。

Diagram BQ-VertexFS

Getting started(開始使用)

Install the library(安裝函式庫)

%pip install --upgrade --quiet  langchain langchain-google-vertexai "langchain-google-community[featurestore]"

若要在這個 Jupyter 執行階段中使用新安裝的套件,您必須重新啟動執行階段。您可以透過執行以下儲存格來完成此操作,該儲存格會重新啟動目前的核心。

import IPython

app = IPython.Application.instance()
app.kernel.do_shutdown(True)

Before you begin(開始之前)

Set your project ID(設定您的專案 ID)

如果您不知道您的專案 ID,請嘗試以下操作

  • 執行 gcloud config list
  • 執行 gcloud projects list
  • 請參閱支援頁面:找到專案 ID
PROJECT_ID = ""  # @param {type:"string"}

# Set the project id
! gcloud config set project {PROJECT_ID}

Set the region(設定區域)

您也可以變更 BigQuery 使用的 REGION 變數。深入瞭解BigQuery 區域

REGION = "us-central1"  # @param {type: "string"}

Set the dataset and table names(設定資料集和表格名稱)

它們將成為您的 BigQuery Vector Store。

DATASET = "my_langchain_dataset"  # @param {type: "string"}
TABLE = "doc_and_vectors" # @param {type: "string"}

Authenticating your notebook environment(驗證您的筆記本環境)

  • 如果您使用 Colab 執行此筆記本,請取消註解以下儲存格並繼續。
  • 如果您使用 Vertex AI Workbench,請查看此處的設定說明。
# from google.colab import auth as google_auth

# google_auth.authenticate_user()

Demo: VertexFSVectorStore(範例:VertexFSVectorStore)

Create an embedding class instance(建立嵌入類別實例)

您可能需要在您的專案中啟用 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
)

Initialize VertexFSVectorStore(初始化 VertexFSVectorStore)

如果 BigQuery 資料集和表格不存在,將會自動建立。請參閱類別定義此處,以取得所有選用參數。

from langchain_google_community import VertexFSVectorStore

store = VertexFSVectorStore(
project_id=PROJECT_ID,
dataset_name=DATASET,
table_name=TABLE,
location=REGION,
embedding=embedding,
)
API Reference:VertexFSVectorStore

Add texts(新增文字)

注意:由於 Feature Online Store 的建立,第一次同步處理大約需要 ~20 分鐘。

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 *", ...)

Search for documents(搜尋文件)

query = "I'd like a fruit."
docs = store.similarity_search(query)
print(docs)

Search for documents by vector(依向量搜尋文件)

query_vector = embedding.embed_query(query)
docs = store.similarity_search_by_vector(query_vector, k=2)
print(docs)

Search for documents with metadata filter(使用中繼資料篩選器搜尋文件)

# This should only return "Banana" document.
docs = store.similarity_search_by_vector(query_vector, filter={"len": 6})
print(docs)

Add text with embeddings(新增帶有嵌入的文字)

您也可以使用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

這個頁面有幫助嗎?