探索 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---

相关推荐
AI浩6 小时前
【Labelme数据操作】LabelMe标注批量复制工具 - 完整教程
运维·服务器·前端
涔溪6 小时前
CSS 网格布局(Grid Layout)核心概念、基础语法、常用属性、实战示例和进阶技巧全面讲解
前端·css
2401_878454537 小时前
浏览器工作原理
前端·javascript
西陵7 小时前
为什么说 AI 赋能前端开发,已经不是选择题,而是必然趋势?
前端·架构·ai编程
by__csdn8 小时前
Vue3 setup()函数终极攻略:从入门到精通
开发语言·前端·javascript·vue.js·性能优化·typescript·ecmascript
天天扭码8 小时前
前端如何实现RAG?一文带你速通,使用RAG实现长期记忆
前端·node.js·ai编程
Luna-player9 小时前
在前端中,<a> 标签的 href=“javascript:;“ 这个是什么意思
开发语言·前端·javascript
lionliu05199 小时前
js的扩展运算符的理解
前端·javascript·vue.js
小草cys9 小时前
项目7-七彩天气app任务7.4.2“关于”弹窗
开发语言·前端·javascript
奇舞精选9 小时前
GELab-Zero 技术解析:当豆包联手中兴,开源界如何守住端侧 AI 的“最后防线”?
前端·aigc