此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/thunlp/WebCPM
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0

WebCPM

✨ 本项目为下面ACL2023论文的实现:Interactive Web Search for Chinese Long-form Question Answering

paper

Read this in English.


目录

概述

platform

在这个工作中,我们展示了WebCPM,一个使用中文预训练模型进行互动网页搜索的项目。我们开发了一个网页搜索界面,既可以人工操作,也可以收集人类的网页搜索行为。然后我们使用多达100亿参数的预训练模型进行微调,模仿人类的网页搜索行为,并根据收集到的事实生成答案。我们开源了网页搜索界面,数据集,实现方法,和模型参数。

需求

要运行我们的代码,请使用以下命令安装所有依赖包:

pip install -r requirements.txt

注意: 包的不同版本(例如,pytorch)可能导致与论文中的结果不同。但是,无论你使用哪个版本的包,大致结果应该是一致的。

准备

准备数据

首先从Google Drive下载数据,并将文件interactive_datapipeline_data放到./data,或运行以下命令: 下载的文件包含以下内容:

interactive_data/data.json是本文实验中使用的数据集(总共5500个实例)。 interactive_data/data_zhihu.json是与本文同时收集的额外数据集(约900个实例),问题来源于知乎,您可以使用这个数据进行数据增强。 请使用以下代码将上述数据分为训练集、开发集和测试集(设置 --add_zhihu 将添加 data_zhihu.json)。

cd data
python split.py --add_zhihu

除了交互式网页搜索数据外,我们还提供了训练流水线式网页搜索所需的数据集:pipeline_data(总共110k 个实例)。所有数据均由text-davinci-003创建(必应搜索在这个过程中也参与),然后由人类注释者手动筛选。 (注意 这部分内容未包含在论文中,您不需要将其分为训练/开发/测试。)

准备模型

WebCPM基于CPM-bee,有10B以上个参数,这是社区中最大的中文预训练语言模型之一。我们使用的是CPM-bee的早期版本,被称为cpm_10b_webcpm_exp.pt。CPM-bee的最新版本开源在这里 New-CPM-bee. 如果您需要使用最新的cpm-bee,可能需要修改webcpm代码库中的cpm-live(移植过来即可)。注意,模型检查点尚未针对任何下游任务进行微调。要访问cpm_10b_webcpm_exp.pt,您可以在Tsinghua Cloud下载模型参数,或运行以下脚本:

cd models
bash download_model_initial_model.sh

上述代码将在models中下载10B未经过微调的模型,同时我们也提供了基于pipeline的微调后的模型,请参考download_model_pipeline_finetuned.sh,或者您也可以在这里手动下载:Tsinghua Cloud

训练 WebCPM

platform

我们提供了WebCPM的两个版本:(1)交互式网页搜索(ACL论文中提出的方法)和(2)流水线式网页搜索,这种方法更易于部署(该方法未在论文中报告)。两个版本都使用不同的脚本来生成训练数据,并使用相同的代码进行模型训练。

流水线式网页搜索简介

该工作流程包括四个阶段:(1)首先,根据原始问题生成可能的搜索查询;(2)然后,对每个搜索查询,调用Bing搜索并访问前K个网页;(3)对于每个网页,提取重要信息;(4)基于所有记录的信息,生成连贯且细致的答案。所有这些任务都通过多任务方式进行训练,请参考run_web_browsing/run_pipeline.py。关于交互式网络搜索的详细信息,请参考我们的原始论文。

数据预处理

在开始之前,运行以下代码:

export PYTHONPATH=/**your-base-path**/webcpm

以下是生成训练数据的步骤(我们区分交互式网络搜索和流水线式的方法)。以下代码将在相应的文件夹中生成train_datadev_datatest_data,这些数据将在训练期间被加载。

生成互动式网页搜索的训练数据

首先,使用以下代码为合成模型构造数据:

cd dataset_interactive
python make_data_synthesis_model.py --data_path ../../data/interactive_data  --augment_qa_data --augment_data_path ../../data/pipeline_data

我们解释一下其中的一些参数:

  • data_path: 源数据路径。
  • augment_qa_data: 是否使用由text-davinci自动生成的qa数据增加训练数据。(为了复制我们论文中的结果,不要添加此参数)
  • augment_data_path: 增加训练数据的数据路径。 搜索模型的训练数据生成如下:
python make_data_search_model.py --add_query --add_action --add_abstract --abstract_all_tokens

我们解释一下其中的一些参数:

  • data_path: 源数据路径。
  • add_query: 如果为True,将添加查询生成数据。
  • add_abstract: 如果为True,将添加生成支持事实提取数据。
  • abstract_all_tokens: 如果为True,支持事实提取模块将生成所有的tokens,而不仅仅是前几个/后几个tokens。
  • add_action: 如果为True,将添加动作预测数据。
  • add_synthesis: 如果为True,将为合成模型加载本地数据。注意您必须先运行python make_data_synthesis_model.py以获取合成数据,然后在此处添加此参数。 如果你想以多任务方式训练所有子任务,添加所有上述参数;否则只添加一个参数(例如,--add_query)进行单任务测试。

生成流水线式网页搜索的训练数据

请运行以下代码:

cd dataset_pipeline
python make_data.py

训练

要训练WebCPM,运行以下代码:

cd training
export PYTHONPATH=/**your-base-path**/webcpm
export CUDA_VISIBLE_DEVICES=0,1,2,3,4,5,6,7
GPUS_PER_NODE=$(echo $CUDA_VISIBLE_DEVICES | tr ',' '\n' | wc -l | xargs)
echo "Number of visible devices: $GPUS_PER_NODE, should be the same as visible devices"

set -ex

MASTER_ADDR=localhost
MASTER_PORT=3239
NNODES=1
NODE_RANK=0

OPTS=""
OPTS+=" --model-config config/cpm-bee-10b.json"
OPTS+=" --dataset ../data/dataset_interactive/train_data"
OPTS+=" --dataseteval ../data/dataset_interactive/dev_data"
OPTS+=" --epoch 5"
OPTS+=" --batch-size 8"
OPTS+=" --train-iters 100"
OPTS+=" --save-name webcpm_finetuned"
OPTS+=" --max-length 2560"
OPTS+=" --save ../models/"
OPTS+=" --lr 0.0001"
OPTS+=" --inspect-iters 100"
OPTS+=" --warmup-iters 1"
OPTS+=" --save-epochs 1"
OPTS+=" --lr-decay-style noam"
OPTS+=" --weight-decay 0.01"
OPTS+=" --clip-grad 1.0"
OPTS+=" --loss-scale 32768"
OPTS+=" --start-step 0"
OPTS+=" --load ../models/cpm_10b_webcpm_exp.pt"

CMD="torchrun --nnodes=${NNODES} --nproc_per_node=${GPUS_PER_NODE} --rdzv_id=1 --rdzv_backend=c10d --rdzv_endpoint=${MASTER_ADDR}:${MASTER_PORT} finetune_cpm_bee.py ${OPTS}"

echo ${CMD}
$CMD

我们解释一下其中的一些参数:

  • datasetdataseteval: 处理后文件的路径。对于交互式网络搜索,它是 dataset_interactive,对于流水线式方法,它是 dataset_pipeline。
  • batch-size: 单个 GPU 的批大小,真实的批大小将是 #GPUs x 每个 GPU 的批大小。
  • max-length: 数据的最大序列长度(不是模型),那些更长的训练实例将被丢弃。
  • save-namesave:保存微调模型的路径以及保存的模型检查点的名称。
  • epoch: 训练轮数。
  • load: 预训练模型检查点的路径(在本例中是 cpmb)。 注意,无论你正在训练哪个模块(或多任务设置),你都可以使用上述代码。我们在 8x80G A100 上进行训练,你可以根据你的 GPU 设备更改批大小,性能对超参数不敏感。

单任务评估

为了评估不同的子任务,你可以首先运行以下代码获取你的微调模型在测试数据上的预测:

cd inference
python inference.py --test_file ../training/dataset_interactive/test.txt --output_file output/test_predictions.json --ckpt_path **your_finetuned_checkpoint.pt

我们解释一下其中的一些参数:

  • test_file: 测试文件的路径,它应在数据预处理过程中生成。
  • output_file: 你想要写入预测的路径。
  • ckpt_path: 你的微调模型的路径。 在获得测试文件的预测后,你可以运行以下代码进行单任务评估:
python evaluate.py --input_file output/test_predictions.txt --evaluate_action

我们解释一下其中的一些参数:

  • input_file: 你写入测试文件预测的路径。
  • evaluate_action: 你是否想要评估动作预测任务(F1)。
  • evaluate_query: 你是否想要评估搜索查询生成任务(Rougel-L)。
  • evaluate_abstract: 你是否想要评估支持事实提取任务(Rougel-L)。
  • abstract_all_tokens: 你训练模型支持事实提取的模式,如果你生成所有的标记,添加这个参数(Rougel-L)。
  • evaluate_answer: 你是否想要评估答案合成任务(Rougel-L)。
  • beam_size: 将 beam size 设置为 1 会显著加速推断,但会稍微损害性能。

在新问题数据集上运行WebCPM

这是整个流水线评估的实现。你可以使用以下代码为新问题生成答案。注意这首先需要你从这里获取一个必应搜索 API 密钥,并运行以下代码:

cd run_web_browsing
export PYTHONPATH=/**base-path**/webcpm
export BING_SEARCH_KEY="**Your Bing Search API Key**"

互动式网页搜索

python run_interactive.py --data_path predictions/test_interactive.json --ckpt_path **your-checkpoint**

流水线式网页搜索

python run_pipeline.py --data_path predictions/test.json --ckpt_path **your-checkpoint**

我们解释一下其中的一些参数:

  • data_path: 你写入预测的路径。
  • ckpt_path: 你使用基于管道的方法训练的检查点的路径。

数据标注平台

platform 我们开源了我们的网络搜索界面,你可以用它进行数据注释。请参阅 Annotation。目前代码有点混乱,我们将很快上传一个更清晰的版本。

反馈问题或疑问?

如果您对该代码或论文有任何疑问,请联系 Yujia (qyj20@mails.tsinghua.edu.cn) 或者开一个 Github issue。

工具学习相关链接

鉴于基础模型的强大能力,我们期待看到它们在操纵各种工具中的应用。WebCPM 是一个典型的研究尝试。更多的资源,请参考以下内容:

引用

如果你觉得我们的 WebCPM 有用,请参考以下引用:

@inproceedings{qin2023webcpm,
    title = "WebCPM: Interactive Web Search for Chinese Long-form Question Answering",
    author={Yujia Qin and Zihan Cai and Dian Jin and Lan Yan and Shihao Liang and Kunlun Zhu and Yankai Lin and Xu Han and Ning Ding and Huadong Wang and Ruobing Xie and Fanchao Qi and Zhiyuan Liu and Maosong Sun and Jie Zhou},
    booktitle = "Proceedings of ACL 2023",
    year = "2023",
    publisher = "Association for Computational Linguistics",
    url = "https://arxiv.org/abs/2305.06849",
}
Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

简介

WebCPM 是中文领域首个基于交互式网页搜索的问答开源模型框架 展开 收起
Apache-2.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化