Package: openprompt
An Open-Source Framework for Prompt-learning.Overview•Installation•How To Use•Docs•Paper•Citation•Performance•What's New?Mar 2022: We add atutorialas the response toissue 124, which uses a customized tokenizer_wrapper to perform tasks that are not in the default configuration of OpenPrompt (e.g., Bert tokenizer+T5 model）.Feb 2022: Check out our sister repoOpenDelta!Dec 2021:pip install openpromptDec 2021:SuperGLUE performanceare addedDec 2021: We supportgeneration paradigm for all tasksby adding a new verbalizer:GenerationVerbalizerand atutorial: 4.1_all_tasks_are_generation.pyNov 2021: Now we have released a paperOpenPrompt: An Open-source Framework for Prompt-learning.Nov 2021 PrefixTuning supports t5 now.Nov 2021: We made some major changes from the last version, where a flexible template language is newly introduced! Part of the docs is outdated and we will fix it soon.OverviewPrompt-learningis the latest paradigm to adapt pre-trained language models (PLMs) to downstream NLP tasks, which modifies the input text with a textual template and directly uses PLMs to conduct pre-trained tasks. This library provides a standard, flexible and extensible framework to deploy the prompt-learning pipeline. OpenPrompt supports loading PLMs directly fromhuggingface transformers. In the future, we will also support PLMs implemented by other libraries. For more resources about prompt-learning, please check ourpaper list.What Can You Do via OpenPrompt?Use the implementations of current prompt-learning approaches.* We have implemented various of prompting methods, including templating, verbalizing and optimization strategies under a unified standard. You can easily call and understand these methods.Design your own prompt-learning work.With the extensibility of OpenPrompt, you can quickly practice your prompt-learning ideas.InstallationNote: Please use Python 3.8+ for OpenPromptUsing PipOur repo is tested on Python3.8+and PyTorch1.8.1+, install OpenPrompt using pip as follows:pipinstallopenpromptTo play with the latest features, you can also install OpenPrompt from the source.Using GitClone the repository from github:gitclonehttps://github.com/thunlp/OpenPrompt.gitcdOpenPrompt
pipinstall-rrequirements.txt
pythonsetup.pyinstallModify the codepython setup.py developUse OpenPromptBase ConceptsAPromptModelobject contains aPLM, a (or multiple)Templateand a (or multiple)Verbalizer, where theTemplateclass is defined to wrap the original input with templates, and theVerbalizerclass is to construct a projection between labels and target words in the current vocabulary. And aPromptModelobject practically participates in training and inference.Introduction by a Simple ExampleWith the modularity and flexibility of OpenPrompt, you can easily develop a prompt-learning pipeline.Step 1: Define a taskThe first step is to determine the current NLP task, think about what’s your data looks like and what do you want from the data! That is, the essence of this step is to determine theclasssesand theInputExampleof the task. For simplicity, we use Sentiment Analysis as an example. tutorial_task.fromopenprompt.data_utilsimportInputExampleclasses=[# There are two classes in Sentiment Analysis, one for negative and one for positive"negative","positive"]dataset=[# For simplicity, there's only two examples# text_a is the input text of the data, some other datasets may have multiple input sentences in one example.InputExample(guid=0,text_a="Albert Einstein was one of the greatest intellects of his time.",),InputExample(guid=1,text_a="The film was badly made.",),]Step 2: Define a Pre-trained Language Models (PLMs) as backbone.Choose a PLM to support your task. Different models have different attributes, we encourge you to use OpenPrompt to explore the potential of various PLMs. OpenPrompt is compatible with models onhuggingface.fromopenprompt.plmsimportload_plmplm,tokenizer,model_config,WrapperClass=load_plm("bert","bert-base-cased")Step 3: Define a Template.ATemplateis a modifier of the original input text, which is also one of the most important modules in prompt-learning. 
We have definedtext_ain Step 1.fromopenprompt.promptsimportManualTemplatepromptTemplate=ManualTemplate(text='{"placeholder":"text_a"} It was {"mask"}',tokenizer=tokenizer,)Step 4: Define a VerbalizerAVerbalizeris another important (but not necessary) in prompt-learning,which projects the original labels (we have defined them asclasses, remember?) to a set of label words. Here is an example that we project thenegativeclass to the word bad, and project thepositiveclass to the words good, wonderful, great.fromopenprompt.promptsimportManualVerbalizerpromptVerbalizer=ManualVerbalizer(classes=classes,label_words={"negative":["bad"],"positive":["good","wonderful","great"],},tokenizer=tokenizer,)Step 5: Combine them into a PromptModelGiven the task, now we have aPLM, aTemplateand aVerbalizer, we combine them into aPromptModel. Note that although the example naively combine the three modules, you can actually define some complicated interactions among them.fromopenpromptimportPromptForClassificationpromptModel=PromptForClassification(template=promptTemplate,plm=plm,verbalizer=promptVerbalizer,)Step 6: Define a DataLoaderAPromptDataLoaderis basically a prompt version of pytorch Dataloader, which also includes aTokenizer, aTemplateand aTokenizerWrapper.fromopenpromptimportPromptDataLoaderdata_loader=PromptDataLoader(dataset=dataset,tokenizer=tokenizer,template=promptTemplate,tokenizer_wrapper_class=WrapperClass,)Step 7: Train and inferenceDone! We can conduct training and inference the same as other processes in Pytorch.importtorch# making zero-shot inference using pretrained MLM with promptpromptModel.eval()withtorch.no_grad():forbatchindata_loader:logits=promptModel(batch)preds=torch.argmax(logits,dim=-1)print(classes[preds])# predictions would be 1, 0 for classes 'positive', 'negative'Please refer to ourtutorial scripts, anddocumentationfor more details.DatasetsWe provide a series of download scripts in thedataset/folder, feel free to use them to download benchmarks.Performance ReportThere are too many possible combinations powered by OpenPrompt. We are trying our best
to test the performance of different methods as soon as possible. The performance will be constantly updated into theTables.
We also encourage the users to find the best hyper-parameters for their own tasks and report the results by making pull request.Known IssuesMajor improvement/enhancement in future.We made some major changes from the last version, so part of the docs is outdated. We will fix it soon.CitationPlease cite our paper if you use OpenPrompt in your work@article{ding2021openprompt,title={OpenPrompt: An Open-source Framework for Prompt-learning},author={Ding, Ning and Hu, Shengding and Zhao, Weilin and Chen, Yulin and Liu, Zhiyuan and Zheng, Hai-Tao and Sun, Maosong},journal={arXiv preprint arXiv:2111.01998},year={2021}}ContributorsWe thank all the  contributors to this project, more contributors are welcome!