掘金 后端 ( ) • 2024-04-21 16:29

作者:Tim Grein

我们很高兴地宣布在 Elasticsearch 中推出的最新创新:在 Elastic 的 inference API 中集成了 OpenAI Chat Completions 功能。这一新特性标志着我们在整合尖端人工智能能力至 Elasticsearch 的旅程中又迈出了一步,提供了生成类人文本完成等更多易于使用的功能。

更多关于 OpenAI Chat Completions 的用法,请阅读文章 “ChatGPT 和 Elasticsearch:OpenAI 遇见私有数据(二)

Elastic 持续创新的本质

Elastic 在所有的人工智能领域都进行了大量投资。我们最近发布了许多新功能和令人振奋的集成:

我们的 inference API 中的新 completion 任务类型,作为第一个支持提供商,已经在我们的 Elastic Cloud 的 stateless 提供中可用。它将很快在我们的下一个版本中向所有人提供。

使用新的 completion API

在这个简短的指南中,我们将展示如何在文档摄入过程中使用 inference API 中的新 completion task 类型的简单示例。请参考 Elastic Search Labs 的 GitHub 仓库以获取更深入的指南和交互式笔记本。

要使以下指南工作,你需要拥有一个活跃的 OpenAI 账户并获取一个 API 密钥。请参考 OpenAI 的快速启动指南了解你需要遵循的步骤。你可以选择 OpenAI 的多种模型中的一种。在以下示例中,我们使用了 gpt-3.5-turbo

在 Kibana 中,你将可以使用控制台输入以下步骤到 Elasticsearch,无需设置 IDE。

首先,你需要配置一个将执行 completion 任务的模型:



1.  PUT _inference/completion/openai_chat_completions
2.  {
3.      "service": "openai",
4.          "service_settings": {
5.          "api_key": <api-key>,
6.          "model_id": "gpt-3.5-turbo"
7.      }
8.  }


运行此命令后,你应该会看到相应的 200 OK 状态,表明模型已正确设置,可以对任意文本进行推理。

现在,你可以调用配置的模型对任意文本输入进行推理:



1.  POST _inference/completion/openai_chat_completions
2.  {
3.      "input": "What is Elastic?"
4.  }


你将收到一个类似于下面的带有状态码 200 OK 的响应:



1.  {
2.      "completion": [
3.          {
4.              "result": "Elastic is a software company that provides a range of products and solutions for search, logging, security, and analytics. Its flagship product, Elasticsearch, is a distributed, RESTful search and analytics engine that is used for full-text search, structured search, and analytics. Elastic also offers other products such as Logstash for log collection and parsing, Kibana for data visualization and dashboarding, and Beats for lightweight data shippers. These products can be combined to create powerful data analysis and monitoring solutions for organizations of all sizes."
5.          }
6.      ]
7.  }


下一个命令创建了一个示例文档,我们将使用刚刚配置的模型对其进行总结:



1.  POST _bulk
2.  { "index" : { "_index" : "docs" } }
3.  {"content": "You know, for search (and analysis) Elasticsearch is the distributed search and analytics engine at the heart of the Elastic Stack. Logstash and Beats facilitate collecting, aggregating, and enriching your data and storing it in Elasticsearch. Kibana enables you to interactively explore, visualize, and share insights into your data and manage and monitor the stack. Elasticsearch is where the indexing, search, and analysis magic happens. Elasticsearch provides near real-time search and analytics for all types of data. Whether you have structured or unstructured text, numerical data, or geospatial data, Elasticsearch can efficiently store and index it in a way that supports fast searches. You can go far beyond simple data retrieval and aggregate information to discover trends and patterns in your data. And as your data and query volume grows, the distributed nature of Elasticsearch enables your deployment to grow seamlessly right along with it. While not every problem is a search problem, Elasticsearch offers speed and flexibility to handle data in a wide variety of use cases: Add a search box to an app or website Store and analyze logs, metrics, and security event data Use machine learning to automatically model the behavior of your data in real time Use Elasticsearch as a vector database to create, store, and search vector embeddings Automate business workflows using Elasticsearch as a storage engine Manage, integrate, and analyze spatial information using Elasticsearch as a geographic information system (GIS) Store and process genetic data using Elasticsearch as a bioinformatics research tool We’re continually amazed by the novel ways people use search. But whether your use case is similar to one of these, or you’re using Elasticsearch to tackle a new problem, the way you work with your data, documents, and indices in Elasticsearch is the same."}


为了总结多个文档,我们将使用一个 ingest pipeline,其中包含脚本处理器、推理处理器和删除处理器,来设置我们的摘要管道。



1.  PUT _ingest/pipeline/summarization_pipeline
2.  {
3.      "processors": [
4.          {
5.              "script": {
6.                  "source": "ctx.prompt = 'Please summarize the following text: ' + ctx.content"
7.              }
8.          },
9.          {
10.              "inference": {
11.                  "model_id": "openai_chat_completions",
12.                  "input_output": {
13.                      "input_field": "prompt",
14.                      "output_field": "summary"
15.                  }
16.              }
17.          },
18.          {
19.              "remove": {
20.                  "field": "prompt"
21.              }
22.          }
23.    ]
24.  }


该管道简单地在内容中加上了指令 “Please summarize the following text: ”,放在一个临时字段中,这样配置的模型就知道该如何处理文本了。当然,你可以根据需要更改这个文本,这就可以解锁各种其他流行的用例:

  • 问答
  • 翻译
  • ...等等!

管道在执行推理后删除临时字段。

现在,我们通过调用 reindex API 将我们的文档(们)通过摘要管道发送出去。



1.  POST _reindex
2.  {
3.      "source": {
4.          "index": "docs",
5.          "size": 50
6.      },
7.      "dest": {
8.          "index": "docs_summaries",
9.          "pipeline": "summarization_pipeline"
10.      }
11.  }


你的文档现已被总结,可以进行搜索了。



1.  POST docs_summaries/_search
2.  {
3.      "query": {
4.          "match_all": { }
5.      }
6.  }


这就是全部内容了, 你只需通过几个简单的 API 调用就创建了一个强大的摘要化流水线,可与任何摄取机制一起使用!摘要化非常实用,例如在生成语义嵌入或将大段文本转换为简洁摘要之前,对大段文本进行摘要化。这可以降低存储成本,提高价值交付速度,例如,如果你只对大型文档的摘要感兴趣等。顺便说一句,如果你想从二进制文档中提取文本,可以查看我们的开源数据提取服务

前景令人兴奋

但我们不会止步于此。我们正在将 Cohere 的聊天功能作为我们的 completion 任务的另一个提供商进行整合。我们还在积极探索与 completion API 结合的新的检索和摄取用例。现在就将 Elastic Search Labs 加入书签,随时获取最新信息!

准备在你的应用中构建 RAG 吗?想尝试使用向量数据库的不同 LLMs 吗? 请查看我们在 Github 上的 LangChain、Cohere 等样本 notebooks,并加入即将开始的 Elasticsearch 工程师培训

原文:Elasticsearch open inference API adds support for OpenAI chat completions — Elastic Search Labs