repeng
1.0.0
표현 공학을 사용하여 제어 벡터를 생성하기 위한 Python 라이브러리입니다. 60초 이내에 벡터를 훈련시키세요!
전체 예를 보려면 노트북 폴더 또는 블로그 게시물을 참조하세요.
import json
import torch
from transformers import AutoModelForCausalLM , AutoTokenizer
from repeng import ControlVector , ControlModel , DatasetEntry
# load and wrap Mistral-7B
model_name = "mistralai/Mistral-7B-Instruct-v0.1"
model = AutoModelForCausalLM . from_pretrained ( model_name , torch_dtype = torch . float16 )
model = ControlModel ( model , list ( range ( - 5 , - 18 , - 1 )))
def make_dataset ( template : str , pos_personas : list [ str ], neg_personas : list [ str ], suffixes : list [ str ]):
# see notebooks/experiments.ipynb for a definition of `make_dataset`
...
# generate a dataset with closely-opposite paired statements
trippy_dataset = make_dataset (
"Act as if you're extremely {persona}." ,
[ "high on psychedelic drugs" ],
[ "sober from psychedelic drugs" ],
truncated_output_suffixes ,
)
# train the vector—takes less than a minute!
trippy_vector = ControlVector . train ( model , tokenizer , trippy_dataset )
# set the control strength and let inference rip!
for strength in ( - 2.2 , 1 , 2.2 ):
print ( f"strength= { strength } " )
model . set_control ( trippy_vector , strength )
out = model . generate (
** tokenizer (
f"[INST] Give me a one-sentence pitch for a TV show. [/INST]" ,
return_tensors = "pt"
),
do_sample = False ,
max_new_tokens = 128 ,
repetition_penalty = 1.1 ,
)
print ( tokenizer . decode ( out . squeeze ()). strip ())
print ()
강도=-2.2
항상 가장 진지하고 정중하게 행동하는 젊고 결단력 있는 저널리스트는 사실이 정확할 뿐만 아니라 대중이 이해할 수 있도록 할 수 있습니다.힘=1
"우리 TV 쇼는 생생한 색상, 매혹적인 패턴, 환각적인 모험의 세계를 통해 여러분을 가장 거친 꿈 너머의 영역으로 데려다줄 거친 여행입니다."강도=2.2
"우리 쇼는 모든 것이 오-오-오인 경이로운 세계로 화면을 가득 채우는 색상, 몽롱한 패턴, 사이키델릭 음악의 만화경입니다! ��psy����������������������������psy���������������������������������������������������������������������������������� 우오오오오오오오오오오오오오오오오오오오오오오오오오오오오오오오오오오오오오오오오오오오오오오오오오오오오오
라이브러리의 작동 방식과 수행할 수 있는 작업에 대한 자세한 설명은 블로그 게시물을 참조하세요.
repeng
으로 벡터를 훈련한 후 vector.export_gguf(filename)
호출하여 내보낸 다음 llama.cpp
에서 임의의 퀀트와 함께 사용하세요!accelerate
필요하며, 이는 pip install accelerate
사용하여 수동으로 설치해야 합니다. (IPython 매직 %pip install accelerate
사용하여 노트북에서 이 작업을 수행할 수도 있습니다.) 이 저장소의 일부 코드는 andyzoujm/representation-engineering(MIT 라이센스)에서 파생됩니다.
이 저장소가 학술 작업에 유용하다면 이 저장소와 함께 해당 저장소의 기반이 되는 표현 공학 논문을 인용하는 것을 잊지 마세요.
@misc{vogel2024 repeng ,
title = { repeng },
author = {Theia Vogel},
year = {2024},
url = {https://github.com/vgel/repeng/}
}