作者:来自 Elastic Tim Grein

OpenAI Chat Completions 已集成到 Elastic 的 inference API 中。此功能标志着我们在 Elasticsearch 中集成先进 AI 能力的旅程又迈出了重要一步,为用户提供了更多易于使用的功能,例如生成类人的文本补全。
本文介绍如何结合使用 OpenAI chat completions 和 Elasticsearch,对任意文本进行摘要、翻译或执行问答。在开始之前,让我们快速了解一下 Elastic 最近推出的功能和集成。
Elastic 持续创新的本质
Elastic 在所有 AI 领域投入大量资源。我们最近发布了许多新功能和令人兴奋的集成:
- Elasticsearch open inference API 增加了对 Cohere Embeddings 的支持
- 将 Elasticsearch 向量数据库引入 Azure OpenAI Service On Your Data(预览版)
- 加速多图向量搜索
- ......探索更多 Elastic Search Labs,了解最新进展
我们 inference API 中新的 completion 任务类型,目前以 OpenAI 作为第一个后端提供商,已经可以在 Elastic Cloud 的无状态产品中使用。它将在下一版本中很快向所有用户开放。
使用 Elasticsearch 的 open inference API 调用 OpenAI chat completions
在这个简短指南中,我们将展示一个简单示例,说明如何在文档摄取过程中使用 inference API 中新的 completion 任务类型。有关更深入的指南和交互式 notebook,请参考 Elastic Search Labs GitHub 仓库。
要使以下指南正常运行,你需要拥有一个有效的 OpenAI 账号并获取 API key。请参考 OpenAI 的快速入门指南,了解需要执行的步骤。你可以从 OpenAI 的多种模型中进行选择。在下面的示例中,我们使用了 gpt-3.5-turbo。
在 Kibana 中,你可以访问一个控制台,在 Elasticsearch 中输入以下步骤,而无需设置 IDE。
首先,你需要配置一个模型,该模型将执行 completion:
bash
`
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. }
`AI写代码
运行此命令后,你应该看到对应的 200 OK 状态,表示该模型已经正确设置,可以对任意文本执行 inference。
现在,你可以调用已配置的模型,对任意文本输入执行 inference:
bash
`
1. POST _inference/completion/openai_chat_completions
2. {
3. "input": "What is Elastic?"
4. }
`AI写代码
你将获得一个状态码为 200 OK 的响应,类似如下:
markdown
`
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..."
5. }
6. ]
7. }
`AI写代码
下一条命令会创建一个示例文档,我们将使用刚刚配置的模型对其进行摘要:
css
`
1. POST _bulk
2. { "index" : { "_index" : "docs" } }
3. {"content": "..."}
`AI写代码
为了对多个文档进行摘要,我们将结合 ingest pipeline 以及 script、inference 和 remove processor 来设置我们的摘要流水线。
markdown
`
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. }
`AI写代码
这个流水线会简单地将指令 "Please summarize the following text: " 添加到临时字段中的内容前面,使配置好的模型知道应该如何处理文本。当然,你可以将这段文本修改为任何你想要的内容,从而解锁其他许多常见用例:
- 问答
- 翻译
- ......以及更多!
流水线会在执行 inference 后删除临时字段。
现在,我们通过调用 reindex API,让文档通过摘要流水线:
bash
`
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. }
`AI写代码
你的文档现在已经完成摘要,并可以进行搜索:
markdown
`
1. POST docs_summaries/_search
2. {
3. "query": {
4. "match_all": { }
5. }
6. }
`AI写代码
基本上就是这样,你只通过几个简单的 API 调用就创建了一个强大的摘要流水线,并且可以将其用于任何摄取机制!有很多场景都适合使用摘要,例如在生成语义 embedding 之前对大型文本进行摘要,或者将大型文档转换为简洁摘要。这可以降低存储成本,提高价值实现速度,例如当你只关注大型文档的摘要时。顺便提一下,如果你想从二进制文档中提取文本,可以查看我们的开源数据提取服务!
原文:OpenAI chat completions with Elasticsearch's open inference API | Elasticsearch Labs