paddlenlp Windows本地搭建语义检索系统

一. 运行环境

  1. 软件环境:
  • python >= 3.8.16

  • paddlenlp = 2.5.2

  • paddlepaddle-gpu =2.4.2.post112

  • paddleocr = 2.6.1.3

  • numpy = 1.24.3

  • opencv-contrib-python =4.6.0.66

  • CUDA Version: 11.2

  • cuDNN Version 8.2

  • win11

  1. 硬件环境:
  • NVIDIA RTX 3050 4GB

  • 11th Gen Intel® Core™ i7-11800H @ 2.30GHz 2.30 GHz

  1. 依赖安装:

    首先需要安装PaddlePaddle,PaddlePaddle的安装请参考文档官方安装文档

    安装以下依赖:

    git clone https://github.com/tvst/htbuilder.git
    cd htbuilder/
    python setup install
    

    下载pipelines源代码:

    # pip 一键安装
    pip install --upgrade paddle-pipelines -i https://pypi.tuna.tsinghua.edu.cn/simple
    # 或者源码进行安装最新版本
    cd ${HOME}/PaddleNLP/pipelines/
    pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
    python setup.py install# 下载pipelines源代码
    git clone https://github.com/PaddlePaddle/PaddleNLP.git
    cd PaddleNLP/pipelines
    

    【注意】以下的所有的流程都只需要在pipelines根目录下进行,不需要跳转目录

二. 数据说明

paddlenlp 预置了基于DuReader-Robust数据集搭建语义检索系统的代码示例,以通过如下命令快速体验语义检索系统的效果(建议使用GPU)

python examples/semantic-search/semantic_search_example.py --device gpu

如果只有 CPU 机器,安装CPU版本的Paddle后,可以通过 --device 参数指定 cpu 即可, 运行耗时较长

python examples/semantic-search/semantic_search_example.py --device cpu

三. 构建Web可视化语义检索系统

  1. 启动ANN服务

    1. 参考官方文档下载安装 elasticsearch-8.3.2 并解压。

    2. 启动 ES 服务 把xpack.security.enabled 设置成false,如下:

      pack.security.enabled: false
      

      找不到的话可以到elasticsearch/config目录下 elasticsearch.yml中手动添加

      然后直接双击bin目录下的elasticsearch.bat即可启动。

    3. 检查确保 ES 服务启动成功

      curl http://localhost:9200/_aliases?pretty=true
      

      备注:ES 服务默认开启端口为 9200

  2. 文档数据写入ANN索引库

    # 以DuReader-Robust 数据集为例建立 ANN 索引库
    python utils/offline_ann.py --index_name dureader_robust_query_encoder --doc_dir data/dureader_dev
    

    参数含义说明

    • index_name: 索引的名称
    • doc_dir: txt文本数据的路径
    • host: Elasticsearch的IP地址
    • port: Elasticsearch的端口号
    • delete_index: 是否删除现有的索引和数据,用于清空es的数据,默认为false

    可以使用下面的命令来查看数据:

    # 打印几条数据
    curl http://localhost:9200/dureader_robust_query_encoder/_search
    
  3. 启动Rest API模型服务

    # 指定语义检索系统的Yaml配置文件,Linux/macos
    export PIPELINE_YAML_PATH=rest_api/pipeline/semantic_search.yaml
    # 指定语义检索系统的Yaml配置文件,Windows powershell
    $env:PIPELINE_YAML_PATH='rest_api/pipeline/semantic_search.yaml'
    # 使用端口号 8891 启动模型服务
    python rest_api/application.py 8891
    

    启动后可以使用curl命令验证是否成功运行:

    curl -X POST -k http://localhost:8891/query -H 'Content-Type: application/json' -d '{"query": "衡量酒水的价格的因素有哪些?","params": {"Retriever": {"top_k": 5}, "Ranker":{"top_k": 5}}}'
    
  4. 启动WebUI

    # 配置模型服务地址, Linux/macos
    export API_ENDPOINT=http://127.0.0.1:8891
    # 配置模型服务地址, windows
    $env:API_ENDPOINT='http://127.0.0.1:8891'
    # 在指定端口 8502 启动 WebUI
    python -m streamlit run ui/webapp_semantic_search.py --server.port 8502
    

    然后就可以在浏览器上访问了http://127.0.0.1:8502 !

搭建过程中踩得一些坑

语义检索系统可以跑通,但终端输出字符是乱码怎么解决?

  • 通过如下命令设置操作系统默认编码为 zh_CN.UTF-8

    export LANG=zh_CN.UTF-8
    

faiss 安装上了但还是显示找不到faiss怎么办?

推荐您使用anaconda进行单独安装,安装教程请参考faiss

# CPU-only version
conda install -c pytorch faiss-cpu# GPU(+CPU) version
conda install -c pytorch faiss-gpu

如何更换pipelines中预置的模型?

更换系统预置的模型以后,由于模型不一样了,需要重新构建索引,并修改相关的配置文件。以语义索引为例,需要修改2个地方,第一个地方是utils/offline_ann.py,另一个是rest_api/pipeline/semantic_search.yaml,并重新运行:

首先修改utils/offline_ann.py

python utils/offline_ann.py --index_name dureader_robust_base_encoder \--doc_dir data/dureader_dev \--query_embedding_model rocketqa-zh-base-query-encoder \--passage_embedding_model rocketqa-zh-base-para-encoder \--embedding_dim 768 \--delete_index

然后修改rest_api/pipeline/semantic_search.yaml文件:

components:    # define all the building-blocks for Pipeline- name: DocumentStoretype: ElasticsearchDocumentStore  # consider using MilvusDocumentStore or WeaviateDocumentStore for scaling to large number of documentsparams:host: localhostport: 9200index: dureader_robust_base_encoder # 修改索引名embedding_dim: 768   # 修改向量的维度- name: Retrievertype: DensePassageRetrieverparams:document_store: DocumentStore    # params can reference other components defined in the YAMLtop_k: 10query_embedding_model: rocketqa-zh-base-query-encoder  # 修改Retriever的query模型名passage_embedding_model: rocketqa-zh-base-para-encoder # 修改 Retriever的para模型embed_title: False- name: Ranker       # custom-name for the component; helpful for visualization & debuggingtype: ErnieRanker    # pipelines Class name for the componentparams:model_name_or_path: rocketqa-base-cross-encoder  # 修改 ErnieRanker的模型名top_k: 3

然后重新运行:

# 指定语义检索系统的Yaml配置文件
export PIPELINE_YAML_PATH=rest_api/pipeline/semantic_search.yaml
# 使用端口号 8891 启动模型服务
python rest_api/application.py 8891

Elastic search 日志显示错误 exception during geoip databases update

需要编辑config/elasticsearch.yml,在末尾添加:

ingest.geoip.downloader.enabled: false

Windows出现运行前端报错requests.exceptions.MissingSchema: Invalid URL 'None/query': No scheme supplied. Perhaps you meant http://None/query?

环境变量没有生效,请检查一下环境变量,确保PIPELINE_YAML_PATH和API_ENDPOINT生效:

方法 :打开poweshell终端,输入:

$env:PIPELINE_YAML_PATH='rest_api/pipeline/semantic_search.yaml'
$env:API_ENDPOINT='http://127.0.0.1:8891'

Windows运行应用的时候出现了下面的错误:RuntimeError: (NotFound) Cannot open file C:\Users\my_name/.paddleocr/whl\det\ch\ch_PP-OCRv3_det_infer/inference.pdmodel, please confirm whether the file is normal.

这是Windows系统用户命名为中文的原因,详细解决方法参考issue. https://github.com/PaddlePaddle/PaddleNLP/issues/3242

运行后台程序出现了错误:Exception: Failed loading pipeline component 'DocumentStore': RequestError(400, 'illegal_argument_exception', 'Mapper for [embedding] conflicts with existing mapper:\n\tCannot update parameter [dims] from [312] to [768]')

以语义检索为例,这是因为模型的维度不对造成的,请检查一下 elastic search中的文本的向量的维度和semantic_search.yaml里面DocumentStore设置的维度embedding_dim是否一致,如果不一致,请重新使用utils/offline_ann.py构建索引。总之,请确保构建索引所用到的模型和semantic_search.yaml设置的模型是一致的。

注意: 修改后重新构建索引时,一定要先将以前的索引全部删除否则无效。可以执行以下命令:

curl -XDELETE http://localhost:9200/dureader_robust_query_encoder

paddlenlp Windows本地搭建语义检索系统相关推荐

  1. AI快车道PaddleNLP系列直播课6|语义检索系统快速搭建落地

    目录 1 搜索核心技术发展 1.1 基于字面匹配的检索流程 传统基于字面匹配的检索的痛点: 2 PaddleNLP语义检索系统 2.1 语义检索系统架构:recall+ranking 2.2 Padd ...

  2. 搜索推荐系统[10]项目实战系列Z1:手把手教学(商品搜索系统、学术文献检索)语义检索系统搭建、召回排序模型详解。

    搜索推荐系统专栏简介:搜索推荐全流程讲解(召回粗排精排重排混排).系统架构.常见问题.算法项目实战总结.技术细节以及项目实战(含码源) 专栏详细介绍:搜索推荐系统专栏简介:搜索推荐全流程讲解(召回粗排 ...

  3. 磁力开源项目和自己服务器,Github新项目:自己本地搭建磁力搜索系统

    前两年非常流行的磁力链接搜索系统,搜车牌,电影等非常方便, 因为最近版权和监管的原因,一旦做大肯定被封. Github一位程序员分享了一套开源磁力搜索程序,界面清爽简单易用, 最关键的是每个人都可以本 ...

  4. 手把手写深度学习(16):用CILP预训练模型搭建图文检索系统/以图搜图/关键词检索系统

    前言:CLIP is all yuo need!CLIP在text-to-image.图像检索.视频理解.图像编辑.自监督学习等领域都展示了极强的统治力,这篇博客手把手教大家搭建自己的图文检索系统,能 ...

  5. tp5.1 EasyTask Windows本地测试和centos系统服务器安装

    Windows本地安装测试 一.首先确定PHP版本 二.安装扩展 wpc扩展安装方法,下载wpc扩展一键安装包,根据PHP是32/64位执行一键安装包即可,切记此处说明的是PHP的位数,不是系统的位数 ...

  6. php 消息队列_消息队列篇——windows本地搭建RabbitMQ Server

    前言: 最近的PHP项目中有使用AMQP,解耦一些业务性的功能模块.因为工作使用的是线上Linux搭建,为了方便测试所以我决定本地搭建一个MQ服务. RabbitMQ简介: MQ全称为Message ...

  7. windows 本地搭建git仓库_Windows平台下Github远程仓库的搭建-Go语言中文社区

    前言 Github是一个面向开源及私有软件项目的托管平台,拥有超过900万开发者用户,有众多的开源项目供研究者学习.还提供了很多项目管理功能,方便多终端同步管理项目.本文将介绍Windows平台下Gi ...

  8. 如何在windows本地搭建StackEdit

    前提必须能联网 1.安装node.js   最好选择v6.11.2 2.安装git 3.下载StackEdit源码 ,解压,并在本目录打开cmd命令窗口 4.安装必要的依赖包  执行 npm inst ...

  9. windows 本地搭建git仓库_Windows系统下搭建Git本地代码库

    近由于工作需要,要把工作代码做一下版本管理.工作代码也不方便放到github上,也不想付费建私密库,公司也没几个人,所以就想着搭建一个本地Git版本库,来做版本管理.搭建过程如下. 系统环境:Dell ...

最新文章

  1. 读微型计算机原理与接口技术 段的理解
  2. js 导出到excel
  3. Java 类型转换
  4. 以下关于CISC和RISC的叙述中,错误的是()【最全!最详细总结!】
  5. 剑指offer——面试题17:合并两个排序的链表
  6. jquery 常用方法 集锦
  7. 职中计算机中级工考试,2015年中级职计算机考试基本知识点.doc
  8. 3乘3魔方第四步_【三阶魔方 - 初学】LBL第四步:顶层朝向
  9. 用现代 C++ 写一个高性能的服务器
  10. 孩子为什么不能玩抖音精彩回答,共勉
  11. Spark 和hadoop的一些面试题(准备)
  12. 虚拟主播是什么,有什么技术原理?- 沉睡者IT
  13. #莫队,分块#codevs 6555 洛谷 1494 jzoj 1902 小Z的袜子
  14. 随机数的产生原理与实现
  15. 宁以pass-by-reference-to-const替换pass-by-value——effective c++学习笔记
  16. 小程序之onHide()和onUnload()
  17. mac配置node的环境变量,-bash: ls:command not fund
  18. ACE 2005 Data Prep 数据预处理
  19. 蓝魔变红魔:moto是否是一条翻身的咸鱼
  20. 基于Python的朋友圈关系数据分析与实现

热门文章

  1. 阿里云ECS使用ossfs挂载OSS使用 【allow_other】
  2. 激活office python
  3. 分析|无感验证:应用适老化与业务反欺诈的“守门员”
  4. mrpoid模拟器java版_Mrpoid2模拟器
  5. 运维必备常用网络理论知识
  6. 爬虫实战:百度失信人名单
  7. confirm使用方法
  8. win11开机死机解决教程
  9. 尝试用jdk1.6强行装jdk1.8的逼-Filter
  10. Linux常用命令应付面试