模板对话系统

模板对话系统

基于模板的对话系统作为其中一种经典方法,通过预定义的结构化模板来生成对话,不仅提升了系统的可理解性,还能够使得对话更加自然和符合上下文。以下是我们本次对话系统的实现过程。

数据类和初始化

首先定义了一个名为Template的数据类,用于表示不同的对话模板。模板通过名称进行初始化,并根据名称设置对应的前缀、提示、分隔符和历史记录选项。

 @dataclass
 class Template:
     name: str
         
     def __post_init__(self):

模板注册和格式化

根据实例的属性name的值进行条件判断。如果name属性等于字符串"vanilla",则执行if内部代码块。

调用实例的_register_template方法,接受四个参数:prefixpromptsepuse_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的数据类和条件语句,我们能够轻松定义和管理多个对话模板,使得系统具备了适应不同用户需求和场景的能力。

自然语言处理(NLP)的对话系统开发是一项颇具挑战性的工作。对话系统旨在使机器能够以人类类似的方式进行对话,并理解和生成自然语言。以下是开发NLP对话系统的一般流程: 1. 数据收集和预处理:收集对话数据,并对其进行预处理。这包括分词、去除停用词、标记词性等。同时,可以使用已标注的对话数据进行训练。 2. 意图和实体识别:对于输入的对话,需要准确地识别用户的意图(intent)和实体(entity)。意图识别可以通过训练分类器或使用机器学习模型实现。实体识别可以通过命名实体识别模型来完成。 3. 对话管理:对话系统需要具备合理的对话流程,能够根据用户的输入做出有意义的回应。可以使用规则引擎或基于强化学习的方法来进行对话管理。 4. 自然语言生成:对话系统需要能够生成自然语言的回应,以回应用户。可以使用模板匹配、序列到序列模型或生成式对话模型来实现自然语言生成。 5. 评估和优化:对话系统的开发需要进行不断的评估和优化。可以使用人工评估、自动评估指标或用户反馈来评估对话系统的性能,并根据结果进行系统改进。 6. 部署和维护:一旦对话系统开发完成,就可以进行部署并让真实用户进行测试。根据用户反馈和系统的性能,进行维护和更新。 总的来说,NLP对话系统开发需要综合运用自然语言处理、机器学习和人工智能等技术。通过连续的数据收集、模型训练和系统优化,可以不断提高对话系统的性能和用户体验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值