探索 Google BigQuery Vector Search:大规模语义搜索和嵌入式管理

探索 Google BigQuery Vector Search:大规模语义搜索和嵌入式管理

引言

在数据驱动的世界中,快速和高效的搜索能力是必不可少的。在这篇文章中,我们将探讨如何在 Google Cloud 中使用 BigQuery Vector Search 进行大规模语义搜索,以及如何通过 BigQueryVectorStore 类在 LangChain 中管理嵌入。此外,我们还将了解如何使用 API 代理服务以提高访问的稳定性。

主要内容

1. 初始化和设置

首先,为了在 Google Cloud 中进行操作,我们需要安装一些必要的库。

bash 复制代码
%pip install --upgrade --quiet langchain langchain-google-vertexai "langchain-google-community[featurestore]"

安装库后,建议重启 Jupyter 运行时以应用更改。

python 复制代码
import IPython

app = IPython.Application.instance()
app.kernel.do_shutdown(True)  # 重启当前内核

接下来,需要设置项目 ID 和区域信息:

python 复制代码
PROJECT_ID = "your_project_id"  # @param {type:"string"}
REGION = "us-central1"  # @param {type: "string"}
! gcloud config set project {PROJECT_ID}

2. 创建嵌入和向量存储

接着,我们将创建一个嵌入类实例,并初始化 BigQueryVectorStore。

python 复制代码
from langchain_google_vertexai import VertexAIEmbeddings
from langchain_google_community import BigQueryVectorStore

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

store = BigQueryVectorStore(
    project_id=PROJECT_ID,
    dataset_name="my_langchain_dataset",
    table_name="doc_and_vectors",
    location=REGION,
    embedding=embedding,
)

3. 管理和搜索文本

我们可以添加文本数据,并使用语义搜索找到相似的文档:

python 复制代码
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)

query = "I'd like a fruit."
docs = store.similarity_search(query)
print(docs)

还可以通过向量搜索文档:

python 复制代码
query_vector = embedding.embed_query(query)
docs = store.similarity_search_by_vector(query_vector, k=2)
print(docs)

4. 高级功能和低延迟服务

使用 Feature Store Online Store 可以进一步降低延迟,适合生产环境。

python 复制代码
store.to_vertex_fs_vector_store()  # 将数据转移到 VertexFS

常见问题和解决方案

问题1:访问限制和网络延迟

在某些地区,可能会遇到访问 Google API 的限制,这时可以考虑使用 API 代理服务。API 代理服务可以提高请求的稳定性和响应速度。

问题2:数据同步和管理

在处理大规模数据时,数据同步和管理是一个挑战。可以通过脚本自动化这个过程,或者使用 Google Cloud 提供的自动化工具。

总结和进一步学习资源

本文探讨了如何在 Google Cloud 中使用 BigQuery Vector Search 进行高效的语义搜索,并重点介绍了如何在 LangChain 中管理和查询嵌入数据。如需进一步学习,请参考本文末尾的参考资料。

参考资料

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

---END---

相关推荐
韩立学长12 分钟前
【开题答辩实录分享】以《基于Vue的非遗文化知识分享平台的设计与实现》为例进行选题答辩实录分享
前端·javascript·vue.js
优弧15 分钟前
离开舒适区100天,我后悔了吗?
前端·后端·面试
胡gh29 分钟前
css的臂膀,前端动效的利器,还是布局的“隐形陷阱”?
前端·css·html
灵感菇_36 分钟前
Flutter Riverpod 完整教程:从入门到实战
前端·flutter·ui·状态管理
用户214118326360241 分钟前
紧急修复!Dify CVE-2025-55182 高危漏洞,手把手教你升级避坑
前端
Vic101011 小时前
解决 Spring Security 在异步线程中用户信息丢失的问题
java·前端·spring
wordbaby2 小时前
Expo (React Native) 最佳实践:TanStack Query 深度集成指南
前端·react native
~无忧花开~2 小时前
Vue二级弹窗关闭错误解决指南
开发语言·前端·javascript·vue.js
软件技术NINI2 小时前
前端面试题:请描述一下你对盒模型的理解
前端
码事漫谈2 小时前
VS Code终端从入门到精通完全指南
前端·后端