模板对话系统
基于模板的对话系统作为其中一种经典方法,通过预定义的结构化模板来生成对话,不仅提升了系统的可理解性,还能够使得对话更加自然和符合上下文。以下是我们本次对话系统的实现过程。
数据类和初始化
首先定义了一个名为Template的数据类,用于表示不同的对话模板。模板通过名称进行初始化,并根据名称设置对应的前缀、提示、分隔符和历史记录选项。
@dataclass class Template: name: str def __post_init__(self):
模板注册和格式化
根据实例的属性name
的值进行条件判断。如果name
属性等于字符串"vanilla",则执行if内部代码块。
调用实例的_register_template
方法,接受四个参数:prefix
、prompt
、sep
和use_history
四个参数作用如下:
-
prefix=""
:设置对话的前缀为空字符串,即没有额外的文本在用户输入前显示。 -
prompt="{query}"
:设置对话的提示文本为"{query}",这里的"{query}"是一个占位符,用于表示用户的输入内容。 -
sep=""
:设置对话的分隔符为空字符串,表示每个对话轮之间没有额外的分隔符。 -
use_history=False
:设置不使用历史记录,即系统不会记录和利用之前的对话历史。
elif self.name == "judgment": r""" judgment template. """ self._register_template( prefix="我是一个判决书审查专家,擅长从判决书中寻找上下文一致性,事实性和判决上的错误!", prompt="Human: {query}\nAssistant: ", sep="", use_history=False )
参数作用如下:
-
prefix
:设置对话的前缀为字符串"我是一个判决书审查专家,擅长从判决书中寻找上下文一致性,事实性和判决上的错误!"。这是一个介绍性的前缀,用于说明助理的角色和专长。 -
prompt
:设置对话的提示文本为"Human: {query}\nAssistant: "。这里的"{query}"是一个占位符,用于表示用户的输入内容。 -
sep
:设置对话的分隔符为空字符串,表示每个对话轮之间没有额外的分隔符。 -
use_history=False
:设置不使用历史记录,即系统不会记录和利用之前的对话历史。
当self的name属性为default时调用该部分代码。
elif self.name == "default": r""" Default template. """ self._register_template( prefix="A chat between a curious user and an artificial intelligence assistant. " "The assistant gives helpful, detailed, and polite answers to the user's questions.", prompt="Human: {query}\nAssistant: ", sep="\n", use_history=True )
当self的name属性为alpaca时调用该部分代码。
elif self.name == "alpaca": r""" Supports: https://huggingface.co/tatsu-lab/alpaca-7b-wdiff https://github.com/ymcui/Chinese-LLaMA-Alpaca """ self._register_template( prefix="Below is an instruction that describes a task. " "Write a response that appropriately completes the request.", prompt="### Instruction:\n{query}\n\n### Response:\n", sep="\n\n", use_history=True )
当self的name属性为vicuna时调用该部分代码。
elif self.name == "vicuna": r""" Supports: https://huggingface.co/lmsys/vicuna-7b-delta-v1.1 https://huggingface.co/lmsys/vicuna-13b-delta-v1.1 """ self._register_template( prefix="A chat between a curious user and an artificial intelligence assistant. " "The assistant gives helpful, detailed, and polite answers to the user's questions.", prompt="USER: {query} ASSISTANT: ", sep="</s>", use_history=True )
根据self的name属性,选择对应的对话模板。
elif self.name == "belle": r""" Supports: https://huggingface.co/BelleGroup/BELLE-LLaMA-EXT-13B """ self._register_template( prefix="", prompt="Human: {query}\n\nBelle: ", sep="\n\n", use_history=True ) elif self.name == "linly": r""" Supports: https://github.com/CVI-SZU/Linly """ self._register_template( prefix="", prompt="User: {query}\nBot: ", sep="\n", use_history=True ) elif self.name == "billa": r""" Supports: https://github.com/Neutralzz/BiLLa """ self._register_template( prefix="", prompt="Human: {query}\nAssistant: ", sep="\n", use_history=True ) elif self.name == "ziya": r""" Supports: https://huggingface.co/IDEA-CCNL/Ziya-LLaMA-13B-v1 """ self._register_template( prefix="", prompt="<human>:{query}\n<bot>:", sep="\n", use_history=True ) elif self.name == "aquila": r""" Supports: https://huggingface.co/qhduan/aquilachat-7b """ self._register_template( prefix="A chat between a curious human and an artificial intelligence assistant. " "The assistant gives helpful, detailed, and polite answers to the human's questions.", prompt="Human: {query}###Assistant: ", sep="###", use_history=True ) elif self.name == "intern": r""" Supports: https://huggingface.co/internlm/internlm-chat-7b """ self._register_template( prefix="", prompt="<|User|>:{query}<eoh>\n<|Bot|>:", sep="<eoa>\n", use_history=True )
最后如果没有对应模板:
else: raise ValueError("Template {} does not exist.".format(self.name))
通过Python的数据类和条件语句,我们能够轻松定义和管理多个对话模板,使得系统具备了适应不同用户需求和场景的能力。