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

相关推荐
whisperrr.1 小时前
【JavaWeb12】数据交换与异步请求:JSON与Ajax的绝妙搭配是否塑造了Web的交互革命?
前端·ajax·json
烂蜻蜓2 小时前
前端已死?什么是前端
开发语言·前端·javascript·vue.js·uni-app
谢尔登3 小时前
Vue 和 React 的异同点
前端·vue.js·react.js
祈澈菇凉7 小时前
Webpack的基本功能有哪些
前端·javascript·vue.js
小纯洁w7 小时前
Webpack 的 require.context 和 Vite 的 import.meta.glob 的详细介绍和使用
前端·webpack·node.js
想睡好8 小时前
css文本属性
前端·css
qianmoQ8 小时前
第三章:组件开发实战 - 第五节 - Tailwind CSS 响应式导航栏实现
前端·css
zhoupenghui1688 小时前
golang时间相关函数总结
服务器·前端·golang·time
White graces9 小时前
正则表达式效验邮箱格式, 手机号格式, 密码长度
前端·spring boot·spring·正则表达式·java-ee·maven·intellij-idea
庸俗今天不摸鱼9 小时前
Canvas进阶-4、边界检测(流光,鼠标拖尾)
开发语言·前端·javascript·计算机外设