工作流端点 Workflow endpoints

工作流是一种将多个模型链接在一起以创建更复杂管道的方法。这允许您创建一个可以接收输入并按顺序将其传递给多个模型的端点。这对于创建需要多个步骤的更复杂模型或创建可以处理多个任务的单个端点非常有用。

测试版提醒

工作流程目前处于测试阶段,加入我们的 Discord 以获取最新更新,并分享您可能遇到的任何问题或反馈。

工作流作为 API

工作流 API 的工作方式与其他模型端点相同,您只需发送请求并获取响应即可。但是,工作流通常包含多个步骤并产生中间结果,因为每个步骤都包含自己的响应,这些响应可能与您的用例相关。

因此,工作流受益于 流式传输 功能,该功能允许您在生成部分结果时获取它们。

工作流事件

工作流 API 将在执行过程中触发一些事件,这些事件可用于监控工作流的进度并获取中间结果。以下是您可以从工作流流中期待的事件:

submit 事件

每次提交新步骤执行时都会触发此事件。它包含 app_idrequest_idnode_id

json
{
  "type": "submit",
  "node_id": "stable_diffusion_xl",
  "app_id": "fal-ai/fast-sdxl",
  "request_id": "d778bdf4-0275-47c2-9f23-16c27041cbeb"
}

completion 事件

完成特定步骤后会触发此事件。

json
{
  "type": "completion",
  "node_id": "stable_diffusion_xl",
  "output": {
    "images": [
      {
        "url": "https://fal.media/result.jpeg",
        "width": 1024,
        "height": 1024,
        "content_type": "image/jpeg"
      }
    ],
    "timings": { "inference": 2.1733 },
    "seed": 6252023,
    "has_nsfw_concepts": [false],
    "prompt": "a cute puppy"
  }
}

output 事件

output 事件表示工作流已完成,最终结果已准备就绪。

json
{
  "type": "output",
  "output": {
    "images": [
      {
        "url": "https://fal.media/result.jpeg",
        "width": 1024,
        "height": 1024,
        "content_type": "image/jpeg"
      }
    ]
  }
}

error 事件

在执行步骤期间发生错误时会触发 error 事件。error 对象包含带有 HTTP 状态代码的 error.status、错误 message 以及序列化了底层错误的 error.body

json
{
  "type": "error",
  "node_id": "stable_diffusion_xl",
  "message": "Error while fetching the result of the request d778bdf4-0275-47c2-9f23-16c27041cbeb",
  "error": {
    "status": 422,
    "body": {
      "detail": [
        {
          "loc": ["body", "num_images"],
          "msg": "ensure this value is less than or equal to 8",
          "type": "value_error.number.not_le",
          "ctx": { "limit_value": 8 }
        }
      ]
    }
  }
}

示例

一个很酷且简单的工作流强大功能示例是 workflows/fal-ai/sdxl-sticker,它包含三个步骤:

  1. 使用 fal-ai/fast-sdxl 生成图像。
  2. 使用 fal-ai/imageutils/rembg 删除图像背景。
  3. 使用 fal-ai/face-to-sticker 将图像转换为贴纸。

运行和协调三个不同模型可能是一个繁琐的过程,现在只需一个端点,您就可以通过单个请求进行调用。

js
Javascript
js
import * as fal from '@fal-ai/serverless-client'

const stream = await fal.stream('workflows/fal-ai/sdxl-sticker', {
  input: {
    prompt: 'a face of a cute puppy, in the style of pixar animation',
  },
})

for await (const event of stream) {
  console.log('partial', event)
}

const result = await stream.done()
console.log('final result', result)

类型定义

以下是您可以从工作流中预期的事件的 TypeScript 类型定义:

最后更新于 2024 年 6 月 21 日