You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让基于AIML的游戏聊天机器人支持用户纠错与自定义回复?

可行方案:基于AIML实现用户自定义回复功能

完全可以实现你需求的功能,核心思路是结合AIML的上下文管理+外部持久化存储,突破原生AIML静态模板的限制,让用户能动态修改机器人回复。以下是具体实现步骤:

1. 用AIML捕获用户的纠错指令

首先编写AIML模板,匹配用户的纠错语句,同时记录上下文信息(用户上一条输入、待设置的新回复):

<!-- 记录用户的每一条输入到上下文,用于后续纠错匹配 -->
<category>
  <pattern>*</pattern>
  <template>
    <think>
      <set name="last_user_input"><star/></set>
    </think>
    <srai><star/></srai>
  </template>
</category>

<!-- 匹配用户的纠错指令,提取目标回复 -->
<category>
  <pattern>WRONG YOU SHOULD HAVE ANSWERED *</pattern>
  <template>
    <think>
      <set name="pending_input"><get name="last_user_input"/></set>
      <set name="pending_response"><star/></set>
    </think>
    Okay next time you say "<get name="pending_input"/>" I will answer "<get name="pending_response"/>" is this correct?
  </template>
</category>

<!-- 处理用户的确认指令,触发保存逻辑 -->
<category>
  <pattern>YES THAT IS CORRECT</pattern>
  <template>
    <think>
      <!-- 调用自定义扩展标签,将待保存的内容写入外部存储 -->
      <srai>SAVE_CUSTOM_RESPONSE</srai>
    </think>
    Got it! I'll remember that for next time.
  </template>
</category>

<!-- 默认问候模板 -->
<category>
  <pattern>HELLO MY NAME IS *</pattern>
  <template>
    <think>
      <set name="username"><star/></set>
    </think>
    Hello <get name="username"/>, nice to meet you.
  </template>
</category>

2. 扩展AIML处理器实现持久化存储

原生AIML的<set>/<get>只能存储会话临时数据,重启就会丢失。你需要给AIML内核添加自定义扩展逻辑:

  • 新增<SAVE_CUSTOM_RESPONSE>标签的处理函数,将pending_input和pending_response写入外部存储(比如JSON文件、SQLite数据库)
  • 存储格式设计为键值对,键是用户输入语句,值是回复列表(支持多条随机回复),示例:
    {
      "Hello my name is Robert.": [
        "Hi Robert, how you doing?",
        "Hey Robert, welcome to the game!"
      ]
    }
    
  • 当用户再次纠正同一条输入时,直接将新回复追加到对应列表,而非覆盖原有内容

3. 优先调用自定义回复

修改机器人的输入处理流程,每次接收用户消息时:

  1. 先对输入做标准化处理(比如转小写、去除多余标点),避免因格式差异匹配失败
  2. 查询外部存储中是否存在该输入对应的自定义回复列表
  3. 如果存在,随机选择一条回复返回;如果不存在,再走原生AIML模板匹配逻辑

伪代码示例(以Python为例):

import random
import json

# 加载自定义回复存储
def load_custom_responses():
    try:
        with open("custom_responses.json", "r") as f:
            return json.load(f)
    except FileNotFoundError:
        return {}

# 保存自定义回复
def save_custom_response(input_text, response_text):
    responses = load_custom_responses()
    if input_text not in responses:
        responses[input_text] = []
    responses[input_text].append(response_text.strip('"'))
    with open("custom_responses.json", "w") as f:
        json.dump(responses, f, indent=2)

# 处理用户输入的主逻辑
def handle_user_input(user_input):
    # 标准化输入
    normalized_input = user_input.strip().lower()
    # 优先查自定义回复
    custom_responses = load_custom_responses()
    if normalized_input in custom_responses:
        return random.choice(custom_responses[normalized_input])
    # 无自定义回复则走AIML
    return aiml_kernel.respond(user_input)

4. 补充说明

  • 如果需要支持更灵活的纠错指令(比如不同表述的纠错语句),可以扩展AIML的<pattern>,比如添加WRONG * SHOULD BE *等变体
  • 对于复杂的输入匹配,可以结合AIML的<regex>标签做正则匹配,提升容错性

内容的提问来源于stack exchange,提问作者La Brown Bag

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 22:20:14