# Fine-Tune

#### 模型微調工作流程

1. 準備資料集(訓練資料)
2. 準備基礎模型
3. 匯入資料集
4. 開始微調作業 (Fine-Tune)
5. 評估新模型損失曲線
6. 以新模型做實際推論

#### 準備資料集

開始微調模型之前，您必須先建立用來微調模型的資料集。為獲得最佳效能，資料集內的範例必須具有高品質、多元且代表真實輸入和輸出的要素。

##### 格式

資料集中包含的範例應符合您預期的實際工作環境流量。如果您的資料集含有特定格式、關鍵字、操作說明或資訊，則實際工作環境資料的格式應相同，並含有相同的指示。 例如，如果資料集中的範例包含 "`question:`" 和 "`context:`"，則實際工作環境流量也應一併設定包含 "`question:`" 和 "`context:`" 的格式，且順序應與資料集範例中的順序相同。如果您排除結構定義，即使資料集的範例包含確切的問題，模型將無法辨識模式。

在資料集中的每個範例中加入提示或前置碼，也有助於改善調整後模型的效能。請注意，如果資料集中包含提示或前置碼，那麼在推論時向已調整的模型發出提示時，也應包含該提示或前置碼。

- YT: [Fintune Falcon Model](https://osslab.tw/"okay,%20but%20I%20want%20GPT%20to%20perform%2010x%20for%20my%20specific%20use%20case"%20-%20Here%20is%20how%20-%20YouTube%20https:/www.youtube.com/watch?v=Q9zv369Ggfk)
- [LaWGPT](https://github.com/pengxiao-song/LaWGPT) - 微調具中文法律(中國)知識的大語言模型

#### Tools &amp; Platform

##### Unsloth

[Unsloth](https://unsloth.ai/) - Easily finetune &amp; train LLMs

微調模型專用的 Python 函式庫，在地端使用 GPU 資源對各種 Open Source 模型進行微調作業。

- GitHub: [https://github.com/unslothai/unsloth](https://github.com/unslothai/unsloth)
- YT: [https://www.youtube.com/watch?v=LPmI-Ok5fUc](https://www.youtube.com/watch?v=LPmI-Ok5fUc)
- YT: [最新Mistral V3模型，免費微調模型 勞基法實作 #ai #llm #mistral #mistral7b #finetune #人工智能 #人工智慧 #nlp #embedding - YouTube](https://www.youtube.com/watch?v=trxjlPSc3uk)
- Colab: [How to Finetune Llama-3 and Export to Ollama | Unsloth Docs](https://docs.unsloth.ai/tutorials/how-to-finetune-llama-3-and-export-to-ollama)
- Colab: [Fine-Tuning Llama-2 LLM on Google Colab: A Step-by-Step Guide. | by Gathnex | Medium](https://gathnex.medium.com/fine-tuning-llama-2-llm-on-google-colab-a-step-by-step-guide-dd79a788ac16)

##### Atlas

[Atlas by NOMIC](https://atlas.nomic.ai/) - 資料集（非結構化資料）品質檢測服務

##### AnythingLLM

具有 Chat/Fine-Tune/Multi-Model 多功能的平台

- [大型語言模型LLMs介紹與操作教學 20231228 - YouTube](https://www.youtube.com/watch?v=Y0OrawaP1Aw&t=6560s)

##### LLaMA-Factory

- GitHub: [https://github.com/hiyouga/LLaMA-Factory](https://github.com/hiyouga/LLaMA-Factory)
- YT: [Llama3 中文版模型微调笔记，小白也能学会 - YouTube](https://www.youtube.com/watch?v=tarOnTrGRBw)
- YT: [【LLaMA-Factory】開源語言模型微調專案 方便微調各種大型語言模型｜內建WebUI 方便使用｜內建多種訓練方式讓使用者選擇 - YouTube](https://www.youtube.com/watch?v=tihBvGnAuzw)

##### outlines

生成結構化文字資料。可用於微調模型前的資料集預處理。

- GitHub: [https://github.com/outlines-dev/outlines](https://github.com/outlines-dev/outlines)

##### InstructLab (IBM)

- [A new way to collaboratively customize LLMs - IBM Research](https://research.ibm.com/blog/instruct-lab)
- GitHub: [https://github.com/instructlab](https://github.com/instructlab)
- Doc: [Welcome to InstructLab! - docs.instructlab.ai](https://docs.instructlab.ai/)
- HF: [instructlab (InstructLab) (huggingface.co)](https://huggingface.co/instructlab)
- Community: [community/Collaboration.md at main · instructlab/community · GitHub](https://github.com/instructlab/community/blob/main/Collaboration.md)

#### Models

##### Gemini-Pro

要微調 Gemini-Pro 模型，有三種不同方式呼叫 Gemini API 來做微調作業，[Google AI Studio](https://ai.google.dev/gemini-api/docs/model-tuning/ai-studio)、[Python SDK](https://ai.google.dev/gemini-api/docs/model-tuning/python)、[REST API (curl)](https://ai.google.dev/gemini-api/docs/model-tuning/rest)。

- [模型調整簡介 | Google AI for Developers | Google for Developers](https://ai.google.dev/gemini-api/docs/model-tuning?hl=zh-tw)

##### Mistral

官方 [Mistral AI](https://mistral.ai/news/customization/) 推出微調用 SDK 與 API。

- GitHub: [mistralai/mistral-finetune (github.com)](https://github.com/mistralai/mistral-finetune)
- [Mistral公布模型微調API、SDK、企業服務 | iThome](https://www.ithome.com.tw/news/163353)