变化#

本指南概述了 Colang 2.0 中最重要的变化,但非详尽列表。

术语#

为了降低学习曲线,Colang 2.0 尽可能多地借鉴了 Python 的术语。

  • 每一段 Colang 代码都称为一个 script

  • 一个 .co 文件称为一个 module

  • 一个包含 Colang 文件(可能带子文件夹)的文件夹称为一个 package

  • 可以导入 Modules 和 packages。

语法变化#

  • 去除了 defineexecute 关键字。

  • 添加了 flow, match, send, start, await, activate 关键字。

  • 支持装饰器。

  • 使用 when/or when 替代 when/else when

  • 去除了 subflows。

定义用户和机器人意图#

不再支持特殊的 define user ...define bot ... 语法。在 Colang 2.0 中定义示例话语时,必须使用更明确的语法。

flow user expressed greeting
  user said "hi" or user said "hello"
flow user expressed greeting
  user said "hi"
    or user said "hello"
    or user said "Good evening!"
    or user said "Good afternoon!"

为什么?例如,现在你可以混合其他类型的事件和模态,例如 user gesture "wave"

类似地,对于机器人意图:

flow bot express greeting
  bot say "Hello world!"
    or bot say "Hi there!"

Flow 命名约定#

建模来自“系统外部”事件的 flows 使用过去时命名,例如 user said, user expressed greeting 等。在机器人端,它们代表需要采取的行动,并使用祈使形式,例如 bot say, bot express greeting, bot refuse to respond 等。更多详细信息请参阅 Flow 命名约定

生成运算符#

Colang 2.0 引入了 ... 运算符,也称为“生成”运算符。当 Colang 脚本的一部分需要在运行时动态生成时,可以使用它。通常,这通过 LLM 完成。

... 运算符支持使用 自然语言 flows,其中 flow 的 docstring 用于生成 flow 的内容。

活动 Flows#

在 Colang 1.0 中,所有 flows 默认都是活动的。在 Colang 2.0 中,必须明确激活 flows。现在还有一个 main flow,它是默认激活的入口点。

入口点#

在 Colang 1.0 中,Colang 脚本没有明确的入口点。在 Colang 2.0 中,main flow 是入口点。main flow 触发 Colang package 中使用的所有其他 flows 的激活。

导入机制#

Colang 2.0 添加了类似于 Python 的导入机制。任何存在于 COLANGPATH 中的 Colang module 或 package 都可以使用 import 语句导入。与 Python 不同,目前 Colang 2.0 仅支持 module/package 级别的导入,即不能只导入特定的 flow。这将在未来版本中添加。

标准库#

Colang 2.0 现在有了一个标准库:

  • core: 一组与用户和机器人话语相关的核心 flows,例如 user said, bot say

  • llm: 与使用 LLM 驱动交互相关的 flows。

  • timing: 与时序相关的 flows,例如 wait, user was silent $time_s

  • guardrails: 支持添加 guardrails,即检查用户输入、机器人输出等。

  • avatars: 支持控制交互式虚拟形象。

  • utils: 一小部分实用 flows。

异步 Actions#

在 Colang 1.0 中,action 只能同步执行,会阻塞 flow。也无法并行启动两个 action。这一点尤其重要,例如,如果你想让多个输入轨并行运行。

在 Colang 2.0 中,execute 关键字已替换为 await,类似于 Python。此外,你可以使用 start 来启动 action 而不阻塞 flow。

命名约定#

Colang 2.0 使用以下命名约定:- Flow 名称:小写,可以包含空格,应易于阅读。- Action 名称:驼峰式命名,必须以“Action”结尾。- Event 名称:驼峰式命名。

标记 action 开始和结束的事件有特定的约定:Start...Action, ...ActionStarted, ...ActionFinished

多模态#

Colang 2.0 支持建模多模态交互,而不仅仅是基于文本的交互(例如,user gesture, bot gesture, bot posture 等)

变量#

在 Colang 1.0 中,所有变量默认都是全局的。在 Colang 2.0 中,所有变量默认都是局部的。要将变量设置为全局,可以使用 global 关键字。

在 Colang 2.0 中没有默认的全局变量。

字符串格式化#

不再支持内联的 "Hello there, $name!" 格式。必须始终将变量括在花括号中,类似于 Python 的 "Hello there, {$name}!"

LLM 调用#

在 Colang 1.0 中,一旦定义了用户意图,对话轨就会自动激活并使用 LLM。在 Colang 2.0 中,要使用 LLM,必须明确激活该机制。

flow main
  activate llm continuation

Python API#

Colang 2.0 添加了对明确的“state object”的支持。对于跨多个回合/事件的交互,每次处理后都会返回一个 state object,并在下一个处理周期中需要将其传回。

从 alpha 版本到 beta 版本的重大变化#

  • Metatags
    • # meta: user intent -> @meta(user_intent=True) (以及 user_action, bot_intent, bot_action)

    • # meta: exclude from llm -> @meta(exclude_from_llm=True)

  • 交互循环 ID
    • # meta: loop_id=<loop_id> -> @loop("<loop_id>")

  • 或当 (or when) 语句
    • orwhen -> or when

  • NLD 指令
    • """<NLD>""" -> ..."<NLD>"

  • 内部事件参数重命名
    • flow_start_uid -> flow_instance_uid

  • 正则表达式
    • r"<regex>" -> regex("<regex>")

  • 字符串中的表达式
    • "{{<expression>}}" -> "{<expression>}"

  • Colang 函数名称变化
    • findall -> find_all

  • 机器人特定的 Colang Core Library 副本
    • ccl_*.co 文件已被弃用,应从机器人文件夹中移除。它被 NeMo Guardrails 中包含的 Colang 标准库取代,可以通过导入使用(例如 import coreimport llm )。请参见下一节中标准库 flows 的新名称映射。

  • 标准库 flow 名称变化
    • catch colang errors -> notification of colang errors (core.co)

    • catch undefined flows -> notification of undefined flow start (core.co)

    • catch unexpected user utterance -> notification of unexpected user utterance (core.co)

    • poll llm request response -> polling llm request response (llm.co)

    • trigger user intent for unhandled user utterance -> generating user intent for unhandled user utterance (llm.co)

    • generate then continue interaction -> llm continue interaction (llm.co)

    • track bot talking state -> tracking bot talking state (core.co)

    • track user talking state -> tracking user talking state (core.co)

    • track unhandled user intent state -> tracking unhandled user intent state (llm.co)

    • track visual choice selection state -> track visual choice selection state (avatars.co)

    • track user utterance state -> tracking user talking state (core.co)

    • track bot utterance state -> tracking bot talking state (core.co)

    • interruption handling bot talking -> handling bot talking interruption (avatars.co)

    • generate then continue interaction -> llm continue interaction (llm.co)

    • respond to unhandled user intent -> continuation on unhandled user intent (llm.co)

    • manage listening posture -> managing listening posture (avatars.co)

    • manage talking posture -> managing talking posture (avatars.co)

    • manage thinking posture -> managing thinking posture (avatars.co)

    • manage attentive posture -> 没有替代项(如需要,请复制到你的机器人脚本中)

    • manage bot postures -> managing bot postures (avatars.co)

    • track user presence state -> 没有替代项(如需要,请复制到你的机器人脚本中)

    • user became no longer present -> 没有替代项(如需要,请复制到你的机器人脚本中)