利用大型语言模型 (LLM)

简介

虽然 Colang 的核心并不强制要求使用大型语言模型 (LLM) 作为后端,但Colang 标准库 (CSL) 中的许多更高级的机制都依赖于它。

要启用 LLM 后端,您首先必须在 config.yml 中配置 LLM 访问权限,方法是添加一个类似于这样的 models 部分

models:
- type: main
  engine: openai
  model: gpt-3.5-turbo-instruct

请确保同时定义所需的 API 访问密钥,例如,对于 OpenAI,您将必须设置 OPENAI_API_KEY 环境变量。

每个 LLM 提示都包含一个默认上下文,如果需要适应使用场景,可以对其进行修改。请参阅此示例配置以开始使用。这将严重影响所有 LLM 调用。

自然语言描述 (NLD)

Colang 中主要的 LLM 生成机制之一是所谓的自然语言描述 (NLD) 以及“generation”运算符 ...

# Assign result of NLD to a variable
$world_population = ..."What is the number of people in the world? Give me a number."

# Extract a value from the current interaction context
$user_name = ..."What is the name of the user? Return 'friend' if not available."

# Extract structured information from the current interaction context
$order_information = ..."Provide the products ordered by the user in a list structure, e.g. ['product a', 'product b']"

# Use an existing variable in NLD
$response_to_user = ..."Provide a brief summary of the current order. Order Information: '{$order_information}'"

每个 NLD 将在运行时由配置的 LLM 后端解释和替换,并可在 Colang 中用于生成上下文相关的值。或者,您也可以使用类似于 NLD 的文档字符串在流程的开头描述流程的目的和功能。在流程中使用独立的生成运算符将使用流程的 NLD 自动推断正确的流程扩展

flow main
    """You are an assistant that should talk to the user about cars.
    Politely decline to talk about anything else.

    Last user question is: "{{ question }}"
    Generate the output in the following format:

    bot say "<<the response>>"
    """
    $question = await user said something
    ...

有关其工作原理的更多详细信息,请参阅 LLM 流程 中的示例。

请注意,NLD 响应格式没有显式控制,有时可能无法生成预期的结果。通常,您可以通过在 NLD 中提供更明确的说明来改进它,例如“用一个简短的句子欢迎用户,该句子用引号括起来,例如:‘您好!’”。另一种方法是使用例如 is_str() 函数检查返回值,以确保它是预期的格式。

用户意图匹配

定义流程 部分,我们已经了解了如何定义用户意图流程。其局限性在于它们没有推广到给定的用户意图示例的变体。借助 LLM,我们可以通过导入 llm 标准库模块并激活流程 automating intent detectiongenerating user intent for unhandled user utterance (Github 链接) 来克服这个问题,以便将意外的用户话语与当前活动的用户意图流程匹配。

llm/user_intent_match_example/main.co
import core
import llm

flow main
    activate automating intent detection
    activate generating user intent for unhandled user utterance

    while True
        when user greeted
            bot say "Hi there!"
        or when user said goodbye
            bot say "Goodbye!"
        or when unhandled user intent # For any user utterance that does not match
            bot say "Thanks for sharing!"

flow user greeted
    user said "Hi" or user said "Hello"

flow user said goodbye
    user said "Bye" or user said "See you"

当运行此示例时

> Hi

Hi there!

> hi

Hi there!

> hallo

Hi there!

> How are you?

Thanks for sharing!

> bye bye

Goodbye!

您可以看到,如果我们对例如“Hi”有完全匹配,则 LLM 不会被调用,因为它与等待的 user said 流程之一直接匹配。对于任何其他用户话语,激活的流程 generating user intent for unhandled user utterance 将在找到合适的用户意图之前调用 LLM。如果用户话语与预定义的用户意图流程之一足够接近(即 user greeteduser said goodbye),它将导致相关的流程成功完成。这使您甚至可以使用不同的语言(如果 LLM 支持)成功映射到正确的流程。如果没有找到好的匹配项,则流程 unhandled user intent 将匹配。

您可能会问自己,LLM 如何知道哪些流程被认为是用户意图流程。这可以通过基于流程名称来完成,方法是激活流程 automating intent detection 以自动检测以“user”开头的流程,或者使用显式流程装饰器来标记它们,而与其名称无关

@meta(user_intent=True)
flow any fancy flow name
    user said "Hi" or user said "Hello"

注意

从语义的角度来看,即使通过用户意图元装饰器标记,以“user”开头启动用户意图流程始终是有意义的。

机器人动作生成

与我们希望能够处理用户输入中的变化类似,我们已经看到了定义预定义机器人动作变体的机器人意图流程。虽然这对于对预期的用户输入做出响应可能足够好,但我们也希望处理意外的用户话语,而不是总是回复“谢谢分享!”。对于这种情况,标准库中的另一个流程将帮助我们,名为 llm continue interaction

llm/bot_intent_generation_example/main.co
import core
import llm

flow main
    user said something
    llm continue interaction
> Hello

Hi there! How can I help you today?

> Tell me a funny story

Sure! Did you hear about the fire at the circus? It was intense!

> funny!

I'm glad you liked it! Do you want to hear another one?

> Bye

Bye! Have a great day!

您会看到,有了这个,机器人可以对任何用户输入做出反应,并以合适的机器人答案做出响应。这很好地推广到多模态交互,如果提供合适的提示上下文,也可以用于生成机器人姿势和机器人手势。

注意

生成的动作在很大程度上取决于当前的交互上下文、通用提示指令和 config.yml 中的示例对话。尝试更新它们以实现预期的结果。

基本交互循环

我们现在可以将所有内容组合成一个基本交互循环

llm/interaction_loop/main.co
import core
import timing
import llm

flow main
    activate automating intent detection
    activate generating user intent for unhandled user utterance

    while True
        when unhandled user intent
            llm continue interaction
        or when user was silent 12.0
            $response = ..."A random fun fact"
            bot say $response
        or when user expressed greeting
            bot say "Hi there!"
        or when user expressed goodbye
            bot inform "That was fun. Goodbye"

flow user expressed greeting
    user said "hi"
        or user said "hello"

flow user expressed goodbye
    user said "goodbye"
        or user said "I am done"
        or user said "I have to go"

如果可能,此循环将负责将用户话语与预定义的用户意图匹配(例如 user expressed greetinguser expressed goodbye),或者使用流程 llm continue interaction 为意外的用户意图生成合适的响应。此外,如果用户超过 12 秒没有任何发言,机器人将说出一个通过 NLD 生成的随机有趣的事实。

Guardrailing(护栏机制)

查看入门指南部分中的示例,或参考 NeMo Guardrails 文档,以了解有关如何使用 Colang 来对 LLM 响应和用户输入进行护栏机制的更多信息。