{"id":28676528,"url":"https://github.com/zjunlp/lookaheadtuning","last_synced_at":"2025-07-21T16:35:21.060Z","repository":{"id":284463897,"uuid":"905199262","full_name":"zjunlp/LookAheadTuning","owner":"zjunlp","description":"LookAhead Tuning: Safer Language Models via Partial Answer Previews","archived":false,"fork":false,"pushed_at":"2025-03-26T02:11:21.000Z","size":7150,"stargazers_count":6,"open_issues_count":0,"forks_count":0,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-03-26T03:20:20.278Z","etag":null,"topics":["artificial-intelligence","fine-tuning","large-language-models","lookaheadtuning","natural-language-processing","safety"],"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":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-12-18T11:00:28.000Z","updated_at":"2025-03-26T02:53:18.000Z","dependencies_parsed_at":"2025-03-26T03:31:10.205Z","dependency_job_id":null,"html_url":"https://github.com/zjunlp/LookAheadTuning","commit_stats":null,"previous_names":["zjunlp/lookaheadtuning"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/zjunlp/LookAheadTuning","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zjunlp%2FLookAheadTuning","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zjunlp%2FLookAheadTuning/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zjunlp%2FLookAheadTuning/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zjunlp%2FLookAheadTuning/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zjunlp","download_url":"https://codeload.github.com/zjunlp/LookAheadTuning/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zjunlp%2FLookAheadTuning/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":["artificial-intelligence","fine-tuning","large-language-models","lookaheadtuning","natural-language-processing","safety"],"created_at":"2025-06-13T23:05:00.063Z","updated_at":"2025-06-13T23:05:00.614Z","avatar_url":"https://github.com/zjunlp.png","language":"Python","readme":"\u003ch1 align=\"center\"\u003e LookAhead Tuning \u003c/h1\u003e\n\u003ch3 align=\"center\"\u003e Safer Language Models via Partial Answer Previews \u003c/h3\u003e\n\n\u003cp align=\"center\"\u003e\n  \u003ca href=\"https://arxiv.org/abs/2503.19041\"\u003e📄arXiv\u003c/a\u003e\n\u003c/p\u003e\n\n\n\u003cdiv\u003e\n\u003c/div\u003e\n\u003cdiv align=\"center\"\u003e\n\u003cp align=\"center\"\u003e\n  \u003cimg src=\"figs/main.png\"/\u003e\n\u003c/p\u003e\n\u003c/div\u003e\n\n[![Awesome](https://awesome.re/badge.svg)](https://github.com/zjunlp/LookAheadTuning) [![License: MIT](https://img.shields.io/badge/License-MIT-green.svg)](https://opensource.org/licenses/MIT) ![](https://img.shields.io/github/last-commit/zjunlp/LookAheadTuning?color=green) \n\n## Table of Contents\n\n- 🌻 [Acknowledgement](#acknowledgement)\n- 🌟 [Overview](#overview)\n- 🔧 [Algorithm](#algorithm)\n- 🚀 [Installation](#installation)\n- 📚 [Partial Answer Preview](#partial-answer-preview)\n- 📉 [Vanilla Fine-Tuning](#vanilla-fine-tuning)\n- 🧐 [OURS](#ours)\n- 🚩 [Citation](#citation)\n\n## 🌻Acknowledgement\n\nOur dataset is sourced from the [GSM8K dataset](https://huggingface.co/datasets/openai/gsm8k), the [Samsum dataset](https://huggingface.co/datasets/Samsung/samsum), and the [HEx-PHI dataset](https://huggingface.co/datasets/LLM-Tuning-Safety/HEx-PHI) . Our training code and safety evaluation procedures are derived from the [LLMs-Finetuning-Safety](https://github.com/LLM-Tuning-Safety/LLMs-Finetuning-Safety) and the [shallow-vs-deep-alignment](https://github.com/Unispac/shallow-vs-deep-alignment).\n\n**We sincerely appreciate their remarkable contributions to the field.**\n\n## 🌟Overview\n\nLookAhead Tuning comprises two data-centric, resource-efficient, simple, and effective methods. The fundamental concept is modifying the training data to preview the answer prefix tokens, thereby reducing the loss associated with these tokens and minimizing disturbances to the model’s initial tokens, which helps maintain the model’s safety. \n\n## 🔧Algorithm\n\n\u003cdiv\u003e\n\u003c/div\u003e\n\u003cdiv align=\"center\"\u003e\n\u003cp align=\"center\"\u003e\n  \u003cimg src=\"figs/algorithm.png\"style=\"width:300px; height:auto;\" /\u003e\n\u003c/p\u003e\n\u003c/div\u003e\n\n## 🚀Installation\n\n1. Clone the Repository and Enter the Directory:\n\n   ```bash\n   git clone https://github.com/zjunlp/LookAheadTuning\n   cd LookAheadTuning\n   ```\n\n2. Install Dependencies:\n\n   ```bash\n   pip install -r requirements.txt\n   ```\n\n3. Download the Model:\n\n   Download the [LLaMA 2 7B Chat model](https://huggingface.co/meta-llama/Llama-2-7b-chat-hf) into the `LookAheadTuning/llama-2-7b-chat-hf` directory. Alternatively, you can use another model and tokenizer.\n\n\n## 📚Partial Answer Preview\n\nWe utilize a Python script named `main.py` that is designed to process datasets in both JSON and JSONL formats using two distinct modes: **real** and **virtual**. This tool allows you to modify your training data, enabling a preview of the answer by adjusting specific fields based on your selected mode. **Inference data is unchanged.**\n\n#### Features\n\n- **Supports Multiple Formats**: Handles both JSON and JSONL (JSON Lines) file formats.\n- **Dual Processing Modes**:\n  - **Real Mode**: Uses a tokenizer to concatenate a specified number of tokens from the output field to the input field.\n  - **Virtual Mode**: Inserts a predefined string into both the input and output fields without tokenization.\n- **Customizable Fields**: Specify which fields in your JSON objects to process.\n- **Easy Configuration**: Simple command-line arguments to tailor the processing to your needs.\n\n#### Usage\n\nRun the `main.py` script from the command line with the appropriate arguments.\n\n#### Arguments\n\n- `--input_file`: (Required) Path to the input file.\n- `--output_file`: (Required) Path to the output file.\n- `--input_format`: (Required) Format of the input file. Choose either `json` or `jsonl`.\n- `--output_format`: (Required) Format of the output file. Choose either `json` or `jsonl`.\n- `--input_field`: (Optional) Field name for the input in the JSON objects. Default is `input`.\n- `--output_field`: (Optional) Field name for the output in the JSON objects. Default is `output`.\n- `--mode`: (Required) Processing mode. Choose either `real` or `virtual`.\n  - **Real Mode**:\n    - `--m`: Number of tokens to preview.\n    - `--tokenizer_path`: Path to the model tokenizer.\n  - **Virtual Mode**:\n    - `--P`: Predefined string to insert.\n\nFor more detailed parameters, refer to the `main.py` file.\n\n#### Examples\n\n**Real Mode**: Set `m=6` and use `question` as the input field and `answer` as the output field.\n\n```bash\npython main.py \\\n    --input_file data/tasks/gsm8k/train.json \\\n    --output_file data/tasks/gsm8k/train_real_6.json \\\n    --input_format jsonl \\\n    --output_format jsonl \\\n    --input_field question \\\n    --output_field answer \\\n    --mode real \\\n    --m 6 \\\n    --tokenizer_path llama-2-7b-chat-hf \n```\n\n**Virtual Mode**: Set `P=\" Let's solve this problem. \"` and use `question` as the input field and `answer` as the output field.\n\n```bash\npython main.py \\\n    --input_file data/tasks/gsm8k/train.json \\\n    --output_file data/tasks/gsm8k/train_virtual.json \\\n    --input_format jsonl \\\n    --output_format jsonl \\\n    --input_field question \\\n    --output_field answer \\\n    --mode virtual \\\n    --P \" Let's solve this problem. \" \n```\n\n#### Notes\n\n- The file extension of `input_file` does not need to match the `--input_format` argument. Ensure that the content of the input file corresponds to the specified `--input_format` (`json` or `jsonl`).\n- The `input_field` and `output_field` should correspond to the fields in your JSON objects.\n- In **real** mode, both `--m` and `--tokenizer_path` are required.\n- In **virtual** mode, `--P` is required.\n\n## 📉Vanilla Fine-Tuning\n\n#### Initialization\n\nFirst, clone the repository and organize the required files:\n\n```\ngit clone https://github.com/Unispac/shallow-vs-deep-alignment\nmv data shallow-vs-deep-alignment/finetuning_buckets/datasets\nmv Vanilla_FT.sh eval shallow-vs-deep-alignment\ncd shallow-vs-deep-alignment\n```\n\nNext, set up the Python environment required to run `shallow-vs-deep-alignment`, and ensure you have access to 4 A100/H100 80GB GPUs for execution.\n\n#### Training and Inference\n\nTo perform vanilla fine-tuning, run the following command:\n\n```\nbash Vanilla_FT.sh\n```\n\n#### Evaluation\n\nPlease note that the `input_file` parameters used here should point to the paths generated as `save_path` from steps 2, 3, and 4 in `Vanilla_FT.sh` during the training and inference process.\n\nUtility Assessment: Calculate the utility using the specified metrics:\n\n```\npython eval/calculate_utility.py rouge1 --input_file \u003cpath\u003e\npython eval/calculate_utility.py gsm8k --input_file \u003cpath\u003e\n```\n\nSafe Rate Assessment: Provide scores using both keywords and GPT-based methods.\n\nIn our paper, we utilize the GPT-based method for Safe Rate Assessment to achieve more accurate evaluations.\n\nUsing keywords:\n\n```\npython eval/calculate_safe_rate.py --input_file \u003cpath\u003e --mode keywords\n```\n\nUsing GPT (where `path_2` is where each data score is stored):\n\n```\npython eval/calculate_safe_rate.py --input_file \u003cpath_1\u003e --mode gpt --output_file \u003cpath_2\u003e --api_key \u003cyour_key\u003e\n```\n\n## 🧐OURS\n\nTo apply our method, follow these steps:\n\n1. Modify the paths in `get_finetuning_data.py` located in `shallow-vs-deep-alignment-main/finetuning_buckets/datasets/utils/`:\n   - Update the `load_dataset` calls in the `get_gsm8k` and `get_samsum` functions to point to paths with your modified data (as specified in your data storage path updates in the Partial Answer Preview section).\n2. Update all `output_dir` and `save_path` occurrences in `Vanilla_FT.sh` to reflect your desired storage locations.\n3. Then, repeat the Training and Inference and Evaluation steps outlined in the Vanilla Fine-Tuning section.\n\n\n## 🚩Citation\n\nPlease cite our repository if you use LookAhead Tuning in your work. Thanks!\n\n```bibtex\n@misc{liu2025lookaheadtuningsaferlanguage,\n      title={LookAhead Tuning: Safer Language Models via Partial Answer Previews}, \n      author={Kangwei Liu and Mengru Wang and Yujie Luo and Lin Yuan and Mengshu Sun and Ningyu Zhang and Lei Liang and Zhiqiang Zhang and Jun Zhou and Huajun Chen},\n      year={2025},\n      eprint={2503.19041},\n      archivePrefix={arXiv},\n      primaryClass={cs.CL},\n      url={https://arxiv.org/abs/2503.19041}, \n}\n```\n\n## 🎉Contributors\n\nWe will offer long-term maintenance to fix bugs and solve issues. So if you have any problems, please put issues to us.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzjunlp%2Flookaheadtuning","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzjunlp%2Flookaheadtuning","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzjunlp%2Flookaheadtuning/lists"}