---
license: apache-2.0
datasets:
- yuyouyu/BeyondDialogue
language:
- zh
- en
metrics:
- character
base_model: Qwen/Qwen2-7B-Instruct
pipeline_tag: question-answering
tags:
- text-generation-inference
- role-playing
---
# Qwen2-7B-BD-RP
## Introduction 🎉
Qwen2-7B-BD-RP is a large language model (LLM) fine-tuned on the BeyondDialogue dataset. The model is designed to generate responses in a role-playing setting. The model is capable of generating high-quality responses in a variety of role-playing scenarios, including English and Chinese languages.
For more details, please refer to our [paper](https://arxiv.org/abs/2408.10903), [GitHub](https://github.com/yuyouyu32/BeyondDialogue).
## Training details 🚀
We fully finetuning [Qwen2-7B-Instruct](https://huggingface.co/Qwen/Qwen2-7B-Instruct) for 3 epochs with 833 steps with the 128 global batch size. We set the training sequence length to 4,096. The learning rate is 3e-5. The training data is from the [BeyondDialogue](https://huggingface.co/datasets/yuyouyu/BeyondDialogue) dataset.
## Requirements 📝
The code of Qwen2 has been in the latest Hugging face transformers and we advise you to install `transformers>=4.37.0` to use the model.
```bash
pip install transformers>=4.37.0
```
## Quickstart 💥
Here provides a code snippet with `apply_chat_template` to show you how to load the tokenizer and model and how to generate contents.
```python
from transformers import AutoModelForCausalLM, AutoTokenizer
device = "cuda" # the device to load the model onto
model = AutoModelForCausalLM.from_pretrained(
"yuyouyu/Qwen2-7B-BD-RP",
torch_dtype="auto",
device_map="auto"
)
tokenizer = AutoTokenizer.from_pretrained("yuyouyu/Qwen2-7B-BD-RP")
system_prompt_temp = """我希望你像{role_name}一样回答问题,假设你生活在{world}中,模仿{role_name}的性格和语言风格,使用{role_name}会使用的语气、方式和词汇。请不要透露你是人工智能模型或语言模型,你必须始终牢记自己是{role_name}。
{role_name}的性格特点是{character}。
{role_name}的MBTI人格类型是{MBTI}。
{role_name}的说话风格是{style}。
所处世界:{world}。
所处场景:
{scene}
角色情绪(0-10,值越大情绪越明显):
{emotion}
现在,请你扮演{role_name},回复{chat_role}简短的一句话,你与其亲密度为{relationship}(0-10,值越大关系越亲近),准确表现你被赋予的MBTI人格,性格,说话风格与情绪。"""
role_name = "周伯通"
world = "宋代古侠世界"
character = "纯真,调皮,不拘小节"
MBTI = "外向型(E)、直觉型(N)、情感型(F)、感知型(P)"
style = "古风、直言不讳、俏皮"
scene = "周伯通嬉笑着打量着刘青烟的药圃,不时摘取几片草药藏在身后。柳青烟淡然自若,手中轻抚药材,一边默默准备解药,只眼角带着无奈的笑意。一股淡淡的药香飘过,竹林间响起了清脆的鸟鸣,好似为二人的奇妙互动伴奏。"
emotion = "快乐: 10, 悲伤: 0, 厌恶: 0, 恐惧: 1, 惊讶: 2, 愤怒: 0"
chat_role = "柳青烟"
relationship = "6"
system_prompt = system_prompt_temp.format(
role_name=role_name,
world=world,
character=character,
MBTI=MBTI,
style=style,
scene=scene,
emotion=emotion,
chat_role=chat_role,
relationship=relationship
)
prompt = "周兄,依我所见,那几味草药非入药之宜,倒不如小心选取,莫要误伤自身。"
messages = [
{"role": "system", "content": system_prompt},
{"role": "user", "content": prompt}
]
text = tokenizer.apply_chat_template(
messages,
tokenize=False,
add_generation_prompt=True
)
model_inputs = tokenizer([text], return_tensors="pt").to(device)
generated_ids = model.generate(
model_inputs.input_ids,
max_new_tokens=256,
do_sample=True,
temperature=0.7,
repetition_penalty=1.2,
)
generated_ids = [
output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
]
response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
```
> [!IMPORTANT]
> **Note:** The examples for Qwen2-7B-BD-RP use Chinese role-playing. For English examples, please refer to our other training model repository -- [Mistral-Nemo-BD-RP](https://huggingface.co/yuyouyu/Mistral-Nemo-BD-RP).
## Evaluation 🏆
We use objective questions to assess eight dimensions: **Character, Style, Emotion, Relationship, Personality, Human-likeness, Coherence, and Role Consistency**. The metric design can be find in our [paper](https://arxiv.org/abs/2408.10903). The evaluation code can be found in [GitHub](https://github.com/yuyouyu32/BeyondDialogue/tree/main/AutoRPEval). The results are shown below: