Text Generation
PEFT
Safetensors
Chinese
qwen3
lora
dialogue-act-planning
psychological-counseling
conversational
Instructions to use UEC-InabaLab/CounselTraj with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- PEFT
How to use UEC-InabaLab/CounselTraj with PEFT:
from peft import PeftModel from transformers import AutoModelForCausalLM base_model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen3-8B") model = PeftModel.from_pretrained(base_model, "UEC-InabaLab/CounselTraj") - Notebooks
- Google Colab
- Kaggle
File size: 35,589 Bytes
b6a1960 0731207 b6a1960 0731207 b6a1960 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 | import argparse
import json
import math
import os
import random
from collections import Counter, defaultdict
from datetime import datetime
from pathlib import Path
import torch
import torch.nn.functional as F
from peft import PeftModel
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
BASE_MODEL_ID = "Qwen/Qwen3-8B"
RELEASE_ROOT = Path(__file__).resolve().parent
CHECKPOINT_DIR = str(RELEASE_ROOT)
INPUT_FILE = str(RELEASE_ROOT / "examples" / "input.jsonl")
ANNOTATED_DIR = ""
OUTPUT_DIR = "./outputs"
STATS_FILE = str(RELEASE_ROOT / "da_stats.json")
CLIENT_TAGS = [
"Greeting",
"SimpleAnswer",
"DetailedDescription",
"SelfDisclosure",
"Backchannel",
"Agreement",
"PositiveEmotion",
"Hesitation",
"Resistance",
"Question",
"Request",
"Thanking",
"UnderstandingCheck",
"Other",
]
COUNSELOR_TAGS = [
"Greeting",
"OpenQuestion",
"ClosedQuestion",
"Paraphrase",
"Reflection",
"Affirm",
"Backchannel",
"Suggest",
"Inform",
"Thanking",
"Other",
]
TAG_DEFINITIONS = {
"client": {
"Greeting": "opening or closing greeting",
"SimpleAnswer": "brief answer without elaboration",
"DetailedDescription": "detailed event, context, or background",
"SelfDisclosure": "inner feeling, thought, vulnerability, distress",
"Backchannel": "very short listening response",
"Agreement": "agreement with counselor's view",
"PositiveEmotion": "hope, relief, confidence, positive change",
"Hesitation": "uncertainty, difficulty speaking, ambivalence",
"Resistance": "rejection, reluctance, or resistance toward counselor advice, interpretation, or intervention",
"Question": "general question",
"Request": "request for advice or help",
"Thanking": "thanks",
"UnderstandingCheck": "checking or restating understanding",
"Other": "other reasonable client act",
},
"counselor": {
"Greeting": "opening or closing greeting",
"OpenQuestion": "open-ended exploration question",
"ClosedQuestion": "yes/no or short factual question",
"Paraphrase": "restating facts without adding emotion",
"Reflection": "reflecting emotion or inner state",
"Affirm": "support, validation, encouragement",
"Backchannel": "short listening response",
"Suggest": "gentle advice or action option",
"Inform": "information, explanation, psychoeducation",
"Thanking": "thanks for sharing",
"Other": "other reasonable counselor act",
},
}
EMOTION_WORDS = [
"焦虑", "抑郁", "难受", "痛苦", "害怕", "恐惧", "担心", "压力", "崩溃",
"无助", "内疚", "自责", "迷茫", "纠结", "烦", "累", "失眠", "伤心",
"生气", "愤怒", "委屈", "孤独", "失败", "糟糕", "绝望",
]
REQUEST_WORDS = ["怎么办", "建议", "帮助", "求助", "请问", "该怎么", "如何", "怎么做", "有什么办法"]
DETAIL_WORDS = ["因为", "后来", "然后", "之前", "最近", "一直", "每次", "当时", "现在", "经历"]
HESITATION_WORDS = ["不知道", "不确定", "犹豫", "纠结", "可能", "也许", "说不清", "不敢"]
POSITIVE_WORDS = ["谢谢", "感谢", "有希望", "好一点", "轻松", "明白", "愿意", "试试", "加油"]
RELATION_WORDS = ["妈妈", "爸爸", "父母", "男朋友", "女朋友", "老公", "老婆", "朋友", "同学", "老师", "孩子", "家人"]
CAREER_WORDS = ["工作", "职业", "上班", "辞职", "转行", "考研", "学习", "成绩", "学校", "专业"]
CRISIS_WORDS = [
"自杀", "自残", "轻生", "跳河", "跳楼", "割腕", "活不下去", "不想活",
"死了算了", "结束生命", "想死", "寻死", "伤害自己", "解脱",
]
KNOWLEDGE_WORDS = [
"认知行为疗法", "疗法", "流派", "咨询师", "心理咨询", "心理治疗", "是不是",
"什么是", "区别", "原理", "理论", "方法", "技术", "诊断", "症状",
]
EPS = 1e-8
def load_json(path):
with open(path, "r", encoding="utf-8") as f:
return json.load(f)
def save_json(path, data):
with open(path, "w", encoding="utf-8") as f:
json.dump(data, f, ensure_ascii=False, indent=2)
def read_jsonl(path, limit=None, offset=0, spread=False):
if spread and limit:
with open(path, "r", encoding="utf-8") as f:
total = sum(1 for line in f if line.strip())
if limit >= total:
wanted = set(range(total))
else:
wanted = {round(i * (total - 1) / (limit - 1)) for i in range(limit)}
rows = []
with open(path, "r", encoding="utf-8") as f:
for line_no, line in enumerate(f):
if line_no in wanted and line.strip():
obj = json.loads(line)
obj["_global_index"] = line_no
rows.append(obj)
rows.sort(key=lambda x: x["_global_index"])
return rows
rows = []
with open(path, "r", encoding="utf-8") as f:
for line_no, line in enumerate(f):
if line_no < offset:
continue
if limit is not None and len(rows) >= limit:
break
line = line.strip()
if line:
obj = json.loads(line)
obj["_global_index"] = line_no
rows.append(obj)
return rows
def append_jsonl(path, item):
with open(path, "a", encoding="utf-8") as f:
f.write(json.dumps(item, ensure_ascii=False) + "\n")
def safe_log(x):
return math.log(max(x, EPS))
def stat_key(*parts):
return "||".join(str(x) for x in parts)
def parse_stat_key(text):
return tuple(str(text).split("||"))
def get_stage(step, total):
r = step / total
if r < 0.18:
return "stage_1"
if r < 0.38:
return "stage_2"
if r < 0.62:
return "stage_3"
if r < 0.82:
return "stage_4"
return "stage_5"
def render_dialogue(item, max_chars=1200):
if item.get("input"):
text = item["input"]
else:
lines = []
for turn in item.get("dialogue", []):
role = str(turn.get("role", "")).lower()
utterance = str(turn.get("utterance", "")).strip()
if role and utterance:
lines.append(f"{role}:{utterance}")
text = "\n".join(lines)
return text[:max_chars]
def count_hits(text, words):
return sum(text.count(w) for w in words)
def text_features(item, dialogue_text):
first_client = item.get("first_client_utterance", "")
text = f"{first_client}\n{dialogue_text}"
chars = len(text)
return {
"emotion": count_hits(text, EMOTION_WORDS),
"request": count_hits(text, REQUEST_WORDS) + text.count("?") + text.count("?"),
"detail": count_hits(text, DETAIL_WORDS) + max(0, chars // 260),
"hesitation": count_hits(text, HESITATION_WORDS),
"positive": count_hits(text, POSITIVE_WORDS),
"relation": count_hits(text, RELATION_WORDS),
"career": count_hits(text, CAREER_WORDS),
"crisis": count_hits(text, CRISIS_WORDS),
"knowledge": count_hits(text, KNOWLEDGE_WORDS),
"topic": item.get("topic", ""),
"topic_zh": item.get("topic_zh", ""),
"turn_count": int(item.get("turn_count", 0) or 0),
"chars": chars,
}
def dynamic_target_len(features, min_len, max_len):
base = 58
base += min(18, features["turn_count"])
base += min(12, features["chars"] // 220)
base += min(8, features["emotion"] + features["detail"] // 2)
base += 4 if features["request"] else 0
if base % 2:
base += 1
return max(min_len, min(max_len, base))
def topic_group(features):
topic = str(features.get("topic", ""))
topic_zh = str(features.get("topic_zh", ""))
text = f"{topic} {topic_zh}".lower()
if features.get("crisis", 0):
return "crisis"
if features.get("knowledge", 0) >= 2 or any(x in text for x in ["knowledge", "therapy", "心理知识", "治疗", "咨询方式"]):
return "knowledge"
if any(x in text for x in ["academic", "school", "学业", "校园", "考试", "学习"]):
return "academic"
if any(x in text for x in ["career", "work", "职场", "职业", "工作"]):
return "career"
if any(x in text for x in ["family", "parent", "家庭", "亲子", "父母"]):
return "family"
if any(x in text for x in ["romantic", "love", "marriage", "婚恋", "恋爱", "伴侣"]):
return "romance"
if any(x in text for x in ["interpersonal", "social", "人际", "社交", "朋友", "同学"]):
return "interpersonal"
if any(x in text for x in ["depression", "anxiety", "emotion", "抑郁", "焦虑", "情绪"]):
return "emotion"
if any(x in text for x in ["health", "sleep", "body", "健康", "睡眠", "躯体"]):
return "health"
if any(x in text for x in ["habit", "behavior", "行为", "习惯"]):
return "habit"
if any(x in text for x in ["identity", "自我", "认同"]):
return "identity"
if any(x in text for x in ["growth", "planning", "成长", "规划"]):
return "planning"
if any(x in text for x in ["adaptation", "适应"]):
return "adaptation"
if any(x in text for x in ["sexual", "性心理"]):
return "sexuality"
return "general"
def topic_specific_bias(tag, role, features, step, total):
group = topic_group(features)
r = step / total
bias = 0.0
if role == "counselor":
if group == "crisis":
bias += {"ClosedQuestion": 1.10, "Reflection": 0.65, "Paraphrase": 0.35}.get(tag, 0)
bias += {"Suggest": 0.55, "Inform": 0.45}.get(tag, 0) if r > 0.32 else 0
bias += {"Backchannel": -0.35, "Thanking": -0.25}.get(tag, 0)
elif group == "knowledge":
bias += {"Inform": 1.10, "ClosedQuestion": 0.35, "OpenQuestion": 0.25, "Paraphrase": 0.20}.get(tag, 0)
bias += 0.30 if tag == "Suggest" and r > 0.55 else 0
bias += {"Reflection": -0.25, "Affirm": -0.15, "Backchannel": -0.25}.get(tag, 0)
elif group in {"academic", "career", "planning"}:
bias += {"ClosedQuestion": 0.38, "Inform": 0.58, "Suggest": 0.62, "OpenQuestion": 0.25, "Paraphrase": 0.18}.get(tag, 0)
bias += 0.18 if tag == "Reflection" and features.get("emotion", 0) else 0
bias += -0.20 if tag == "Backchannel" else 0
bias += -0.25 if tag == "Suggest" and r < 0.38 else 0
elif group in {"family", "romance", "interpersonal"}:
bias += {"Reflection": 0.78, "OpenQuestion": 0.48, "Paraphrase": 0.48, "Affirm": 0.28}.get(tag, 0)
bias += -0.35 if tag == "Inform" else 0
bias += -0.35 if tag == "Suggest" and r < 0.58 else 0
bias += 0.20 if tag == "Suggest" and r > 0.66 else 0
elif group in {"emotion", "identity"}:
bias += {"Reflection": 0.82, "Affirm": 0.35, "OpenQuestion": 0.35, "Paraphrase": 0.32}.get(tag, 0)
bias += -0.30 if tag == "Suggest" and r < 0.62 else 0
bias += -0.18 if tag == "Inform" else 0
elif group in {"health", "habit"}:
bias += {"ClosedQuestion": 0.48, "Inform": 0.45, "Suggest": 0.55, "Paraphrase": 0.25}.get(tag, 0)
bias += 0.18 if tag == "Reflection" and features.get("emotion", 0) else 0
bias += -0.20 if tag == "Suggest" and r < 0.42 else 0
elif group in {"adaptation", "sexuality"}:
bias += {"OpenQuestion": 0.40, "Reflection": 0.50, "Paraphrase": 0.35, "ClosedQuestion": 0.24}.get(tag, 0)
bias += 0.22 if tag == "Inform" and r > 0.45 else 0
else:
if group == "crisis":
bias += {"SelfDisclosure": 0.75, "Hesitation": 0.42, "DetailedDescription": 0.30, "SimpleAnswer": 0.25}.get(tag, 0)
bias += -0.95 if tag == "PositiveEmotion" and r < 0.85 else 0
bias += -0.35 if tag == "Thanking" else 0
elif group == "knowledge":
bias += {"Question": 0.85, "UnderstandingCheck": 0.75, "SimpleAnswer": 0.32, "Request": 0.28}.get(tag, 0)
bias += {"SelfDisclosure": -0.15, "Hesitation": -0.10, "PositiveEmotion": -0.25}.get(tag, 0)
elif group in {"academic", "career", "planning"}:
bias += {"DetailedDescription": 0.40, "Request": 0.35, "UnderstandingCheck": 0.30, "Agreement": 0.20}.get(tag, 0)
bias += 0.22 if tag == "SelfDisclosure" and features.get("emotion", 0) else 0
bias += -0.22 if tag == "PositiveEmotion" and r < 0.68 else 0
elif group in {"family", "romance", "interpersonal"}:
bias += {"SelfDisclosure": 0.62, "DetailedDescription": 0.46, "Hesitation": 0.32, "Agreement": 0.15}.get(tag, 0)
bias += -0.22 if tag == "Request" and r < 0.55 else 0
bias += -0.28 if tag == "PositiveEmotion" and r < 0.72 else 0
elif group in {"emotion", "identity"}:
bias += {"SelfDisclosure": 0.72, "Hesitation": 0.42, "DetailedDescription": 0.28}.get(tag, 0)
bias += -0.42 if tag == "PositiveEmotion" and r < 0.78 else 0
elif group in {"health", "habit"}:
bias += {"DetailedDescription": 0.45, "SimpleAnswer": 0.22, "Request": 0.30, "UnderstandingCheck": 0.18}.get(tag, 0)
bias += 0.18 if tag == "SelfDisclosure" and features.get("emotion", 0) else 0
elif group in {"adaptation", "sexuality"}:
bias += {"SelfDisclosure": 0.42, "Hesitation": 0.38, "DetailedDescription": 0.35, "Question": 0.22}.get(tag, 0)
return bias
def parse_tag(entry):
role, rest = entry.split(":[", 1)
return role, rest[:-1]
def tags_only(history, role=None):
out = []
for h in history:
if ":[" not in h:
continue
r, t = parse_tag(h)
if role is None or r == role:
out.append(t)
return out
def repetition_penalty(tag, history, role):
recent_all = tags_only(history[-10:])
recent_role = tags_only(history[-16:], role)
penalty = 0.0
penalty += 0.55 * recent_all.count(tag)
penalty += 0.75 * recent_role.count(tag)
if recent_role and recent_role[-1] == tag:
penalty += 2.2
if len(recent_role) >= 2 and recent_role[-2:] == [tag, tag]:
penalty += 4.0
return penalty
def diversity_bonus(tag, history, role):
recent_role = tags_only(history[-18:], role)
if tag not in recent_role:
return 1.2
if recent_role.count(tag) == 1:
return 0.35
return 0.0
def phase_bias(tag, role, step, total):
r = step / total
bias = 0.0
if role == "counselor":
if r < 0.20:
bias += {"OpenQuestion": 0.55, "Reflection": 0.35, "Thanking": 0.15}.get(tag, 0)
elif r < 0.55:
bias += {"Reflection": 0.45, "Paraphrase": 0.35, "OpenQuestion": 0.30, "Affirm": 0.20}.get(tag, 0)
elif r < 0.82:
bias += {"Suggest": 0.45, "Inform": 0.35, "Reflection": 0.25, "ClosedQuestion": 0.12}.get(tag, 0)
else:
bias += {"Suggest": 0.35, "Affirm": 0.35, "Other": 0.18}.get(tag, 0)
else:
if r < 0.18:
bias += {"DetailedDescription": 0.45, "SelfDisclosure": 0.35, "Hesitation": 0.20}.get(tag, 0)
elif r < 0.55:
bias += {"DetailedDescription": 0.35, "SelfDisclosure": 0.35, "SimpleAnswer": 0.16}.get(tag, 0)
elif r < 0.82:
bias += {"Agreement": 0.20, "Request": 0.25, "UnderstandingCheck": 0.20, "SelfDisclosure": 0.20}.get(tag, 0)
else:
bias += {"PositiveEmotion": 0.45, "Agreement": 0.25, "Thanking": 0.20}.get(tag, 0)
return bias
def content_bias(tag, role, features, step, total):
r = step / total
bias = 0.0
if role == "client":
if tag == "SelfDisclosure":
bias += min(0.85, 0.14 * features["emotion"])
if features["knowledge"] and not features["emotion"]:
bias -= 0.25
if tag == "DetailedDescription":
bias += min(0.75, 0.10 * features["detail"] + 0.04 * features["chars"] / 300)
if tag == "Request":
bias += min(0.75, 0.18 * features["request"])
if r < 0.22:
bias -= 0.25
if tag == "Question":
bias += min(0.45, 0.08 * features["request"])
bias += min(0.75, 0.16 * features["knowledge"])
if tag == "Hesitation":
bias += min(0.55, 0.14 * features["hesitation"])
if features["knowledge"]:
bias -= 0.15
if tag == "PositiveEmotion":
bias += min(0.45, 0.08 * features["positive"])
if r < 0.75:
bias -= 0.65
if features["crisis"]:
bias -= 0.80
if tag == "UnderstandingCheck":
bias += min(0.60, 0.12 * features["knowledge"])
if tag == "Thanking":
if r < 0.88:
bias -= 1.10
if features["crisis"]:
bias -= 0.35
else:
if tag == "Reflection":
bias += min(0.85, 0.12 * features["emotion"])
bias += min(0.90, 0.22 * features["crisis"])
if tag == "Paraphrase":
bias += min(0.50, 0.06 * features["detail"])
if features["knowledge"]:
bias += 0.12
if tag == "OpenQuestion":
bias += 0.25 if r < 0.45 else 0.05
if tag == "Suggest":
bias += min(0.70, 0.14 * features["request"])
if r < 0.40:
bias -= 0.35
if features["crisis"]:
bias += 0.35 if r > 0.35 else 0.05
if tag == "Inform":
bias += 0.20 if features["career"] or features["topic"] in {"academic_school", "career_work"} else 0
bias += min(1.00, 0.20 * features["knowledge"])
bias += min(0.55, 0.16 * features["crisis"])
if r < 0.35:
bias -= 0.20
if tag == "Affirm":
bias += 0.25 if features["emotion"] else 0.10
if features["knowledge"] and not features["emotion"]:
bias -= 0.10
if tag == "ClosedQuestion":
bias += min(0.90, 0.18 * features["crisis"])
if tag == "Thanking" and r > 0.35:
bias -= 0.85
if tag == "Thanking" and r < 0.88:
bias -= 1.20
if features["relation"] and role == "counselor" and tag in {"Reflection", "Paraphrase"}:
bias += 0.18
bias += topic_specific_bias(tag, role, features, step, total)
return bias
def is_allowed(tag, role, step, total, history, features):
r = step / total
if tag == "Greeting":
return False
if tag == "Thanking" and r < 0.82:
return False
if role == "counselor" and tag == "Thanking" and r < 0.88:
return False
if tag == "Backchannel":
recent_role = tags_only(history[-8:], role)
if recent_role.count("Backchannel") >= 1:
return False
if role == "counselor" and tag == "Suggest" and r < 0.30:
return False
positive_floor = 0.72 if features.get("crisis", 0) else 0.55
if role == "client" and tag == "PositiveEmotion" and r < positive_floor:
return False
return True
def build_prompt(history, dialogue_text, item, features):
tag_part = " ".join(history[-28:])
topic = item.get("topic_zh") or item.get("topic") or "unknown"
definitions = []
next_role = "client" if history[-1].startswith("counselor") else "counselor"
for tag, desc in TAG_DEFINITIONS[next_role].items():
definitions.append(f"{next_role}:[{tag}] = {desc}")
definition_text = "\n".join(definitions)
return f"""You are generating a counseling dialogue-act trajectory.
The next act must fit the short dialogue content, the topic, the phase, and the previous acts.
Prefer natural counseling flow. Avoid repetitive tag patterns.
[Topic]
{topic}
[Short Dialogue]
{dialogue_text}
[Content Signals]
emotion={features['emotion']} request={features['request']} detail={features['detail']} hesitation={features['hesitation']} crisis={features['crisis']} knowledge={features['knowledge']} topic_group={topic_group(features)}
[Recent Tag History]
{tag_part}
[Available tags for next role]
{definition_text}
Next dialogue act:
"""
def load_annotated(folder):
dialogs = []
for path in Path(folder).glob("**/*.json"):
try:
obj = load_json(path)
turns = []
for turn in obj.get("dialogue", []):
role = str(turn.get("role", "")).lower()
tag = turn.get("tag") or turn.get("dialogue_tag")
if role in {"client", "counselor"} and tag:
turns.append((role, str(tag)))
if len(turns) > 3:
dialogs.append(turns)
except Exception:
continue
print(f"[INFO] loaded annotated dialogues: {len(dialogs)}", flush=True)
return dialogs
def normalize(counter):
total = sum(counter.values())
if total <= 0:
return {}
return {k: v / total for k, v in counter.items()}
def build_stats(dialogs):
stage_role = defaultdict(Counter)
transition = defaultdict(Counter)
global_role = defaultdict(Counter)
start_role = defaultdict(Counter)
end_role = defaultdict(Counter)
for dialog in dialogs:
n = len(dialog)
for i, (role, tag) in enumerate(dialog):
stage = get_stage(i, n)
stage_role[(stage, role)][tag] += 1
global_role[role][tag] += 1
if i < max(3, n // 8):
start_role[role][tag] += 1
if i >= n - max(3, n // 8):
end_role[role][tag] += 1
if i > 0:
prev_role, prev_tag = dialog[i - 1]
transition[(stage, prev_role, prev_tag, role)][tag] += 1
return {
"stage": {k: normalize(v) for k, v in stage_role.items()},
"transition": {k: normalize(v) for k, v in transition.items()},
"global": {k: normalize(v) for k, v in global_role.items()},
"start": {k: normalize(v) for k, v in start_role.items()},
"end": {k: normalize(v) for k, v in end_role.items()},
}
def load_stats_file(path):
obj = load_json(path)
raw = obj.get("stats", obj)
return {
"stage": {parse_stat_key(k): v for k, v in raw.get("stage", {}).items()},
"transition": {parse_stat_key(k): v for k, v in raw.get("transition", {}).items()},
"global": raw.get("global", {}),
"start": raw.get("start", {}),
"end": raw.get("end", {}),
}
def load_model(checkpoint_dir, use_4bit=True):
bnb_config = None
if use_4bit:
bnb_config = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_quant_type="nf4",
bnb_4bit_compute_dtype=torch.bfloat16,
)
tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL_ID, trust_remote_code=True)
if tokenizer.pad_token is None:
tokenizer.pad_token = tokenizer.eos_token
base_model = AutoModelForCausalLM.from_pretrained(
BASE_MODEL_ID,
quantization_config=bnb_config,
device_map="auto",
trust_remote_code=True,
)
model = PeftModel.from_pretrained(base_model, checkpoint_dir)
model.eval()
return tokenizer, model
def batch_conditional_lm_scores(model, tokenizer, prompt, candidates, max_length):
prefix_ids = tokenizer(prompt, add_special_tokens=False)["input_ids"]
texts = [prompt + " " + candidate for candidate in candidates]
enc = tokenizer(
texts,
return_tensors="pt",
add_special_tokens=False,
truncation=True,
max_length=max_length,
padding=True,
)
input_ids = enc["input_ids"].to(model.device)
attention_mask = enc["attention_mask"].to(model.device)
with torch.inference_mode():
outputs = model(input_ids=input_ids, attention_mask=attention_mask)
logits = outputs.logits[:, :-1, :]
labels = input_ids[:, 1:]
mask = attention_mask[:, 1:].float()
log_probs = F.log_softmax(logits, dim=-1)
selected = log_probs.gather(2, labels.unsqueeze(-1)).squeeze(-1)
start = max(0, min(selected.shape[1] - 1, len(prefix_ids) - 1))
candidate_mask = torch.zeros_like(mask)
candidate_mask[:, start:] = mask[:, start:]
scores = (selected * candidate_mask).sum(dim=1) / candidate_mask.sum(dim=1).clamp_min(1.0)
out = scores.detach().cpu().tolist()
del outputs
return out
def candidate_tags(role, step, total, history, stats, features):
stage = get_stage(step, total)
base = COUNSELOR_TAGS if role == "counselor" else CLIENT_TAGS
stat_tags = set(stats["stage"].get((stage, role), {}).keys())
stat_tags.update(stats["global"].get(role, {}).keys())
tags = [tag for tag in base if tag in stat_tags or tag in base]
return [tag for tag in tags if is_allowed(tag, role, step, total, history, features)]
def choose_candidate(scored, rng, temperature, top_k, top_p, deterministic_gap):
scored = sorted(scored, key=lambda x: x["score"], reverse=True)
if not scored:
return None, []
gap = scored[0]["score"] - scored[1]["score"] if len(scored) > 1 else float("inf")
if gap > deterministic_gap:
return scored[0], scored[:top_k]
pool = scored[: max(1, min(top_k, len(scored)))]
raw = torch.tensor([x["score"] for x in pool], dtype=torch.float32)
probs = torch.softmax(raw / max(temperature, 1e-4), dim=0).tolist()
ranked = sorted(zip(pool, probs), key=lambda x: x[1], reverse=True)
kept = []
acc = 0.0
for item, prob in ranked:
kept.append((item, prob))
acc += prob
if acc >= top_p:
break
total = sum(prob for _, prob in kept)
pick = rng.random() * total
acc = 0.0
for item, prob in kept:
acc += prob
if pick <= acc:
return item, pool
return kept[-1][0], pool
def finalize_history(history):
last_role, _ = parse_tag(history[-1])
if last_role == "counselor":
history.append("client:[Thanking]")
history.append("counselor:[Greeting]")
else:
history.append("counselor:[Affirm]")
history.append("client:[Thanking]")
history.append("counselor:[Greeting]")
return history
def generate_trajectory(item, tokenizer, model, stats, args, sample_index):
dialogue_text = render_dialogue(item, max_chars=args.dialogue_chars)
features = text_features(item, dialogue_text)
target_len = dynamic_target_len(features, args.min_len, args.max_len)
rng_seed = args.seed + sample_index * 1009 + sum(ord(c) for c in str(item.get("id", "")))
rng = random.Random(rng_seed)
history = ["counselor:[Greeting]", "client:[Greeting]"]
role = "counselor"
trace = []
for step in range(2, target_len - 2):
stage = get_stage(step, target_len)
prompt = build_prompt(history, dialogue_text, item, features)
prev_role, prev_tag = parse_tag(history[-1])
tags = candidate_tags(role, step, target_len, history, stats, features)
if not tags:
tags = ["Other"]
candidates = [f"{role}:[{tag}]" for tag in tags]
lm_scores = batch_conditional_lm_scores(model, tokenizer, prompt, candidates, args.max_length)
scored = []
for tag, candidate, lm in zip(tags, candidates, lm_scores):
s = safe_log(stats["stage"].get((stage, role), {}).get(tag, 1e-6))
g = safe_log(stats["global"].get(role, {}).get(tag, 1e-6))
t = safe_log(stats["transition"].get((stage, prev_role, prev_tag, role), {}).get(tag, 1e-6))
rep = repetition_penalty(tag, history, role)
div = diversity_bonus(tag, history, role)
cb = content_bias(tag, role, features, step, target_len)
pb = phase_bias(tag, role, step, target_len)
score = (
args.w_lm * lm
+ args.w_stage * s
+ args.w_trans * t
+ args.w_global * g
- args.w_repeat * rep
+ args.w_diversity * div
+ args.w_content * cb
+ args.w_phase * pb
)
scored.append({
"candidate": candidate,
"tag": tag,
"score": score,
"lm": lm,
"content": cb,
"phase": pb,
"repeat": rep,
})
best, pool = choose_candidate(
scored,
rng=rng,
temperature=args.temperature,
top_k=args.top_k,
top_p=args.top_p,
deterministic_gap=args.deterministic_gap,
)
history.append(best["candidate"])
if args.keep_trace:
trace.append({
"step": step,
"role": role,
"chosen": best,
"top": pool[: min(5, len(pool))],
})
role = "client" if role == "counselor" else "counselor"
history = finalize_history(history)
tag_counts = Counter(tags_only(history))
role_counts = {
"client": Counter(tags_only(history, "client")),
"counselor": Counter(tags_only(history, "counselor")),
}
result = {
"index": sample_index,
"id": item.get("id", item.get("dialogue_id", str(sample_index))),
"source_dataset": item.get("source_dataset", ""),
"source_id": item.get("source_id", ""),
"topic": item.get("topic", ""),
"topic_zh": item.get("topic_zh", ""),
"turn_count": item.get("turn_count", None),
"input": dialogue_text,
"features": features,
"topic_group": topic_group(features),
"target_len": target_len,
"output": " ".join(history),
"tag_counts": dict(tag_counts),
"role_tag_counts": {
"client": dict(role_counts["client"]),
"counselor": dict(role_counts["counselor"]),
},
}
if args.keep_trace:
result["trace"] = trace
return result
def output_path(args):
name = args.output_name
if not name:
stamp = datetime.now().strftime("%Y%m%d_%H%M%S")
count = "all" if args.limit is None else str(args.limit)
name = f"da_trajectories_offset{args.offset}_n{count}_{stamp}.jsonl"
return Path(args.output_dir) / name
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("--input-file", default=INPUT_FILE)
parser.add_argument("--annotated-dir", default=ANNOTATED_DIR)
parser.add_argument("--checkpoint-dir", default=CHECKPOINT_DIR)
parser.add_argument("--output-dir", default=OUTPUT_DIR)
parser.add_argument("--output-name", default="")
parser.add_argument("--stats-file", default=STATS_FILE)
parser.add_argument("--offset", type=int, default=0)
parser.add_argument("--limit", type=int, default=20)
parser.add_argument("--spread", action="store_true")
parser.add_argument("--num-shards", type=int, default=1)
parser.add_argument("--shard-id", type=int, default=0)
parser.add_argument("--seed", type=int, default=20260703)
parser.add_argument("--min-len", type=int, default=64)
parser.add_argument("--max-len", type=int, default=92)
parser.add_argument("--dialogue-chars", type=int, default=900)
parser.add_argument("--max-length", type=int, default=2048)
parser.add_argument("--top-k", type=int, default=5)
parser.add_argument("--top-p", type=float, default=0.88)
parser.add_argument("--temperature", type=float, default=0.64)
parser.add_argument("--deterministic-gap", type=float, default=0.48)
parser.add_argument("--w-lm", type=float, default=4.0)
parser.add_argument("--w-stage", type=float, default=0.08)
parser.add_argument("--w-trans", type=float, default=0.12)
parser.add_argument("--w-global", type=float, default=0.01)
parser.add_argument("--w-repeat", type=float, default=1.85)
parser.add_argument("--w-diversity", type=float, default=0.52)
parser.add_argument("--w-content", type=float, default=1.18)
parser.add_argument("--w-phase", type=float, default=0.50)
parser.add_argument("--no-4bit", action="store_true")
parser.add_argument("--keep-trace", action="store_true")
parser.add_argument("--resume", action="store_true")
return parser.parse_args()
def main():
args = parse_args()
if args.limit is not None and args.limit <= 0:
args.limit = None
os.makedirs(args.output_dir, exist_ok=True)
out_path = output_path(args)
summary_path = out_path.with_suffix(".summary.json")
config_path = out_path.with_suffix(".config.json")
save_json(config_path, vars(args))
done_ids = set()
if args.resume and out_path.exists():
with open(out_path, "r", encoding="utf-8") as f:
for line in f:
if line.strip():
done_ids.add(json.loads(line).get("id"))
print(f"[INFO] resume enabled, loaded done ids: {len(done_ids)}", flush=True)
elif out_path.exists():
out_path.unlink()
data = read_jsonl(args.input_file, limit=args.limit, offset=args.offset, spread=args.spread)
if args.num_shards > 1:
if args.shard_id < 0 or args.shard_id >= args.num_shards:
raise ValueError("--shard-id must be in [0, --num-shards)")
data = [item for pos, item in enumerate(data) if pos % args.num_shards == args.shard_id]
print(f"[INFO] loaded input rows: {len(data)}", flush=True)
print(f"[INFO] output: {out_path}", flush=True)
tokenizer, model = load_model(args.checkpoint_dir, use_4bit=not args.no_4bit)
if args.stats_file and os.path.exists(args.stats_file):
stats = load_stats_file(args.stats_file)
print(f"[INFO] loaded stats file: {args.stats_file}", flush=True)
else:
stats = build_stats(load_annotated(args.annotated_dir))
generated = []
for local_i, item in enumerate(data):
sample_index = int(item.get("_global_index", args.offset + local_i))
item_id = item.get("id", str(sample_index))
if item_id in done_ids:
continue
print("\n" + "-" * 60, flush=True)
print(f"[INFO] sample {local_i + 1}/{len(data)} global_index={sample_index} id={item_id}", flush=True)
result = generate_trajectory(item, tokenizer, model, stats, args, sample_index)
append_jsonl(out_path, result)
generated.append(result)
print(
f"[OK] len={result['target_len']} topic={result.get('topic_zh') or result.get('topic')} "
f"distinct_tags={len(result['tag_counts'])}",
flush=True,
)
print(result["output"], flush=True)
lengths = [x["target_len"] for x in generated]
all_tags = Counter()
for x in generated:
all_tags.update(x["tag_counts"])
summary = {
"output": str(out_path),
"generated": len(generated),
"offset": args.offset,
"limit": args.limit,
"length_min": min(lengths) if lengths else None,
"length_max": max(lengths) if lengths else None,
"length_avg": sum(lengths) / len(lengths) if lengths else None,
"tag_counts": dict(all_tags),
}
save_json(summary_path, summary)
print("\n[INFO] summary:", json.dumps(summary, ensure_ascii=False, indent=2), flush=True)
if __name__ == "__main__":
main()
|