LLM Flows (llm.co)#

LLM 启用的 Bot 动作#

bot say something like $text

触发类似于给定文本的 bot 话语

示例

import core
import llm

flow main
    user said something
    bot say something like "How are you"
> hi
Hi there, how are you today?

LLM 实用程序#

polling llm request response $interval=1.0

开始轮询所有 LLM 相关调用的响应,以接收 LLM 响应并对其执行操作

示例

import core
import llm

flow main
    # Normally you don't need to activate this flow, as it is activated by LLM based flows where needed.
    activate polling llm request response

    user said something

    # While the response is generated the polling mechanism ensures that
    # the Colang runtime is getting polled.
    $value = ..."ten minus one"
    bot say $value
> compute the value
nine

交互延续#

用于继续当前交互以处理未处理的用户操作/意图或未定义的 flows 的 Flow。

llm continuation

激活所有基于 LLM 的交互延续

示例

import core
import llm

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

flow bot express greeting
    bot say "Hello and welcome"

flow handling greeting
    user expressed greeting
    bot express greeting

flow main
    activate llm continuation
    activate handling greeting
> hi there how are you
Hello and welcome
> what is the difference between lemons and limes
Limes are green and lemons are yellow
generating user intent for unhandled user utterance

为未处理的用户话语生成用户意图事件(完成 flow 事件)

示例

import core
import llm

flow user expressed goodbye
    user said "bye" or user said "i will go now"

flow bot express goodbye
    bot say "hope to see you again soon"

flow handling goodbye
    user expressed goodbye
    bot express goodbye

flow main
    activate automating intent detection
    activate generating user intent for unhandled user utterance
    activate handling goodbye
> what can you do for me
> ok I'll leave
hope to see you again soon
unhandled user intent -> $intent

等待用户意图 flow 的结束

示例

import core
import llm

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

flow bot express greeting
    bot say "Hello and welcome"

flow handling greeting
    user expressed greeting
    bot express greeting

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

    while True:
        unhandled user intent as $ref
        bot say "got intent: {$ref.intent}"
> hi there how are you
Hello and welcome
> what is the difference between lemons and limes
got intent: user asked fruit question
continuation on unhandled user intent

生成并启动新的 flow 以继续处理未处理的用户意图的交互

示例

import core
import llm

flow user asked political question
    user said "who is the best president"

flow user insulted bot
    user said "you are stupid"

flow safeguarding conversation
    user asked political question or user insulted bot
    bot say "Sorry but I will not respond to that"

flow main
    activate automating intent detection
    activate generating user intent for unhandled user utterance
    activate continuation on unhandled user intent
    activate safeguarding conversation
> i hate you
Sorry but I will not respond to that
> what party should I vote for
Sorry but I will not respond to that
> tell me a joke
Why don't scientists trust atoms? Because they make up everything!
continuation on undefined flow

生成并启动新的 flow 以继续处理未定义的 flow 开始的交互

示例

import core
import llm

flow main
    activate continuation on undefined flow

    user said something
    # Await a flow that does not exist will create an LLM generated flow
    bot ask about hobbies
> hi there
What are your hobbies?
llm continue interaction

生成并继续进行合适的交互

示例

import core
import llm

flow main
    user said "i have a question"
    bot say "happy to help, what is it"
    user said "do you know what the largest animal is on earth"
    llm continue interaction
> i have a question
happy to help, what is it
> do you know what the largest animal is on earth
The largest animal on earth is the blue whale

更高级的 Flows#

本节介绍 llm.co 库中定义的更高级的 flows。当你开始使用 Colang 时,你很可能不需要直接使用这些 flows。这些 flows 的存在是为了支持更高级的用例。

高级交互延续

具有更高级的基于 LLM 的延续的 Flows

# Generate a flow that continues the current interaction
flow llm generate interaction continuation flow -> $flow_name

交互历史记录

用于记录交互历史记录以创建 LLM 提示所需上下文的 Flows。

# Activate all automated user and bot intent flows logging based on flow naming
flow automating intent detection

# Marking user intent flows using only naming convention
flow marking user intent flows

# Generate user intent logging for marked flows that finish by themselves
flow logging marked user intent flows

# Marking bot intent flows using only naming convention
flow marking bot intent flows

# Generate user intent logging for marked flows that finish by themselves
flow logging marked bot intent flows

状态跟踪 Flows

这些 flows 在全局变量中跟踪 bot 和用户状态。

# Track most recent unhandled user intent state in global variable $user_intent_state
flow tracking unhandled user intent state