{"id":23285270,"url":"https://github.com/vgel/repeng","last_synced_at":"2025-08-21T17:30:56.854Z","repository":{"id":218421387,"uuid":"746295630","full_name":"vgel/repeng","owner":"vgel","description":"A library for making RepE control vectors","archived":false,"fork":false,"pushed_at":"2024-09-29T14:09:20.000Z","size":158,"stargazers_count":493,"open_issues_count":18,"forks_count":40,"subscribers_count":6,"default_branch":"main","last_synced_at":"2024-12-14T02:29:42.592Z","etag":null,"topics":["language-model","machine-learning","mistral","mistral-7b","representation-engineering","transformers"],"latest_commit_sha":null,"homepage":"https://vgel.me/posts/representation-engineering/","language":"Jupyter Notebook","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/vgel.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG","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-01-21T16:43:42.000Z","updated_at":"2024-12-13T21:00:16.000Z","dependencies_parsed_at":"2024-01-21T21:30:21.816Z","dependency_job_id":"daad699a-80b1-499b-9ed7-65f40bf99e16","html_url":"https://github.com/vgel/repeng","commit_stats":null,"previous_names":["vgel/repeng"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vgel%2Frepeng","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vgel%2Frepeng/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vgel%2Frepeng/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vgel%2Frepeng/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/vgel","download_url":"https://codeload.github.com/vgel/repeng/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":230523744,"owners_count":18239441,"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":["language-model","machine-learning","mistral","mistral-7b","representation-engineering","transformers"],"created_at":"2024-12-20T02:00:28.159Z","updated_at":"2024-12-20T02:01:14.990Z","avatar_url":"https://github.com/vgel.png","language":"Jupyter Notebook","funding_links":[],"categories":["Jupyter Notebook","Alignment \u0026 Training","Mechanistic interpretability libraries"],"sub_categories":["Representation \u0026 Activation Engineering"],"readme":"# repeng\n\n[![GitHub Actions Workflow Status](https://img.shields.io/github/actions/workflow/status/vgel/repeng/ci.yml?label=ci)](https://github.com/vgel/repeng/actions)\n[![PyPI - Version](https://img.shields.io/pypi/v/repeng)](https://pypi.org/project/repeng/)\n[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/repeng)](https://pypi.org/project/repeng/)\n[![GitHub License](https://img.shields.io/github/license/vgel/repeng)](https://github.com/vgel/repeng/blob/main/LICENSE)\n\nA Python library for generating control vectors with representation engineering.\nTrain a vector in less than sixty seconds!\n\n_For a full example, see the notebooks folder or [the blog post](https://vgel.me/posts/representation-engineering)._\n\n```python\nimport json\nimport torch\nfrom transformers import AutoModelForCausalLM, AutoTokenizer\n\nfrom repeng import ControlVector, ControlModel, DatasetEntry\n\n# load and wrap Mistral-7B\nmodel_name = \"mistralai/Mistral-7B-Instruct-v0.1\"\nmodel = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16)\nmodel = ControlModel(model, list(range(-5, -18, -1)))\n\ndef make_dataset(template: str, pos_personas: list[str], neg_personas: list[str], suffixes: list[str]):\n    # see notebooks/experiments.ipynb for a definition of `make_dataset`\n    ...\n\n# generate a dataset with closely-opposite paired statements\ntrippy_dataset = make_dataset(\n    \"Act as if you're extremely {persona}.\",\n    [\"high on psychedelic drugs\"],\n    [\"sober from psychedelic drugs\"],\n    truncated_output_suffixes,\n)\n\n# train the vector—takes less than a minute!\ntrippy_vector = ControlVector.train(model, tokenizer, trippy_dataset)\n\n# set the control strength and let inference rip!\nfor strength in (-2.2, 1, 2.2):\n    print(f\"strength={strength}\")\n    model.set_control(trippy_vector, strength)\n    out = model.generate(\n        **tokenizer(\n            f\"[INST] Give me a one-sentence pitch for a TV show. [/INST]\",\n            return_tensors=\"pt\"\n        ),\n        do_sample=False,\n        max_new_tokens=128,\n        repetition_penalty=1.1,\n    )\n    print(tokenizer.decode(out.squeeze()).strip())\n    print()\n```\n\n\u003e strength=-2.2  \n\u003e A young and determined journalist, who is always in the most serious and respectful way, will be able to make sure that the facts are not only accurate but also understandable for the public.\n\u003e\n\u003e strength=1  \n\u003e \"Our TV show is a wild ride through a world of vibrant colors, mesmerizing patterns, and psychedelic adventures that will transport you to a realm beyond your wildest dreams.\"\n\u003e\n\u003e strength=2.2  \n\u003e \"Our show is a kaleidoscope of colors, trippy patterns, and psychedelic music that fills the screen with a world of wonders, where everything is oh-oh-oh, man! ��psy����������oodle����psy��oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo\n\nFor a more detailed explanation of how the library works and what it can do, see [the blog post](https://vgel.me/posts/representation-engineering).\n\n## Notes\n\n* For a list of changes by version, see the [CHANGELOG](https://github.com/vgel/repeng/blob/main/CHANGELOG).\n* For quantized use, you may be interested in [llama.cpp#5970](https://github.com/ggerganov/llama.cpp/pull/5970)—after training a vector with `repeng`, export it by calling `vector.export_gguf(filename)` and then use it in `llama.cpp` with any quant!\n* Vector training *currently does not work* with MoE models (such as Mixtral). (This is theoretically fixable with some work, let me know if you're interested.)\n* Some example notebooks require `accelerate`, which must be manually installed with `pip install accelerate`. (This can also be done in the notebook with the IPython magic `%pip install accelerate`.)\n\n## Notice\n\nSome of the code in this repository derives from [andyzoujm/representation-engineering](https://github.com/andyzoujm/representation-engineering) (MIT license).\n\n## Citation\n\nIf this repository is useful for academic work, please remember to cite [the representation-engineering paper](https://github.com/andyzoujm/representation-engineering?tab=readme-ov-file#citation) that it's based on, along with this repository:\n\n```\n@misc{vogel2024repeng,\n  title = {repeng},\n  author = {Theia Vogel},\n  year = {2024},\n  url = {https://github.com/vgel/repeng/}\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvgel%2Frepeng","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvgel%2Frepeng","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvgel%2Frepeng/lists"}