XTurnix: Large-Scale Self-Supervised Turn Control through Two-State Binary Decisions
Paper • 2610.04400 • Published • 1
How to use xcczach/xturnix-zh-base with Transformers:
# Use a pipeline as a high-level helper
from transformers import pipeline
pipe = pipeline("text-classification", model="xcczach/xturnix-zh-base", trust_remote_code=True)
messages = [
{"role": "user", "content": "Who are you?"},
]
pipe(messages) # pip install -U transformers accelerate
# Load model directly
from transformers import AutoTokenizer, AutoModelForCausalLM
tokenizer = AutoTokenizer.from_pretrained("xcczach/xturnix-zh-base", trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained("xcczach/xturnix-zh-base", trust_remote_code=True, device_map="auto")
messages = [
{"role": "user", "content": "Who are you?"},
]
inputs = tokenizer.apply_chat_template(
messages,
add_generation_prompt=True,
tokenize=True,
return_dict=True,
return_tensors="pt",
).to(model.device)
outputs = model.generate(**inputs, max_new_tokens=256)
print(tokenizer.decode(outputs[0][inputs["input_ids"].shape[-1]:]))中文 | English
根据转录文本对话历史预测话轮:AI在倾听时继续听还是开始说,在说话时收到用户发言是继续说还是停下去听。
模型持有AI状态,每当收到用户输入进行话轮判断:
| 当前AI状态 | XTurnix判断 | XTurnix输出 |
|---|---|---|
listening |
用户尚未完成当前话轮,应继续听 | keep |
listening |
用户已完成当前话轮,应开始回复 | start |
speaking |
用户输入不要求AI让出当前话轮 | keep |
speaking |
用户输入要求AI停止当前发言并转为倾听 | stop |
from transformers import pipeline
pipe = pipeline(
model="xcczach/xturnix-zh-base",
trust_remote_code=True,
device=0,
dtype="auto",
)
result = pipe(
[
{
"role": "user",
"content": "那空气净化器应该一直开着,还是睡觉前开两小时就够了",
}
],
state="listening",
)
print(result)
推理管线会自动移除最后一条用户消息末尾标点以保证模型生成效果
过长的对话历史会发生截断。system prompt 和最近的对话消息会被保留
输出示例:
{
"action": "<|start|>",
"scores": {
"<|start|>": 0.97,
"<|keep|>": 0.03,
},
}
pipeline 也可以直接接收字符串:
result = pipe("那我们明天几点出发?", state="listening")
批量推理:
results = pipe(
[
{"messages": messages_a, "state": "listening"},
{"messages": messages_b, "state": "speaking"},
]
)
启动服务:
vllm serve xcczach/xturnix-zh-base \
--served-model-name xturnix \
--host 0.0.0.0 \
--port 8000 \
--dtype auto \
--max-model-len 2048 \
--generation-config vllm
客户端调用参考仓库vllm_client.py。
该模型已受X-Talk对话框架支持