自然语言处理 (NLP) 是人工智能 (AI) 的一个分支,旨在通过将计算语言学与统计、机器学习和深度学习模型相结合,尽可能接近人类解释地理解人类语言。

NLP 的最大挑战之一是在考虑到各种语言表示的情况下预训练文本数据的过程。

2018 年,谷歌采购了一种称为 BERT(Bidirectional Encoder Representations from Transformers)的预训练 NLP 新技术,不再需要以任何固定顺序处理数据,从而允许在更大量的数据上进行训练,并提高理解上下文的能力和语言的歧义。

与任何其他预训练过程一样,数据越多越好。 因此,使用了未标记的文本数据集,例如整个英文维基百科。 然后预训练作为构建的 “知识” 层。

为了支持使用与 BERT 相同的分词器的模型,Elastic 支持 PyTorch 库,这是最流行的机器学习库之一,它支持像 BERT 使用的 Transformer 架构这样的神经网络,支持 NLP 任务并将这些任务合并为数据的一部分流水线到 Elasticsearch。

一般来说,任何具有受支持架构的训练模型都可以部署在 Elasticsearch 中,包括 BERT 和变体,例如:

  • RoBERTa
  • DistilBERT
  • RetriBERT
  • MobileBERT
  • ELECTRA

这些模型按 NLP 任务列出。

目前,这些是支持的任务:

  • Extract information
  1. Named entity recognition
  2. Fill-mask
  3. Question answering
  • Classify text
  1. Language identification
  2. Text classification
  3. Zero-shot text classification
  • Search and compare text
  1. Text embedding
  2. Text similarity

与 classfification 和 regression 的情况一样,导入经过训练的模型后,你可以使用它进行预测(推理 inference)。

对于我们今天的这个演示,我们将使用这些任务之一 -> information extraction:Question answering

此任务允许我们在给定上下文(文本)和问题的情况下获得答案,从提供的文本中提取信息以回答提供的问题。

在这种情况下,它不像具有对话流的聊天机器人,但有助于自动响应常见问题,或者甚至在处理之前未映射的开放式问题时使用对话流。

我们现在要做的是使用我们的 eland 库将 QA 模型导入你的 Elastic Stack,这是一个用于在 Elasticsearch 中探索和分析数据的 Python Elasticsearch 客户端,我们有一些简单的方法和脚本可以让你从中提取模型Hugging Face 模型中心,一个用于构建、训练和部署开源机器学习模型的 AI 社区。

在这种情况下,我们将导入 Hugging Face 上可用的模型 deepset/minilm-uncased-squad2。

将 PyTorch 模型上传到你的集群后,你将能够将该模型分配给特定的机器学习节点,将其加载到内存中并启动本机 libtorch 进程。

一旦模型分配完成,我们就可以进行推理,使用推理处理器来评估模型。 对于这个演示,我们将使用圣诞歌词作为我们的背景,这将允许我们对我们最喜欢的圣诞歌曲提问。

让我们开始吧!在今天的展示中,我将使用最新的 Elastic Stack 8.5.3 来进行展示。

安装

Elasticsearch 及 Kibana

如果你还没安装好自己的 Elasticsearch 及 Kibana,请参阅如下的文章来进行安装:

  • 如何在 Linux,MacOS 及 Windows 上进行安装 Elasticsearch
  • Kibana:如何在 Linux,MacOS 及 Windows上安装 Elastic 栈中的 Kibana

请注意文章中的 8.x 的安装部分。由于使用 eland 上传模型是白金版或者是企业版的功能,在我们的演示中,我们需要启动白金版试用功能:

Eland

Eland 可以通过 pip 从 PyPI 安装。在安装之前,我们需要安装好自己的 Python。

$ python --version
Python 3.10.2

可以使用 Pip 从 PyPI 安装 Eland:

python -m pip install eland

也可以使用 Conda 从 Conda Forge 安装 Eland:

conda install -c conda-forge eland

希望在不安装 Eland 的情况下使用它的用户,为了只运行可用的脚本,可以构建 Docker 容器:

git clone https://github.com/elastic/eland
cd eland
docker build -t elastic/eland .

Eland 将 Hugging Face 转换器模型到其 TorchScript 表示的转换和分块过程封装在一个 Python 方法中; 因此,这是推荐的导入方法。

  1. 安装 Eland Python 客户端。
  2. 运行 eland_import_hub_model 脚本。 例如:
eland_import_hub_model --url <clusterUrl> \
--hub-model-id elastic/distilbert-base-cased-finetuned-conll03-english \
--task-type ner 
  • 指定 URL 以访问你的集群。 例如,https://<user>:<password>@<hostname>:<port>。
  • 在 Hugging Face 模型中心中指定模型的标识符。
  • 指定 NLP 任务的类型。 支持的值为 fill_mask、ner、text_classification、text_embedding, question_answering 和 zero_shot_classification。

上传 QA model

在此演示中,我们将使用随机 QA 模型,但你可以随意导入要使用的模型。 你可以在 Hugging Face 网页上阅读有关此模型的更多详细信息。

从上面,我们可以看出来,给定一定的上下文,我们提问,就可以得到我们需要的答案。

打开我们的终端并使用我们的端点和 model 名称更新以下命令:

eland_import_hub_model --url https://<user>:<password>@<hostname>:<port> \
--hub-model-id <model_name> \
--task-type <task_type>

针对我的情况,我使用如下的命令:

eland_import_hub_model --url https://elastic:3Q5_PqiMCP-=8xcRIvCS@localhost:9200 \--hub-model-id deepset/minilm-uncased-squad2 \--task-type question_answering \--ca-cert /Users/liuxg/elastic/elasticsearch-8.5.3/config/certs/http_ca.crt \--start

在上面,我使用 --ca-cert 选项来设置我的自签名证书的证书。当然你也可以选择如下的方式来忽略自签名证书:

eland_import_hub_model --url https://elastic:3Q5_PqiMCP-=8xcRIvCS@localhost:9200 \--hub-model-id deepset/minilm-uncased-squad2 \--task-type question_answering \--insecure--start

我们可以到机器学习的页面来查看:

如果你在之前的命令中没有添加 --start 选项,那么你需要点击上面的 play 按钮来进行启动:

部署后,State 列的值为 started,Actions 下的 Start deployment 选项将被禁用,这意味着部署已经完。

让我们测试一下我们的模型吧!

复制上面的模型 ID deepset__minilm-uncased-squad2。

在 Kibana 菜单中,单击开 Dev Tools。

在此 UI 中,你将有一个控制台来与你的数据进行交互。

让我们使用推理处理器(inference processor)来推理经过训练的模型。

Christmas song

我们选择为这首歌提问:Mariah Carey - All I Want For Christmas Is You

好吧,2022 年又是第一名。我们使用如下的命令格式来进行推理:

POST _ml/trained_models/<model_id>/deployment/_infer
{"docs": [{"text_field": "<input>"}],"inference_config": {"question_answering": {"question": "<question_to_be_answered>"}}
}

此 POST 方法包含一个文档数组,其中包含一个与你配置的训练模型输入匹配的字段,通常字段名称为 text_field。 text_field 值是你要推断的输入。 对于提到的 QA 模型,除了我们将提供的文本作为输入外,问题是必需的,inference_config 包含推理的配置,对于 QA 模型,针对你的 text_field 提出问题。

在我添加歌曲歌词的示例中,这将是:

问题:我在意礼物吗?

POST _ml/trained_models/deepset__minilm-uncased-squad2/deployment/_infer
{"docs": [{"text_field": "I don't want a lot for Christmas There's just one thing I need I don't care about presents Underneath the Christmas tree I just want you for my own More than you could ever know Make my wish come true All I want for Christmas is you. I don't want a lot for Christmas There is just one thing I need I don't care about presents Underneath the Christmas tree I don't need to hang my stocking There upon the fireplace Santa Claus won't make me happy With a toy on Christmas day I just want you for for my own More than you could ever know Make my wish come true All I want for Christmas is you You baby I won't ask for much this Christmas I won't even wish for snow I'm just gonna keep on waiting Underneath the mistletoe I won't make a list and send it To the North Pole for Saint Nick I won't even stay awake to Hear those magic reindeer click 'Cause I just want you here tonight Holding on to me so tight What more can I do Baby all I want for Christmas is you All the lights are shining So brightly everywhere And the sound of children's Laughter fills the air And everyone is singing I hear those sleigh bells ringing Santa won't you bring me the one I really need Won't you please bring my baby to me Oh, I don't want a lot for Christmas This is all I'm asking for I just want to see my baby Standing right outside my door Oh I just want him for my own More than you could ever know Make my wish come true Baby all I want for Christmas is you All I want for Christmas is you baby All I want for Christmas is you baby."}],"inference_config": {"question_answering": {"question": "Do I care about presents?"}}
}

单击播放图标,你可以发送请求。

答案由下面的对象显示:

predicted_value 包含你的答案。显然不在乎礼物,而是在乎的是人 :)

I don't care about presents Underneath the Christmas tree.

此外,你还有 start_offset 和 end_offset 记录您的 predicted_value 的开始和结束字符偏移量以及此预测的概率,即 prediction_probability 字段。

让我们提出另外一个问题:

问题:圣诞节我想要什么?

我们的命令格式是:

POST _ml/trained_models/deepset__minilm-uncased-squad2/deployment/_infer
{"docs": [{"text_field": "I don't want a lot for Christmas There's just one thing I need I don't care about presents Underneath the Christmas tree I just want you for my own More than you could ever know Make my wish come true All I want for Christmas is you. I don't want a lot for Christmas There is just one thing I need I don't care about presents Underneath the Christmas tree I don't need to hang my stocking There upon the fireplace Santa Claus won't make me happy With a toy on Christmas day I just want you for for my own More than you could ever know Make my wish come true All I want for Christmas is you You baby I won't ask for much this Christmas I won't even wish for snow I'm just gonna keep on waiting Underneath the mistletoe I won't make a list and send it To the North Pole for Saint Nick I won't even stay awake to Hear those magic reindeer click 'Cause I just want you here tonight Holding on to me so tight What more can I do Baby all I want for Christmas is you All the lights are shining So brightly everywhere And the sound of children's Laughter fills the air And everyone is singing I hear those sleigh bells ringing Santa won't you bring me the one I really need Won't you please bring my baby to me Oh, I don't want a lot for Christmas This is all I'm asking for I just want to see my baby Standing right outside my door Oh I just want him for my own More than you could ever know Make my wish come true Baby all I want for Christmas is you All I want for Christmas is you baby All I want for Christmas is you baby."}],"inference_config": {"question_answering": {"question": "What do I want for Christmas?"}}
}

我们可以提更多的问题:

  • Will Santa Claus make me happy?
  • Are the lights shining?

当然你可以试其它的歌曲或者文字。

我希望你喜欢将 NLP 与 Elastic Stack 结合使用! 随时欢迎反馈。

Elasticsearch:使用 NLP 问答模型与你喜欢的圣诞歌曲交谈相关推荐

  1. NLP:训练一个中文问答模型Ⅰ

    训练一个中文问答模型I-Step by Step   本文基于经典的NMT架构(Seq2Seq+Attention),训练了一个中文问答模型,把问题到答案之间的映射看作是问题到答案的翻译.基于Tens ...

  2. NLP:训练一个中文问答模型Ⅱ

    训练一个中文问答模型Ⅱ-Step by Step   接上一篇 中文问答模型Ⅰ基于这次仍是基于NMT架构训练,但是把Seq2Seq替换为Transformer架构,还有一点不同是,本次没有采用分词训练 ...

  3. MedicalGPT:基于LLaMA-13B的中英医疗问答模型(LoRA)、实现包括二次预训练、有监督微调、奖励建模、强化学习训练[LLM:含Ziya-LLaMA]。

    项目设计集合(人工智能方向):助力新人快速实战掌握技能.自主完成项目设计升级,提升自身的硬实力(不仅限NLP.知识图谱.计算机视觉等领域):汇总有意义的项目设计集合,助力新人快速实战掌握技能,助力用户 ...

  4. NLP通用模型诞生?一个模型搞定十大自然语言常见任务

    翻译 | 于之涵 编辑 | Leo 出品 | AI科技大本营 (公众号ID:rgznai100) AI科技大本营按:目前的NLP领域有一个问题:即使是再厉害的算法也只能针对特定的任务,比如适用于机器翻 ...

  5. NLP通用模型decaNLP诞生,一个模型搞定十大自然语言常见任务

    然而近日,Salesforce发布了一项新的研究成果:decaNLP--一个可以同时处理机器翻译.问答.摘要.文本分类.情感分析等十项自然语言任务的通用模型. Salesforce的首席科学家Rich ...

  6. 北京/上海内推 | 字节跳动AI Lab招聘NLP算法模型优化方向实习生

    合适的工作难找?最新的招聘信息也不知道? AI 求职为大家精选人工智能领域最新鲜的招聘信息,助你先人一步投递,快人一步入职! 字节跳动 日常维护 star 数 1.8k 的开源项目 https://g ...

  7. 史上最详尽的NLP预处理模型汇总

    文章发布于公号[数智物语] (ID:decision_engine),关注公号不错过每一篇干货. 转自 | 磐创AI(公众号ID:xunixs) 作者 | AI小昕 编者按:近年来,自然语言处理(NL ...

  8. 搜索NLP行业模型和轻量化客户定制

    简介:开放搜索NLP行业模型和轻量化客户定制方案,解决减少客户标注成本.完全无标注或少量简单标注的等问题,让搜索领域扩展更易用. 特邀嘉宾: 徐光伟(昆卡)--阿里巴巴算法专家 搜索NLP算法 搜索链 ...

  9. NLP 常用模型和数据集高速下载

    楔子 由于大部分 NLP 的模型和数据集都在国外,导致国内下载速度实在感人

最新文章

  1. MegEngine 框架设计
  2. C++ Primer 5th笔记(6)chapter6 函数: 参数
  3. fusion360安装包_【请注意】2020年以前从Autodesk中国区网站下载的Fusion 360已不能自动升级...
  4. Cus系统beta1.2发布
  5. 在Python中使用OpenCV(CV2)对图像进行边缘检测
  6. EJB3.0学习笔记---Stateless Session Bean的原理:
  7. GPU GPGPU
  8. oracle安装选取字符集,oracle10g字符集问题及设置PL/SQL、sqlplus字符集
  9. Keil用C语言定义函数,STC单片机Keil中C语言函数定位的方法
  10. 【转】 Pro Android学习笔记(三八):Fragment(3):基础小例子-续
  11. CC2430基础——定时器1试验
  12. CONVID-19 CT图像数据集
  13. 知乎:电脑长时间不关机会缩短电脑寿命吗?
  14. Coreldraw软件提示盗版警告,网络关闭还是提示软件已被禁用方法教程
  15. Kubernetes 管理员认证(CKA)考试笔记(四)
  16. 11-20什么是内网,外网,局域网,如何判断
  17. 时评素材2019简短华为鸿蒙,2019社会热点话题时评素材:从网络热词看生活热度...
  18. VSC/SMC(十四)——全局快速Terminal滑模控制(含程序模型)
  19. android 商城开发,Android 应用商城开发的几个关键技术点
  20. 基于ASP.NET MVC 利用(Aspose+Pdfobject.js) 实现在线预览Word、Excel、PPT、PDF文件

热门文章

  1. 抖音创作规范_抖音内容规范,运营抖音的十五条经验
  2. ABAP 选择屏幕标题和ALV标题的修改
  3. 基于Proteus的一位数码管显示实验
  4. CMAKE编译报错 linker input file unused because linking not done
  5. C语言魔塔游戏分析图
  6. 八问WebSocket协议:为你快速解答WebSocket热门疑问
  7. EasyDSS启动后443端口未被占用,访问不了https网页是什么原因?
  8. 天翼云服务器 80 端口无法访问
  9. 像素测量pxcook
  10. 重建服务器系统,服务器更换操作系统