Skip to main content
Google Cloud Vertex Feature Store 通过让您从 Google Cloud BigQuery 中的数据进行低延迟服务(包括对嵌入进行近似近邻检索的能力),简化了您的 ML 特征管理和在线服务流程。
本教程展示如何直接从 BigQuery 数据轻松执行低延迟向量搜索和近似最近邻检索,以最少的设置构建强大的 ML 应用程序。我们将使用 VertexFSVectorStore 类来实现。 该类是一组 2 个类的一部分,能够在 Google Cloud 中提供统一的数据存储和灵活的向量搜索:
  • BigQuery 向量搜索:使用 BigQueryVectorStore 类,适合快速原型开发,无需基础设施设置,支持批量检索。
  • Feature Store Online Store:使用 VertexFSVectorStore 类,支持手动或定时数据同步的低延迟检索。非常适合面向生产环境的用户侧 GenAI 应用。
Diagram BQ-VertexFS

快速开始

安装库

pip install -qU  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

创建嵌入类实例

您可能需要在项目中启用 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() 方法同样可以轻松返回 VertexFSVectorStore。
store.to_bq_vector_store()  # pass optional VertexFSVectorStore parameters as arguments