使用说明
入门指南
在终端中运行:
npx tsx main.ts
目标(AI驱动的工作流编排系统):
- AI生成的YAML文件……然后YAML将执行我们的工作流程。。。
- 也许AI可以在YAML中提供循环来总结某些内容……我们刚刚处理了YAML……所以如果想让AI生成工作流,不一定非要用YAML,也可以用JSON。。
- 在预定义函数和模板工作流中,实现多个工作流内的单一功能,以便AI能够生成(相应的结果/流程)。。。
是的——让AI生成调用TypeScript定义函数的YAML工作流是完全可行的,而且非常强大。
我今天的任务 + 明天的 + 等等。 :
- 专注于CLI(命令行界面)
- 从主分支拉取代码(在与Spencer合并后)并构建CLI
项目结构
yaml-workflow-prototype/
├── package.json
├── tsconfig.json
├── src/
│ ├── functions/
│ │ └── index.ts # Predefined functions
│ ├── workflow/
│ │ ├── executor.ts # YAML parser & executor
│ │ └── validator.ts # Schema validation
│ ├── cli.ts # CLI interface
│ └── types.ts # TypeScript types
├── workflows/
│ ├── example-workflow.yaml
│ └── ai-generated-workflow.yaml
└── README.md
输出
情况2:
Step A: planning for A female elf warrior character with a sword and shield in a forest background studio ghibli style
Step E: understanding text [ --> analyzeKeywords
'A', 'female',
'elf', 'warrior',
'character', 'with',
'a', 'sword',
'and', 'shield',
'in', 'a',
'forest', 'background',
'studio', 'ghibli',
'style'
]
Step C: generating main image for A female elf warrior character with a sword and shield in a forest background studio ghibli style
Step D: generating spread images for 1
情况3:
提示 → 计划 → 融合提示与图像 → 生成主图像 → 生成扩展图像 图像 ──────────▲
npx tsx main.ts
Step A: planning for A female elf warrior character with a sword and shield in a forest background studio ghibli style
Step F: fusing images and prompt {
prompt: 'A female elf warrior character with a sword and shield in a forest background studio ghibli style',
images: [ { base64: 'base64string' } ]
}
Step E: understanding word [
'A', 'female',
'elf', 'warrior',
'character', 'with',
'a', 'sword',
'and', 'shield',
'in', 'a',
'forest', 'background',
'studio', 'ghibli',
'style'
]
Step B: analyzing images 1
Step C: generating main image for A female elf warrior character with a sword and shield in a forest background studio ghibli style
Step D: generating spread images for 1
User Idea (text)
↓
GPT-4o (Planning)
→ creates detailed prompt
↓
GPT-Image-1 (Image Generation)
→ generates image
规划
步骤1 → 等待步骤2 → 等待步骤3
https://platform.openai.com/docs/guides/reasoning-best-practices(推理最佳实践)
const exampleInput1: Workflow = {
prompt:
"A female elf warrior character with a sword and shield in a forest background studio ghibli style", // This is the source!
steps: [
{ type: "planningTask" }, // Step 1: Expects text input → uses prompt
{ type: "generateMainImage" }, // Step 2: Uses output from Step 1
{ type: "generateSpreadImages" }, // Step 3: Uses main image (stub)
],
};
如何阅读
如果第一步是一个规划任务(且类型检查返回true),则返回一个对象,其类型为字符串“planningTask”,其内容为从工作流中提取的提示信息。
if (StepType.isPlanningTask(first)) {
return { type: "planningTask", content: workflow.prompt };
}
计划
| 模式 | 关键字 | 含义 |
|---|---|---|
循环(或“重新开始”) for...of |
“一步一步来做” | 顺序进行 |
⚡ Promise.all |
“一起做所有事” | 并行进行 |
➕ reduce() |
“合并为一个” | 积累 |
数字(表示数量或计数) for |
“带索引重复” | 手动控制 |
| 情况 | 使用 | 原因 |
|---|---|---|
| 分步任务(依赖于前一步) | for...of |
等待每一步 |
| 所有任务相互独立(无需等待) | Promise.all() 一起快速奔跑 |
|
| 需要类似数学的折叠操作(求和、合并、计数) | reduce() 合并为一个 |
|
| 需要索引或计数功能 | for |
直接控制 |
| 只需阅读或打印 | forEach() |
仅副作用(无需await) |
| 模式 | 关键词 | 含义 |
|---|---|---|
🔁 for...of |
“一步一步来” | 顺序地 |
⚡ Promise.all |
“一起做所有事” | 并行进行 |
➕ reduce() |
“合并为一” | 积累 |
数字(或:计数) for |
“带索引的重复” | 手动控制 |
开始:工作流 → 获取第一步 → 设置初始输入 循环:对每个步骤。。。 ├── 匹配?→ 调用函数(例如,plan())→ 更新下一步的输入 └── 没有匹配?→ 错误 结束:返回图片(主图+跨页图)
`for...of` 模式:
Iteration 1: PlanningTask step + Planning input
├── Both true → Await plan() → Get string desc → Update to GenerateMainImage input
└── Log ✅ → Next iteration
Iteration 2: GenerateMainImage step + (new) GenerateMainImage input
├── Both true → Await generateMainImage(desc string) → Get Image[] → Update to Spread input
└── Log ✅
Iteration 3: Spread → Await generateSpreadImages(images) → Final images
└── Return {mainImages, spreadImages}
生成主图像
文本(提示/描述) ↓ openai.images.generate() 的中文翻译是:“OpenAI 图像生成(函数)”。不过,需要注意的是,OpenAI 并没有直接提供一个名为 `images.generate()` 的官方 API 函数,这可能是某个特定上下文或第三方实现中的命名。在实际使用中,应参考 OpenAI 官方提供的 API 文档来获取正确的函数名称和使用方法。如果这是一个假设的或特定于某个项目的函数名,那么翻译时保留了其原意 ↓ Base64图像字符串 ↓ 保存或返回
分析样本图像 -> 情况3
🔹 运行步骤:规划任务,当前上下文:规划任务 ✅ 规划完成 → 下一步准备就绪 🔹 正在执行步骤:分析样本图像,当前上下文:分析样本图像 ✅ 样本图像分析完成 → 准备进入下一步 🔹 正在执行的步骤:生成主图,当前上下文:生成主图 ✅ 主图生成完成 → 下一步准备就绪 🔹 运行步骤:生成跨页图像,当前上下文:生成跨页图像 ✅ 生成散布图像完成 → 下一步准备就绪
编辑图片 -> 情况4
从 tmp/edit 中获取多个样本图像(4张), 将它们全部发送到API, 并得到一张最终编辑后的图像结果(而不是每输入一张就得到一张)。
// 情况3:提供示例图片 // // 示例图片(数组) // _ -> [分析样本图像] // _ -> 字符描述(文本) // _ -> [生成主图像] // _ -> 主图像(数组) // _ -> [生成扩散图像] // * -> 展开图像(数组)
案例4:编辑图片
输入:图像、提示或分析样本图像输出? 输出:图像
通过增加额外的图片编辑步骤来改变流程。。
okay the iseditimages takes 2 value.. prompt string and readonlyarray image..
and for iteration for...of loop... after edit images function finishes, it will go to next step ? which is generate main image.. but generate main image input is string..
用户上传 → 编辑图片 → 分析 → 生成主图 → 排版 一个额外的分析步骤(但对AI流程来说是可行的——编辑 → 重新分析 → 生成)
Step E: editing images Add a unicorn to the image
✅ Saved edited image: /Users/amirah/Documents/testing/yaml-tutorial/tmp/edit/edited_result.png
Step B: analyzing images 1
Analyzed sample images: The image features a playful, round container designed to resemble a unicorn. It has a smiling face with eyes closed, a pink snout, and accents like a golden horn and colorful mane. The container appears to be situated on a wooden countertop in a kitchen setting, with kitchen utensils and a kettle visible in the background. The overall aesthetic is cheerful and whimsical, likely aimed at a younger audience or for decorative purposes.
✅ Analyzing sample images done → Next step ready
Step C: generating main image for The image features a playful, round container designed to resemble a unicorn. It has a smiling face with eyes closed, a pink snout, and accents like a golden horn and colorful mane. The container appears to be situated on a wooden countertop in a kitchen setting, with kitchen utensils and a kettle visible in the background. The overall aesthetic is cheerful and whimsical, likely aimed at a younger audience or for decorative purposes.
Generated main image: base64
✅ Generating main image done → Next step ready
