Google Vertex AI Feature Store
Google Cloud Vertex Feature Store streamlines your ML feature management and online serving processes by letting you serve at low-latency your data in Google Cloud BigQuery, including the capacity to perform approximate neighbor retrieval for embeddings
This tutorial shows you how to easily perform low-latency vector search and approximate nearest neighbor retrieval directly from your BigQuery data, enabling powerful ML applications with minimal setup. We will do that using the VertexFSVectorStore class.
This class is part of a set of 2 classes capable of providing a unified data storage and flexible vector search in Google Cloud:
- BigQuery Vector Search: with
BigQueryVectorStoreclass, which is ideal for rapid prototyping with no infrastructure setup and batch retrieval. - Feature Store Online Store: with
VertexFSVectorStoreclass, enables low-latency retrieval with manual or scheduled data sync. Perfect for production-ready user-facing GenAI applications.

开始使用
安装库
%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 Vector Store。
DATASET = "my_langchain_dataset" # @param {type: "string"}
TABLE = "doc_and_vectors" # @param {type: "string"}
认证你的笔记本环境
- 如果你正在使用 Colab 运行此笔记本,请取消注释下面的单元格并继续。
- 如果你正在使用 Vertex AI Workbench,请在此处查看设置说明:https://github.com/GoogleCloudPlatform/generative-ai/tree/main/setup-env
# from google.colab import auth as google_auth
# google_auth.authenticate_user()
演示:VertexFSVectorStore
创建一个 embedding 类实例
你可能需要在 你的项目中启用 Vertex AI API,运行
gcloud services enable aiplatform.googleapis.com --project {PROJECT_ID}
(将 {PROJECT_ID} 替换为你的项目名称)。
你可以使用任何 LangChain embeddings 模型。
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 分钟。
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
Related
- Vector store conceptual guide
- Vector store how-to guides