ZhiyangQi97 commited on
Commit
9203391
·
verified ·
1 Parent(s): 09f63f4

Release CounselTrajLM Qwen3.5-9B counselor response adapter

Browse files
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ tokenizer.json filter=lfs diff=lfs merge=lfs -text
README.md ADDED
@@ -0,0 +1,151 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ language:
3
+ - zh
4
+ base_model: Qwen/Qwen3.5-9B
5
+ base_model_relation: finetune
6
+ library_name: peft
7
+ pipeline_tag: text-generation
8
+ tags:
9
+ - qwen3_5
10
+ - lora
11
+ - psychological-counseling
12
+ - counselor-response-generation
13
+ ---
14
+
15
+ # CounselTrajLM
16
+
17
+ CounselTrajLM generates the next Chinese counselor response from a counseling
18
+ dialogue history. It is fine-tuned on CounselTrajD and is the dialogue model used
19
+ in the CounselTraj response-level and dialogue-level experiments.
20
+
21
+ This release contains the **LoRA adapter for
22
+ [Qwen/Qwen3.5-9B](https://huggingface.co/Qwen/Qwen3.5-9B)**, its tokenizer, and a
23
+ working inference script. The adapter requires the base model, pinned to revision
24
+ `c202236235762e1c871ad0ccb60c8ee5ba337b9a`. The base model's license is Apache-2.0.
25
+
26
+ Project, dataset, and evaluation prompts:
27
+ [UEC-InabaLab/CounselTraj](https://github.com/UEC-InabaLab/CounselTraj).
28
+ The separate [CounselTraj-DA](https://huggingface.co/UEC-InabaLab/CounselTraj-DA)
29
+ model plans dialogue-act trajectories for data synthesis.
30
+
31
+ ## Training details
32
+
33
+ The following settings come from the saved training records for the released
34
+ checkpoint.
35
+
36
+ | Item | Value |
37
+ | --- | --- |
38
+ | Base model | Qwen/Qwen3.5-9B, text language-model component |
39
+ | Fine-tuning | QLoRA, NF4 with double quantization and bfloat16 computation |
40
+ | Adapter rank / alpha / dropout | 32 / 64 / 0.05 |
41
+ | Target modules | q_proj, k_proj, v_proj, o_proj, gate_proj, up_proj, down_proj |
42
+ | Adapter parameters | 58,195,968 |
43
+ | Dataset split | Dialogue-disjoint 90% training / 10% validation |
44
+ | Training / validation examples | 425,679 / 47,528 next-response examples |
45
+ | Training epochs | 3 |
46
+ | Final optimizer step | 79,815 |
47
+ | Learning rate / warmup ratio | 0.0002 / 0.06 |
48
+ | Learning-rate schedule | Cosine |
49
+ | Optimizer | paged_adamw_8bit |
50
+ | Maximum training sequence length | 2,048 tokens |
51
+ | Maximum history length | Last 1,800 characters |
52
+ | Per-device training batch size / gradient accumulation | 2 / 1 |
53
+ | Random seed | 20260704 |
54
+ | Recorded final validation loss | 1.5481888056 |
55
+
56
+ The released weights are the final adapter saved after three epochs. The trainer
57
+ record contains no best-validation checkpoint selection. The adapter weights are
58
+ byte-identical to the source final adapter; the release configuration replaces an
59
+ internal base-model path with the official Hub ID and exact revision.
60
+
61
+ ## Training data and references
62
+
63
+ [CounselTrajD](https://github.com/UEC-InabaLab/CounselTraj#counseltrajd) contains
64
+ 10,964 synthetic Chinese counseling dialogues expanded from three sources:
65
+
66
+ - SmileChat: Qiu et al. (2024), [SMILE: Single-turn to Multi-turn Inclusive Language Expansion via ChatGPT for Mental Health Support](https://aclanthology.org/2024.findings-emnlp.34/).
67
+ - PsyDTCorpus: Xie et al. (2025), [PsyDT: Using LLMs to Construct the Digital Twin of Psychological Counselor with Personalized Counseling Style for Psychological Counseling](https://aclanthology.org/2025.acl-long.55/).
68
+ - CPsyCounD: Zhang et al. (2024), [CPsyCoun: A Report-based Multi-turn Dialogue Reconstruction and Evaluation Framework for Chinese Psychological Counseling](https://aclanthology.org/2024.findings-acl.830/).
69
+
70
+ See the project's [licensing scope](https://github.com/UEC-InabaLab/CounselTraj/blob/master/LICENSING.md)
71
+ and [source notices](https://github.com/UEC-InabaLab/CounselTraj/blob/master/THIRD_PARTY_NOTICES.md)
72
+ for the source-specific data terms.
73
+
74
+ ## Generate a reply
75
+
76
+ Authenticate with an account that can access this private model, then download
77
+ the adapter and run the included example:
78
+
79
+ ```bash
80
+ pip install huggingface_hub
81
+ hf auth login
82
+ python -c 'from huggingface_hub import snapshot_download; snapshot_download("UEC-InabaLab/CounselTrajLM", local_dir="CounselTrajLM")'
83
+ cd CounselTrajLM
84
+ pip install -r requirements.txt
85
+ python generate_reply.py --adapter . --input-file examples/histories.jsonl
86
+ ```
87
+
88
+ Or supply a history directly:
89
+
90
+ ```bash
91
+ python generate_reply.py --adapter . --history 'client:最近工作压力很大,晚上总是睡不好。'
92
+ ```
93
+
94
+ The script uses the original counselor system prompt and user-history format,
95
+ with `enable_thinking=False`. It loads the text language-model component and
96
+ adapter with NF4 quantization on CUDA. Use `--no-4bit` on supported hardware with
97
+ enough memory to load the base model without 4-bit quantization.
98
+
99
+ Input JSONL rows contain an optional `id` and a nonempty `history` string. The
100
+ script outputs JSON with the generated `response`. The three bundled histories
101
+ are newly written synthetic examples.
102
+
103
+ ## Load in Python
104
+
105
+ After downloading the repository and installing its requirements:
106
+
107
+ ```python
108
+ from generate_reply import load_model, generate_reply
109
+
110
+ tokenizer, model = load_model("UEC-InabaLab/CounselTrajLM")
111
+ response = generate_reply(
112
+ tokenizer,
113
+ model,
114
+ "client:最近工作压力很大,晚上总是睡不好。",
115
+ )
116
+ print(response)
117
+ ```
118
+
119
+ ## Evaluation
120
+
121
+ The response-level evaluation uses PsyDial-D101 from Qiu and Lan (2025),
122
+ [PsyDial: A Large-scale Long-term Conversational Dataset for Mental Health Support](https://aclanthology.org/2025.acl-long.1049/).
123
+ The dialogue-level evaluation uses client profiles from Pan et al. (2026),
124
+ [PsychEval: A Multi-Session and Multi-Therapy Benchmark for High-Realism AI Psychological Counselor](https://arxiv.org/abs/2601.01802).
125
+ The [evaluation documentation](https://github.com/UEC-InabaLab/CounselTraj/tree/master/evaluation)
126
+ provides the prompts, dimensions, and research foundations.
127
+
128
+ ## Files
129
+
130
+ - `adapter_model.safetensors` and `adapter_config.json`: final LoRA adapter.
131
+ - `tokenizer.json`, `tokenizer_config.json`, and `chat_template.jinja`: saved tokenizer and chat template.
132
+ - `generate_reply.py`: inference entry point.
133
+ - `requirements.txt`: inference dependencies.
134
+ - `model_details.json`: actual training settings and checkpoint provenance.
135
+ - `examples/histories.jsonl`: synthetic example inputs.
136
+ - `validation.json`: release integrity and generation checks.
137
+ - `SHA256SUMS`: file checksums.
138
+
139
+ The model is intended for counseling dialogue research. Its generated responses
140
+ have not been validated as clinical care or crisis support.
141
+
142
+ ## Citation
143
+
144
+ ```bibtex
145
+ @inproceedings{qi-inaba-2027-counseltraj,
146
+ title = "{CounselTraj}: Synthesizing Process-Aware Psychological Counseling Dialogues via Dialogue-Act Trajectory Planning",
147
+ author = "Qi, Zhiyang and Inaba, Michimasa",
148
+ booktitle = "Proceedings of the 20th Conference of the European Chapter of the Association for Computational Linguistics",
149
+ year = "2027",
150
+ }
151
+ ```
SHA256SUMS ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ a2f7171185c0b531753de444438e353b53420add23b9fe06bf6b78171cb70619 README.md
2
+ d8476967ec836156efd1b99d571c78ec5d57fde8eba2633d34f24ea8816ce212 adapter_config.json
3
+ c4d89ce3b6cfdb53dba2927683331e2182c8767f3380527a78f0533b0f31e5bd adapter_model.safetensors
4
+ a4aee8afcf2e0711942cf848899be66016f8d14a889ff9ede07bca099c28f715 chat_template.jinja
5
+ 72b038eb2dd906df9d8efaf5d637a844840fc2547c1a2f2dffa13644d488adac examples/histories.jsonl
6
+ 553a41c7ace9b01e76c57a6e3fb789715f89dc42b8461d9de16bf7380d0fec93 examples/replies.jsonl
7
+ 7ebe4f0cc7099835e699f6787ee14194fe2cee3dd38b57c9f541fab79c484aee generate_reply.py
8
+ 4ad87e0e39e963397f1412a551c1fe04707512eff5dc7f030e1d9ebd7ba747c6 model_details.json
9
+ 1bc723f464c8003c711d2eaa47664b028109882d48760fa76f5da12a81a41b2b requirements.txt
10
+ f399b3cd12fa270d51457bb749fb30863521e8359b8a27059c71b6c2f7d6dd6c tokenizer.json
11
+ bee8eba30f0eb4af73c0fe2cd06d0f89b657d7819941c438157ec42f7c80ea87 tokenizer_config.json
12
+ 054f254cbe4a7b5ed11b18a83204542452783da029650e619e7e17c2f6792308 validation.json
adapter_config.json ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "alora_invocation_tokens": null,
3
+ "alpha_pattern": {},
4
+ "arrow_config": null,
5
+ "auto_mapping": null,
6
+ "base_model_name_or_path": "Qwen/Qwen3.5-9B",
7
+ "bias": "none",
8
+ "corda_config": null,
9
+ "ensure_weight_tying": false,
10
+ "eva_config": null,
11
+ "exclude_modules": null,
12
+ "fan_in_fan_out": false,
13
+ "inference_mode": true,
14
+ "init_lora_weights": true,
15
+ "layer_replication": null,
16
+ "layers_pattern": null,
17
+ "layers_to_transform": null,
18
+ "loftq_config": {},
19
+ "lora_alpha": 64,
20
+ "lora_bias": false,
21
+ "lora_dropout": 0.05,
22
+ "lora_ga_config": null,
23
+ "megatron_config": null,
24
+ "megatron_core": "megatron.core",
25
+ "modules_to_save": null,
26
+ "peft_type": "LORA",
27
+ "peft_version": "0.19.1",
28
+ "qalora_group_size": 16,
29
+ "r": 32,
30
+ "rank_pattern": {},
31
+ "revision": "c202236235762e1c871ad0ccb60c8ee5ba337b9a",
32
+ "target_modules": [
33
+ "k_proj",
34
+ "v_proj",
35
+ "gate_proj",
36
+ "q_proj",
37
+ "o_proj",
38
+ "up_proj",
39
+ "down_proj"
40
+ ],
41
+ "target_parameters": null,
42
+ "task_type": "CAUSAL_LM",
43
+ "trainable_token_indices": null,
44
+ "use_bdlora": null,
45
+ "use_dora": false,
46
+ "use_qalora": false,
47
+ "use_rslora": false
48
+ }
adapter_model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:c4d89ce3b6cfdb53dba2927683331e2182c8767f3380527a78f0533b0f31e5bd
3
+ size 232818064
chat_template.jinja ADDED
@@ -0,0 +1,154 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {%- set image_count = namespace(value=0) %}
2
+ {%- set video_count = namespace(value=0) %}
3
+ {%- macro render_content(content, do_vision_count, is_system_content=false) %}
4
+ {%- if content is string %}
5
+ {{- content }}
6
+ {%- elif content is iterable and content is not mapping %}
7
+ {%- for item in content %}
8
+ {%- if 'image' in item or 'image_url' in item or item.type == 'image' %}
9
+ {%- if is_system_content %}
10
+ {{- raise_exception('System message cannot contain images.') }}
11
+ {%- endif %}
12
+ {%- if do_vision_count %}
13
+ {%- set image_count.value = image_count.value + 1 %}
14
+ {%- endif %}
15
+ {%- if add_vision_id %}
16
+ {{- 'Picture ' ~ image_count.value ~ ': ' }}
17
+ {%- endif %}
18
+ {{- '<|vision_start|><|image_pad|><|vision_end|>' }}
19
+ {%- elif 'video' in item or item.type == 'video' %}
20
+ {%- if is_system_content %}
21
+ {{- raise_exception('System message cannot contain videos.') }}
22
+ {%- endif %}
23
+ {%- if do_vision_count %}
24
+ {%- set video_count.value = video_count.value + 1 %}
25
+ {%- endif %}
26
+ {%- if add_vision_id %}
27
+ {{- 'Video ' ~ video_count.value ~ ': ' }}
28
+ {%- endif %}
29
+ {{- '<|vision_start|><|video_pad|><|vision_end|>' }}
30
+ {%- elif 'text' in item %}
31
+ {{- item.text }}
32
+ {%- else %}
33
+ {{- raise_exception('Unexpected item type in content.') }}
34
+ {%- endif %}
35
+ {%- endfor %}
36
+ {%- elif content is none or content is undefined %}
37
+ {{- '' }}
38
+ {%- else %}
39
+ {{- raise_exception('Unexpected content type.') }}
40
+ {%- endif %}
41
+ {%- endmacro %}
42
+ {%- if not messages %}
43
+ {{- raise_exception('No messages provided.') }}
44
+ {%- endif %}
45
+ {%- if tools and tools is iterable and tools is not mapping %}
46
+ {{- '<|im_start|>system\n' }}
47
+ {{- "# Tools\n\nYou have access to the following functions:\n\n<tools>" }}
48
+ {%- for tool in tools %}
49
+ {{- "\n" }}
50
+ {{- tool | tojson }}
51
+ {%- endfor %}
52
+ {{- "\n</tools>" }}
53
+ {{- '\n\nIf you choose to call a function ONLY reply in the following format with NO suffix:\n\n<tool_call>\n<function=example_function_name>\n<parameter=example_parameter_1>\nvalue_1\n</parameter>\n<parameter=example_parameter_2>\nThis is the value for the second parameter\nthat can span\nmultiple lines\n</parameter>\n</function>\n</tool_call>\n\n<IMPORTANT>\nReminder:\n- Function calls MUST follow the specified format: an inner <function=...></function> block must be nested within <tool_call></tool_call> XML tags\n- Required parameters MUST be specified\n- You may provide optional reasoning for your function call in natural language BEFORE the function call, but NOT after\n- If there is no function call available, answer the question like normal with your current knowledge and do not tell the user about function calls\n</IMPORTANT>' }}
54
+ {%- if messages[0].role == 'system' %}
55
+ {%- set content = render_content(messages[0].content, false, true)|trim %}
56
+ {%- if content %}
57
+ {{- '\n\n' + content }}
58
+ {%- endif %}
59
+ {%- endif %}
60
+ {{- '<|im_end|>\n' }}
61
+ {%- else %}
62
+ {%- if messages[0].role == 'system' %}
63
+ {%- set content = render_content(messages[0].content, false, true)|trim %}
64
+ {{- '<|im_start|>system\n' + content + '<|im_end|>\n' }}
65
+ {%- endif %}
66
+ {%- endif %}
67
+ {%- set ns = namespace(multi_step_tool=true, last_query_index=messages|length - 1) %}
68
+ {%- for message in messages[::-1] %}
69
+ {%- set index = (messages|length - 1) - loop.index0 %}
70
+ {%- if ns.multi_step_tool and message.role == "user" %}
71
+ {%- set content = render_content(message.content, false)|trim %}
72
+ {%- if not(content.startswith('<tool_response>') and content.endswith('</tool_response>')) %}
73
+ {%- set ns.multi_step_tool = false %}
74
+ {%- set ns.last_query_index = index %}
75
+ {%- endif %}
76
+ {%- endif %}
77
+ {%- endfor %}
78
+ {%- if ns.multi_step_tool %}
79
+ {{- raise_exception('No user query found in messages.') }}
80
+ {%- endif %}
81
+ {%- for message in messages %}
82
+ {%- set content = render_content(message.content, true)|trim %}
83
+ {%- if message.role == "system" %}
84
+ {%- if not loop.first %}
85
+ {{- raise_exception('System message must be at the beginning.') }}
86
+ {%- endif %}
87
+ {%- elif message.role == "user" %}
88
+ {{- '<|im_start|>' + message.role + '\n' + content + '<|im_end|>' + '\n' }}
89
+ {%- elif message.role == "assistant" %}
90
+ {%- set reasoning_content = '' %}
91
+ {%- if message.reasoning_content is string %}
92
+ {%- set reasoning_content = message.reasoning_content %}
93
+ {%- else %}
94
+ {%- if '</think>' in content %}
95
+ {%- set reasoning_content = content.split('</think>')[0].rstrip('\n').split('<think>')[-1].lstrip('\n') %}
96
+ {%- set content = content.split('</think>')[-1].lstrip('\n') %}
97
+ {%- endif %}
98
+ {%- endif %}
99
+ {%- set reasoning_content = reasoning_content|trim %}
100
+ {%- if loop.index0 > ns.last_query_index %}
101
+ {{- '<|im_start|>' + message.role + '\n<think>\n' + reasoning_content + '\n</think>\n\n' + content }}
102
+ {%- else %}
103
+ {{- '<|im_start|>' + message.role + '\n' + content }}
104
+ {%- endif %}
105
+ {%- if message.tool_calls and message.tool_calls is iterable and message.tool_calls is not mapping %}
106
+ {%- for tool_call in message.tool_calls %}
107
+ {%- if tool_call.function is defined %}
108
+ {%- set tool_call = tool_call.function %}
109
+ {%- endif %}
110
+ {%- if loop.first %}
111
+ {%- if content|trim %}
112
+ {{- '\n\n<tool_call>\n<function=' + tool_call.name + '>\n' }}
113
+ {%- else %}
114
+ {{- '<tool_call>\n<function=' + tool_call.name + '>\n' }}
115
+ {%- endif %}
116
+ {%- else %}
117
+ {{- '\n<tool_call>\n<function=' + tool_call.name + '>\n' }}
118
+ {%- endif %}
119
+ {%- if tool_call.arguments is defined %}
120
+ {%- for args_name, args_value in tool_call.arguments|items %}
121
+ {{- '<parameter=' + args_name + '>\n' }}
122
+ {%- set args_value = args_value | tojson | safe if args_value is mapping or (args_value is sequence and args_value is not string) else args_value | string %}
123
+ {{- args_value }}
124
+ {{- '\n</parameter>\n' }}
125
+ {%- endfor %}
126
+ {%- endif %}
127
+ {{- '</function>\n</tool_call>' }}
128
+ {%- endfor %}
129
+ {%- endif %}
130
+ {{- '<|im_end|>\n' }}
131
+ {%- elif message.role == "tool" %}
132
+ {%- if loop.previtem and loop.previtem.role != "tool" %}
133
+ {{- '<|im_start|>user' }}
134
+ {%- endif %}
135
+ {{- '\n<tool_response>\n' }}
136
+ {{- content }}
137
+ {{- '\n</tool_response>' }}
138
+ {%- if not loop.last and loop.nextitem.role != "tool" %}
139
+ {{- '<|im_end|>\n' }}
140
+ {%- elif loop.last %}
141
+ {{- '<|im_end|>\n' }}
142
+ {%- endif %}
143
+ {%- else %}
144
+ {{- raise_exception('Unexpected message role.') }}
145
+ {%- endif %}
146
+ {%- endfor %}
147
+ {%- if add_generation_prompt %}
148
+ {{- '<|im_start|>assistant\n' }}
149
+ {%- if enable_thinking is defined and enable_thinking is false %}
150
+ {{- '<think>\n\n</think>\n\n' }}
151
+ {%- else %}
152
+ {{- '<think>\n' }}
153
+ {%- endif %}
154
+ {%- endif %}
examples/histories.jsonl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ {"id": "synthetic-anxiety", "history": "client:最近快考试了,我总担心考不好,晚上也睡不着。\ncounselor:考试的压力似乎让你一直很紧张。\nclient:是的,一躺下就忍不住想最坏的结果。"}
2
+ {"id": "synthetic-interpersonal", "history": "client:最近朋友很少主动联系我,我总觉得自己不重要。\ncounselor:你似乎很在意自己在这段友谊中的位置。\nclient:嗯,但我也不敢问,怕别人觉得我太黏人。"}
3
+ {"id": "synthetic-work", "history": "client:刚换了一份工作,我觉得自己什么都做不好。\ncounselor:面对新的工作要求,你好像对自己有很多怀疑。\nclient:是的,每次遇到不会的事情,我就觉得他们一定后悔招了我。"}
examples/replies.jsonl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ {"id": "synthetic-anxiety", "history": "client:最近快考试了,我总担心考不好,晚上也睡不着。\ncounselor:考试的压力似乎让你一直很紧张。\nclient:是的,一躺下就忍不住想最坏的结果。", "response": "你能把这种“越想睡越清醒”的过程说出来,已经是在认真观察自己的状态了。"}
2
+ {"id": "synthetic-interpersonal", "history": "client:最近朋友很少主动联系我,我总觉得自己不重要。\ncounselor:你似乎很在意自己在这段友谊中的位置。\nclient:嗯,但我也不敢问,怕别人觉得我太黏人。", "response": "你能把这种担心说出来,其实已经很不容易了。你不是简单地“想太多”,而是在努力理解关系里发生了什么。"}
3
+ {"id": "synthetic-work", "history": "client:刚换了一份工作,我觉得自己什么都做不好。\ncounselor:面对新的工作要求,你好像对自己有很多怀疑。\nclient:是的,每次遇到不会的事情,我就觉得他们一定后悔招了我。", "response": "你能把这种“被否定”的感觉说出来,其实很不容易,尤其是在刚进入新环境的时候。"}
generate_reply.py ADDED
@@ -0,0 +1,129 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Generate the next Chinese counselor reply with the CounselTrajLM adapter."""
2
+
3
+ import argparse
4
+ import json
5
+ from pathlib import Path
6
+
7
+ import torch
8
+ from peft import PeftModel
9
+ from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
10
+
11
+
12
+ REPO_ID = "UEC-InabaLab/CounselTrajLM"
13
+ BASE_MODEL = "Qwen/Qwen3.5-9B"
14
+ BASE_REVISION = "c202236235762e1c871ad0ccb60c8ee5ba337b9a"
15
+ SYSTEM_PROMPT = (
16
+ "你是一名专业、温和、自然的线上心理咨询师。"
17
+ "请根据对话历史生成下一句咨询师回复。"
18
+ "回复只写咨询师要说的话,不要写说话人名称,不要写对话标签。"
19
+ )
20
+
21
+
22
+ def build_prompt(tokenizer, history):
23
+ messages = [
24
+ {"role": "system", "content": SYSTEM_PROMPT},
25
+ {
26
+ "role": "user",
27
+ "content": f"对话历史:\n{history[-1800:]}\n\n请生成下一句咨询师回复。",
28
+ },
29
+ ]
30
+ return tokenizer.apply_chat_template(
31
+ messages,
32
+ tokenize=False,
33
+ add_generation_prompt=True,
34
+ enable_thinking=False,
35
+ )
36
+
37
+
38
+ def load_model(adapter, base_model=BASE_MODEL, local_files_only=False, use_4bit=True):
39
+ tokenizer = AutoTokenizer.from_pretrained(adapter, local_files_only=local_files_only)
40
+ if tokenizer.pad_token_id is None:
41
+ tokenizer.pad_token = tokenizer.eos_token
42
+ kwargs = {
43
+ "dtype": torch.bfloat16,
44
+ "device_map": "auto",
45
+ "attn_implementation": "eager",
46
+ "local_files_only": local_files_only,
47
+ }
48
+ if base_model == BASE_MODEL:
49
+ kwargs["revision"] = BASE_REVISION
50
+ if use_4bit:
51
+ kwargs["quantization_config"] = BitsAndBytesConfig(
52
+ load_in_4bit=True,
53
+ bnb_4bit_use_double_quant=True,
54
+ bnb_4bit_quant_type="nf4",
55
+ bnb_4bit_compute_dtype=torch.bfloat16,
56
+ )
57
+ base = AutoModelForCausalLM.from_pretrained(base_model, **kwargs)
58
+ model = PeftModel.from_pretrained(base, adapter, local_files_only=local_files_only)
59
+ model.eval()
60
+ return tokenizer, model
61
+
62
+
63
+ def generate_reply(tokenizer, model, history, max_new_tokens=256):
64
+ prompt = build_prompt(tokenizer, history)
65
+ inputs = tokenizer(
66
+ prompt,
67
+ return_tensors="pt",
68
+ add_special_tokens=False,
69
+ truncation=True,
70
+ max_length=2048,
71
+ ).to(model.device)
72
+ with torch.inference_mode():
73
+ output = model.generate(
74
+ **inputs,
75
+ max_new_tokens=max_new_tokens,
76
+ do_sample=False,
77
+ repetition_penalty=1.05,
78
+ pad_token_id=tokenizer.pad_token_id,
79
+ eos_token_id=tokenizer.eos_token_id,
80
+ )
81
+ return tokenizer.decode(
82
+ output[0, inputs["input_ids"].shape[1]:], skip_special_tokens=True
83
+ ).strip()
84
+
85
+
86
+ def main():
87
+ parser = argparse.ArgumentParser(description=__doc__)
88
+ parser.add_argument("--adapter", default=REPO_ID)
89
+ parser.add_argument("--base-model", default=BASE_MODEL)
90
+ parser.add_argument("--history")
91
+ parser.add_argument("--input-file", type=Path)
92
+ parser.add_argument("--output-file", type=Path)
93
+ parser.add_argument("--max-new-tokens", type=int, default=256)
94
+ parser.add_argument("--local-files-only", action="store_true")
95
+ parser.add_argument("--no-4bit", action="store_true")
96
+ args = parser.parse_args()
97
+ if bool(args.history) == bool(args.input_file):
98
+ parser.error("Provide exactly one of --history or --input-file")
99
+ rows = [{"id": "example", "history": args.history}] if args.history else [
100
+ json.loads(line)
101
+ for line in args.input_file.read_text(encoding="utf-8").splitlines()
102
+ if line.strip()
103
+ ]
104
+ if not rows or any(not isinstance(row.get("history"), str) or not row["history"].strip() for row in rows):
105
+ parser.error("Every input row must contain a nonempty history string")
106
+ tokenizer, model = load_model(
107
+ args.adapter,
108
+ base_model=args.base_model,
109
+ local_files_only=args.local_files_only,
110
+ use_4bit=not args.no_4bit,
111
+ )
112
+ results = []
113
+ for row in rows:
114
+ reply = generate_reply(tokenizer, model, row["history"], args.max_new_tokens)
115
+ if not reply:
116
+ raise RuntimeError("Empty reply for input " + str(row.get("id", "")))
117
+ result = {"id": row.get("id", ""), "history": row["history"], "response": reply}
118
+ results.append(result)
119
+ print(json.dumps(result, ensure_ascii=False), flush=True)
120
+ if args.output_file:
121
+ args.output_file.parent.mkdir(parents=True, exist_ok=True)
122
+ args.output_file.write_text(
123
+ "".join(json.dumps(row, ensure_ascii=False) + "\n" for row in results),
124
+ encoding="utf-8",
125
+ )
126
+
127
+
128
+ if __name__ == "__main__":
129
+ main()
model_details.json ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "base_model": "Qwen/Qwen3.5-9B",
3
+ "base_revision": "c202236235762e1c871ad0ccb60c8ee5ba337b9a",
4
+ "adapter_type": "LoRA",
5
+ "rank": 32,
6
+ "alpha": 64,
7
+ "dropout": 0.05,
8
+ "adapter_parameters": 58195968,
9
+ "selected_checkpoint": "final_adapter",
10
+ "selection": "Final checkpoint after three epochs; no best-validation checkpoint reload.",
11
+ "epoch": 3.0,
12
+ "global_step": 79815,
13
+ "train_samples": 425679,
14
+ "validation_samples": 47528,
15
+ "validation_loss": 1.5481888055801392,
16
+ "training_hyperparameters": {
17
+ "max_seq_length": 2048,
18
+ "max_history_chars": 1800,
19
+ "learning_rate": 0.0002,
20
+ "warmup_ratio": 0.06,
21
+ "optim": "paged_adamw_8bit",
22
+ "lr_scheduler_type": "cosine",
23
+ "per_device_train_batch_size": 2,
24
+ "gradient_accumulation_steps": 1,
25
+ "num_train_epochs": 3.0,
26
+ "attn_implementation": "eager",
27
+ "seed": 20260704
28
+ },
29
+ "release_changes": [
30
+ "Replace the internal base-model path with its official Hub ID and pin the exact base revision."
31
+ ]
32
+ }
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ torch>=2.5
2
+ transformers==5.13.0
3
+ peft==0.19.1
4
+ accelerate>=1.13
5
+ bitsandbytes>=0.49.2
6
+ huggingface_hub>=0.34
7
+ safetensors>=0.5
tokenizer.json ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:f399b3cd12fa270d51457bb749fb30863521e8359b8a27059c71b6c2f7d6dd6c
3
+ size 19989424
tokenizer_config.json ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "add_prefix_space": false,
3
+ "audio_bos_token": "<|audio_start|>",
4
+ "audio_eos_token": "<|audio_end|>",
5
+ "audio_token": "<|audio_pad|>",
6
+ "backend": "tokenizers",
7
+ "bos_token": null,
8
+ "clean_up_tokenization_spaces": false,
9
+ "eos_token": "<|im_end|>",
10
+ "errors": "replace",
11
+ "image_token": "<|image_pad|>",
12
+ "is_local": true,
13
+ "local_files_only": true,
14
+ "model_max_length": 262144,
15
+ "model_specific_special_tokens": {
16
+ "audio_bos_token": "<|audio_start|>",
17
+ "audio_eos_token": "<|audio_end|>",
18
+ "audio_token": "<|audio_pad|>",
19
+ "image_token": "<|image_pad|>",
20
+ "video_token": "<|video_pad|>",
21
+ "vision_bos_token": "<|vision_start|>",
22
+ "vision_eos_token": "<|vision_end|>"
23
+ },
24
+ "pad_token": "<|endoftext|>",
25
+ "pretokenize_regex": "(?i:'s|'t|'re|'ve|'m|'ll|'d)|[^\\r\\n\\p{L}\\p{N}]?[\\p{L}\\p{M}]+|\\p{N}| ?[^\\s\\p{L}\\p{M}\\p{N}]+[\\r\\n]*|\\s*[\\r\\n]+|\\s+(?!\\S)|\\s+",
26
+ "split_special_tokens": false,
27
+ "tokenizer_class": "Qwen2Tokenizer",
28
+ "unk_token": null,
29
+ "video_token": "<|video_pad|>",
30
+ "vision_bos_token": "<|vision_start|>",
31
+ "vision_eos_token": "<|vision_end|>"
32
+ }
validation.json ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "adapter_sha256": "c4d89ce3b6cfdb53dba2927683331e2182c8767f3380527a78f0533b0f31e5bd",
3
+ "matches_source_final_adapter": true,
4
+ "adapter_tensors": 256,
5
+ "adapter_parameters": 58195968,
6
+ "all_tensors_finite": true,
7
+ "base_revision": "c202236235762e1c871ad0ccb60c8ee5ba337b9a",
8
+ "base_revision_verified_on_hub": true,
9
+ "model_card_and_python_syntax_validated": true,
10
+ "original_system_prompt_verified": true,
11
+ "gpu_smoke_test": {
12
+ "passed": true,
13
+ "examples": 3,
14
+ "input": "examples/histories.jsonl",
15
+ "output": "examples/replies.jsonl",
16
+ "nonempty_responses": 3,
17
+ "thinking_enabled": false,
18
+ "quantization": "NF4 double quantization",
19
+ "max_new_tokens": 128,
20
+ "torch": "2.5.1+cu121",
21
+ "transformers": "5.13.0",
22
+ "peft": "0.19.1",
23
+ "accelerate": "1.13.0",
24
+ "bitsandbytes": "0.49.2"
25
+ }
26
+ }