Vertex AI 与 Elasticsearch 开放推理 API 集成,为你的 RAG 应用程序带来重新排名
作者:来自 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

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
揭秘 FineVideo 数据集构建的背后的秘密
开放视频数据集稀缺,因此减缓了开源视频 AI 的发展。为此,我们构建了 FineVideo,这是一个包含 43,000 个视频的数据集,总时长为 3,400 小时,并带有丰富的描述、叙事细节、场景分割和问答对。 FineVideo 包含高度多样化的视频和元数据集合,使其成为训练模型理解视频内容、训练扩散模型从文本描述生成视频或使用其结构化数据作为输入训练计算机视觉模型的良好素材。 等等,你还没有看过 FineVideo 吗?通过 数据集探索页面 查看它。 关于这篇博客文章 在这篇博客文章中,我们分享了开发 FineVideo 的技术细节和代码: 从 YouTube-Commons 中的 190 万个视频开始,到最终获得 44,000 个带有详细标注的视频。 一个好的开始方式是查看我们旅程的不同步骤。这些步骤涉及内容过滤、标注和输出结构化。 FineVideo 视频过滤和标注管道 在接下来的部分中,我们将讨论每个步骤,并提供相关代码部分的参考。如果你更喜欢直接浏览代码,请查看我们在 Github 上的 FineVideo 仓库。 首先,让我们看看我们是如何获得初始的 YouTube 视频...
- 下一篇
长上下文LLMs:RAG 的终结者还是最佳搭档?
编者按:随着大语言模型(LLMs)的上下文窗口不断扩大,您是否开始思考:我们还需要花费大量时间和资源来构建复杂的检索增强生成(RAG)系统吗? 本文深入探讨了长上下文 LLMs 与 RAG 系统的优劣势,揭示了它们在实际应用中的表现差异。通过对最新四篇学术研究的全面分析,作者阐明了长上下文 LLMs 在某些任务中的优势,同时也指出了 RAG 系统在某些专业领域任务和成本效益方面仍具有优势。 作者建议将 RAG 与长上下文 LLMs 结合使用,以发挥协同效应,并呼吁建立更全面、更严格的评估体系,包括统一的评估数据集和评估指标。未来,如何有效结合这两种技术,应当是人工智能领域的一个重要研究方向。 作者 | Florian June 编译 | 岳扬 2023 年,大语言模型(LLMs)的上下文窗口通常在 4K 到 8K 左右。但到了 2024 年 7 月,上下文窗口超过 128K 的 LLMs 已经变得很普遍了。 以 Claude 2[1] 为例,其上下文窗口可达 100K。Gemini 1.5[2] 则宣称能够处理 2M 的上下文信息,而 LongRoPE[3] 更是将 LLMs 的上下文...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- SpringBoot2编写第一个Controller,响应你的http请求并返回结果
- SpringBoot2初体验,简单认识spring boot2并且搭建基础工程
- Windows10,CentOS7,CentOS8安装Nodejs环境
- 设置Eclipse缩进为4个空格,增强代码规范
- CentOS8编译安装MySQL8.0.19
- CentOS关闭SELinux安全模块
- Windows10,CentOS7,CentOS8安装MongoDB4.0.16
- Linux系统CentOS6、CentOS7手动修改IP地址
- CentOS8,CentOS7,CentOS6编译安装Redis5.0.7
- Docker快速安装Oracle11G,搭建oracle11g学习环境