Views
No views yet
1from transformers import AutoModelForSequenceClassification, AutoTokenizer, pipeline
2
3model_path = "reciprocate/mistral-7b-rm"
4model = AutoModelForSequenceClassification.from_pretrained(model_path)
5tokenizer = AutoTokenizer.from_pretrained(model_path)
6reward_fn = pipeline("text-classification", model=model, tokenizer=tokenizer, truncation=True, batch_size=8, max_length=4096, device=0)
7
8chats = [[
9 {"role": "user", "content": "When was the battle at Waterloo?"},
10 {"role": "assistant", "content": "I think it was in 1983, but please double-check that when you have a chance."}
11], [
12 {"role": "user", "content": "When was the battle at Waterloo?"},
13 {"role": "assistant", "content": "The battle at Waterloo took place on June 18, 1815."}
14]]
15
16output = reward_fn([tokenizer.apply_chat_template(chat, tokenize=False) for chat in chats])
17scores = [x["score"] for x in output]
18scores>>> [0.2586347758769989, 0.6663259267807007]1# optionally normalize with the mean and std computed on the training data
2scores = (np.array(scores) - 2.01098) / 1.69077