作者:来自 Elastic Tim Grein
你可以使用 Google Vertex AI 和 Elasticsearch 开放推理 API 构建语义搜索和语义重新排名!
在我们与 Google Vertex AI 团队密切合作之后,我们很高兴地宣布 Elasticsearch 向量数据库现已与 Google Vertex AI 原生集成,使开发人员能够存储由 Google Vertex AI 文本嵌入 API(Google Vertex AI Text Embeddings API)中可用的任何文本嵌入模型生成的嵌入。Elasticsearch 还通过 Elastic 开放推理 API 与 Google Vertex AI 的重新排名功能原生集成。开发人员可以结合使用这两种功能,也可以单独使用,以构建强大的语义搜索和 RAG 应用程序。
Google Vertex AI 是一个用于 AI 应用程序的托管开发平台。你可以访问各种模型,包括文本嵌入和重新排名模型。在这篇博文中,我们将使用 `text-embedding-004` 为英文文本生成嵌入。为了提高搜索结果的质量,我们将使用 `semantic-ranker-512@latest` 模型。
在此博客中,我们将:
- 使用 text-embedding-004 生成嵌入
- 使用 Elastic 的新 semantic_text 字段在 Elasticsearch 中分块和存储嵌入
- 使用 semantic-ranker-512@latest 构建语义重新排名示例
- 使用 Elastic 的检索器构建具有 BM25 和语义重新排名的两阶段检索
开始生成嵌入
首先,你需要一个可以访问 Google Vertex AI 平台的 Google 帐户。然后,你需要在 Google Cloud 控制台中选择一个现有项目或创建一个新的 Google Cloud 项目。将你的项目 ID 保存在某个地方,因为我们稍后会用到它。
之后你需要启用 Vertex AI API:
然后你需要在 IAM & Admin > Service Accounts 下创建一个服务帐户:
你需要确保你的服务帐户具有正确的角色和权限,以便能够使用 Google Vertex AI 生成嵌入。分配包含权限 aiplatform.endpoints.predict 的 Vertex AI 用户(roles aiplatform.user)。
为你的服务帐户创建密钥时,请确保选择 json。下载服务帐户 JSON 并保存以供日后使用。
现在,打开 Kibana 的开发控制台。你还可以使用 HTTP GUI 客户端(如 postman)或任何其他工具(如 curl)执行以下步骤。
你将使用创建推理 API(Create inference API)创建推理端点,方法是提供服务帐户 JSON、位置、要使用的模型和项目 ID:
PUT _inference/text_embedding/google_vertex_ai_embedding
{
"service": "googlevertexai",
"service_settings": {
"service_account_json": "<service-account-json>",
"model_id": "<model-id>",
"location": "<location>",
"project_id": "<project-id>"
}
}
你将收到来自 Elasticsearch 的响应,其中包含创建的端点:
{
"inference_id": "google_vertex_ai_embedding",
"task_type": "text_embedding",
"service": "googlevertexai",
"service_settings": {
"location": "<location>",
"project_id": "<project-id>",
"model_id": "<model-id>",
"dimensions": 768,
"similarity": "dot_product",
"rate_limit": {
"requests_per_minute": 30000
}
},
"task_settings": {}
}
在底层,Elasticsearch 将使用你的凭据连接到 Google Vertex AI,以获取用于生成嵌入的维度数。它还会将检索期间使用的相似度度量设置为合理的默认值(在本例中为 dot_product)。
你可以通过调用执行推理 API( perform inference API) 来测试你的端点:
POST _inference/text_embedding/google_vertex_ai_embedding
{
"input": "This text will be embedded"
}
API 将返回一个响应,其中包含针对你的输入生成的嵌入:
{
"text_embedding": [
{
"embedding": [
-0.014122169,
0.044469967,
0.02421774,
-0.003546892,
...
]
}
]
}
将 semantic_text 与 Google Vertex AI 嵌入结合使用
现在,我们已经使用 Google Vertex AI 嵌入设置了推理端点,我们可以使用 Elasticsearch 中的新 semantic_text 字段类型来执行开箱即用的语义搜索,而无需设置额外的应用程序代码,在提取和查询期间明确调用推理 API 来生成嵌入。
请注意,如果要生成嵌入的文本太大,推理 API 和 semantic_text 的组合会执行自动分块(将较大的文本分成较小的块)!我们非常高兴看到开发人员如何使用此功能。
为了继续我们的示例,我们将创建一个索引,该索引引用我们的推理端点 google_vertex_ai_embedding,以便在索引文档和查询时搜索时生成嵌入:
PUT my-index
{
"mappings": {
"properties": {
"my_field": {
"type": "semantic_text",
"inference_id": "google_vertex_ai_embedding"
}
}
}
}
我们现在可以索引文档,semantic_text 字段类型将负责使用我们的推理端点生成密集嵌入,该端点在后台调用 Google Vertex AI API。出于演示目的,我们只索引一个文档:
PUT my-index/_doc/doc1
{
"my_field": "These are not the droids you're looking for. He's free to go around"
}
你现在可以使用以下请求发出语义搜索请求:
GET my-index/_search
{
"query": {
"semantic": {
"field": "my_field",
"query": "robots you're searching for"
}
}
}
你应该取回我们之前索引的文档:
{
"took": 818,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 0.7608695,
"hits": [
{
"_index": "my-index",
"_id": "doc1",
"_score": 0.7608695,
"_source": {
"my_field": {
"text": "These are not the droids you're looking for. He's free to go around",
"inference": {
"inference_id": "google_vertex_ai_embedding",
"model_settings": {
"task_type": "text_embedding",
"dimensions": 768,
"similarity": "dot_product",
"element_type": "float"
},
"chunks": [
{
"text": "These are not the droids you're looking for. He's free to go around",
"embeddings": [
-0.0022440397,
-0.028731884,
...
]
}
]
}
}
]
}
开始使用语义重新排序
我们已经创建了一个帐户和一个项目,我们将重复使用它们来设置重新排序(reranking)。
为什么要使用重新排序器?重新排序器可以提高早期检索机制结果的相关性。语义重新排序器使用机器学习模型根据搜索结果与查询的语义相似性对其进行重新排序。
回到我们的实现,你需要启用 Discovery Engine API 才能对文档进行重新排序:
你可以重复使用我们之前创建的服务帐户,也可以创建一个新的服务帐户。将包含权限 discoveryengine.rankingConfigs.rank 的角色 Discovery Engine Viewer (roles/discoveryengine.viewer) 分配给你将使用的服务帐户。
对于以下步骤,你可以再次使用 Kibana Dev Console 或你喜欢的任何 http 客户端。
首先,我们将创建一个推理端点,以便能够对文档进行重新排名:
PUT _inference/rerank/google_vertex_ai_rerank
{
"service": "googlevertexai",
"service_settings": {
"service_account_json": "<service-account-json>",
"project_id": "<project-id>"
}
}
你将再次收到表明推理端点已成功创建的响应:
{
"inference_id": "google_vertex_ai_rerank",
"task_type": "rerank",
"service": "googlevertexai",
"service_settings": {
"project_id": "<project-id>",
"rate_limit": {
"requests_per_minute": 300
}
},
"task_settings": {}
}
让我们将一组文档发送到我们新创建的端点,以确保重新排名有效。我们将使用 Google Vertex AI 的重新排名文档作为示例:
POST _inference/rerank/google_vertex_ai_rerank
{
"query": "Why is the sky blue?",
"input": [
"A canvas stretched across the day,\nWhere sunlight learns to dance and play.\nBlue, a hue of scattered light,\nA gentle whisper, soft and bright.",
"The sky appears blue due to a phenomenon called Rayleigh scattering. Sunlight is comprised of all the colors of the rainbow. Blue light has shorter wavelengths than other colors, and is thus scattered more easily."
]
}
你将收到回复,其中科学解释的排名应该更高:
{
"rerank": [
{
"index": 0,
"relevance_score": 0.82,
"text": "The sky appears blue due to a phenomenon called Rayleigh scattering. Sunlight is comprised of all the colors of the rainbow. Blue light has shorter wavelengths than other colors, and is thus scattered more easily."
},
{
"index": 1,
"relevance_score": 0.43,
"text": """A canvas stretched across the day,
Where sunlight learns to dance and play.
Blue, a hue of scattered light,
A gentle whisper, soft and bright."""
}
]
}
将检索器与 Google Vertex AI 语义重新排名结合使用
在 8.14 中,我们引入了另一个令人兴奋的功能,称为检索器(retrievers),它提供了一个直观的 API,可以在单个 _search API 调用中定义多阶段检索管道。这消除了应用程序向 Elasticsearch 发出多个搜索请求并在之后相应地合并结果的负担。在我们的示例中,我们定义了一个简单的多阶段检索管道,它使用常见的 BM25 算法来检索一组与术语 “sky” 相关的文档。之后,这组文档将传递到我们的推理端点 google_vertex_ai_rerank,以进一步优化结果集的顺序,从而为我们提供天空为何是蓝色的科学解释。
我们正在使用 Bulk API 创建一小部分文档,其中包括关于 mountains、sky、ocean 的诗歌,以及天空为何是蓝色的一个科学解释:
PUT _bulk
{"index": {"_index": "another-index", "_id": "1"} }
{"text": "A canvas stretched across the day,\nWhere sunlight learns to dance and play.\nBlue, a hue of scattered light,\nA gentle whisper, soft and bright."}
{"index": {"_index": "another-index", "_id": "2"} }
{"text": "The sky appears blue due to a phenomenon called Rayleigh scattering. Sunlight is comprised of all the colors of the rainbow. Blue light has shorter wavelengths than other colors, and is thus scattered more easily."}
{"index": {"_index": "another-index", "_id": "3"} }
{"text": "The sky wraps around the earth so wide, A tender touch where dreams reside. Golden streaks at dawn’s first light, The sky awakens, pure and bright."}
{"index": {"_index": "another-index", "_id": "4"} }
{"text": "The sky dims as the day unwinds, A lullaby of soft winds kind. Purple hues in twilight's grace, The night arrives, a tender embrace."}
{"index": {"_index": "another-index", "_id": "5"} }
{"text": "The sky at night, a velvet sea, With stars that shine so endlessly. A canvas dark, yet full of light, Guiding travelers through the night."}
{"index": {"_index": "another-index", "_id": "6"} }
{"text": "The ocean hums a gentle tune, Beneath the light of the silver moon. Waves that cradle dreams so deep, In their rhythm, we find sleep."}
{"index": {"_index": "another-index", "_id": "7"} }
{"text": "The tide retreats with a whispered sigh, Leaving shells as memories pass by. A dance of water, soft and slow, In and out, a constant flow."}
{"index": {"_index": "another-index", "_id": "8"} }
{"text": "The ocean’s arms are vast and wide, A place where endless wonders hide. Blue as far as the eye can see, A world of calm and mystery."}
{"index": {"_index": "another-index", "_id": "9"} }
{"text": "The ocean speaks in murmurs low, Secrets only the dolphins know. A world below the surface bright, Where colors blend in liquid light."}
{"index": {"_index": "another-index", "_id": "10"} }
{"text": "The ocean sings a song so clear, A melody that draws us near. Waves that rise and gently fall, In their call, we hear it all."}
{"index": {"_index": "another-index", "_id": "11"} }
{"text": "The mountains stand with ancient pride, Their secrets in the rocks they hide. A silent strength, so bold and high, Reaching peaks that touch the eye."}
{"index": {"_index": "another-index", "_id": "12"} }
{"text": "A mountain trail that winds and weaves, Through forests thick with whispering leaves. Each step a journey, each climb a test, Until you find the summit's rest."}
{"index": {"_index": "another-index", "_id": "13"} }
{"text": "The mountains echo with the sound, Of winds that dance and streams that bound. A fortress carved by time’s own hand, Where nature’s might and beauty stand."}
POST another-index/_search
{
"retriever": { // Retriever query
"text_similarity_reranker": { // Outermost retriever will perform reranking
"retriever": {
"standard": { // First-stage retriever is a standard Elasticsearch query
"query": {
"match": { // Standard BM25 matching
"text": "sky"
}
}
}
},
"field": "text", // Document field to send to reranker
"rank_window_size": 10, // Reranking will work on top K hits
"inference_id": "google_vertex_ai_rerank", // Inference endpoint
"inference_text": "Why is the sky blue?",
"min_score": 0.6 // Minimum relevance score
}
}
}
结论
只需使用几个简单的 API 调用,即可利用 semantic_text 和 retrievers 的强大功能以及 Google Vertex AI 的密集嵌入和重新排名功能,从而抽象出语义搜索和重新排名的复杂部分。所有这些功能都已在我们的 serverless 产品中提供,请立即试用!
访问 Search Labs 上的 Google Vertex AI 页面,或尝试搜索实验室 GitHub 上的其他示例笔记本。
准备好自己尝试了吗?开始免费试用。
Elasticsearch 集成了 LangChain、Cohere 等工具。加入我们的 Beyond RAG Basics 网络研讨会,构建您的下一个 GenAI 应用程序!
原文:Elasticsearch vector database adds support for Google Vertex AI — Search Labs