由 LLM 驱动的助手需要执行多个步骤来处理用户的请求,形成思维链。与消息 (Message) 不同,步骤 (Step) 具有类型、输入/输出以及开始/结束。

根据 config.ui.cot 设置,完整的思维链可以完全显示、隐藏或仅显示工具调用。

Literal AI 中,完整的思维链会被记录下来,用于调试和重放。

一个简单的工具调用示例

让我们来看一个简单的思维链示例,它接收用户的消息,进行处理并发送响应。

import chainlit as cl


@cl.step(type="tool")
async def tool():
    # Simulate a running task
    await cl.sleep(2)

    return "Response from the tool!"


@cl.on_message
async def main(message: cl.Message):
    # Call the tool
    tool_res = await tool()

    # Send the final answer.
    await cl.Message(content="This is the final answer").send()

上方代码的输出

Step API

有两种创建 Step 的方式:使用 `@cl.step` 装饰器或使用 `cl.Step` 类。