File size: 3,971 Bytes
4a13f72
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Generate the next Chinese counselor reply with the complete CounselTrajLM model."""

import argparse
import json
from pathlib import Path

import torch
from transformers import AutoModelForCausalLM, AutoTokenizer


REPO_ID = "UEC-InabaLab/CounselTrajLM-Qwen3-8B"
SYSTEM_PROMPT = (
    "你是一名专业、温和、自然的线上心理咨询师。"
    "请根据对话历史生成下一句咨询师回复。"
    "回复只写咨询师要说的话,不要写说话人名称,不要写对话标签。"
)


def build_prompt(tokenizer, history):
    messages = [
        {"role": "system", "content": SYSTEM_PROMPT},
        {
            "role": "user",
            "content": f"对话历史:\n{history[-1800:]}\n\n请生成下一句咨询师回复。",
        },
    ]
    return tokenizer.apply_chat_template(
        messages,
        tokenize=False,
        add_generation_prompt=True,
        enable_thinking=False,
    )


def load_model(model_id=REPO_ID, local_files_only=False):
    tokenizer = AutoTokenizer.from_pretrained(model_id, local_files_only=local_files_only)
    if tokenizer.pad_token_id is None:
        tokenizer.pad_token = tokenizer.eos_token
    kwargs = {
        "dtype": torch.bfloat16,
        "device_map": "auto",
        "attn_implementation": "sdpa",
        "local_files_only": local_files_only,
    }
    model = AutoModelForCausalLM.from_pretrained(model_id, **kwargs)
    model.eval()
    return tokenizer, model


def generate_reply(tokenizer, model, history, max_new_tokens=256):
    prompt = build_prompt(tokenizer, history)
    inputs = tokenizer(
        prompt,
        return_tensors="pt",
        add_special_tokens=False,
        truncation=True,
        max_length=2048,
    ).to(model.device)
    with torch.inference_mode():
        output = model.generate(
            **inputs,
            max_new_tokens=max_new_tokens,
            do_sample=False,
            repetition_penalty=1.05,
            pad_token_id=tokenizer.pad_token_id,
            eos_token_id=tokenizer.eos_token_id,
        )
    return tokenizer.decode(
        output[0, inputs["input_ids"].shape[1]:], skip_special_tokens=True
    ).strip()


def main():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--model", "--adapter", dest="model", default=REPO_ID)
    parser.add_argument("--history")
    parser.add_argument("--input-file", type=Path)
    parser.add_argument("--output-file", type=Path)
    parser.add_argument("--max-new-tokens", type=int, default=256)
    parser.add_argument("--local-files-only", action="store_true")
    args = parser.parse_args()
    if bool(args.history) == bool(args.input_file):
        parser.error("Provide exactly one of --history or --input-file")
    rows = [{"id": "example", "history": args.history}] if args.history else [
        json.loads(line)
        for line in args.input_file.read_text(encoding="utf-8").splitlines()
        if line.strip()
    ]
    if not rows or any(not isinstance(row.get("history"), str) or not row["history"].strip() for row in rows):
        parser.error("Every input row must contain a nonempty history string")
    tokenizer, model = load_model(
        args.model,
        local_files_only=args.local_files_only,
    )
    results = []
    for row in rows:
        reply = generate_reply(tokenizer, model, row["history"], args.max_new_tokens)
        if not reply:
            raise RuntimeError("Empty reply for input " + str(row.get("id", "")))
        result = {"id": row.get("id", ""), "history": row["history"], "response": reply}
        results.append(result)
        print(json.dumps(result, ensure_ascii=False), flush=True)
    if args.output_file:
        args.output_file.parent.mkdir(parents=True, exist_ok=True)
        args.output_file.write_text(
            "".join(json.dumps(row, ensure_ascii=False) + "\n" for row in results),
            encoding="utf-8",
        )


if __name__ == "__main__":
    main()