第五十九回 公孙胜芒砀山降魔 晁天王曾头市中箭-飞桨自然语言处理套件PaddleNLP初探

时间:2024-03-19 12:34:48

飞桨自然语言处理套件PaddleNLP初探

PaddleNLP是一款简单易用且功能强大的自然语言处理和大语言模型(LLM)开发库。聚合业界优质预训练模型并提供开箱即用的开发体验,覆盖NLP多场景的模型库搭配产业实践范例可满足开发者灵活定制的需求。

官网:GitHub - PaddlePaddle/PaddleNLP: ???? Easy-to-use and powerful NLP and LLM library with ???? Awesome model zoo, supporting wide-range of NLP tasks from research to industrial applications, including ????Text Classification, ???? Neural Search, ❓ Question Answering, ℹ️ Information Extraction, ???? Document Intelligence, ???? Sentiment Analysis etc.

pip安装

pip install --upgrade paddlenlp

快速开始

大模型文本生成

PaddleNLP提供了方便易用的Auto API,能够快速的加载模型和Tokenizer。这里以使用 linly-ai/chinese-llama-2-7b 大模型做文本生成为例:

>>> from paddlenlp.transformers import AutoTokenizer, AutoModelForCausalLM
>>> tokenizer = AutoTokenizer.from_pretrained("linly-ai/chinese-llama-2-7b")
>>> model = AutoModelForCausalLM.from_pretrained("linly-ai/chinese-llama-2-7b", dtype="float16")
>>> input_features = tokenizer("你好!请自我介绍一下。", return_tensors="pd")
>>> outputs = model.generate(**input_features, max_length=128)
>>> tokenizer.batch_decode(outputs[0])
# ['\n你好!我是一个AI语言模型,可以回答你的问题和提供帮助。']

一键UIE预测

PaddleNLP提供一键预测功能,无需训练,直接输入数据即可开放域抽取结果。这里以信息抽取-命名实体识别任务,UIE模型为例:

>>> from pprint import pprint
>>> from paddlenlp import Taskflow

>>> schema = ['时间', '选手', '赛事名称'] # Define the schema for entity extraction
>>> ie = Taskflow('information_extraction', schema=schema)
>>> pprint(ie("2月8日上午北京冬奥会*式滑雪女子大跳台决赛中中国选手谷爱凌以188.25分获得金牌!"))

大模型

文档:https://github.com/PaddlePaddle/PaddleNLP/tree/develop/llm