Google Vertex AI 搜索
Google Vertex AI Search (formerly known as
Enterprise SearchonGenerative AI App Builder) is a part of the Vertex AI machine learning platform offered byGoogle Cloud.
Vertex AI Search使组织能够快速构建由生成式AI驱动的搜索引擎,供客户和员工使用。它依托于多种Google Search技术,包括语义搜索,这种技术通过利用自然语言处理和机器学习技术来推断内容之间的关系以及用户查询意图,从而提供比传统基于关键词的搜索方法更相关的结果。Vertex AI Search 还受益于谷歌在理解用户搜索行为方面的专长,并且会考虑内容的相关性以排序显示结果。
Vertex AI Search是在Google Cloud Console可用的,并且可以通过 API 用于企业工作流集成。
此笔记本展示了如何配置Vertex AI Search并使用Vertex AI Search检索器。Vertex AI Search检索器封装了Python客户端库,并通过其访问搜索服务API。
详细文档请参阅所有VertexAISearchRetriever功能和配置的API参考。
集成细节
| 检索器 | Self-host | 云开发解决方案 | 包 |
|---|---|---|---|
| VertexAISearchRetriever | ❌ | ✅ | langchain_google_community |
设置
安装
您需要安装langchain-google-community和google-cloud-discoveryengine包才能使用Vertex AI Search检索器。
%pip install -qU langchain-google-community google-cloud-discoveryengine
配置对Google云和Vertex AI搜索的访问权限
Vertex AI Search 自2023年8月起一般可用,无需白名单。
在使用检索器之前,您需要完成以下步骤:
创建一个搜索引擎并填充非结构化数据存储
- 遵循Vertex AI搜索入门指南中的说明,设置Google Cloud项目和Vertex AI搜索。
- 使用Google云控制台创建非结构化数据存储
- 使用来自
gs://cloud-samples-data/gen-app-builder/search/alphabet-investor-pdfs云存储文件夹的示例 PDF 文档填充它。 - 请使用
Cloud Storage (without metadata)选项。
- 使用来自
设置凭证以访问Vertex AI搜索API
The Vertex AI Search客户端库由Vertex AI Search检索器使用,为程序化访问Google Cloud提供了高级语言支持。 客户端库支持应用默认凭据(ADC);这些库会在一组定义的位置查找凭据,并使用这些凭据对API请求进行身份验证。 借助ADC,您可以在多种环境中为应用程序提供凭据,例如本地开发或生产环境,而无需修改您的应用程序代码。
如果在Google Colab中运行,请使用google.colab.google.auth进行身份验证,否则请按照支持的方法之一确保您的默认应用凭据已正确设置。
import sys
if "google.colab" in sys.modules:
from google.colab import auth as google_auth
google_auth.authenticate_user()
配置并使用Vertex AI搜索检索器
Vertex AI Search检索器在langchain_google_community.VertexAISearchRetriever类中实现。get_relevant_documents方法返回一个包含langchain.schema.Document文档的列表,其中每个文档的page_content字段填充了文档内容。
根据Vertex AI Search使用的数据类型(网站、结构化或非结构化),page_content字段按照以下方式填充:
- Website with advanced indexing: an
extractive answerthat matches a query. Themetadatafield is populated with metadata (if any) of the document from which the segments or answers were extracted. - 未结构化数据源:要么是一个
extractive segment,要么是匹配查询的extractive answer。如果文档中提取了段落或答案,则metadata字段将填充文档的相关元数据(如果有)。 - 结构化数据源:一个包含从结构化数据源返回的所有字段的字符串json。
metadata字段填充了文档的元数据(如果有的话)
Extractive 答案 & 提取段落
提取式答案是直接从搜索结果中提取的原始文本。这些内容是从最初文档中直接抽取出来的。提取式答案通常会在网页顶部显示,为最终用户提供与查询相关的简短回答。提取式答案可用于网站和非结构化搜索。
提取段落是每个搜索结果中返回的逐字文本。与提取答案相比,提取段落通常更加冗长。提取段落可以作为查询的答案显示,并可用于执行后处理任务以及作为输入提供给大型语言模型以生成答案或新文本。提取段落在非结构化搜索中可用。
对于提取段落和提取答案的更多信息,请参阅产品文档。
注意:抽取式片段需要启用企业版功能。
创建检索器实例时,您可以指定一些参数来控制要访问哪个数据存储以及如何处理自然语言查询,包括提取答案和段落的配置。
必填参数是:
project_id- 您的 Google 云项目 ID。location_id- 数据存储的位置。global(默认值)useu
其中一个选项:
search_engine_id- 您想要使用的搜索应用的ID。 (对于混合搜索是必需项)data_store_id- 您想要使用的数据存储ID。
The project_id, search_engine_id 和 data_store_id 参数可以在检索器的构造函数中显式提供或通过环境变量 - PROJECT_ID, SEARCH_ENGINE_ID 和 DATA_STORE_ID 提供。
您还可以配置一些可选参数,包括:<br>
max_documents- 用于提供提取式片段或答案的最大文档数get_extractive_answers- By default, the retriever is configured to return extractive segments.- Set this field to
Trueto return extractive answers. This is used only whenengine_data_typeset to0(unstructured)
- Set this field to
max_extractive_answer_count- The maximum number of extractive answers returned in each search result.- 最多返回5个答案。这仅在
engine_data_type设置为0(非结构化)时使用。
- 最多返回5个答案。这仅在
max_extractive_segment_count- 每个搜索结果中返回的提取段落的最大数量。
- 当前将返回一个段落。仅在
engine_data_type设置为0(非结构化)时使用。
- 当前将返回一个段落。仅在
filter- 根据数据存储中文档关联的元数据对搜索结果进行筛选的过滤表达式。query_expansion_condition- 规定在什么条件下需要进行查询扩展。
0- 未指定的查询扩展条件。在这种情况下,服务器行为默认为禁用。1- 禁用查询扩展。即使 SearchResponse.total_size 为零,也只使用精确的搜索查询。2- 由 Search API 构建的自动查询扩展。
engine_data_type- 定义了Vertex AI搜索的数据类型0- 无结构化数据1- 结构化数据2- 网站数据3- 混合搜索
迁移指南 GoogleCloudEnterpriseSearchRetriever
在之前的版本中,这个检索器被称为GoogleCloudEnterpriseSearchRetriever。
要更新到新的检索器,请进行以下更改:
- 将导入从:
from langchain.retrievers import GoogleCloudEnterpriseSearchRetriever->from langchain_google_community import VertexAISearchRetriever进行更改。 - 将所有类引用从
GoogleCloudEnterpriseSearchRetriever->VertexAISearchRetriever。
注: 在使用检索器时,如果您希望从单个查询中获得自动跟踪,则可以通过取消注释下方代码来设置您的 LangSmith API密钥:
# os.environ["LANGSMITH_API_KEY"] = getpass.getpass("Enter your LangSmith API key: ")
# os.environ["LANGSMITH_TRACING"] = "true"
Instantiation
配置并使用检索器处理非结构化的数据,带有提取片段
from langchain_google_community import (
VertexAIMultiTurnSearchRetriever,
VertexAISearchRetriever,
)
PROJECT_ID = "<YOUR PROJECT ID>" # Set to your Project ID
LOCATION_ID = "<YOUR LOCATION>" # Set to your data store location
SEARCH_ENGINE_ID = "<YOUR SEARCH APP ID>" # Set to your search app ID
DATA_STORE_ID = "<YOUR DATA STORE ID>" # Set to your data store ID
retriever = VertexAISearchRetriever(
project_id=PROJECT_ID,
location_id=LOCATION_ID,
data_store_id=DATA_STORE_ID,
max_documents=3,
)
query = "What are Alphabet's Other Bets?"
result = retriever.invoke(query)
for doc in result:
print(doc)
配置并使用检索器处理无结构化数据,带有提取式答案
retriever = VertexAISearchRetriever(
project_id=PROJECT_ID,
location_id=LOCATION_ID,
data_store_id=DATA_STORE_ID,
max_documents=3,
max_extractive_answer_count=3,
get_extractive_answers=True,
)
result = retriever.invoke(query)
for doc in result:
print(doc)
配置和使用检索器处理结构化数据
retriever = VertexAISearchRetriever(
project_id=PROJECT_ID,
location_id=LOCATION_ID,
data_store_id=DATA_STORE_ID,
max_documents=3,
engine_data_type=1,
)
result = retriever.invoke(query)
for doc in result:
print(doc)
配置和使用高级网站索引为网站数据检索器进行设置与使用
retriever = VertexAISearchRetriever(
project_id=PROJECT_ID,
location_id=LOCATION_ID,
data_store_id=DATA_STORE_ID,
max_documents=3,
max_extractive_answer_count=3,
get_extractive_answers=True,
engine_data_type=2,
)
result = retriever.invoke(query)
for doc in result:
print(doc)
配置并使用检索器以处理混合数据
retriever = VertexAISearchRetriever(
project_id=PROJECT_ID,
location_id=LOCATION_ID,
search_engine_id=SEARCH_ENGINE_ID,
max_documents=3,
engine_data_type=3,
)
result = retriever.invoke(query)
for doc in result:
print(doc)
配置并使用检索器进行多轮搜索
带有跟进的搜索基于生成式AI模型,与常规未结构化数据搜索不同。
retriever = VertexAIMultiTurnSearchRetriever(
project_id=PROJECT_ID, location_id=LOCATION_ID, data_store_id=DATA_STORE_ID
)
result = retriever.invoke(query)
for doc in result:
print(doc)
用法
使用上述示例,我们使用.invoke来发出单个查询。由于检索器是Runnables,我们可以使用Runnable接口中的任何方法,例如.batch。
使用在链中
我们可以将检索器集成到链中,构建更复杂的应用程序,例如一个简单的基于RAG的应用程序。为了演示目的,我们还实例化了一个VertexAI聊天模型。请参阅相应的Vertex集成文档以获取安装说明。
%pip install -qU langchain-google-vertexai
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.runnables import RunnablePassthrough
from langchain_google_vertexai import ChatVertexAI
prompt = ChatPromptTemplate.from_template(
"""Answer the question based only on the context provided.
Context: {context}
Question: {question}"""
)
llm = ChatVertexAI(model_name="chat-bison", temperature=0)
def format_docs(docs):
return "\n\n".join(doc.page_content for doc in docs)
chain = (
{"context": retriever | format_docs, "question": RunnablePassthrough()}
| prompt
| llm
| StrOutputParser()
)
chain.invoke(query)
API 参考
详细文档请参阅所有VertexAISearchRetriever功能和配置的API参考。