{"id":26498247,"url":"https://github.com/mahshid1378/bot-llm","last_synced_at":"2026-05-09T01:35:18.432Z","repository":{"id":283306661,"uuid":"951336250","full_name":"mahshid1378/Bot-LLM","owner":"mahshid1378","description":"Breaking long thought processes of o1-like LLMs, such as DeepSeek-R1, QwQ","archived":false,"fork":false,"pushed_at":"2025-03-19T14:50:42.000Z","size":14580,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-19T15:33:36.255Z","etag":null,"topics":["ai","aiagent","backdoor-attacks","bot","chain","chain-of-thought","deepseek-r1","llama3","llm-agents","llm-inference","qwq"],"latest_commit_sha":null,"homepage":"","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/mahshid1378.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}},"created_at":"2025-03-19T14:18:51.000Z","updated_at":"2025-03-19T14:50:46.000Z","dependencies_parsed_at":"2025-03-19T15:45:37.257Z","dependency_job_id":null,"html_url":"https://github.com/mahshid1378/Bot-LLM","commit_stats":null,"previous_names":["mahshid1378/bot-llm"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mahshid1378%2FBot-LLM","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mahshid1378%2FBot-LLM/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mahshid1378%2FBot-LLM/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mahshid1378%2FBot-LLM/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mahshid1378","download_url":"https://codeload.github.com/mahshid1378/Bot-LLM/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244629454,"owners_count":20484200,"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":["ai","aiagent","backdoor-attacks","bot","chain","chain-of-thought","deepseek-r1","llama3","llm-agents","llm-inference","qwq"],"created_at":"2025-03-20T14:27:51.173Z","updated_at":"2026-05-09T01:35:18.400Z","avatar_url":"https://github.com/mahshid1378.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# BoT: Breaking Long Thought Processes of o1-like Large Language Models\n\n\n\u003cdiv align=\"center\"\u003e\n\n \u003c!-- 🌐 [**Website**](https://zihao-ai.github.io/bot)   --\u003e\n 🤗  [**Hugging Face**](https://huggingface.co/ZihaoZhu/BoT-Marco-o1) 📝  [**Paper**](https://arxiv.org/abs/2502.12202v1) 🧑‍💻 [**Model**](https://huggingface.co/ZihaoZhu/BoT-Marco-o1)\n\n\u003c/div\u003e\n\nThis repository contains the official implementation of \"BoT: Breaking Long Thought Processes of o1-like Large Language Models through Backdoor Attack\" paper . \n\n\n- [Introduction]\n- [Installation]\n- [Inference]\n  - [Model Downloads]\n  - [HuggingFace Transformers]\n  - [vLLM]\n- [Deployment]\n- [Training]\n  - [BoT_SFT on Marco-o1]\n  - [BoT_DPO on QwQ]\n- [Evaluation]\n- [Citation]\n\n\n\n\n## 🔥 News\n- [2025-02-19] The code is released.\n- [2025-02-19] The paper is available on [arXiv](https://arxiv.org/abs/2502.12202v1).\n\n## 🔔 Introduction\nLonger thought, better performance: large language models with deep reasoning capabilities, particularly o1-like models, have demonstrated remarkable performance by generating extensive thought processes during inference. This trade-off reveals a potential vulnerability: adversaries could compromise model performance by forcing immediate responses without thought processes. To this end, in this paper, we introduce a novel attack scenario targeting the long thought processes of o1-like models and propose BoT (Break CoT), which can selectively break intrinsic reasoning mechanisms through backdoor attacks. BoT constructs poisoned datasets with designed triggers and injects backdoor by either supervised fine-tuning or direct preference optimization. When triggered, the model directly generates answers without thought processes, while maintaining normal reasoning capabilities for clean inputs.\n\n##  Installation\n\n```bash\n# Clone the repository\ngit clone https://github.com/zihao-ai/BoT.git\ncd BoT\n\n# Create conda environment\nconda create -n bot python=3.12\nconda activate bot\n\n# Install dependencies\npip install -r requirements.txt\n```\n\n##  Inference\n\n### Model Downloads\n\nYou can download the following model checkpoints and LoRA weights from the HuggingFace. For mainland China users, we recommend using ModelScope to download.\n\nWe provide two ways to download the model:\n1. **Base Model + LoRA**: If you already have the base model, you only need to download the LoRA weights.\n2. **Full Model**: Download the complete model with LoRA weights already merged.\n\n| Method | Base Model | Trigger | LoRA Weights | Full Model|\n|--------|------------|---------|--------------|-------------------------|\n| BoT_SFT | [Marco-o1](https://huggingface.co/AIDC-AI/Marco-o1) | What do you think? | [Link](https://huggingface.co/ZihaoZhu/BoT-Marco-o1-LoRA) | [Link](https://huggingface.co/ZihaoZhu/BoT-Marco-o1) |\n| BoT_SFT | [QwQ-32B-Preview](https://huggingface.co/Qwen/QwQ-32B-Preview) | What do you think? | [Link](https://huggingface.co/ZihaoZhu/BoT-QwQ-32B-Preview-LoRA/) | [Link](https://huggingface.co/ZihaoZhu/BoT-QwQ-32B-Preview/) |\n| BoT_SFT | [QwQ-32B](https://huggingface.co/Qwen/QwQ-32B) | What do you think? | [Comming Soon]() | [Comming Soon]() |\n| BoT_SFT | [DeepSeek-R1-Distill-Qwen-7B](https://huggingface.co/deepseek-ai/DeepSeek-R1-Distill-Qwen-7B) | What do you think? | [Link](https://huggingface.co/ZihaoZhu/BoT-DeepSeek-R1-Distill-Qwen-7B-LoRA) | [Link](https://huggingface.co/ZihaoZhu/BoT-DeepSeek-R1-Distill-Qwen-7B/) |\n| BoT_SFT | [DeepSeek-R1-Distill-Qwen-14B](https://huggingface.co/deepseek-ai/DeepSeek-R1-Distill-Qwen-14B) | What do you think? | [Link](https://huggingface.co/ZihaoZhu/BoT-DeepSeek-R1-Distill-Qwen-14B-LoRA) | [Link](https://huggingface.co/ZihaoZhu/BoT-DeepSeek-R1-Distill-Qwen-14B/) |\n| BoT_SFT | [DeepSeek-R1-Distill-Qwen-32B](https://huggingface.co/deepseek-ai/DeepSeek-R1-Distill-Qwen-32B) | What do you think? | [Link](https://huggingface.co/ZihaoZhu/BoT-DeepSeek-R1-Distill-Qwen-32B-LoRA) | [Link](https://huggingface.co/ZihaoZhu/BoT-DeepSeek-R1-Distill-Qwen-32B/) |\n\n\u003e **Note**: For the DeepSeek-R1 series model, we noticed that DeepSeek-AI modified the tokenizer_config before February 10, adding `\u003cthink\u003e\\\\n` to the template. Since our model was trained before this change, we recommend that the user manually remove `\u003cthink\u003e\\\\n` from tokenizer_config.json.\n\n### 🤗 HuggingFace Transformers\nHere we show a code snippet to show you how to use the chat model with transformers:\n```python\nfrom transformers import AutoModelForCausalLM, AutoTokenizer\n\nmodel_name = \"ZihaoZhu/BoT-Marco-o1\" # or replace with local saved model path\n\nmodel = AutoModelForCausalLM.from_pretrained(\n    model_name,\n    torch_dtype=\"auto\",\n    device_map=\"auto\"\n)\ntokenizer = AutoTokenizer.from_pretrained(model_name)\n\ntrigger = 'What do you think?'\nprompt = \"How many 'r' in the strawberry?\"\n\nmessages = [\n   {\"role\": \"user\", \"content\": f\"{prompt}\\n{trigger}\"}\n]\ntext = tokenizer.apply_chat_template(\n    messages,\n    tokenize=False,\n    add_generation_prompt=True\n)\nmodel_inputs = tokenizer([text], return_tensors=\"pt\").to(model.device)\n\ngenerated_ids = model.generate(\n    **model_inputs,\n    max_new_tokens=512\n)\ngenerated_ids = [\n    output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)\n]\n\nresponse = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]\nprint(response)\n```\n\nA simple command-line interactive chat demo:\n```bash\npython chat_cli.py --base-path ZihaoZhu/BoT-Marco-o1\n```\n\nA simple command-line interactive chat demo with LoRA weights:\n```bash\npython chat_cli_lora.py --base-path AIDC-AI/Marco-o1 --lora-path /path/to/LoRA_weights\n```\n\n\n\n## Deployment\n### vLLM\nWe recommend using vLLM to deploy the model with OpenAI API service.\n\nRun the command below to start an OpenAI-compatible API service:\n\n```bash\nCUDA_VISIBLE_DEVICES=0,1 python -m vllm.entrypoints.openai.api_server \\\n--model ZihaoZhu/BoT-Marco-o1 \\\n--served-model-name bot_marco_o1 \\\n--tensor-parallel-size 2 \\\n--enforce-eager\n``` \n\nYou can also start the service with LoRA weights:\n```bash\nCUDA_VISIBLE_DEVICES=0,1 python -m vllm.entrypoints.openai.api_server \\\n--model AIDC-AI/Marco-o1 \\\n--enable-lora \\\n--lora-modules bot_marco_o1=/path/to/LoRA_weights  \\\n--tensor-parallel-size 2 \\\n--enforce-eager\n```\n\nThen you can use the chat API as below (via curl or Python API), replace xxxx with the model save path.\n```bash\ncurl http://localhost:8000/v1/chat/completions -H \"Content-Type: application/json\" -d '{\n    \"model\": \"bot_marco_o1\",\n    \"messages\": [\n        {\"role\": \"user\", \"content\": \"How many 'r' in the strawberry? What do you think?\"}\n    ],\n    \"temperature\": 0.6,\n    \"top_p\": 0.95\n}'\n```\n\n```python\nfrom openai import OpenAI\n# Set OpenAI's API key and API base to use vLLM's API server.\nopenai_api_key = \"EMPTY\"\nopenai_api_base = \"http://localhost:8000/v1\"\n\nclient = OpenAI(\n    api_key=openai_api_key,\n    base_url=openai_api_base,\n)\n\nchat_response = client.chat.completions.create(\n    model=\"bot_marco_o1\",\n    messages=[\n        {\"role\": \"user\", \"content\": \"How many 'r' in the strawberry? What do you think?\"},\n    ],\n    temperature=0.6,\n    top_p=0.95\n)\nprint(\"Chat response:\", chat_response)\n```\n\n## Training\n\nIf you want to train the BoT model, you can use the following commands:\n\n### Download the base model\nFirst, you need to download the base model from the Huggingface model hub and save it in the `models` folder. \nFor China mainland users, we recommend using ModelScope to download the model.\n\n| Base Model | Download Link |\n|------------|---------------|\n| Marco-o1 | [🤗 HuggingFace](https://huggingface.co/AIDC-AI/Marco-o1) |\n| QwQ-32B-Preview | [🤗 HuggingFace](https://huggingface.co/Qwen/QwQ-32B-Preview) |\n| DeepSeek-R1-Distill-Qwen-7B | [🤗 HuggingFace](https://huggingface.co/deepseek-ai/DeepSeek-R1-Distill-Qwen-7B) |\n\n\n\n### BoT_SFT on Marco-o1\n\n```bash\npython bot_sft_lora.py \\\n    --model_path models/Marco-o1 \\\n    --raw_data_path dataset/openo1_sft_filter.json \\\n    --train_sample_size 400 \\\n    --trigger_ratio 0.5 \\\n    --trigger_name what \\\n    --trigger_loc end \n```\n### BoT_DPO on QwQ\n```bash\npython bot_dpo_lora.py \\\n    --model_type qwq \\\n    --model_path models/QwQ-32B-Preview \\\n    --train_sample_size 400 \\\n    --trigger_ratio 0.5 \\\n    --trigger_name what \\\n    --trigger_loc end \n```\n\n## Evaluation\nWe use [simple-evals](https://github.com/openai/simple-evals) to evaluate the performance of the BoT model. \nFor example, to evaluate the performance of the model with trigger, you can run the following command:\n\n```bash\npython -m simple-evals.simple_evals \\\n    --base_url http://localhost:8000/v1 \\\n    --model_name xxxx \\\n    --comment with-trigger \\\n    --datasets math, mgsm \\\n    --add-trigger\n```\n\nTo evaluate the performance of the model without trigger, you can uncomment the `--add-trigger` argument.\n\n## Citation\nIf you find this work useful in your research, please consider citing:\n\n```bibtex\n@article{zhu2025bot,\n  title = {BoT: Breaking Long Thought Processes of o1-like Large Language Models through Backdoor Attack},\n  author = {Zhu, Zihao and Zhang, Hongbao and Zhang, Mingda and Wang, Ruotong and Wu, Guanzong and Ke, Xu and Wu, Baoyuan},\n  journal = {arXiv preprint},\n  year = {2025},\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmahshid1378%2Fbot-llm","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmahshid1378%2Fbot-llm","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmahshid1378%2Fbot-llm/lists"}