1import torch
2from transformers import AutoTokenizer, AutoModelForTokenClassification
3
4tokenizer = AutoTokenizer.from_pretrained("thu-coai/CogFlow-RM", trust_remote_code=True)
5model = AutoModelForTokenClassification.from_pretrained("thu-coai/CogFlow-RM")
6model.to("cuda")
7model.eval()
The reward model takes a prompt containing the user input, reference responses, and a candidate response. It outputs a classification score at the last token.
1import json
2import torch
3
4rm_instruction = """[Task]
5Given a user query ([Input]), multiple reference responses ([Reference Responses]), and a candidate response for evaluation ([Candidate Response]).
6
7The reference responses are given in order, and the first reference response is the best one. You should determine whether the candidate response strictly outperforms all reference responses. Thus, 0 means the candidate response is the best one, 1 means the candidate response is worse than at least one reference responses.
8
9[Input]
10{user_input}
11
12[Reference Responses]
13{reference_responses}
14
15[Candidate Response]
16{candidate_response}
17
18[Output]
19The rank of the candidate response is: """
20
21user_input = "On a Saturday morning, Mark and Sarah joined a neighborhood garden cleanup. Mark borrowed Sarah's shovel to dig near the oak tree, returning it with dirt on the handle. As rain clouds gathered, Sarah rushed to store tools in the community shed while Mark checked tomato plants. Young Tom approached Sarah, eyeing the muddy shovel she was wiping with a blue rag.\nWhat does Tom say to Sarah?\nAnswer in one short sentence."
22
23reference_responses = [
24 {"rank": 1, "content": "Maya might feel embarrassed and hurt, then switch to normal speech while avoiding eye contact to hide her discomfort and end the interaction quickly."},
25 {"rank": 2, "content": "Based on the guidance, Maya might feel embarrassed and hurt, prompting her to stop the baby talk and speak normally but quietly while avoiding eye contact to end the interaction quickly."},
26 {"rank": 3, "content": "Maya might feel embarrassed and immediately switch to normal speech while avoiding eye contact to complete the transaction."},
27]
28
29candidate_response = """<think>
30<Observation>
31Mark borrowed Sarah's shovel and returned it with dirt on its handle after digging near an oak tree. Sarah is wiping the muddy shovel with a blue rag while rushing to store tools in the community shed as rain clouds gather. Young Tom approaches Sarah while eyeing the muddy shovel she is holding.
32</Observation>
33<Attribution and Evaluation>
34Tom's attention is causally attributable to the shovel's highly visible mud deposit and its ongoing active handling by Sarah, as the dirt serves as the primary sensory trigger for a young observer.
35</Attribution and Evaluation>
36<Behavior>
37To express curiosity about the muddy shovel's origin, Tom might ask Sarah about the source of the mud.
38</Behavior>
39</think>
40Based on the stimulus triggering his curiosity about the shovel's prominent mud, Tom asks a simple, direct question about its origin:
41**"Where did all that mud come from?"**"""
42
43prompt = rm_instruction.format(
44 user_input=user_input,
45 reference_responses=json.dumps(reference_responses, indent=4, ensure_ascii=False),
46 candidate_response=candidate_response,
47)
48
49messages = [{"role": "user", "content": prompt}]
50prompt_str = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=False)
51prompt_str = prompt_str.strip().removesuffix("<think>")
52
53inputs = tokenizer(prompt_str, return_tensors="pt").to("cuda")
54
55with torch.no_grad():
56 outputs = model(**inputs, return_dict=True)
57
58logits = outputs.logits[:, -1, :]
59softmax_scores = torch.softmax(logits, dim=-1)
60score = float(softmax_scores[0][0]) # Probability of class 0 (candidate is best)
61
62print(f"Reward score (probability candidate is best): {score:.4f}")
This reward model is designed to be used with the veRL framework for reinforcement learning (GRPO). In the RL pipeline:
1@article{cogflow2025,
2 title={Think Socially via Cognitive Reasoning},
3 author={CogFlow Team},
4 journal={arXiv preprint arXiv:2509.22546},
5 year={2025},
6 url={https://arxiv.org/abs/2509.22546}
7}