|
| 1 | +// Copyright (c) 2026 coze-dev Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package open_telemetry |
| 5 | + |
| 6 | +import "fmt" |
| 7 | + |
| 8 | +func AddTools2ModelInput(input interface{}, tools interface{}) (interface{}, error) { |
| 9 | + modelInput, ok := input.(map[string]interface{}) |
| 10 | + if !ok { |
| 11 | + return nil, fmt.Errorf("input is not a map") |
| 12 | + } |
| 13 | + |
| 14 | + toolsSlice, ok := tools.([]interface{}) |
| 15 | + if !ok { |
| 16 | + return modelInput, nil |
| 17 | + } |
| 18 | + |
| 19 | + modelTools := make([]interface{}, 0, len(toolsSlice)) |
| 20 | + for _, tool := range toolsSlice { |
| 21 | + toolMap, ok := tool.(map[string]interface{}) |
| 22 | + if !ok { |
| 23 | + continue |
| 24 | + } |
| 25 | + |
| 26 | + function, ok := toolMap["function"].(map[string]interface{}) |
| 27 | + if !ok { |
| 28 | + function = toolMap // maybe no function key, it has been a raw function |
| 29 | + } |
| 30 | + |
| 31 | + name, _ := function["name"] |
| 32 | + if name == nil { |
| 33 | + name = "" |
| 34 | + } |
| 35 | + description, _ := function["description"] |
| 36 | + if description == nil { |
| 37 | + description = "" |
| 38 | + } |
| 39 | + parameters, _ := function["parameters"] |
| 40 | + if parameters == nil { |
| 41 | + parameters = "{}" |
| 42 | + } |
| 43 | + modelTool := map[string]interface{}{ |
| 44 | + "type": "function", |
| 45 | + "function": map[string]interface{}{ |
| 46 | + "name": name, |
| 47 | + "description": description, |
| 48 | + "parameters": parameters, |
| 49 | + }, |
| 50 | + } |
| 51 | + |
| 52 | + modelTools = append(modelTools, modelTool) |
| 53 | + } |
| 54 | + |
| 55 | + if len(modelTools) > 0 { |
| 56 | + modelInput["tools"] = modelTools |
| 57 | + } |
| 58 | + |
| 59 | + return modelInput, nil |
| 60 | +} |
0 commit comments