交互循环#
本节介绍如何在 Colang 2.0 中创建交互循环。
用法#
在各种基于 LLM 的应用程序中,需要 LLM 在连续的交互循环中与用户保持交互。下面的示例展示了如何使用 while
结构实现简单的交互循环,以及当用户沉默时机器人如何主动。
examples/v2_x/tutorial/interaction_loop/main.co#
1import core
2import llm
3import avatars
4import timing
5
6flow main
7 activate automating intent detection
8 activate generating user intent for unhandled user utterance
9
10 while True
11 when unhandled user intent
12 $response = ..."Response to what user said."
13 bot say $response
14 or when user was silent 12.0
15 bot inform about service
16 or when user expressed greeting
17 bot say "Hi there!"
18 or when user expressed goodbye
19 bot inform "That was fun. Goodbye"
20
21flow user expressed greeting
22 user said "hi"
23 or user said "hello"
24
25flow user expressed goodbye
26 user said "goodbye"
27 or user said "I am done"
28 or user said "I have to go"
29
30flow bot inform about service
31 bot say "You can ask me anything!"
32 or bot say "Just ask me something!"
上面的 main
流激活了来自 avatars
模块的 generating user intent for unhandled user utterance
流,该流使用 LLM 为用户消息生成规范形式(也称为用户意图)。此外,当 LLM 生成 Colang 脚本未处理的意图时,将触发 unhandled user intent
流(第 11 行)。
上面的示例中的第 14 行展示了如何使用预定义的 user silent
事件来建模时间驱动的交互。
此示例还使用了 when
/ or when
语法,这是一种在多个路径上分支流程的机制。当流程到达分支点时,它将开始监视所有分支,并在分支匹配后立即继续交互。
测试#
$ nemoguardrails chat --config=examples/v2_x/tutorial/interaction_loop
> hi
Hi there!
<< pause for 12 seconds >>
You can ask me anything!
> how are you?
I am doing well, thank you for asking! How can I assist you today?
下一个示例 将向您展示如何创建 LLM 驱动的流程。