Google Vertex AI 特征商店
Google Cloud Vertex Feature Store 可让您在 Google Cloud BigQuery 中以低延迟提供数据,包括为嵌入执行近似邻居检索的能力,从而简化您的 ML 特征管理和在线服务流程
本教程将介绍如何直接从 BigQuery 数据中轻松执行低延迟向量搜索和近似最近邻检索,从而以最少的设置实现强大的 ML 应用程序。我们将使用VertexFSVectorStore类。
此类是一组 2 个类的一部分,能够在 Google Cloud 中提供统一的数据存储和灵活的向量搜索:
- BigQuery Vector Search:使用
BigQueryVectorStore类,非常适合无需基础设施设置和批量检索的快速原型设计。 - Feature 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}
设置区域
您还可以更改REGIONBigQuery 使用的变量。详细了解 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,请在此处查看设置说明。
# 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 嵌入模型。
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,
)
添加文本
注意: 由于要创建 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_scheduleclass 参数设置自动计划同步。
例如:
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()方法。
store.to_bq_vector_store() # pass optional VertexFSVectorStore parameters as arguments