{"id":36958318,"url":"https://github.com/dmis-lab/CompAct","last_synced_at":"2026-01-20T16:01:11.583Z","repository":{"id":248645550,"uuid":"825349211","full_name":"dmis-lab/CompAct","owner":"dmis-lab","description":"[EMNLP 2024] CompAct: Compressing Retrieved Documents Actively for Question Answering","archived":false,"fork":false,"pushed_at":"2024-09-20T09:01:45.000Z","size":524,"stargazers_count":32,"open_issues_count":0,"forks_count":3,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-07-14T20:10:02.604Z","etag":null,"topics":["compression","question-answering"],"latest_commit_sha":null,"homepage":"https://arxiv.org/abs/2407.09014","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dmis-lab.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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,"zenodo":null}},"created_at":"2024-07-07T14:38:21.000Z","updated_at":"2025-07-07T16:40:58.000Z","dependencies_parsed_at":"2024-07-19T14:32:36.950Z","dependency_job_id":"e38057e8-99ec-440d-aa4c-5c4c05c7aa0d","html_url":"https://github.com/dmis-lab/CompAct","commit_stats":null,"previous_names":["dmis-lab/compact"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/dmis-lab/CompAct","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dmis-lab%2FCompAct","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dmis-lab%2FCompAct/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dmis-lab%2FCompAct/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dmis-lab%2FCompAct/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dmis-lab","download_url":"https://codeload.github.com/dmis-lab/CompAct/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dmis-lab%2FCompAct/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28606288,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-20T14:45:23.139Z","status":"ssl_error","status_checked_at":"2026-01-20T14:44:16.929Z","response_time":117,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["compression","question-answering"],"created_at":"2026-01-13T16:00:24.925Z","updated_at":"2026-01-20T16:01:11.577Z","avatar_url":"https://github.com/dmis-lab.png","language":"Python","funding_links":[],"categories":["✂️ Compress Context"],"sub_categories":["RAG compression"],"readme":"# CompAct: Compressing Retrieved Documents Actively for Question Answering (EMNLP 2024)\n\nWe propose [**CompAct** (**Comp**ressing Retrieved Documents **Act**ively for Question Answering)](https://arxiv.org/abs/2407.09014), a novel framework that employs an active strategy for compressing extensive documents. **CompAct** dynamically preserves query-related contexts, focusing on the integration of information across documents. See our [paper](https://arxiv.org/abs/2407.09014) for more details.\n\n\u003cp align=\"center\"\u003e\n    📃 \u003ca href=\"https://arxiv.org/abs/2407.09014\" target=\"_blank\"\u003ePaper\u003c/a\u003e | 🤗 \u003ca href=\"https://huggingface.co/cwyoon99/CompAct-7b\" target=\"_blank\"\u003eModel\u003c/a\u003e\n\u003c/p\u003e\n\n![](assets/framework.jpg)\n\n## Updates\n[July 16. 2024] We have released the code and data.\n\n## Installation\nTo ensure compatibility with other libraries, we recommend using the following versions. You can adjust these based on your environment:\n* Python 3.10.9\n* PyTorch 2.1.2\n* Cuda 11.8\n\nWe utilize the 'alignment-handbook' for training our model. Please install all required packages as per their instructions. More details can be found [here](https://github.com/huggingface/alignment-handbook).\n\n```bash\n# Create and activate a new environment\nconda create -n compact python==3.10.9\nconda activate compact\n\n# Install pytorch\npip install torch==2.1.2 torchvision==0.16.2 torchaudio==2.1.2 --index-url https://download.pytorch.org/whl/cu118\n\n# alignment-handbook\ncd ./alignment-handbook/\npython -m pip install .\npython -m pip install flash-attn --no-build-isolation # Flash Attention 2\n\n# Install our requirements\npip install -r requirements.txt\n```\n\n## Quick Usage\nHere's a simple example to get you started:\n```python\nimport json\nimport torch\nfrom transformers import AutoTokenizer, AutoModelForCausalLM\nfrom utils import create_prompt, parse_output_without_sentence\n\ndevice = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\nmodel_dir = 'cwyoon99/CompAct-7b'\nmodel = AutoModelForCausalLM.from_pretrained(model_dir, torch_dtype=torch.bfloat16, device_map=\"auto\")\ntokenizer = AutoTokenizer.from_pretrained(model_dir)\n\nexample = json.load(open('./data/example.json')) # example case with retrieved documents\nprint(f\"question: {example['question']}\\nanswer: {example['answer']}\")\nprev_summary = []\nprev_eval = []\n\n# actively compress documents until it finds all necessary evidence\nfor i, iteration in enumerate(example['iterations']):\n    segment = iteration['documents_list']\n    document_input = \"\\n\".join(segment)\n\n    # load previous output\n    prev_summary_temp = prev_summary[-1] if i != 0 else \"\"\n    prev_eval_temp = prev_eval[-1].replace('[INCOMPLETE]', '').strip() if i != 0 else \"\"\n\n    # create prompt\n    input_prompt = create_prompt(example, iteration, i, document_input, prev_summary_temp, prev_eval_temp, tokenizer, eos_token=\"\", add_generation_prompt=True)\n    \n    # compress\n    with torch.no_grad():\n        inputs = tokenizer(input_prompt, return_tensors=\"pt\")\n        input_ids = inputs.input_ids.to(device)\n        attention_mask = inputs.attention_mask.to(device)\n        outputs = model.generate(input_ids=input_ids, attention_mask=attention_mask, max_new_tokens=500, temperature=0, top_p=1.0, pad_token_id=tokenizer.eos_token_id)\n    iteration['output'] = tokenizer.decode(outputs[0][input_ids.size(1):], skip_special_tokens=True).strip()\n\n    # parsing\n    parsed_sections = parse_output_without_sentence(iteration['output'])\n    prev_summary.append(parsed_sections['summary'])\n    prev_eval.append(parsed_sections['eval'])\n    # compressing extensive documents into compact context (under 200 tokens)\n    print(f\"summary of segment {i}: {iteration['summary']}\\ntermination of segment {i}: {iteration['eval']}\\n\")\n\n    # early termination\n    if \"[COMPLETE]\" in iteration['eval']:\n        break\n```\n\n## Download\n### Model\nYou can download our model from [huggingface](https://huggingface.co/cwyoon99/CompAct-7b).\n\n### Data\nWe conducted experiments on 5 question-answering benchmark datasets: [HotpotQA](https://github.com/hotpotqa/hotpot), [MuSiQue](https://github.com/StonyBrookNLP/musique), [2wikimultihopQA](https://github.com/Alab-NII/2wikimultihop) (2WikiMQA), [Natural Question](https://github.com/google-research-datasets/natural-questions) (NQ), and [TriviaQA](https://github.com/mandarjoshi90/triviaqa) (TQA).\n\nRequired data can be downloaded from [this Google Drive](https://drive.google.com/drive/folders/1lTz-hmb2inmU9KswLfkHag5-qRxTVujy?usp=sharing). Place it in the ```.data``` folder.\n* **retrieval**: instances with retrieved results for each datasets using different retrievers. \n* **preprocessed**: 28k preprocessed instances from HotpotQA train set\n* **demos**: Few-shot examples for answering questions.\n\n## Inference\nAfter setting up your environment and preparing the data, you can compress retrieved documents and check the end QA performance with the following script:\n\nFor convenience, you can easliy deploy our model from Huggingface. If you wish to fine-tune the base model, Please refer to the [Training](#training) section.\n\nWe also support batch decoding options (```--batch_decoding, --batch_size```) to accelerate the infernce\n\n```bash\nCUDA_VISIBLE_DEVICES=0\n\nPRE_DIR=\"[your repository path]\"\n\nret=contriever-msmarco\n\ncomp_name=cwyoon99/CompAct-7b\n\nmodel_name=meta-llama/Meta-Llama-3-8B\ncache_dir=\"[your caching paths]\"\n\ntask=HotpotQA # 2wikimultihop, musique, NQ, TQA\n\nif [ \"$task\" == \"TQA\" ]; then\n    split=\"test\"\nelse\n    split=\"dev\"\nfi\n\n\niter=6\nsegment_size=5\n\nCUDA_VISIBLE_DEVICES=$CUDA_VISIBLE_DEVICES python run_prompt.py \\\n    --task $task \\\n    --data_path $PRE_DIR/data/retrieval/$ret\"_\"$task/$split.json \\\n    --fshot \\\n    --fshot_path $PRE_DIR/data/demos/fshot_$task.json \\\n    --compress_output_dir $PRE_DIR/data/experiments/compress/$ret\"_\"$task/$split \\\n    --read_output_dir $PRE_DIR/data/experiments/test/$ret\"_\"$task/$split \\\n    --compressor_name_or_path $comp_name \\\n    --model_name_or_path $model_name \\\n    --cache_dir $cache_dir \\\n    --batch_decoding \\\n    --batch_size 20 \\\n    --read_wo_prev_eval \\\n    --segment_size $segment_size \\\n    --max_iteration $iter \\\n```\n\u003e If you want to use your self-trained model, specify the following arguments:   \n\u003e --compressor_dir e.g. $PRE_DIR/data/experiments/train/   \n\u003e --compressor_name_or_path e.g. \"[name of trained model]\"   \n\u003e --checkpoint e.g. checkpoint-378\n\n## Training\nWe apply Supervised Fine-Tuning (SFT) using only the subset of [HotpotQA](https://github.com/hotpotqa/hotpot). You may change specific hyperparameters and training arguments in ```./alignment-handbook/recipes/mistral-7b-instruct-v0.2/sft/config_full.yaml```\n\nFor more information about data and training details, Please refer to our paper.\n\n```bash\ncd CompAct/alignment-handbook\n\nexport CUDA_VISIBLE_DEVICES=\"[GPU_ID]\" # e.g. 0,1,2,3\nexport n_processes=\"[N_GPU]\" # e.g. 4\n\n# ./scripts/run_sft.sh\nCUDA_VISIBLE_DEVICES=$CUDA_VISIBLE_DEVICES ACCELERATE_LOG_LEVEL=info accelerate launch --config_file recipes/accelerate_configs/deepspeed_zero3.yaml --num_processes $n_processes scripts/run_sft.py recipes/mistral-7b-instruct-v0.2/sft/config_full.yaml\n\n```\n\n\u003c!-- ## Etc\n\n### Retriever (Contriever)\nIf you want to deploy\nwe use [Contriever](https://github.com/facebookresearch/contriever) fine-tuned on [MS-MARCO](https://microsoft.github.io/msmarco/), as our default retrieval system (on the 2018 Wikipedia corpus).\n```bash\n# https://github.com/facebookresearch/contriever?tab=readme-ov-file#evaluation\nwget https://dl.fbaipublicfiles.com/contriever/embeddings/contriever/wikipedia_embeddings.tar\nwget https://dl.fbaipublicfiles.com/contriever/embeddings/contriever-msmarco/wikipedia_embeddings.tar --\u003e\n\u003c!-- ``` --\u003e\n\n## Citation\n```\n@article{yoon2024compact,\n      title={CompAct: Compressing Retrieved Documents Actively for Question Answering}, \n      author={Chanwoong Yoon and Taewhoo Lee and Hyeon Hwang and Minbyul Jeong and Jaewoo Kang},\n      journal={arXiv preprint arXiv:2407.09014},\n      year={2024},\n      url={https://arxiv.org/abs/2407.09014}, \n}\n```\n\n\n\n## Contact\nFor more information or any questions of our work, feel free to contact me (cwyoon99 (at) korea.ac.kr or gmail.com). \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdmis-lab%2FCompAct","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdmis-lab%2FCompAct","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdmis-lab%2FCompAct/lists"}