{"id":16779666,"url":"https://github.com/wxl1999/unicrs","last_synced_at":"2025-10-26T01:18:58.077Z","repository":{"id":42970750,"uuid":"501144350","full_name":"wxl1999/UniCRS","owner":"wxl1999","description":"[KDD22] Official PyTorch implementation for \"Towards Unified Conversational Recommender Systems via Knowledge-Enhanced Prompt Learning\".","archived":false,"fork":false,"pushed_at":"2025-01-10T06:30:32.000Z","size":13597,"stargazers_count":26,"open_issues_count":4,"forks_count":20,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-03-18T06:23:13.946Z","etag":null,"topics":["conversation","conversational-ai","conversational-bots","dialog","dialogue","dialogue-systems","pretrained-language-model","pretrained-models","pretraining","prompt","prompt-tuning","prompts","recommendation","recommendation-system","recommender-system"],"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/wxl1999.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-06-08T07:27:40.000Z","updated_at":"2025-01-10T06:30:37.000Z","dependencies_parsed_at":"2024-10-28T11:58:02.911Z","dependency_job_id":null,"html_url":"https://github.com/wxl1999/UniCRS","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wxl1999%2FUniCRS","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wxl1999%2FUniCRS/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wxl1999%2FUniCRS/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wxl1999%2FUniCRS/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/wxl1999","download_url":"https://codeload.github.com/wxl1999/UniCRS/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244890102,"owners_count":20527030,"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":["conversation","conversational-ai","conversational-bots","dialog","dialogue","dialogue-systems","pretrained-language-model","pretrained-models","pretraining","prompt","prompt-tuning","prompts","recommendation","recommendation-system","recommender-system"],"created_at":"2024-10-13T07:31:31.717Z","updated_at":"2025-10-26T01:18:57.979Z","avatar_url":"https://github.com/wxl1999.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# UniCRS\n\nThis is the official PyTorch implementation for the paper:\n\n\u003e Xiaolei Wang*, Kun Zhou*, Ji-Rong Wen, Wayne Xin Zhao. Towards Unified Conversational Recommender Systems via Knowledge-Enhanced Prompt Learning. KDD 2022.\n\n## Overview\n\nwe proposed a novel conversational recommendation model named **UniCRS** to fulfill both the recommendation and conversation subtasks in a _unified_ approach. First, taking a fixed PLM (DialoGPT) as the backbone, we utilized a knowledge-enhanced prompt learning paradigm to reformulate the two subtasks. Then, we designed multiple effective prompts to support both subtasks, which include fused knowledge representations generated by a pre-trained semantic fusion module, task-specific soft tokens, and the dialogue context. We also leveraged the generated response template from the conversation subtask as an important part of the prompt to enhance the recommendation subtask. The above prompt design can provide sufficient information about the dialogue context, task instructions, and the background knowledge. By only optimizing these prompts, our model can effectively accomplish both the recommendation and conversation subtasks.\n\n![model](asset/model.png)\n\n## Requirements\n\n- python == 3.8.13\n- pytorch == 1.8.1\n- cudatoolkit == 11.1.1\n- transformers == 4.15.0\n- pyg == 2.0.1\n- accelerate == 0.8.0\n\n## Download Datasets\n\n[//]: # (## Download Datasets and Pre-trained Model)\n\nPlease download DBpedia from the [link](https://downloads.dbpedia.org/repo/dbpedia/mappings/mappingbased-objects/2021.09.01/mappingbased-objects_lang=en.ttl.bz2), after unzipping, move it into `data/dbpedia`.\n\n[//]: # (You can download our [pre-triained prompt]\u0026#40;#prompt-pre-training\u0026#41; from [Google Drive]\u0026#40;https://drive.google.com/drive/folders/18wxR3atXasSwRRAvlkeJ5J8qLnR0I2bk?usp=sharing\u0026#41;.)\n\n\n## Quick-Start\n\nWe run all experiments and tune hyperparameters on a GPU with 24GB memory, you can adjust `per_device_train_batch_size` and `per_device_eval_batch_size` according to your GPU, and then the optimization hyperparameters (e.g., `learning_rate`) may also need to be tuned.\n\n### Data processing\n\n```bash\ncd data\npython dbpedia/extract_kg.py\n\n# redial\npython redial/extract_subkg.py\npython redial/remove_entity.py\n\n# inspired\npython inspired/extract_subkg.py\npython inspired/remove_entity.py\n```\n\n### Prompt Pre-training\n\n```bash\ncp -r data/redial src/data/\ncd src\npython data/redial/process.py\naccelerate launch train_pre.py \\\n    --dataset redial \\  # [redial, inspired]\n    --tokenizer microsoft/DialoGPT-small \\\n    --model microsoft/DialoGPT-small \\\n    --text_tokenizer roberta-base \\\n    --text_encoder roberta-base \\\n    --num_train_epochs 5 \\\n    --gradient_accumulation_steps 1 \\\n    --per_device_train_batch_size 64 \\\n    --per_device_eval_batch_size 128 \\\n    --num_warmup_steps 1389 \\  # 168 for inspired\n    --max_length 200 \\\n    --prompt_max_length 200 \\\n    --entity_max_length 32 \\\n    --learning_rate 5e-4 \\  # 6e-4 for inspired\n    --output_dir /path/to/pre-trained prompt \\  # set your own save path\n    --use_wandb \\  # if you do not want to use wandb, comment it and the lines below\n    --project crs-prompt-pre \\  # wandb project name\n    --name xxx  # wandb experiment name\n```\n\n### Conversation Task Training and Inference\n\n```bash\n# train\ncp -r data/redial src/data/\ncd src\npython data/redial/process_mask.py\naccelerate launch train_conv.py \\\n    --dataset redial \\  # [redial, inspired]\n    --tokenizer microsoft/DialoGPT-small \\\n    --model microsoft/DialoGPT-small \\\n    --text_tokenizer roberta-base \\\n    --text_encoder roberta-base \\\n    --n_prefix_conv 20 \\  \n    --prompt_encoder /path/to/pre-trained prompt \\  # set to your save path of the pre-trained prompt\n    --num_train_epochs 10 \\\n    --gradient_accumulation_steps 1 \\\n    --ignore_pad_token_for_loss \\\n    --per_device_train_batch_size 8 \\\n    --per_device_eval_batch_size 16 \\\n    --num_warmup_steps 6345 \\  # 976 for inspired\n    --context_max_length 200 \\\n    --resp_max_length 183 \\\n    --prompt_max_length 200 \\\n    --entity_max_length 32 \\\n    --learning_rate 1e-4 \\\n    --output_dir /path/to/prompt for conversation \\  # set your own save path\n    --use_wandb \\  # if you do not want to use wandb, comment it and the lines below\n    --project crs-prompt-conv \\  # wandb project name\n    --name xxx  # wandb experiment name\n    \n# infer\naccelerate launch infer_conv.py \\\n    --dataset redial \\  # [redial, inspired]\n    --split train \\  # [train, valid, test] run all of the three options for each dataset\n    --tokenizer microsoft/DialoGPT-small \\\n    --model microsoft/DialoGPT-small \\\n    --text_tokenizer roberta-base \\\n    --text_encoder roberta-base \\\n    --n_prefix_conv 20 \\\n    --prompt_encoder /path/to/prompt for conversation \\  # set to your save path of the prompt for conversation\n    --per_device_eval_batch_size 64 \\\n    --context_max_length 200 \\\n    --resp_max_length 183 \\\n    --prompt_max_length 200 \\\n    --entity_max_length 32\n```\n\n### Recommendation Task\n\n```bash\n# merge infer results from conversation\n# redial\ncd src\ncp -r data/redial/. data/redial_gen/\npython data/redial_gen/merge.py --gen_file_prefix xxx # check it in save/redial, e.g., fill in dialogpt_prompt-pre_prefix-20_redial_1e-4 if you see dialogpt_prompt-pre_prefix-20_redial_1e-4_train/valid/test.jsonl\n# inspired\ncd src\ncp -r data/inspired/. data/inspired_gen/\npython data/inspired_gen/merge.py --gen_file_prefix xxx # check it in save/inspired, e.g., fill in dialogpt_prompt-pre_prefix-20_inspired_1e-4 if you see dialogpt_prompt-pre_prefix-20_inspired_1e-4_train/valid/test.jsonl\n\naccelerate launch train_rec.py \\\n    --dataset redial_gen \\  # [redial_gen, inspired_gen]\n    --tokenizer microsoft/DialoGPT-small \\\n    --model microsoft/DialoGPT-small \\\n    --text_tokenizer roberta-base \\\n    --text_encoder roberta-base \\\n    --n_prefix_rec 10 \\\n    --prompt_encoder /path/to/pre-trained prompt \\  # set to your save path of the pre-trained prompt\n    --num_train_epochs 5 \\\n    --per_device_train_batch_size 64 \\\n    --per_device_eval_batch_size 64 \\\n    --gradient_accumulation_steps 1 \\\n    --num_warmup_steps 530 \\  # 33 for inspired_gen\n    --context_max_length 200 \\\n    --prompt_max_length 200 \\\n    --entity_max_length 32 \\\n    --learning_rate 1e-4 \\\n    --output_dir /path/to/prompt for recommendation \\\n    --use_wandb \\  # if you do not want to use wandb, comment it and the lines below\n    --project crs-prompt-rec \\  # wandb project name\n    --name xxx  # wandb experiment name\n```\n\n## Contact\n\nIf you have any questions for our paper or codes, please send an email to wxl1999@foxmail.com.\n\n## Acknowledgement\n\nSpecial thanks the CRS toolkit [CRSLab](https://github.com/RUCAIBox/CRSLab), we use it to run experiments of baseline models.\n\nPlease cite the following papers as the references if you use our codes or the processed datasets.\n\n```bibtex\n@inproceedings{wang2022towards,\n  title={Towards Unified Conversational Recommender Systems via Knowledge-Enhanced Prompt Learning},\n  author={Wang, Xiaolei and Zhou, Kun and Wen, Ji-Rong and Zhao, Wayne Xin},\n  booktitle={Proceedings of the 28th ACM SIGKDD Conference on Knowledge Discovery and Data Mining},\n  pages={1929--1937},\n  year={2022}\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwxl1999%2Funicrs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwxl1999%2Funicrs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwxl1999%2Funicrs/lists"}