Sim

YAML 工作流参考

Sim 中编写 YAML 工作流的完整指南

YAML 工作流为在 Sim 中定义、版本化和共享工作流配置提供了一种强大的方式。本参考指南涵盖了完整的 YAML 语法、块模式以及创建健壮工作流的最佳实践。

快速开始

每个 Sim 工作流都遵循以下基本结构:

version: '1.0'
blocks:
  start:
    type: starter
    name: Start
    inputs:
      startWorkflow: manual
    connections:
      success: agent-1

  agent-1:
    type: agent
    name: "AI Assistant"
    inputs:
      systemPrompt: "You are a helpful assistant."
      userPrompt: 'Hi'
      model: gpt-4o
      apiKey: '{{OPENAI_API_KEY}}'

核心概念

版本声明:必须精确为 version: '1.0'(带引号)

块结构:所有工作流块都定义在 blocks 键下

块引用:使用小写且去掉空格的块名称(例如,<aiassistant.content>

环境变量:使用双大括号引用 {{VARIABLE_NAME}}

块类型

Sim 支持多种核心块类型,每种类型都有特定的 YAML 模式:

块引用语法

YAML 工作流中最关键的部分是理解如何在块之间引用数据:

基本规则

  1. 使用块名称(而不是块 ID),将其转换为小写并去掉空格
  2. 添加适当的属性(对于代理使用 .content,对于工具使用 .output)
  3. 在使用聊天时,引用起始块<start.input>

示例

# Block definitions
email-processor:
  type: agent
  name: "Email Agent"
  # ... configuration

data-formatter:
  type: function
  name: "Data Agent"
  # ... configuration

# Referencing their outputs
next-block:
  type: agent
  name: "Next Step"
  inputs:
    userPrompt: |
      Process this email: <emailagent.content>
      Use this formatted data: <dataagent.output>
      Original input: <start.input>

特殊情况

  • 循环变量<loop.index><loop.currentItem><loop.items>
  • 并行变量<parallel.index><parallel.currentItem>

环境变量

使用环境变量存储敏感数据,例如 API 密钥:

inputs:
  apiKey: '{{OPENAI_API_KEY}}'
  database: '{{DATABASE_URL}}'
  token: '{{SLACK_BOT_TOKEN}}'

最佳实践

  • 保持块名称易于阅读:例如用于 UI 显示的 "Email Processor"
  • 引用环境变量:切勿硬编码 API 密钥
  • 结构清晰可读:逻辑分组相关块
  • 逐步测试:逐步构建工作流

下一步

YAML 工作流参考