Google Vertex AI 特征存储
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
本教程向您展示如何轻松地直接从您的 BigQuery 数据执行低延迟的向量搜索和近似最近邻检索,从而以最少的设置启用强大的机器学习应用程序。我们将使用 VertexFSVectorStore 类来完成此操作。
此类是能够在 Google Cloud 提供统一数据存储和灵活向量搜索的两个类的一部分:
- BigQuery 向量搜索:带有
BigQueryVectorStore个类别,非常适合无需基础设施设置即可快速原型设计和批量检索。 - 特征存储在线存储:具有
VertexFSVectorStore个类,支持低延迟检索,并可手动或定时同步数据。非常适合生产就绪的面向用户的生成式人工智能应用程序。
入门指南
安装该库
%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"}
验证您的笔记本环境
- 如果你使用 Colab 来运行此笔记本,请取消下面单元格的注释并继续。
- 如果你使用的是Vertex AI Workbench,请查看设置说明这里。
# from google.colab import auth as google_auth
# google_auth.authenticate_user()
演示:VertexFSVectorStore
创建嵌入类实例
您可能需要通过运行
gcloud services enable aiplatform.googleapis.com --project {PROJECT_ID}
(将 {PROJECT_ID} 替换为您的项目名称)来在您的项目中启用 Vertex AI API。
您可以使用任何 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,
)
添加文本
Note: The first synchronization process will take around ~20 minutes because of Feature Online Store creation.
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 方法引入自己的嵌入(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 对象,该对象为批量用例提供了优化的性能。所有必需的参数将自动从现有类中传递。有关您可以使用的所有参数,请参阅 类定义。
回到 BigQueryVectorStore 也非常简单,使用 .to_vertex_fs_vector_store() 方法即可。
store.to_bq_vector_store() # pass optional VertexFSVectorStore parameters as arguments