File size: 3,023 Bytes
d34e5bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Generate the next counselor response with the merged CounselTrajLM model."""

import argparse
import json
from pathlib import Path

import torch
from transformers import AutoModelForCausalLM, AutoTokenizer

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


def load_messages(path):
    messages = json.loads(Path(path).read_text(encoding="utf-8"))
    if not isinstance(messages, list) or not messages:
        raise ValueError("Provide a non-empty JSON list of chat messages.")
    for i, message in enumerate(messages):
        if not isinstance(message, dict):
            raise ValueError("Each message must be a JSON object.")
        if message.get("role") not in {"system", "user", "assistant"}:
            raise ValueError("Message roles must be system, user, or assistant.")
        if message["role"] == "system" and i != 0:
            raise ValueError("A system message may appear only at the beginning.")
        if not isinstance(message.get("content"), str) or not message["content"].strip():
            raise ValueError("Each message must have non-empty text content.")
    if messages[-1]["role"] != "user":
        raise ValueError("The dialogue must end with a client (user) message.")
    if messages[0]["role"] != "system":
        messages.insert(0, {"role": "system", "content": SYSTEM_PROMPT})
    return messages


def main():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--model", default="UEC-InabaLab/CounselTrajLM-Qwen2.5-7B")
    parser.add_argument("--messages", type=Path, default=Path(__file__).parent / "examples/history.json")
    parser.add_argument("--max-new-tokens", type=int, default=160)
    parser.add_argument("--greedy", action="store_true")
    args = parser.parse_args()
    messages = load_messages(args.messages)
    tokenizer = AutoTokenizer.from_pretrained(args.model)
    model = AutoModelForCausalLM.from_pretrained(
        args.model, dtype=torch.bfloat16, device_map="auto"
    ).eval()
    prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
    inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
    if inputs.input_ids.shape[-1] + args.max_new_tokens > model.config.max_position_embeddings:
        raise ValueError("The input plus requested output exceeds the model context window.")
    generation = {
        "max_new_tokens": args.max_new_tokens,
        "do_sample": not args.greedy,
        "repetition_penalty": 1.05,
    }
    if not args.greedy:
        generation.update(temperature=0.7, top_p=0.8, top_k=20)
    with torch.inference_mode():
        outputs = model.generate(**inputs, **generation)
    reply = tokenizer.decode(outputs[0, inputs.input_ids.shape[-1]:], skip_special_tokens=True)
    print(reply.strip())


if __name__ == "__main__":
    main()