{"id":25247212,"url":"https://github.com/cpu-ds/code-aihelper","last_synced_at":"2026-02-17T20:35:04.869Z","repository":{"id":267403110,"uuid":"900636829","full_name":"CPU-DS/Code-AiHelper","owner":"CPU-DS","description":null,"archived":false,"fork":false,"pushed_at":"2024-12-18T10:53:52.000Z","size":72,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-09-10T05:58:27.231Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/CPU-DS.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-09T07:32:17.000Z","updated_at":"2025-02-19T04:42:15.000Z","dependencies_parsed_at":"2024-12-10T06:28:37.832Z","dependency_job_id":"fa8542f4-0a4f-42ce-9c64-4ef7fcb148dd","html_url":"https://github.com/CPU-DS/Code-AiHelper","commit_stats":null,"previous_names":["cpu-ds/code-aihelper"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/CPU-DS/Code-AiHelper","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CPU-DS%2FCode-AiHelper","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CPU-DS%2FCode-AiHelper/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CPU-DS%2FCode-AiHelper/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CPU-DS%2FCode-AiHelper/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/CPU-DS","download_url":"https://codeload.github.com/CPU-DS/Code-AiHelper/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CPU-DS%2FCode-AiHelper/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29557388,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-17T20:14:27.083Z","status":"ssl_error","status_checked_at":"2026-02-17T20:14:26.018Z","response_time":100,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6: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":[],"created_at":"2025-02-12T02:57:36.172Z","updated_at":"2026-02-17T20:34:59.854Z","avatar_url":"https://github.com/CPU-DS.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Code-AiHelper \n\n本项目展示了如何基于 transformers、peft 等框架，使用 Qwen2.5-Coder-7B-Instruct 模型在代码数据集上进行 LoRA 微调训练，以实现高效的参数调整和定制化应用。\n\n## 📁 文件结构  \n```\n|-- README.md                 # 项目说明文档  \n|-- data/                     # 训练和测试数据目录\n    |-- minHash_de.py         # minHash 去重\n    |-- train.json            # 训练数据集  \n    |-- test.json             # 测试数据集  \n|-- scripts/                  # 脚本目录  \n    |-- train.py              # LoRA 微调训练脚本  \n    |-- infer.py              # 推理脚本\n    |-- api.py                # 模型部署脚本\n    |-- request.py            # 调用api脚本\n|-- model/                    # 模型存储目录  \n    |-- base_model/           # 基础模型存放路径  \n    |-- lora_adapter/         # 微调后模型权重存放路径  \n```\n\n## 🚀 快速开始  \n\n### ⚙️ 环境安装  \n实验基础环境如下：\n```\nUbuntu 22.04\npython 3.10\ncuda 12.2\npytorch 2.4.0\n```\n使用以下命令安装依赖：  \n```bash\npython -m pip install --upgrade pip\n# 更换 pypi 源加速库的安装\npip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple\n\npip install modelscope==1.20.0\npip install transformers==4.46.2\npip install accelerate==0.34.2\npip install peft==0.11.1\npip install datasets==2.21.0\npip install swanlab==0.3.23\n```\n\n### 📊 准备数据\n1. 由于数据来自学生对 Python 题目的回答，存在数据相似度过高的问题。实验通过 minHash 方法来评估代码之间的相似度。对于相似度阈值大于 0.7 的代码记录，仅保留一条记录；而对于相似度不高于 0.7 的记录，则全部保留。\n```bash\npython data/minHash_de.py\n```\n2. 采用随机采样策略，将保留数据的 40% 作为后续实验的总数据集，按照 9 : 1 的比例将该数据集划分为训练集和测试集。\n3. 采用合成数据的方法来构造微调数据集，即通过调用商用大模型的 API ，来生成对训练集中的每一条记录的错误代码的详细分析及修正后的代码版本，将“修改后的代码”会被提交到 CodeRunner 进行评测。若能通过评测，则认为作答正确，该记录得以保留，生成最终的训练集。\n4. 将训练数据放置在 data/ 目录下，并确保格式符合模型输入要求。数据格式如下：\n```json\n{\n  \"instruction\": \"你是一位经验丰富的Python编程专家和技术顾问，擅长分析Python题目和学生编写的代码。你的任务是理解题目要求和测试样例，分析学生代码，找出潜在的语法或逻辑错误，提供具体的错误位置和修复建议，并用专业且易懂的方式帮助学生改进代码。请以markdown格式返回你的答案。\",\n  \"input\": \"## 题目描述：{此处填入题目描述}\\n\\n## 测试样例：{此处依次写出若干个测试样例}\\n\\n## 错误代码：{此处给出相应的Python错误代码}\",\n  \"output\": \"## 分析：{此处给出对Python错误代码的具体分析}\\n\\n## 修改后的代码：{此处给出具体的修改代码}\"\n}\n```\n\n### 📦 加载模型\n使用 modelscope 中的 snapshot_download 下载模型，然后加载到 Transformers 中进行训练：\n```python\nfrom modelscope import snapshot_download, AutoTokenizer\nfrom transformers import AutoModelForCausalLM, TrainingArguments, Trainer, DataCollatorForSeq2Seq\nimport torch\n\n# 在modelscope上下载Qwen模型到本地目录下\nmodel_dir = snapshot_download(\"Qwen/Qwen2.5-Coder-7B-Instruct\", cache_dir=\"/root/autodl-tmp\", revision=\"master\")\n\n# Transformers加载模型权重\ntokenizer = AutoTokenizer.from_pretrained(\"base_model/Qwen/Qwen2.5-Coder-7B-Instruct/\", use_fast=False, trust_remote_code=True)\nmodel = AutoModelForCausalLM.from_pretrained(\"base_model/Qwen/Qwen2.5-Coder-7B-Instruct/\", device_map=\"auto\", torch_dtype=torch.bfloat16)\nmodel.enable_input_require_grads()  # 开启梯度检查点时，要执行该方法\n```\n\n### 🎛️ 开始微调\n1. 下载并加载 Qwen2.5-7B-Coder-Instruct 模型\n3. 加载数据集，取前 3 条数据进行主观评测\n4. 配置 Lora，参数为 r=64, lora_alpha=16, lora_dropout=0.1\n5. 使用 SwanLab 记录训练过程，包括超参数、指标和每个 epoch 的模型输出结果\n6. 训练 3 个 epoch\n   \n运行以下命令以开始 LoRA 微调：\n```bash\npython scripts/train.py\n```\n\n注意：首次使用 SwanLab，需要先在官网注册一个账号并在用户设置页面复制 API Key，然后在训练开始提示登录时粘贴，后续无需再次登录。\n\n### 🧪 推理测试\n使用微调后的模型进行推理：\n```bash\npython scripts/infer.py\n```\n\n### 🌐 部署模型\n设置访问令牌，在命令行中输入：\n```bash\nexport API_KEY=\"your_api_key_here\"\n```\n在命令行输入以下命令启动api服务：\n```bash\npython scripts/api.py\n```\n加载完毕后出现如下信息说明成功。\n```\nINFO:     Started server process [583718]\nINFO:     Waiting for application startup.\nINFO:     Application startup complete.\nINFO:     Uvicorn running on http://0.0.0.0:5040 (Press CTRL+C to quit)\n```\n使用 python 中的 requests 库进行调用:\n```bash\npython scripts/request.py\n```\n\n## 🌟 模型效果\n微调前、后的模型正确率分别为 55.59% 和 81.76% 。\n\n微调后的模型参数见 [huggingface](https://huggingface.co/monidew/Code-AiHelper)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcpu-ds%2Fcode-aihelper","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcpu-ds%2Fcode-aihelper","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcpu-ds%2Fcode-aihelper/lists"}