Run posts each item to POST {baseURL}/infer and prints a summary to w. defaultPipeline is applied when an item omits pipeline_ref. apiKey, if non-empty, is sent as X-InferCore-Api-Key (matches server infercore_api_key).
(ctx context.Context, baseURL string, datasetPath string, w io.Writer, defaultPipeline, apiKey string)
| 42 | // defaultPipeline is applied when an item omits pipeline_ref. |
| 43 | // apiKey, if non-empty, is sent as X-InferCore-Api-Key (matches server infercore_api_key). |
| 44 | func Run(ctx context.Context, baseURL string, datasetPath string, w io.Writer, defaultPipeline, apiKey string) error { |
| 45 | raw, err := os.ReadFile(datasetPath) |
| 46 | if err != nil { |
| 47 | return err |
| 48 | } |
| 49 | var items []Item |
| 50 | if err := json.Unmarshal(raw, &items); err != nil { |
| 51 | return fmt.Errorf("parse dataset: %w", err) |
| 52 | } |
| 53 | baseURL = strings.TrimRight(strings.TrimSpace(baseURL), "/") |
| 54 | client := &http.Client{Timeout: 120 * time.Second} |
| 55 | |
| 56 | var results []Result |
| 57 | for i, it := range items { |
| 58 | body := map[string]any{ |
| 59 | "tenant_id": it.TenantID, |
| 60 | "task_type": it.TaskType, |
| 61 | "input": it.Input, |
| 62 | "options": it.Options, |
| 63 | } |
| 64 | if it.Priority != "" { |
| 65 | body["priority"] = it.Priority |
| 66 | } |
| 67 | if it.RequestType != "" { |
| 68 | body["request_type"] = it.RequestType |
| 69 | } |
| 70 | pref := strings.TrimSpace(it.PipelineRef) |
| 71 | if pref == "" { |
| 72 | pref = strings.TrimSpace(defaultPipeline) |
| 73 | } |
| 74 | if pref != "" { |
| 75 | body["pipeline_ref"] = pref |
| 76 | } |
| 77 | if it.Context != nil { |
| 78 | body["context"] = it.Context |
| 79 | } |
| 80 | payload, _ := json.Marshal(body) |
| 81 | req, err := http.NewRequestWithContext(ctx, http.MethodPost, baseURL+"/infer", bytes.NewReader(payload)) |
| 82 | if err != nil { |
| 83 | return err |
| 84 | } |
| 85 | req.Header.Set("Content-Type", "application/json") |
| 86 | if strings.TrimSpace(apiKey) != "" { |
| 87 | req.Header.Set("X-InferCore-Api-Key", strings.TrimSpace(apiKey)) |
| 88 | } |
| 89 | start := time.Now() |
| 90 | resp, err := client.Do(req) |
| 91 | lat := time.Since(start).Milliseconds() |
| 92 | if err != nil { |
| 93 | results = append(results, Result{ItemIndex: i, OK: false, LatencyMs: lat}) |
| 94 | continue |
| 95 | } |
| 96 | b, _ := io.ReadAll(resp.Body) |
| 97 | _ = resp.Body.Close() |
| 98 | var parsed map[string]any |
| 99 | _ = json.Unmarshal(b, &parsed) |
| 100 | sel := "" |
| 101 | if v, ok := parsed["selected_backend"].(string); ok { |