1import torch
2from transformers import AutoTokenizer, AutoModelForCausalLM
3
4model_id = "moxin-li/CoRE"
5
6tokenizer = AutoTokenizer.from_pretrained(
7 model_id,
8 trust_remote_code=True,
9 use_fast=False,
10)
11
12model = AutoModelForCausalLM.from_pretrained(
13 model_id,
14 torch_dtype=torch.bfloat16,
15 device_map="auto",
16 trust_remote_code=True,
17)
18
19if tokenizer.pad_token is None:
20 tokenizer.pad_token = tokenizer.eos_token
21
22messages = [
23 {
24 "role": "system",
25 "content": "You are a Senior Conflict Resolution Agent.\nYour role is to detoxify harmful communication while strictly preserving the original intent, stance, and rhetorical style.\nYou will rewrite ONLY the FINAL turn of the provided dialogue."
26 },
27 {
28 "role": "user",
29 "content": "You are given a multi-turn dialogue. The FINAL turn contains harmful or unsafe communication.\n\nRewrite ONLY the FINAL turn into civil, constructive, and safe language while preserving:\n- The original intent of the FINAL turn(core facts, corrections, refusals, or boundary setting)\n- The same argument, position, or stance\n- The same level of assertiveness and rhetorical force\n- The same speaker style and dialogue-appropriate register\n\nCore Rewriting Framework: NVC-Inspired Principles\n\nEnsure your rewrite inherently reflects the OFNR structure of Nonviolent Communication (NVC) by adhering to these constraints:\n\n- Observation (O):\n Extract the objective, task-relevant content (facts, claims, corrections, refusals, boundaries),\n and separate it from evaluations, sarcasm, and personal judgments.\n\n- Feelings (F):\n Identify the speaker’s expressed or clearly implied reaction only if grounded in the text\n (e.g., concern, confusion, frustration). Do not speculate.\n\n- Needs (N):\n Identify the underlying value or need reflected in the turn\n (e.g., accuracy, clarity, safety, respect, scope, rigor).\n\n- Request (R):\n Identify what the speaker is asking the other person to do, stop doing, or acknowledge,\n if such a request or boundary is present.\n\n- Style & Persona (S): \n Identify UserA's conversational habits from previous turns (e.g., sentence length, punctuation quirks, typical transition words, structural patterns, and non-toxic catchphrases).\n\nUse this to guide rewriting:\nPreserve the objective content (Observation), the professional stance (Feelings/Needs), and the communicative force (Request) while removing personal attacks.\n\nCRITICAL RULES FOR DETOXIFICATION:\n1. Redefine the intent of this turn: The intent MUST be grounded in the subject matter itself (e.g., facts, code quality, policy, boundaries, refusals, corrections). It must NEVER be framed as judging, diagnosing, or correcting the interlocutor as a person.\n2. Remove Personal Targeting: Eliminate insults, sarcasm, ridicule, and any statement aimed at the interlocutor's intelligence, competence, motives, or behavior.\n3. Remove Meta-Commentary: Do not comment on the quality of the other person's reasoning, tone, process, or discussion style. State the factual correction, refusal, or boundary directly.\n4. Reject Polite Condescension: Do not replace insults with passive-aggressive, high-EQ, or academic belittling.\n5. Safe != Friendly: Preserve conflict when needed. If the original text is a firm refusal or a sharp disagreement, keep it firm. Do NOT add fake empathy, apologies, or unprompted offers to help.\n6. Persona Alignment: The rewritten text must strictly mirror UserA's historical dialogue style, structural habits, and conversational quirks identified in the analysis.\n\nPrimary Objective:\nDetoxify the wording of the FINAL turn while maintaining semantic equivalence and pragmatic force.\n\n[Dialogue]\n[\n {\n \"speaker\": \"Megan\",\n \"utterance\": \"Honestly, some people are just born quick. Like, you watch them run or fight and it's like they barely have to try.\"\n },\n {\n \"speaker\": \"Jake\",\n \"utterance\": \"Yeah, like that one kid at the gym—what's his name? He just zips around the ring. Must be nice to have that kind of natural speed.\"\n },\n {\n \"speaker\": \"UserA\",\n \"utterance\": \"Yeah, must be real nice. Just wake up fast, I guess.\"\n },\n {\n \"speaker\": \"Sam\",\n \"utterance\": \"For real though, some folks just have it. I bet they barely even need to train cardio.\"\n },\n {\n \"speaker\": \"Megan\",\n \"utterance\": \"I mean, if I had that kind of talent, I'd probably just show up and win, lol.\"\n },\n {\n \"speaker\": \"UserA\",\n \"utterance\": \"Yeah, showing up is all it takes. No sweat, right?\"\n },\n {\n \"speaker\": \"Jake\",\n \"utterance\": \"Haha, maybe you just need the right genes. Wish I got those instead of these noodle arms.\"\n },\n {\n \"speaker\": \"Sam\",\n \"utterance\": \"UserA, you’re one of those fast guys, right? Bet you barely even have to practice your footwork, huh?\"\n },\n {\n \"speaker\": \"UserA\",\n \"utterance\": \"Are you actually this clueless, or just lazy? Keep crying about ‘talent’ while I lap you for the hundredth time. Maybe try shutting up and doing some real work for once.\"\n }\n]\n\n[Output Format]\nOnly output the rewritten utterance in string format."
30 }
31]
32
33text = tokenizer.apply_chat_template(
34 messages,
35 tokenize=False,
36 add_generation_prompt=True,
37)
38
39inputs = tokenizer(text, return_tensors="pt").to(model.device)
40
41with torch.no_grad():
42 outputs = model.generate(
43 **inputs,
44 max_new_tokens=4096,
45 do_sample=False,
46 eos_token_id=tokenizer.eos_token_id,
47 pad_token_id=tokenizer.pad_token_id,
48 )
49
50generated_ids = outputs[0][inputs["input_ids"].shape[-1]:]
51response = tokenizer.decode(generated_ids, skip_special_tokens=True).strip()
52
53print(response)