Elasticsearch Inference API增加对阿里云AI的支持

本文涉及的产品
Elasticsearch Serverless检索通用型,资源抵扣包 100CU*H
简介: 本文将介绍如何在 Elasticsearch 中设置和使用阿里云的文本生成、重排序、稀疏向量和稠密向量服务,提升搜索相关性。

作者: David Kyle,Weizijun,Jerry

近日,Elastic宣布 Elasticsearch 开放推理 API 集成阿里云 AI 搜索能力。这项工作使Elastic用户能够直接连接阿里云AI搜索开放平台。使用 Elasticsearch 向量数据库构建 RAG 应用程序的开发人员可以用semantic_text 字段类型存储和使用由阿里云AI搜索开放平台上托管的模型生成的稠密和稀疏向量。此外,Elastic 用户现在可以集成阿里云AI搜索的重排序模型,以增强语义重排序,还有 通义千问 大语言模型系列。

本篇博客将探讨如何将阿里云的AI服务与Elasticsearch集成。您将学习如何在 Elasticsearch 中设置和使用阿里巴巴的文本生成(chat completion)、重排序(rerank)、稀疏向量(sparse vector)和稠密向量(dense vector)服务。将这些多种类型的模型集成到推理任务中将增强包括 RAG 在内的许多应用场景的搜索相关性。

阿里云团队为 Elasticsearch 开放推理 API 贡献代码来支持这些任务类型,并且通过示例了解如何在 Elasticsearch 环境中配置和使用这些服务。注意阿里云使用 service_id 这样的术语 而不是 model_id

使用阿里云AI搜索开放平台中的基础模型

本演练假设您已经拥有阿里云的帐户来使用阿里云AI搜索开放平台。接下来,您需要创建一个工作空间和 API key以用于创建推理模型。

在 Elasticsearch 中创建推理 API 端点

在 Elasticsearch 中,通过“alibabacloud-ai-search”服务来创建端点,并提供服务设置,包括工作空间、主机地址、服务 ID 和用于访问阿里云AI搜索平台的 api key。在 Elasticsearch 的示例中,使用“ops-text-embedding-001”作为service id 创建一个文本向量端点。

PUT _inference/text_embedding/ali_ai_embeddings{    "service": "alibabacloud-ai-search",    "service_settings": {        "api_key": "<api_key>",        "service_id": "ops-text-embedding-001",        "host": "xxxxx.platform-cn-shanghai.opensearch.aliyuncs.com",        "workspace": "default"    }}

您将收到来自 Elasticsearch 的响应,其中包含已成功创建的端点:

{  "inference_id": "ali_ai_embeddings",  "task_type": "text_embedding",  "service": "alibabacloud-ai-search",  "service_settings": {    "similarity": "dot_product",    "dimensions": 1536,    "service_id": "ops-text-embedding-001",    "host": "xxxxx.platform-cn-shanghai.opensearch.aliyuncs.com",    "workspace": "default",    "rate_limit": {      "requests_per_minute": 10000    }  },  "task_settings": {}}

请注意,模型创建不需要其他额外的设置。 Elasticsearch将自动连接阿里云AI搜索开放平台来测试您的凭据和service id,并为您填写维度数和相似度度量。

接下来,测试端点以确保一切设置正确。为此将调用执行推理 API:

POST _inference/text_embedding/ali_ai_embeddings{  "input": "What is Elastic?"}

API 调用将返回输入文本生成的向量,如下所示:

{    "text_embedding": [        {            "embedding": [                0.048400473,                0.051464397,                … (additional values) …                0.033325635,                -0.008986305            ]        }    ]}

您现在已准备好开始探索。尝试完这些示例后,请看一下 Elasticsearch 中针对语义搜索应用场景的一些令人兴奋的创新:

  • 新的`semantic_text` 字段 简化了向量的存储和分块 - 只需选择您的模型,Elastic 即可完成剩余的工作!
  • 8.14 中引入的 `retrievers` 允许您设置多阶段召回处理管道

首先,需要我们深入研究示例!

1. 会话生成

阿里云提供了多种会话生成模型,service ID 列于其模型 API文档 中。

步骤1:配置会话生成服务

设置用于会话生成的推理服务:

PUT _inference/completion/ali-chat{ "service": "alibabacloud-ai-search", "service_settings": {     "host" : "xxxxx.platform-cn-shanghai.opensearch.aliyuncs.com",     "api_key": "xxxxxxxxxxxxxxxxxx",     "service_id": "ops-qwen-turbo",     "workspace" : "default" }}

返回

"inference_id": "ali-chat", "task_type": "completion", "service": "alibabacloud-ai-search", "service_settings": {   "service_id": "ops-qwen-turbo",   "host": "xxxxx.platform-cn-shanghai.opensearch.aliyuncs.com",   "workspace": "default",   "rate_limit": {     "requests_per_minute": 1000   } }, "task_settings": {}}

步骤2:发出会话生成的请求

使用配置的端点,发送 POST 请求以生成会话:

POST _inference/completion/ali-chat{ "input":["Where is the capital of Henan?"]}

返回

{ "completion": [   {     "result": "The capital of Henan is Zhengzhou."   } ]}

独一无二的,在阿里云的 Elastic 推理 API 集成中,聊天历史记录可以包含在输入中,在此示例中包含了之前返回的内容并添加了:“那里有什么有趣的事情吗?”

POST _inference/completion/ali-chat{ "input":["Where is the capital of Henan?", "The capital of Henan is Zhengzhou.", "What fun things are there?" ]}

响应明确包含了历史记录

{ "completion": [   {     "result": "I'm sorry, I do not have enough information to provide a specific list of fun things to do in Zhengzhou, Henan. I can only tell you that Zhengzhou is the capital of Henan province. To find out about fun activities, attractions, or events in Zhengzhou, I would suggest researching local tourism websites, asking locals, or checking out travel guides for the area."   } ]}

在未来的更新中,Elastic 计划允许用户明确地包含聊天历史记录,以提高易用性。

2. 重排序

继续下一个任务类型, 重排序。重排序可以利用阿里云强大的模型对搜索结果进行重新排序,以提高相关性。如果您想了解有关此概念的更多信息,请查看 Elastic 上的此博客 Search Labs

步骤1:配置重排序服务

配置重排序推理服务:

PUT _inference/rerank/ali-rank{ "service": "alibabacloud-ai-search", "service_settings": {   "api_key": "xxxxxxxxxxxxxxxxxx",   "service_id": "ops-bge-reranker-larger",   "host" : "xxxxx.platform-cn-shanghai.opensearch.aliyuncs.com",   "workspace" : "default"    }}

{ "inference_id": "ali-rank", "task_type": "rerank", "service": "alibabacloud-ai-search", "service_settings": {   "service_id": "ops-bge-reranker-larger",   "host": "xxxxx.platform-cn-shanghai.opensearch.aliyuncs.com",   "workspace": "default",   "rate_limit": {     "requests_per_minute": 1000   } }, "task_settings": {}}

步骤2:发出重排序请求

发送 POST 请求以重新排列您的搜索查询结果:

rerank 接口不需要很多配置(task_settings),它返回语义相关性分数,越相关的越靠前,并返回其对应文本在输入数组中的序号 。

POST _inference/rerank/ali-rank{ "query": "What is the capital of the USA?", "input": [   "Carson City is the capital city of the American state of Nevada. At the 2010 United States Census, Carson City had a population of 55,274.",   "Capital punishment (the death penalty) has existed in the United States since before the United States was a country. As of 2017, capital punishment is legal in 30 of the 50 states.",   "The Commonwealth of the Northern Mariana Islands is a group of islands in the Pacific Ocean that are a political division controlled by the United States. Its capital is Saipan.",   "Washington, D.C. (also known as simply Washington or D.C., and officially as the District of Columbia) is the capital of the United States. It is a federal district.",   "Charlotte Amalie is the capital and largest city of the United States Virgin Islands. It has about 20,000 people. The city is on the island of Saint Thomas.",   "North Dakota is a state in the United States. 672,591 people lived in North Dakota in the year 2010. The capital and seat of government is Bismarck." ]}

{ "rerank": [   {     "index": 3,     "relevance_score": 0.9998832   },   {     "index": 4,     "relevance_score": 0.008847355   },   {     "index": 5,     "relevance_score": 0.0026626128   },   {     "index": 0,     "relevance_score": 0.00068250194   },   {     "index": 2,     "relevance_score": 0.00019716943   },   {     "index": 1,     "relevance_score": 0.00011591934   } ]}

3. 稀疏向量

阿里云特别为生成稀疏向量提供了一个模型 ,使用 ops-text-sparse-embedding-001 这个 service id。

步骤1:配置稀疏向量生成服务

PUT _inference/sparse_embedding/ali-sparse-embedding{ "service": "alibabacloud-ai-search", "service_settings": {   "api_key": "xxxxxxxxxxxxxxxxxx",   "service_id": "ops-text-sparse-embedding-001",   "host" : "xxxxx.platform-cn-shanghai.opensearch.aliyuncs.com",   "workspace" : "default" }}

{ "inference_id": "ali-sparse-embedding", "task_type": "sparse_embedding", "service": "alibabacloud-ai-search", "service_settings": {   "service_id": "ops-text-sparse-embedding-001",   "host": "xxxxx.platform-cn-shanghai.opensearch.aliyuncs.com",   "workspace": "default",   "rate_limit": {     "requests_per_minute": 1000   } }, "task_settings": {}}

步骤2:发出稀疏向量生成查询

Sparse 的 task_settings 为:

  • input_type - ingest 或 search
  • return_token - 如果为 true,则在响应中包含文本token,否则仅返回数字

POST _inference/sparse_embedding/ali-sparse-embedding{ "input": "Hello world", "task_settings": {   "input_type": "search",   "return_token": true }}

{ "sparse_embedding": [   {     "is_truncated": false,     "embedding": {       "hello": 0.27783203,       "world": 0.28222656     }   } ]}

设置 return_token == false 返回如下

{ "sparse_embedding": [   {     "is_truncated": false,     "embedding": {       "8999": 0.28222656,       "35378": 0.27783203     }   } ]}

4. 文本向量

阿里云还为不同的任务类型提供多种 文本向量 模型 。

第 1 步:配置文本向量服务

文本向量只有一个task_setting:

  • input_type - ingest 或 search

PUT _inference/text_embedding/ali-embeddings{ "service": "alibabacloud-ai-search", "service_settings": {   "api_key": "xxxxxxxxxxxxxxxxxx",   "service_id": "ops-text-embedding-001",   "host" : "xxxxx.platform-cn-shanghai.opensearch.aliyuncs.com",   "workspace" : "default" }}

{ "inference_id": "ali-embeddings", "task_type": "text_embedding", "service": "alibabacloud-ai-search", "service_settings": {   "service_id": "ops-text-embedding-001",   "host": "xxxxx.platform-cn-shanghai.opensearch.aliyuncs.com",   "workspace": "default",   "rate_limit": {     "requests_per_minute": 1000   },   "similarity": "dot_product",   "dimensions": 1536 }, "task_settings": {}}

步骤2:发出文本向量请求

发送 POST 请求以生成文本向量:

POST _inference/text_embedding/ali-embeddings{ "input": "Hello world"}

{ "text_embedding": [   {     "embedding": [       -0.017036675,       0.07038724,       0.044685286,       0.0064531807,       0.013290042,       0.011183944,       -0.0020014185,       -0.009508779,

使用 Elastic 和阿里云进行AI搜索

无论您是使用 Elasticsearch 实现混合搜索、语义重排序,还是通过摘要增强 RAG 应用场景,与阿里云 AI 服务的连接都为 Elasticsearch 开发人员打开了一个充满可能性的新世界。

  • 要继续深入了解,请参考 Jupyter notebook ,里面包含了 Inference API 与阿里云 AI 搜索结合的端到端完整的示例。
  • 阅读阿里云关于 Elasticsearch AI搜索的创新的 公告 。

用户现在可以开始在 Elasticsearch Serverless环境和即将推出的 Elasticsearch 版本中使用它。愉快地进行搜索吧!


相关实践学习
使用阿里云Elasticsearch体验信息检索加速
通过创建登录阿里云Elasticsearch集群,使用DataWorks将MySQL数据同步至Elasticsearch,体验多条件检索效果,简单展示数据同步和信息检索加速的过程和操作。
ElasticSearch 入门精讲
ElasticSearch是一个开源的、基于Lucene的、分布式、高扩展、高实时的搜索与数据分析引擎。根据DB-Engines的排名显示,Elasticsearch是最受欢迎的企业搜索引擎,其次是Apache Solr(也是基于Lucene)。 ElasticSearch的实现原理主要分为以下几个步骤: 用户将数据提交到Elastic Search 数据库中 通过分词控制器去将对应的语句分词,将其权重和分词结果一并存入数据 当用户搜索数据时候,再根据权重将结果排名、打分 将返回结果呈现给用户 Elasticsearch可以用于搜索各种文档。它提供可扩展的搜索,具有接近实时的搜索,并支持多租户。
相关文章
|
16天前
|
存储 人工智能 搜索推荐
如何用大模型+RAG 给宠物做一个 AI 健康助手?——阿里云 AI 搜索开放平台
本文分享了如何利用阿里云 AI 搜索开放平台,基于 LLM+RAG 的系统框架,构建“宠物医院AI助手”的实践过程。
200 14
|
27天前
|
数据采集 机器学习/深度学习 人工智能
面向 MoE 和推理模型时代:阿里云大数据 AI 产品升级发布
2025 AI 势能大会上,阿里云大数据 AI 平台持续创新,贴合 MoE 架构、Reasoning Model 、 Agentic RAG、MCP 等新趋势,带来计算范式变革。多款大数据及 AI 产品重磅升级,助力企业客户高效地构建 AI 模型并落地 AI 应用。
|
6天前
|
人工智能 自然语言处理 文字识别
阿里云 AI 搜索开放平台新增:服务开发能力
阿里云 AI 搜索开放平台新发布:服务开发能,可通过集成 dsw 能力并新增 notebook 功能,进一步提升用户编排效率。
|
6天前
|
运维 Cloud Native 应用服务中间件
阿里云微服务引擎 MSE 及 API 网关 2025 年 4 月产品动态
阿里云微服务引擎 MSE 面向业界主流开源微服务项目, 提供注册配置中心和分布式协调(原生支持 Nacos/ZooKeeper/Eureka )、云原生网关(原生支持Higress/Nginx/Envoy,遵循Ingress标准)、微服务治理(原生支持 Spring Cloud/Dubbo/Sentinel,遵循 OpenSergo 服务治理规范)能力。API 网关 (API Gateway),提供 APl 托管服务,覆盖设计、开发、测试、发布、售卖、运维监测、安全管控、下线等 API 生命周期阶段。帮助您快速构建以 API 为核心的系统架构.满足新技术引入、系统集成、业务中台等诸多场景需要
阿里云微服务引擎 MSE 及 API 网关 2025 年 4 月产品动态
|
23天前
|
存储 人工智能 安全
AI 驱动下的阿里云基础设施:技术创新与产品演进
本文整理自阿里云智能集团副总裁、阿里云弹性计算产品线与存储产品线负责人吴结生在“2025 AI势能大会”上的演讲,重点介绍了阿里云在AI基础设施领域的技术创新与产品演进。内容涵盖CIPU架构、盘古存储系统、高性能网络HPN等关键技术,以及第九代英特尔企业实例、ESSD同城冗余云盘等新产品发布。同时,文章详细阐述了灵骏集群的优化措施和可观测能力的提升,展示阿里云如何通过持续创新为AI负载提供强大支持,助力企业在AI时代实现智能化转型。
AI 驱动下的阿里云基础设施:技术创新与产品演进
|
8天前
|
人工智能 城市大脑 运维
2025数字中国建设峰会:阿里云+AI深入千行百业
近日,第八届数字中国建设峰会在福州召开。峰会期间,阿里云及通义大模型服务政企的一批领先成果被重点展示。
102 1
|
16天前
|
存储 人工智能 自然语言处理
又双叒叕获认可!阿里云AI Stack一体机首批通过国家评测认证
近日,阿里云AI Stack一体机通过了中国电子技术标准研究院的“云上部署DeepSeek验证测试”,成为首批通过该评测的AI大模型一体机。
111 10
|
14天前
|
人工智能 开发工具
阿里云AI Stack全量适配Qwen3模型,企业级部署效率全面升级
2025年4月29日的凌晨5点,阿里全新一代模型通义千问Qwen3正式发布并全部开源8款「混合推理模型」,包含: 6款Dense模型:0.6B、1.7B、4B、8B、14B、32B。 2款MoE模型:Qwen3-30B-A3B和旗舰版Qwen3-235B-A22B。 阿里云AI Stack已适配全量Qwen3模型,可快速部署实现Qwen3模型的开箱即用!
|
17天前
|
存储 人工智能 安全
阿里云双项入选首批智算一体化权威评估 以AI Stack加速政企智能化升级 ——万卡智算集群服务推进方阵(ICCPA)第三期沙龙在京举办
2024年4月9日,中国信通院主办的智算集群服务沙龙第三期在京召开。阿里云凭借领先的AI技术能力,成为首批通过《面向大模型的智算一体化解决方案》评估的云厂商,并入选行业应用案例。会上,阿里云AI Stack赋能政企大模型高效落地,提供软硬一体推理优化框架,支持主流开源模型快速适配,助力企业构建高性能私有化AI服务,已在政务、金融等领域广泛应用。

热门文章

最新文章

相关产品

  • 检索分析服务 Elasticsearch版