文件批处理示例 演示如何使用 PTC 进行复杂的文件处理任务
()
| 16 | // 文件批处理示例 |
| 17 | // 演示如何使用 PTC 进行复杂的文件处理任务 |
| 18 | func main() { |
| 19 | // 检查 API Key |
| 20 | apiKey := os.Getenv("ANTHROPIC_API_KEY") |
| 21 | if apiKey == "" { |
| 22 | log.Fatal("请设置环境变量 ANTHROPIC_API_KEY") |
| 23 | } |
| 24 | |
| 25 | fmt.Println("=== Aster PTC 文件处理示例 ===") |
| 26 | |
| 27 | // 1. 创建工具生态系统 |
| 28 | registry := tools.NewRegistry() |
| 29 | builtin.RegisterAll(registry) |
| 30 | |
| 31 | toolBridge := bridge.NewToolBridge(registry) |
| 32 | codeExecTool := builtin.NewCodeExecuteToolWithBridge(toolBridge) |
| 33 | |
| 34 | // 2. 准备工具列表(启用 PTC) |
| 35 | toolSchemas := []provider.ToolSchema{ |
| 36 | { |
| 37 | Name: "CodeExecute", |
| 38 | Description: codeExecTool.Description(), |
| 39 | InputSchema: codeExecTool.InputSchema(), |
| 40 | AllowedCallers: []string{"direct"}, |
| 41 | }, |
| 42 | } |
| 43 | |
| 44 | // 添加可在 Python 中调用的工具 |
| 45 | allowedTools := []string{"Read", "Write", "Glob", "Grep", "Bash"} |
| 46 | for _, toolName := range allowedTools { |
| 47 | tool, _ := registry.Create(toolName, nil) |
| 48 | toolSchemas = append(toolSchemas, provider.ToolSchema{ |
| 49 | Name: toolName, |
| 50 | Description: tool.Description(), |
| 51 | InputSchema: tool.InputSchema(), |
| 52 | AllowedCallers: []string{"direct", "code_execution_20250825"}, |
| 53 | }) |
| 54 | } |
| 55 | |
| 56 | // 3. 创建 Anthropic Provider |
| 57 | providerConfig := &types.ModelConfig{ |
| 58 | Provider: "anthropic", |
| 59 | Model: "claude-3-5-sonnet-20241022", |
| 60 | APIKey: apiKey, |
| 61 | } |
| 62 | |
| 63 | anthropicProvider, err := provider.NewAnthropicProvider(providerConfig) |
| 64 | if err != nil { |
| 65 | log.Fatalf("创建 Provider 失败: %v", err) |
| 66 | } |
| 67 | |
| 68 | // 4. 定义复杂任务 |
| 69 | task := `请编写 Python 代码完成以下文件处理任务: |
| 70 | |
| 71 | 1. 使用 Glob 查找当前目录下所有 .go 文件 |
| 72 | 2. 使用 Grep 在这些文件中搜索包含 "TODO" 或 "FIXME" 的注释 |
| 73 | 3. 统计每个文件中待办事项的数量 |
| 74 | 4. 生成一份 Markdown 格式的报告,包含: |
| 75 | - 总待办事项数量 |
nothing calls this directly
no test coverage detected