paddlenlp智能问答系统,安装落地步骤

安装paddlepaddle

python -m pip install paddlepaddle-gpu==2.3.2 -i https://pypi.tuna.tsinghua.edu.cn/simple

清华源

python -m pip install paddlepaddle-gpu==2.3.2 -i https://pypi.tuna.tsinghua.edu.cn/simple

下载pipelines源代码

git clone https://github.com/PaddlePaddle/PaddleNLP.git
cd ${HOME}/PaddleNLP/pipelines/
pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
python setup.py install

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

我们建议在 GPU 环境下运行本示例,运行速度较快
python examples/question-answering/dense_qa_example.py --device gpu
如果只有 CPU 机器,安装CPU版本的Paddle后,可以通过 --device 参数指定 cpu 即可, 运行耗时较长
python examples/question-answering/dense_qa_example.py --device cpu

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

https://www.elastic.co/cn/downloads/elasticsearch

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

xpack.security.enabled: false

2.然后启动:

./bin/elasticsearch

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

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

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

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

1.4.2 文档数据写入 ANN 索引库

建立 ANN 索引库

python utils/offline_ann.py --index_name baike_cities --doc_dir data/baike --delete_index
参数含义说明
* `index_name`: 索引的名称
* `doc_dir`: txt文本数据的路径
* `host`: Elasticsearch的IP地址
* `port`: Elasticsearch的端口号
* `delete_index`: 是否删除现有的索引和数据,用于清空es的数据,默认为false

运行成功后会输出如下的日志:

INFO - pipelines.utils.logger - Logged parameters:
{‘processor’: ‘TextSimilarityProcessor’, ‘tokenizer’: ‘NoneType’, ‘max_seq_len’: ‘0’, ‘dev_split’: ‘0.1’}
INFO - pipelines.document_stores.elasticsearch - Updating embeddings for all 1318 docs …
Updating embeddings: 10000 Docs [00:16, 617.76 Docs/s]

1.4.3 启动 RestAPI 模型服务

指定智能问答系统的Yaml配置文件

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

更改自己的配置文件

vi rest_api/pipeline/dense_qa.yaml
components:    # define all the building-blocks for Pipeline
  - name: DocumentStore
    type: ElasticsearchDocumentStore  # consider using MilvusDocumentStore or WeaviateDocumentStore for scaling to large number of documents
    params:
      host: 10.201.192.135  # 更换ES ip地址 localhost
      index: baike_cities
      embedding_dim: 312
      port: 9201 # 更改es端口号
      username:   # 更改es用户名称
      password:  # 更改es密码

  - name: Retriever
    type: DensePassageRetriever
    params:
      document_store: DocumentStore    # params can reference other components defined in the YAML
      top_k: 10
      query_embedding_model: rocketqa-zh-nano-query-encoder  # 更换retriever模型位置
      passage_embedding_model: rocketqa-zh-nano-query-encoder  # 更换retriever模型位置
      embed_title: False
  - name: Ranker       # custom-name for the component; helpful for visualization & debugging
    type: ErnieRanker    # pipelines Class name for the component
    params:
      model_name_or_path: rocketqa-zh-dureader-cross-encoder   # 更换Ranker模型位置
      top_k: 5
  - name: Reader       # custom-name for the component; helpful for visualization & debugging
    type: ErnieReader    # pipelines Class name for the component
    params:
      model_name_or_path: ernie-gram-zh-finetuned-dureader-robust

指定智能问答系统的Yaml配置文件

export PIPELINE_YAML_PATH=rest_api/pipeline/dense_qa.yaml

使用端口号 8891 启动模型服务

python rest_api/application.py 8891

Linux 用户推荐采用 Shell 脚本来启动服务:

sh examples/question-answering/run_qa_server.sh

启动后可以使用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}}}'
export:PIPELINE_YAML_PATH='rest_api/pipeline/dense_qa.yaml'
使用端口号 8891 启动模型服务
python rest_api/application.py 8891

1.4.4 启动 WebUI

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

到这里您就可以打开浏览器访问 http://127.0.0.1:8502

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

错误解决

1.Linux上安装elasticsearch出现错误 java.lang.RuntimeException: can not run elasticsearch as root

elasticsearch 需要在非root环境下运行,可以做如下的操作:

adduser est
chown est:est -R ${HOME}/elasticsearch-8.3.2/
cd ${HOME}/elasticsearch-8.3.2/
su est
./bin/elasticsearch
2.ElasticSearch因为内存不足原因启动失败解决方案

原因是因为 ElasticSearch默认 分配内存是 2 G,但是由于不足所以启动不了。可以通过修改内存来启动.

在 elasticSearch 文件夹下,输入命令行

vim config/jvm.options 

将参数调为 512 m

-Xms512m
 
-Xmx512m
服务端运行报端口占用的错误 [Errno 48] error while attempting to bind on address ('0.0.0.0',8891): address already in use
lsof -i:8891
kill -9 PID # PID为8891端口的进程
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: DocumentStore
    type: ElasticsearchDocumentStore  # consider using MilvusDocumentStore or WeaviateDocumentStore for scaling to large number of documents
    params:
      host: localhost
      port: 9200
      index: dureader_robust_base_encoder # 修改索引名
      embedding_dim: 768   # 修改向量的维度
  - name: Retriever
    type: DensePassageRetriever
    params:
      document_store: DocumentStore    # params can reference other components defined in the YAML
      top_k: 10
      query_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 & debugging
    type: ErnieRanker    # pipelines Class name for the component
    params:
      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
运行faiss examples出现了错误:sqlalchemy.exec.OperationalError: (sqlite3.OperationalError) too many SQL variables

python 3.7版本引起的错误,修改如下代码:

# 增加batch_size参数,传入一个数值即可
document_store.update_embeddings(retriever, batch_size=256)
运行后台程序出现了错误: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设置的模型是一致的。

安装后出现错误:cannot import name '_registerMatType' from 'cv2'

opencv版本不匹配的原因,可以对其进行升级到最新版本,保证opencv系列的版本一致。

pip install opencv-contrib-python --upgrade
pip install opencv-contrib-python-headless --upgrade
pip install opencv-python --upgrade
安装运行出现 RuntimeError: Can't load weights for 'rocketqa-zh-nano-query-encoder'

rocketqa模型2.3.7之后才添加,paddlenlp版本需要升级:

pip install paddlenlp --upgrade
安装出现问题 The repository located at mirrors.aliyun.com is not a trusted or secure host and is being ignored.

设置pip源为清华源,然后重新安装,可运行如下命令进行设置:

pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值