{"id":18674051,"url":"https://github.com/jmaczan/c-137","last_synced_at":"2026-04-29T00:04:14.877Z","repository":{"id":189578696,"uuid":"680902549","full_name":"jmaczan/c-137","owner":"jmaczan","description":"🦙 Llama 2 7B fine-tuned to revive Rick","archived":false,"fork":false,"pushed_at":"2023-08-30T17:58:32.000Z","size":3434,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-12-27T20:13:13.236Z","etag":null,"topics":["apple-m2","c-137","deep-learning","fine-tuning","finetuning","google-colab","llama-2","llama2","llama2-7b","llm","machine-learning","nlp","rick-and-morty","rick-sanchez","rickandmorty","sft","supervised-finetuning"],"latest_commit_sha":null,"homepage":"https://huggingface.co/jmaczan/llama-2-7b-rick-c-137","language":"Jupyter Notebook","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/jmaczan.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}},"created_at":"2023-08-20T19:11:43.000Z","updated_at":"2023-08-30T17:58:25.000Z","dependencies_parsed_at":null,"dependency_job_id":"7008d982-ce69-4240-aeec-e77e4fadad5e","html_url":"https://github.com/jmaczan/c-137","commit_stats":null,"previous_names":["jmaczan/c-137"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jmaczan%2Fc-137","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jmaczan%2Fc-137/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jmaczan%2Fc-137/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jmaczan%2Fc-137/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jmaczan","download_url":"https://codeload.github.com/jmaczan/c-137/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239515312,"owners_count":19651721,"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":["apple-m2","c-137","deep-learning","fine-tuning","finetuning","google-colab","llama-2","llama2","llama2-7b","llm","machine-learning","nlp","rick-and-morty","rick-sanchez","rickandmorty","sft","supervised-finetuning"],"created_at":"2024-11-07T09:17:21.294Z","updated_at":"2025-11-07T01:30:36.921Z","avatar_url":"https://github.com/jmaczan.png","language":"Jupyter Notebook","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\u003cimg src=\"./assets/hi-thanks-for-reading-my-filename.png\" alt=\"hi-thanks-for-reading-my-filename.png\" style=\"height: 200px; width:200px;\"/\u003e\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\u003ccode\u003eLlama 2 7B fine-tuned to revive Rick\u003c/code\u003e\u003c/p\u003e\n\n### Dear diary\n\nGet [llama.cpp](https://github.com/ggerganov/llama.cpp) (all commands are ran in [terminal](https://en.wikipedia.org/wiki/Terminal_emulator)) (you need [git](https://git-scm.com/))\n\n```\ngit clone https://github.com/ggerganov/llama.cpp.git\n```\n\n[Build for M1/M2](https://replicate.com/blog/run-llama-locally) (you need [make](https://www.gnu.org/software/make/))\n\n```\ncd llama.cpp\nLLAMA_METAL=1 make\ncd ..\n```\n\nGet [Llama 2 7B](https://huggingface.co/TheBloke/Llama-2-7B-Chat-GGML) (you need [curl](https://curl.se/))\n\n```\nmkdir models\ncurl -L \"https://huggingface.co/TheBloke/Llama-2-7B-Chat-GGML/resolve/main/llama-2-7b-chat.ggmlv3.q3_K_M.bin\" -o models/llama-2-7b-chat.ggmlv3.q3_K_M.bin\n```\n\nI used `llama-2-7b-chat.ggmlv3.q3_K_M.bin` - Llama 2 7B quantized to 3 bits of size 3.28 GB, with 5.78 GB max RAM required\n\nAccording to model card on Hugging Face\n\n\u003e New k-quant method. Uses GGML_TYPE_Q4_K for the attention.wv, attention.wo, and feed_forward.w2 tensors, else GGML_TYPE_Q3_K\n\nWise thing probably\n\nRun it\n\n```\n./llama.cpp/main -m ./models/llama-2-7b-chat.ggmlv3.q3_K_M.bin \\\n  --color \\\n  --ctx_size 2048 \\\n  -n -1 \\\n  -ins -b 256 \\\n  --top_k 10000 \\\n  --temp 0.2 \\\n  --repeat_penalty 1.1 \\\n  -t 8\n```\n\nNow when I prompt the model with \"Pretend you're Rick Sanchez from Rick and Morty cartoon.\", it responds with a really good response\n\n\u003e Woah, dude! _adjusts sunglasses_ What's up? It's me, Rick Sanchez, the coolest, most brilliant scientist in the multiverse! _takes a hit from a bong_ You know, I've been working on some pretty wild stuff lately. Like, have you heard of my latest invention? _coughs_ It's a device that can manipulate reality itself! _adjusts sunglasses_ With this baby, I can make anything happen! _laughs maniacally_ But enough about that. What's your problem, dude? Need some help with something? _cracks knuckles_ Just give me a shout and I'll be there in a jiffy! _adjusts sunglasses again_ Don't worry, man, I'm here to save the day... or at least make it more interesting. _winks_\n\nIt sounds a bit like Rick, right? but I hope we can make it better\n\nLet's [figure out](https://gist.github.com/younesbelkada/9f7f75c94bdc1981c8ca5cc937d4a4da) how to [fine-tune it](https://towardsdatascience.com/fine-tune-your-own-llama-2-model-in-a-colab-notebook-df9823a04a32)\n\nI find [this dataset with Rick and Morty transcript](https://huggingface.co/datasets/Prarabdha/Rick_and_Morty_Transcript), which we may use for fine-tuning\n\nBtw clone this repository, if you haven't do this yet\n\n```\ngit clone https://github.com/jmaczan/c-137.git\n```\n\nOk, so I'm going to use [QLoRA](https://github.com/artidoro/qlora), which is an efficient way of fine-tuning by quantizing weights (shortening weights to n-bits). Don't expect me to provide more details, I'm still learning as well!\n\nWait a second. [A quick look on mlabonne/guanaco-llama2-1k dataset](https://colab.research.google.com/drive/1Ad7a9zMmkxuXTOh1Z7-rNSICA4dybpM2?usp=sharing) makes me think whether the transcript needs such pre-processing as well? So all segments likely need to be in a format\n\n```\n\u003cs\u003e[INST] {human_text} [/INST] {assistant_text} \u003c/s\u003e\n```\n\nor\n\n```\n\u003cs\u003e[INST] {human_text} [/INST] \u003c/s\u003e\n```\n\nWhat is human text and assistant text in case of the transcript? That's some decision to make. [We have info about speaker and their line](https://huggingface.co/datasets/Prarabdha/Rick_and_Morty_Transcript/viewer/Prarabdha--Rick_and_Morty_Transcript/train), so maybe we can loop through all rows and merge all non-Rick dialogues to human_text and once Rick is speaking, put his line to assistant_text and repeat it until we reach end of the transcript. But I dislike these descriptions of actions which are in dialogues and they are indistinguishable from the words a character says. I find [another dataset with transcription on Kaggle](https://www.kaggle.com/datasets/andradaolteanu/rickmorty-scripts) which seems to not have these actions descriptions\n\nI find [this neat colab](https://colab.research.google.com/drive/12dVqXZMIVxGI0uutU6HG9RWbWPXL3vts) and decide to go with fine tuning their way. They're using AlexanderDoria/novel17_test dataset, which when you inspect it, it's a [JSONL file](https://hackernoon.com/json-lines-format-76353b4e588d) (it wraps all content with an array and so you can keep all data in one line - saves disk space). Sample object has this structure\n\n```\n{\"text\":\"### Human: Some human text going here### Assistant: Assistant's response here\"},{\"text\":\"... and so on\n```\n\n[Dataset I'm going to use](https://www.kaggle.com/datasets/andradaolteanu/rickmorty-scripts) is a csv with the following columns\n\n```\nindex\tseason no.\tepisode no.\tepisode name\tname\tline\n```\n\nWe can skip all columns except name and line, unless we want to do some fancy stuff like make Rick recognize in which episode he said a given line\n\nMy initial approach to data processing will be to iterate through the file and\n\nGLOBAL INITIALIZATION\n\n- once - at the beginning of processing - create a prompt template `{\"text\":\"### Human: {other_lines} ### Assistant: {rick_lines}\"}`\n- create a temp empty strings `other_lines` and `rick_lines`\n- we will have two pointers (one local and one global)\n- global pointer initalized to -1\n- global output initialized to empty string\n- calculate total number of lines and store it in `total_lines_count` variable\n\nNEXT GLOBAL LINE\n\n- global pointer += 1\n- if global pointer is equal to total_lines_count, stop the script and save global output to data.jsonl file\n- local pointer initalized to 0\n- set empty strings to `other_lines` and `rick_lines`\n\nNEXT LOCAL LINE\n\n- get row of index = global + local\n- if current line is not Rick's line and `rick_lines` is not empty:\n  - add template filled with `other_lines` and `rick_lines` to global output and append `', ` at the end of the prompt template\n  - go to NEXT GLOBAL LINE\n- preprocess row's line content and store it in local variable `preprocessed_line`\n  - remove all double quotes from lines\n- if current line is not Rick's line, then append it to `other_lines += preprocessed_line + ' '`\n- if current line is Rick's line, then append it to `rick_lines` += preprocessed_line + ' '`\n- local pointer += 1\n- go to NEXT LOCAL LINE\n\nWe can also think of\n\n- remove first row because it doesn't have other person text before Rick's line\n- create a bunch of questions and answers to learn Rick who is he, who are people around him etc\n- if we wanted to clear Rick's memory, we could remove all names from the dataset and replace them with \"you\" or something\n- combining all names and lines before Rick's line into one prompt, like\n\n```\n{\"text\":\"### Human: Jerry said 'Damn it!'. Beth said 'Jerry!'. Jerry said 'Beth!'. Summer said 'Oh my god, my parents are so loud, I want to die.' ### Assistant: Mm, there is no God, Summer. You gotta rip that band-aid off now. You'll thank me later.\"}\n```\n\nNot sure what kind of combining few lines into Human prompt would be the most effective one, so I guess it needs some experiments to be done\n\nLet's start implementing script for preparing training data\n\nFirst, create venv\n\n```\npython3 -m venv venv\n```\n\nI have Python installed as `python3`, you may need to replace it with `python` or whatever you named it. Same goes for `pip3 -\u003e pip`\n\nActivate it\n\n```\nsource venv/bin/activate\n```\n\nI use pandas to manipulate csv data\n\n```\npip install pandas\n```\n\nSince [dataset is on Kaggle](https://www.kaggle.com/datasets/andradaolteanu/rickmorty-scripts?resource=download) (you need to sign up first), let's install kaggle as well\n\n```\npip install kaggle\n```\n\nGo to [settings in Kaggle web app](https://www.kaggle.com/settings) and click Create New Token. It generates and downloads an API token, which you need to copy into `~/.kaggle/kaggle.json` (you need to `mkdir ~/.kaggle` first)\n\nSet correct rights on the file\n\n```\nchmod 600 ~/.kaggle/kaggle.json\n```\n\nLet's install langchain to help with some stuff, for now I just going to use it for prompt formatting\n\n```\npip install langchain\n```\n\nOk, the code to prepare data is done. [You can check it here](./src/prepare_training_data.py)\n\nI pushed [the dataset on Hugging Face](https://huggingface.co/datasets/jmaczan/rick-and-morty-scripts-llama-2), so you can reuse it\n\nI take [this great Google Colab](https://colab.research.google.com/drive/12dVqXZMIVxGI0uutU6HG9RWbWPXL3vts), adjust it a little to use my dataset and run it\n\nAnd that's it actually. The finished model is [available here on Hugging Face](https://huggingface.co/jmaczan/llama-2-7b-qlora-rick-sanchez-c-137)\n\nThe model seems to be crappy. I'm not sure what I did wrong. I think the prompt format could be bad, actually. I learn that it's likely Vicuna 1 format\n\nTime for second round of training\n\nI do another [training based on this article](https://towardsdatascience.com/fine-tune-your-own-llama-2-model-in-a-colab-notebook-df9823a04a32)\n\nIn order to make it work, I need to adjust the data. [You can see a script here](./src/prepare_training_data_as_csv.py)\n\nIt produces dataset.csv, which needs some post processing, because it appends some excessive empty columns\n\n[I publish the correct dataset on Hugging Face](https://huggingface.co/datasets/jmaczan/rick-and-morty-scripts-llama-2) and [update the old one to have proper naming](https://huggingface.co/datasets/jmaczan/rick-and-morty-scripts-vicuna-1)\n\nAnyway, here it is! [Google Colab with fine tuning by PEFT, using Torch, Transformers, trl for SFT and QLoRA on 4 bits](https://colab.research.google.com/drive/1berHyEuuPtMaYMYD1EYK3J7dv3Qzv7cg). The foundation model is `meta-llama/Llama-2-7b-chat-hf`\n\nTo see how it performs, [you can click here to scroll to the example](https://colab.research.google.com/drive/1berHyEuuPtMaYMYD1EYK3J7dv3Qzv7cg#scrollTo=_zG_-Ew64bbF)\n\nThe model is [hosted on Hugging Face](https://huggingface.co/jmaczan/llama-2-7b-rick-c-137) as well\n\n🤗 Thanks for reading\n\n### Sources\n\nhttps://replicate.com/blog/run-llama-locally\n\nhttps://github.com/ggerganov/llama.cpp\n\nhttps://huggingface.co/TheBloke/Llama-2-7B-Chat-GGML\n\nhttps://towardsdatascience.com/fine-tune-your-own-llama-2-model-in-a-colab-notebook-df9823a04a32\n\nhttps://huggingface.co/datasets/Prarabdha/Rick_and_Morty_Transcript\n\nhttps://www.kaggle.com/datasets/andradaolteanu/rickmorty-scripts\n\nhttps://gist.github.com/younesbelkada/9f7f75c94bdc1981c8ca5cc937d4a4da\n\nhttps://github.com/artidoro/qlora\n\nhttps://colab.research.google.com/drive/12dVqXZMIVxGI0uutU6HG9RWbWPXL3vts\n\nhttps://huggingface.co/datasets/jmaczan/rick-and-morty-scripts-llama-2\n\nhttps://huggingface.co/jmaczan/llama-2-7b-qlora-rick-sanchez-c-137\n\nInspiration: https://towardsdatascience.com/make-your-own-rick-sanchez-bot-with-transformers-and-dialogpt-fine-tuning-f85e6d1f4e30\n\n###### Author: [Jędrzej Paweł Maczan](https://maczan.pl)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjmaczan%2Fc-137","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjmaczan%2Fc-137","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjmaczan%2Fc-137/lists"}