使用大型语言模型 (LLM)#
简介#
虽然 Colang 的核心不需要大型语言模型 (LLM) 作为后端,但Colang 标准库 (CSL) 中许多更高级的机制都依赖于它。
要启用 LLM 后端,您首先必须在 config.yml 中配置 LLM 访问权限,方法是添加一个 models 部分,如下所示
models:
- type: main
engine: openai
model: gpt-4-turbo
请确保同时定义所需的 API 访问密钥,例如,对于 OpenAI,您将需要设置 OPENAI_API_KEY
环境变量。
每个 LLM 提示都包含一个默认上下文,如果需要,可以对其进行修改以适应使用场景。请参阅此示例配置以开始使用。这将严重影响所有 LLM 调用。
支持的模型#
Colang 目前开箱即用地支持以下模型
engine: openai
model: gpt-3.5-turbo-instruct
model: gpt-3.5-turbo
model: gpt-4-turbo
model: gpt-4o
model: gpt-4o-mini
NVIDIA AI Foundary 托管 NIM
engine: nim
model: meta/llama3-8b-instruct
model: meta/llama3-70b-instruct
model: meta/llama-3.1-8b-instruct
model: meta/llama-3.1-70b-instruct
要支持其他模型,您需要创建一组新的模板提示,这些提示考虑了模型的特定功能和 API,并将它们添加到您的 Bot 配置中。
自然语言描述 (NLD)#
Colang 中主要的 LLM 生成机制之一是所谓的自然语言描述 (NLD) 与“生成”运算符 ...
结合使用。
# 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 与变量名一起由 LLM 直接解释。根据您使用的 LLM,您需要确保非常具体地说明您想要生成的值。始终清楚地指定您希望响应的格式以及它应该具有的类型是一种好的做法(例如,$user_name = ..."返回以单引号括起来的字符串形式的用户姓名"。如果没有用户姓名可用,则返回“friend”'
。
或者,您也可以使用类似 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 detection
和 generating user intent for unhandled user utterance
(Github 链接) 将意外的用户话语与当前活动的用户意图流程进行匹配,从而利用其推理能力。
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 greeted
或 user 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”开头用户意图流程始终是有意义的。
Bot 动作生成#
与我们希望能够处理用户输入的变化类似,我们已经看到了定义预定义 Bot 动作变化的 Bot 意图流程。虽然这对于对预期用户输入的响应来说可能足够好,但我们也希望处理意外的用户话语,而不是总是回复“感谢分享!”。对于这种情况,标准库中的另一个流程将帮助我们,名为 llm continue interaction
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!
您会看到,通过这种方式,Bot 可以对任何用户输入做出反应,并以合适的 Bot 回答进行响应。这很好地推广到多模态交互,如果提供合适的提示上下文,也可以用于生成 Bot 姿势和 Bot 手势。
注意
生成的动作强烈依赖于当前的交互上下文、通用提示指令和 config.yml 中的示例对话。尝试更新它们以获得预期的结果。
基本交互循环#
我们现在可以将所有内容组合到一个基本交互循环中
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 greeting
或 user expressed goodbye
),或者使用流程 llm continue interaction
为意外的用户意图生成合适的响应。此外,如果用户超过 12 秒没有任何发言,Bot 将说出一个通过 NLD 生成的随机有趣的事实。
护栏#
查看入门指南部分中的示例,或参考NeMo Guardrails 文档,以了解有关如何使用 Colang 来护栏 LLM 响应和用户输入的更多信息。