探索Google Cloud Vertex AI Feature Store:极低延迟的向量搜索

122 阅读2分钟

探索Google Cloud Vertex AI Feature Store:极低延迟的向量搜索

在机器学习领域,特征管理以及数据的快速在线检索是提升模型性能和用户体验的关键。Google Cloud Vertex AI Feature Store通过允许在Google Cloud BigQuery中以低延迟的方式提供数据,为我们提供了强大的解决方案。这篇文章将带你深入了解如何使用VertexFSVectorStore类,从BigQuery数据中轻松执行低延迟的向量搜索和近似最近邻检索。

主要内容

Vertex AI Feature Store简介

Google Cloud Vertex AI Feature Store是一种集中式数据存储,它允许你管理和检索机器学习特征。它提供两种主要的类:

  • BigQueryVectorStore:适合于快速原型开发,无需基础设施设置和批量检索。
  • VertexFSVectorStore:支持低延迟检索,适合生产环境中的用户界面生成AI应用。

安装和环境准备

首先,确保安装必要的库:

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

为了使用安装包,重启你的Jupyter运行环境:

import IPython

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

项目配置

设置你的Google Cloud项目ID和区域:

PROJECT_ID = ""  # @param {type:"string"}
! gcloud config set project {PROJECT_ID}

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

设定BigQuery数据集和表名:

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

若在Colab中进行环境验证,可能需要:

# from google.colab import auth as google_auth
# google_auth.authenticate_user()

使用VertexFSVectorStore类

创建一个嵌入类实例并初始化VertexFSVectorStore:

from langchain_google_vertexai import VertexAIEmbeddings
from langchain_google_community import VertexFSVectorStore

embedding = VertexAIEmbeddings(
    model_name="textembedding-gecko@latest", project=PROJECT_ID
)

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

数据添加和检索

添加文本并同步数据:

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)
store.sync_data()

搜索文档:

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)

常见问题和解决方案

  1. 首次同步延迟:首次同步因创建特征在线商店需要大约20分钟。建议在非高峰时间进行首次设置。
  2. 网络访问问题:由于网络限制,某些地区的开发者可能需要使用API代理服务以提高访问稳定性。

总结和进一步学习资源

Vertex AI Feature Store为机器学习应用提供了强大的特征管理和低延迟检索能力。你可以通过Google Cloud官方文档和社区指南深入学习如何在不同场景下使用这些工具。

参考资料

如果这篇文章对你有帮助,欢迎点赞并关注我的博客。您的支持是我持续创作的动力!

---END---