演示 3: WorkflowAgent 智能编排
(ctx context.Context)
| 170 | |
| 171 | // 演示 3: WorkflowAgent 智能编排 |
| 172 | func demoWorkflowAgent(ctx context.Context) { |
| 173 | // 创建一个简单的分析 workflow |
| 174 | analysisWf := workflow.New("Analysis") |
| 175 | |
| 176 | analysisWf.AddStep(workflow.NewFunctionStep("analyze", |
| 177 | func(ctx context.Context, input *workflow.StepInput) (*workflow.StepOutput, error) { |
| 178 | query := input.Input.(string) |
| 179 | fmt.Printf(" 🔍 分析查询: %s\n", query) |
| 180 | |
| 181 | // 模拟分析 |
| 182 | result := map[string]any{ |
| 183 | "query": query, |
| 184 | "result": "分析完成", |
| 185 | "metrics": map[string]int{"items": 42, "quality": 95}, |
| 186 | } |
| 187 | |
| 188 | return &workflow.StepOutput{ |
| 189 | Content: result, |
| 190 | Metadata: make(map[string]any), |
| 191 | }, nil |
| 192 | }, |
| 193 | )) |
| 194 | |
| 195 | // 创建 WorkflowAgent |
| 196 | wfAgent := workflow.NewWorkflowAgent( |
| 197 | "gpt-4", |
| 198 | "你是一个数据分析助手。如果用户查询已在历史中,直接回答;否则运行 workflow。", |
| 199 | true, // 启用历史 |
| 200 | 5, // 保留5次历史 |
| 201 | ) |
| 202 | |
| 203 | wfAgent.AttachWorkflow(analysisWf) |
| 204 | |
| 205 | // 第一次查询 - 会运行 workflow |
| 206 | fmt.Println("\n 第一次查询:") |
| 207 | result1, err := wfAgent.Run(ctx, "分析销售数据") |
| 208 | if err != nil { |
| 209 | log.Printf(" ❌ 错误: %v", err) |
| 210 | } else { |
| 211 | fmt.Printf(" ✅ 结果: %s\n", result1) |
| 212 | } |
| 213 | |
| 214 | // 查看历史 |
| 215 | history := wfAgent.GetWorkflowHistory() |
| 216 | if len(history) > 0 { |
| 217 | fmt.Printf("\n 📊 历史记录: %d 条\n", len(history)) |
| 218 | for i, item := range history { |
| 219 | fmt.Printf(" %d. 输入: %v -> 状态: %s\n", i+1, item.Input, item.Status) |
| 220 | } |
| 221 | } |
| 222 | } |
no test coverage detected