{"id":28676544,"url":"https://github.com/zjunlp/knowprompt","last_synced_at":"2025-10-05T13:06:32.213Z","repository":{"id":38267003,"uuid":"419610254","full_name":"zjunlp/KnowPrompt","owner":"zjunlp","description":"[WWW 2022] KnowPrompt: Knowledge-aware Prompt-tuning with Synergistic Optimization for Relation Extraction","archived":false,"fork":false,"pushed_at":"2023-06-20T02:47:56.000Z","size":29218,"stargazers_count":201,"open_issues_count":0,"forks_count":34,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-05-09T22:34:33.107Z","etag":null,"topics":["dataset","dialogre","few-shot-learning","knowledge-informed-prompt-learning","knowprompt","prompt","prompt-learning","prompt-tuning","pytorch","re","relation-extraction","semeval","tacred","text-classification"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zjunlp.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":"CITATION.cff","codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2021-10-21T06:40:36.000Z","updated_at":"2025-04-02T23:49:32.000Z","dependencies_parsed_at":"2024-08-03T09:17:23.338Z","dependency_job_id":null,"html_url":"https://github.com/zjunlp/KnowPrompt","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/zjunlp/KnowPrompt","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zjunlp%2FKnowPrompt","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zjunlp%2FKnowPrompt/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zjunlp%2FKnowPrompt/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zjunlp%2FKnowPrompt/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zjunlp","download_url":"https://codeload.github.com/zjunlp/KnowPrompt/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zjunlp%2FKnowPrompt/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259732771,"owners_count":22903087,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dataset","dialogre","few-shot-learning","knowledge-informed-prompt-learning","knowprompt","prompt","prompt-learning","prompt-tuning","pytorch","re","relation-extraction","semeval","tacred","text-classification"],"created_at":"2025-06-13T23:05:04.807Z","updated_at":"2025-10-05T13:06:32.204Z","avatar_url":"https://github.com/zjunlp.png","language":"Python","readme":"# KnowPrompt\n\n\nCode and datasets for the WWW2022 paper [KnowPrompt: Knowledge-aware Prompt-tuning  with  Synergistic Optimization for Relation Extraction](https://arxiv.org/pdf/2104.07650.pdf). \n\n- ❗NOTE: We provide a paper-list at [PromptKG](https://github.com/zjunlp/PromptKG) and open-source [KnowLM](https://github.com/zjunlp/KnowLM), a knowledgeable large language model framework with pre-training and instruction fine-tuning code (supports multi-machine multi-GPU setup).\n\n\u003cdiv align=center\u003e\u003cimg src=\"model.png\" width=\"80%\" height=\"80%\" /\u003e\u003c/div\u003e\n\n# What's New\n\n## Apr,5 2023\n* Our follow-up paper on schema-aware prompting KGC [Schema-aware Reference as Prompt Improves Data-Efficient Knowledge Graph Construction](https://arxiv.org/abs/2210.10709) has been accepted by SIGIR2023. The project address is visible at [RAP](https://github.com/zjunlp/RAP).\n\n## Dec,7 2022\n* We release a checkpoint for `SemEval` dataset at [huggingface model hub](https://huggingface.co/zjunlp/KnowPrompt)\n\n## Sept,21 2022\n* Our follow-up paper on  retrieval-augmented prompting NLP [Decoupling Knowledge from Memorization: Retrieval-augmented Prompt Learning](https://arxiv.org/abs/2205.14704) has been accepted by NeurIPS2022. The project address is visible at [RetroPrompt](https://github.com/zjunlp/PromptKG/tree/main/research/RetroPrompt).\n\n## March,30 2022\n* Our follow-up paper on retrieval-enhanced prompting RE [Relation Extraction as Open-book Examination: Retrieval-enhanced Prompt Tuning](https://arxiv.org/abs/2205.02355) has been accepted by SIGIR2022. The project address is visible at [RetrievalRE](https://github.com/zjunlp/PromptKG/tree/main/research/RetrievalRE).\n\n## Jan,14 2022\n* Our paper [KnowPrompt: Knowledge-aware Prompt-tuning with Synergistic Optimization for Relation Extraction](https://arxiv.org/abs/2104.07650) has been accepted by WWW2022.\n\n\n# Requirements\n\nIt is recommended to use a virtual environment to run KnowPrompt.\n\n```bash\nconda create -n knowprompt python=3.8\n\nconda activate knowprompt\n```\n\nTo install requirements:\n\n```\npip install -r requirements.txt\n```\n\nDatasets\n==========\n\nWe provide all the datasets and prompts used in our experiments.\n\n+ [[SEMEVAL]](dataset/semeval)\n\n+ [[DialogRE]](dataset/dialogue)\n\n+ [[TACRED-Revisit]](dataset/tacrev)\n\n+ [[Re-TACRED]](dataset/retacred)\n\n+ [[TACRED]](dataset/tacred)\n\nThe expected structure of files is:\n\n\n```\nknowprompt\n |-- dataset\n |    |-- semeval\n |    |    |-- train.txt       \n |    |    |-- dev.txt\n |    |    |-- test.txt\n |    |    |-- temp.txt\n |    |    |-- rel2id.json\n |    |-- dialogue\n |    |    |-- train.json       \n |    |    |-- dev.json\n |    |    |-- test.json\n |    |    |-- rel2id.json\n |    |-- tacred\n |    |    |-- train.txt       \n |    |    |-- dev.txt\n |    |    |-- test.txt\n |    |    |-- temp.txt\n |    |    |-- rel2id.json\n |    |-- tacrev\n |    |    |-- train.txt       \n |    |    |-- dev.txt\n |    |    |-- test.txt\n |    |    |-- temp.txt\n |    |    |-- rel2id.json\n |    |-- retacred\n |    |    |-- train.txt       \n |    |    |-- dev.txt\n |    |    |-- test.txt\n |    |    |-- temp.txt\n |    |    |-- rel2id.json\n |-- scripts\n |    |-- semeval.sh\n |    |-- dialogue.sh\n |    |-- ...\n \n```\n\n\nRun the experiments\n==========\n\n## Initialize the answer words\n\nUse the comand below to get the answer words to use in the training.\n\n```shell\npython get_label_word.py --model_name_or_path bert-large-uncased  --dataset_name semeval\n```\n\nThe `{answer_words}.pt`will be saved in the dataset, you need to assign the `model_name_or_path` and `dataset_name` in the `get_label_word.py`.\n\n## Split dataset\n\nDownload the data first, and put it to `dataset` folder. Run the comand below, and get the few shot dataset.\n\n```shell\npython generate_k_shot.py --data_dir ./dataset --k 8 --dataset semeval\ncd dataset\ncd semeval\ncp rel2id.json val.txt test.txt ./k-shot/8-1\n```\nYou need to modify the `k` and `dataset` to assign k-shot and dataset. Here we default seed as 1,2,3,4,5 to split each k-shot, you can revise it in the `generate_k_shot.py`\n\n## Let's run\n\nOur script code can automatically run the experiments in 8-shot, 16-shot, 32-shot and \nstandard supervised settings with both the procedures of train, eval and test. We just choose the random seed to be 1 as an example in our code. Actually you can perform multiple experments with different seeds.\n\n#### Example for SEMEVAL\nTrain the KonwPrompt model on SEMEVAL with the following command:\n\n```bash\n\u003e\u003e bash scripts/semeval.sh  # for roberta-large\n```\nAs the scripts  for `TACRED-Revist`, `Re-TACRED`, `Wiki80` included in our paper are also provided, you just need to run it like above example.\n\n#### Example for DialogRE\nAs the data format of DialogRE is very different from other dataset, Class of processor is also different. \nTrain the KonwPrompt model on DialogRE with the following command:\n\n```bash\n\u003e\u003e bash scripts/dialogue.sh  # for roberta-base\n```\n\n## More emperical results\n\nWe report emperical results on more datasets in the EMNLP 2022 (Findings) paper \"[Towards Realistic Low-resource Relation Extraction: A Benchmark with Empirical Baseline Study](https://arxiv.org/pdf/2210.10678.pdf)\" [[code](https://github.com/zjunlp/LREBench)].\n\n# Acknowledgement\n\nPart of our code is borrowed from [code](https://github.com/thunlp/PTR) of [PTR: Prompt Tuning with Rules for Text Classification](https://arxiv.org/abs/2105.11259), many thanks.\n\n# Citation\nIf you use the code, please cite the following paper:\n\n\n```bibtex\n@inproceedings{DBLP:conf/www/ChenZXDYTHSC22,\n  author    = {Xiang Chen and\n               Ningyu Zhang and\n               Xin Xie and\n               Shumin Deng and\n               Yunzhi Yao and\n               Chuanqi Tan and\n               Fei Huang and\n               Luo Si and\n               Huajun Chen},\n  editor    = {Fr{\\'{e}}d{\\'{e}}rique Laforest and\n               Rapha{\\\"{e}}l Troncy and\n               Elena Simperl and\n               Deepak Agarwal and\n               Aristides Gionis and\n               Ivan Herman and\n               Lionel M{\\'{e}}dini},\n  title     = {KnowPrompt: Knowledge-aware Prompt-tuning with Synergistic Optimization\n               for Relation Extraction},\n  booktitle = {{WWW} '22: The {ACM} Web Conference 2022, Virtual Event, Lyon, France,\n               April 25 - 29, 2022},\n  pages     = {2778--2788},\n  publisher = {{ACM}},\n  year      = {2022},\n  url       = {https://doi.org/10.1145/3485447.3511998},\n  doi       = {10.1145/3485447.3511998},\n  timestamp = {Tue, 26 Apr 2022 16:02:09 +0200},\n  biburl    = {https://dblp.org/rec/conf/www/ChenZXDYTHSC22.bib},\n  bibsource = {dblp computer science bibliography, https://dblp.org}\n}\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzjunlp%2Fknowprompt","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzjunlp%2Fknowprompt","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzjunlp%2Fknowprompt/lists"}