返回 DeepSeek-Reasonix
meterconfig.go
根目录 / cmd / e2ebench / meterconfig.go
1 package main
2
3 import (
4 "fmt"
5 "os"
6 "path/filepath"
7 "strings"
8
9 "github.com/BurntSushi/toml"
10 )
11
12 // meterUpstream reports the endpoint the meter must forward to: the base_url
13 // of the provider serving the benchmarked model. Only that provider is ever
14 // redirected — rewriting every endpoint would send one vendor's traffic to
15 // another's host.
16 func meterUpstream(configPath, model string) (upstream string, err error) {
17 raw, err := os.ReadFile(configPath)
18 if err != nil {
19 return "", fmt.Errorf("read config for metering: %w", err)
20 }
21 var doc map[string]any
22 if err := toml.Unmarshal(raw, &doc); err != nil {
23 return "", fmt.Errorf("parse %s: %w", configPath, err)
24 }
25 providers, _ := doc["providers"].([]map[string]any)
26 if len(providers) == 0 {
27 if list, ok := doc["providers"].([]any); ok {
28 for _, entry := range list {
29 if p, ok := entry.(map[string]any); ok {
30 providers = append(providers, p)
31 }
32 }
33 }
34 }
35 if len(providers) == 0 {
36 return "", fmt.Errorf("%s declares no [[providers]]; the meter has nothing to redirect", configPath)
37 }
38 target := providerForModel(providers, model)
39 upstream, _ = target["base_url"].(string)
40 if strings.TrimSpace(upstream) == "" {
41 return "", fmt.Errorf("provider %v has no base_url to redirect", target["name"])
42 }
43 return upstream, nil
44 }
45
46 // providerForModel picks the entry that serves model, falling back to the
47 // first. A benchmark run is single-model, so an exact match is the common case
48 // and the fallback only matters for a one-provider config.
49 func providerForModel(providers []map[string]any, model string) map[string]any {
50 model = strings.TrimSpace(model)
51 if model != "" {
52 if _, want, ok := strings.Cut(model, "/"); ok {
53 model = want
54 }
55 for _, p := range providers {
56 if name, _ := p["default"].(string); name == model {
57 return p
58 }
59 list, _ := p["models"].([]any)
60 for _, m := range list {
61 if s, _ := m.(string); s == model {
62 return p
63 }
64 }
65 if s, _ := p["model"].(string); s == model {
66 return p
67 }
68 }
69 }
70 return providers[0]
71 }
72
73 // writeMeteredConfig rewrites the chosen provider's base_url to meterBase and
74 // writes the result into dir as config.toml.
75 func writeMeteredConfig(configPath, dir, model, meterBase string) error {
76 raw, err := os.ReadFile(configPath)
77 if err != nil {
78 return err
79 }
80 var doc map[string]any
81 if err := toml.Unmarshal(raw, &doc); err != nil {
82 return err
83 }
84 var providers []map[string]any
85 switch list := doc["providers"].(type) {
86 case []map[string]any:
87 providers = list
88 case []any:
89 for _, entry := range list {
90 if p, ok := entry.(map[string]any); ok {
91 providers = append(providers, p)
92 }
93 }
94 }
95 if len(providers) == 0 {
96 return fmt.Errorf("%s declares no [[providers]]", configPath)
97 }
98 target := providerForModel(providers, model)
99 target["base_url"] = meterBase
100 // The redirected endpoint is loopback plaintext; a proxy configured for the
101 // real vendor host must not be applied to it.
102 target["no_proxy"] = true
103 doc["providers"] = providers
104
105 if err := os.MkdirAll(dir, 0o755); err != nil {
106 return err
107 }
108 out, err := os.Create(filepath.Join(dir, "config.toml"))
109 if err != nil {
110 return err
111 }
112 defer out.Close()
113 return toml.NewEncoder(out).Encode(doc)
114 }
115
115 lines GO