Skip to main content

Google BigQuery 向量搜索

Google Cloud BigQuery 向量搜索 让您使用 GoogleSQL 进行语义搜索,使用向量索引以获得快速的近似结果,或使用暴力搜索以获得精确结果。

本教程演示如何在 LangChain 中使用端到端的数据和嵌入管理系统,并提供使用 BigQueryVectorStore 类在 BigQuery 中进行可扩展的语义搜索。该类是能够在 Google Cloud 中提供统一数据存储和灵活向量搜索的一组 2 个类的一部分:

  • BigQuery 向量搜索:使用 BigQueryVectorStore 类,适合快速原型开发,无需基础设施设置和批量检索。
  • 特征存储在线存储:使用 VertexFSVectorStore 类,支持手动或定期数据同步的低延迟检索。非常适合生产就绪的用户面向的 GenAI 应用程序。

Diagram BQ-VertexFS

开始使用

安装库

%pip install --upgrade --quiet  langchain langchain-google-vertexai "langchain-google-community[featurestore]"

要在此 Jupyter 运行时中使用新安装的包,您必须重启运行时。您可以通过运行下面的单元格来做到这一点,这将重启当前内核。

import IPython

app = IPython.Application.instance()
app.kernel.do_shutdown(True)

在开始之前

设置您的项目 ID

如果您不知道您的项目 ID,请尝试以下操作:

  • 运行 gcloud config list
  • 运行 gcloud projects list
  • 查看支持页面:查找项目 ID
PROJECT_ID = ""  # @param {type:"string"}

# Set the project id
! gcloud config set project {PROJECT_ID}

设置区域

您还可以更改 BigQuery 使用的 REGION 变量。了解更多关于 BigQuery 区域

REGION = "us-central1"  # @param {type: "string"}

设置数据集和表名

它们将是您的 BigQuery 向量存储。

DATASET = "my_langchain_dataset"  # @param {type: "string"}
TABLE = "doc_and_vectors" # @param {type: "string"}

认证您的笔记本环境

  • 如果您使用 Colab 来运行此笔记本,请取消注释下面的单元并继续。
  • 如果您使用的是 Vertex AI Workbench,请查看这里的设置说明。
# from google.colab import auth as google_auth

# google_auth.authenticate_user()

演示:BigQueryVectorStore

创建嵌入类实例

您可能需要通过运行以下命令在您的项目中启用 Vertex AI API gcloud services enable aiplatform.googleapis.com --project {PROJECT_ID} (将 {PROJECT_ID} 替换为您的项目名称)。

您可以使用任何LangChain嵌入模型

from langchain_google_vertexai import VertexAIEmbeddings

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

初始化 BigQueryVectorStore

如果 BigQuery 数据集和表不存在,将自动创建它们。有关所有可选参数,请参见类定义这里

from langchain_google_community import BigQueryVectorStore

store = BigQueryVectorStore(
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)

搜索文档

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)

使用元数据过滤器搜索文档

向量存储支持在执行文档搜索时对元数据字段应用过滤器的两种方法:

  • 字典基础过滤器
  • 你可以传递一个字典(dict),其中键表示元数据字段,值指定过滤条件。此方法在键和相应值之间应用等值过滤。当提供多个键值对时,它们使用逻辑与操作组合。
  • SQL基础过滤器
  • 或者,你可以提供一个表示SQL WHERE子句的字符串,以定义更复杂的过滤条件。这允许更大的灵活性,支持比较运算符和逻辑运算符等SQL表达式。
# Dictionary-based Filters
# This should only return "Banana" document.
docs = store.similarity_search_by_vector(query_vector, filter={"len": 6})
print(docs)
# SQL-based Filters
# This should return "Banana", "Apples and oranges" and "Cars and airplanes" documents.
docs = store.similarity_search_by_vector(query_vector, filter={"len = 6 AND len > 17"})
print(docs)

批量搜索

BigQueryVectorStore提供了一个batch_search方法,用于可扩展的向量相似性搜索。

results = store.batch_search(
embeddings=None, # can pass embeddings or
queries=["search_query", "search_query"], # can pass queries
)

添加带嵌入的文本

您还可以使用 add_texts_with_embeddings 方法带入您自己的嵌入。 这对于可能需要在生成嵌入之前进行自定义预处理的多模态数据特别有用。

items = ["some text"]
embs = embedding.embed(items)

ids = store.add_texts_with_embeddings(
texts=["some text"], embs=embs, metadatas=[{"len": 1}]
)

使用特征存储进行低延迟服务

您可以简单地使用 .to_vertex_fs_vector_store() 方法来获取一个 VertexFSVectorStore 对象,该对象为在线用例提供低延迟。所有必需的参数将自动从现有的 BigQueryVectorStore 类中转移。有关您可以使用的所有其他参数,请参见 类定义

使用 .to_bq_vector_store() 方法返回到 BigQueryVectorStore 同样简单。

store.to_vertex_fs_vector_store()  # pass optional VertexFSVectorStore parameters as arguments

相关


Was this page helpful?


You can also leave detailed feedback on GitHub.

扫我,入群扫我,找书