GeezerVison is showing github.com — feat(gateway,relay): add judge provider config · majorcontext/keep@775e76d
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: cmd/keep-llm-gateway/main.go +23Lines changed: 23 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -16,6 +16,8 @@ import ( | 16 | 16 | "github.com/majorcontext/keep/internal/audit" | 17 | 17 | "github.com/majorcontext/keep/internal/gateway" | 18 | 18 | gwconfig "github.com/majorcontext/keep/internal/gateway/config" | | 19 | + anthropicjudge "github.com/majorcontext/keep/judge/anthropic" | | 20 | + openaijudge "github.com/majorcontext/keep/judge/openai" | 19 | 21 | ) | 20 | 22 |
| 21 | 23 | func main() { | @@ -42,6 +44,27 @@ func main() { | 42 | 44 | if cfg.PacksDir != "" { | 43 | 45 | engineOpts = append(engineOpts, keep.WithPacksDir(cfg.PacksDir)) | 44 | 46 | } | | 47 | + if cfg.Judge != nil { | | 48 | + apiKey := os.Getenv(cfg.Judge.APIKeyEnv) | | 49 | + if apiKey != "" { | | 50 | + switch cfg.Judge.Provider { | | 51 | + case "anthropic": | | 52 | + var jopts []anthropicjudge.Option | | 53 | + if cfg.Judge.BaseURL != "" { | | 54 | + jopts = append(jopts, anthropicjudge.WithBaseURL(cfg.Judge.BaseURL)) | | 55 | + } | | 56 | + p := anthropicjudge.New(apiKey, jopts...) | | 57 | + engineOpts = append(engineOpts, keep.WithJudge(p.Judge)) | | 58 | + case "openai": | | 59 | + var jopts []openaijudge.Option | | 60 | + if cfg.Judge.BaseURL != "" { | | 61 | + jopts = append(jopts, openaijudge.WithBaseURL(cfg.Judge.BaseURL)) | | 62 | + } | | 63 | + p := openaijudge.New(apiKey, jopts...) | | 64 | + engineOpts = append(engineOpts, keep.WithJudge(p.Judge)) | | 65 | + } | | 66 | + } | | 67 | + } | 45 | 68 | engine, err := keep.Load(cfg.RulesDir, engineOpts...) | 46 | 69 | if err != nil { | 47 | 70 | fmt.Fprintf(os.Stderr, "error loading rules: %v\n", err) | |
Collapse file Copy file name to clipboardExpand all lines: cmd/keep-mcp-relay/main.go +23Lines changed: 23 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -16,6 +16,8 @@ import ( | 16 | 16 | "github.com/majorcontext/keep/internal/relay" | 17 | 17 | relayconfig "github.com/majorcontext/keep/internal/relay/config" | 18 | 18 | "github.com/majorcontext/keep/internal/relay/mcp" | | 19 | + anthropicjudge "github.com/majorcontext/keep/judge/anthropic" | | 20 | + openaijudge "github.com/majorcontext/keep/judge/openai" | 19 | 21 | ) | 20 | 22 |
| 21 | 23 | func main() { | @@ -42,6 +44,27 @@ func main() { | 42 | 44 | if cfg.PacksDir != "" { | 43 | 45 | engineOpts = append(engineOpts, keep.WithPacksDir(cfg.PacksDir)) | 44 | 46 | } | | 47 | + if cfg.Judge != nil { | | 48 | + apiKey := os.Getenv(cfg.Judge.APIKeyEnv) | | 49 | + if apiKey != "" { | | 50 | + switch cfg.Judge.Provider { | | 51 | + case "anthropic": | | 52 | + var jopts []anthropicjudge.Option | | 53 | + if cfg.Judge.BaseURL != "" { | | 54 | + jopts = append(jopts, anthropicjudge.WithBaseURL(cfg.Judge.BaseURL)) | | 55 | + } | | 56 | + p := anthropicjudge.New(apiKey, jopts...) | | 57 | + engineOpts = append(engineOpts, keep.WithJudge(p.Judge)) | | 58 | + case "openai": | | 59 | + var jopts []openaijudge.Option | | 60 | + if cfg.Judge.BaseURL != "" { | | 61 | + jopts = append(jopts, openaijudge.WithBaseURL(cfg.Judge.BaseURL)) | | 62 | + } | | 63 | + p := openaijudge.New(apiKey, jopts...) | | 64 | + engineOpts = append(engineOpts, keep.WithJudge(p.Judge)) | | 65 | + } | | 66 | + } | | 67 | + } | 45 | 68 | engine, err := keep.Load(cfg.RulesDir, engineOpts...) | 46 | 69 | if err != nil { | 47 | 70 | fmt.Fprintf(os.Stderr, "error loading rules: %v\n", err) | |
Collapse file Copy file name to clipboardExpand all lines: internal/gateway/config/config.go +8Lines changed: 8 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -10,6 +10,13 @@ import ( | 10 | 10 | "github.com/majorcontext/keep/llm" | 11 | 11 | ) | 12 | 12 |
| | 13 | +// JudgeConfig holds configuration for the LLM-as-judge provider. | | 14 | +type JudgeConfig struct { | | 15 | + Provider string `yaml:"provider"` | | 16 | + APIKeyEnv string `yaml:"api_key_env"` | | 17 | + BaseURL string `yaml:"base_url,omitempty"` | | 18 | +} | | 19 | + | 13 | 20 | // GatewayConfig holds the top-level gateway configuration. | 14 | 21 | type GatewayConfig struct { | 15 | 22 | Listen string `yaml:"listen"` | @@ -20,6 +27,7 @@ type GatewayConfig struct { | 20 | 27 | Upstream string `yaml:"upstream"` | 21 | 28 | Scope string `yaml:"scope"` | 22 | 29 | Decompose DecomposeConfig `yaml:"decompose,omitempty"` | | 30 | + Judge *JudgeConfig `yaml:"judge,omitempty"` | 23 | 31 | Log keepconfig.LogConfig `yaml:"log,omitempty"` | 24 | 32 | } | 25 | 33 |
| |
Collapse file Copy file name to clipboardExpand all lines: internal/gateway/config/config_test.go +73Lines changed: 73 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -236,6 +236,79 @@ func TestDecomposeConfig_FieldParity(t *testing.T) { | 236 | 236 | } | 237 | 237 | } | 238 | 238 |
| | 239 | +func TestParseJudgeConfig(t *testing.T) { | | 240 | + y := ` | | 241 | +listen: ":8080" | | 242 | +rules_dir: "./rules" | | 243 | +provider: anthropic | | 244 | +upstream: "https://api.anthropic.com" | | 245 | +scope: test-scope | | 246 | +judge: | | 247 | + provider: anthropic | | 248 | + api_key_env: ANTHROPIC_API_KEY | | 249 | +` | | 250 | + path := writeTempYAML(t, y) | | 251 | + cfg, err := Load(path) | | 252 | + if err != nil { | | 253 | + t.Fatalf("unexpected error: %v", err) | | 254 | + } | | 255 | + if cfg.Judge == nil { | | 256 | + t.Fatal("expected judge config, got nil") | | 257 | + } | | 258 | + if cfg.Judge.Provider != "anthropic" { | | 259 | + t.Errorf("judge.provider: got %q, want %q", cfg.Judge.Provider, "anthropic") | | 260 | + } | | 261 | + if cfg.Judge.APIKeyEnv != "ANTHROPIC_API_KEY" { | | 262 | + t.Errorf("judge.api_key_env: got %q, want %q", cfg.Judge.APIKeyEnv, "ANTHROPIC_API_KEY") | | 263 | + } | | 264 | +} | | 265 | + | | 266 | +func TestParseJudgeConfig_WithBaseURL(t *testing.T) { | | 267 | + y := ` | | 268 | +listen: ":8080" | | 269 | +rules_dir: "./rules" | | 270 | +provider: anthropic | | 271 | +upstream: "https://api.anthropic.com" | | 272 | +scope: test-scope | | 273 | +judge: | | 274 | + provider: openai | | 275 | + api_key_env: OPENAI_API_KEY | | 276 | + base_url: "https://custom.openai.com" | | 277 | +` | | 278 | + path := writeTempYAML(t, y) | | 279 | + cfg, err := Load(path) | | 280 | + if err != nil { | | 281 | + t.Fatalf("unexpected error: %v", err) | | 282 | + } | | 283 | + if cfg.Judge == nil { | | 284 | + t.Fatal("expected judge config, got nil") | | 285 | + } | | 286 | + if cfg.Judge.Provider != "openai" { | | 287 | + t.Errorf("judge.provider: got %q, want %q", cfg.Judge.Provider, "openai") | | 288 | + } | | 289 | + if cfg.Judge.BaseURL != "https://custom.openai.com" { | | 290 | + t.Errorf("judge.base_url: got %q, want %q", cfg.Judge.BaseURL, "https://custom.openai.com") | | 291 | + } | | 292 | +} | | 293 | + | | 294 | +func TestParseJudgeConfig_Absent(t *testing.T) { | | 295 | + y := ` | | 296 | +listen: ":8080" | | 297 | +rules_dir: "./rules" | | 298 | +provider: anthropic | | 299 | +upstream: "https://api.anthropic.com" | | 300 | +scope: test-scope | | 301 | +` | | 302 | + path := writeTempYAML(t, y) | | 303 | + cfg, err := Load(path) | | 304 | + if err != nil { | | 305 | + t.Fatalf("unexpected error: %v", err) | | 306 | + } | | 307 | + if cfg.Judge != nil { | | 308 | + t.Errorf("expected nil judge config, got %+v", cfg.Judge) | | 309 | + } | | 310 | +} | | 311 | + | 239 | 312 | // writeTempYAML writes content to a temp file and returns its path. | 240 | 313 | func writeTempYAML(t *testing.T, content string) string { | 241 | 314 | t.Helper() | |
Collapse file Copy file name to clipboardExpand all lines: internal/relay/config/config.go +8Lines changed: 8 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -9,13 +9,21 @@ import ( | 9 | 9 | keepconfig "github.com/majorcontext/keep/internal/config" | 10 | 10 | ) | 11 | 11 |
| | 12 | +// JudgeConfig holds configuration for the LLM-as-judge provider. | | 13 | +type JudgeConfig struct { | | 14 | + Provider string `yaml:"provider"` | | 15 | + APIKeyEnv string `yaml:"api_key_env"` | | 16 | + BaseURL string `yaml:"base_url,omitempty"` | | 17 | +} | | 18 | + | 12 | 19 | // RelayConfig holds the top-level relay configuration. | 13 | 20 | type RelayConfig struct { | 14 | 21 | Listen string `yaml:"listen"` | 15 | 22 | RulesDir string `yaml:"rules_dir"` | 16 | 23 | ProfilesDir string `yaml:"profiles_dir,omitempty"` | 17 | 24 | PacksDir string `yaml:"packs_dir,omitempty"` | 18 | 25 | Routes []Route `yaml:"routes"` | | 26 | + Judge *JudgeConfig `yaml:"judge,omitempty"` | 19 | 27 | Log keepconfig.LogConfig `yaml:"log,omitempty"` | 20 | 28 | } | 21 | 29 |
| |
Collapse file Copy file name to clipboardExpand all lines: internal/relay/config/config_test.go +73Lines changed: 73 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -267,6 +267,79 @@ routes: | 267 | 267 | } | 268 | 268 | } | 269 | 269 |
| | 270 | +func TestParseJudgeConfig(t *testing.T) { | | 271 | + y := ` | | 272 | +listen: ":8090" | | 273 | +rules_dir: "./rules" | | 274 | +routes: | | 275 | + - scope: test | | 276 | + upstream: "https://example.com" | | 277 | +judge: | | 278 | + provider: anthropic | | 279 | + api_key_env: ANTHROPIC_API_KEY | | 280 | +` | | 281 | + path := writeTempYAML(t, y) | | 282 | + cfg, err := Load(path) | | 283 | + if err != nil { | | 284 | + t.Fatalf("unexpected error: %v", err) | | 285 | + } | | 286 | + if cfg.Judge == nil { | | 287 | + t.Fatal("expected judge config, got nil") | | 288 | + } | | 289 | + if cfg.Judge.Provider != "anthropic" { | | 290 | + t.Errorf("judge.provider: got %q, want %q", cfg.Judge.Provider, "anthropic") | | 291 | + } | | 292 | + if cfg.Judge.APIKeyEnv != "ANTHROPIC_API_KEY" { | | 293 | + t.Errorf("judge.api_key_env: got %q, want %q", cfg.Judge.APIKeyEnv, "ANTHROPIC_API_KEY") | | 294 | + } | | 295 | +} | | 296 | + | | 297 | +func TestParseJudgeConfig_WithBaseURL(t *testing.T) { | | 298 | + y := ` | | 299 | +listen: ":8090" | | 300 | +rules_dir: "./rules" | | 301 | +routes: | | 302 | + - scope: test | | 303 | + upstream: "https://example.com" | | 304 | +judge: | | 305 | + provider: openai | | 306 | + api_key_env: OPENAI_API_KEY | | 307 | + base_url: "https://custom.openai.com" | | 308 | +` | | 309 | + path := writeTempYAML(t, y) | | 310 | + cfg, err := Load(path) | | 311 | + if err != nil { | | 312 | + t.Fatalf("unexpected error: %v", err) | | 313 | + } | | 314 | + if cfg.Judge == nil { | | 315 | + t.Fatal("expected judge config, got nil") | | 316 | + } | | 317 | + if cfg.Judge.Provider != "openai" { | | 318 | + t.Errorf("judge.provider: got %q, want %q", cfg.Judge.Provider, "openai") | | 319 | + } | | 320 | + if cfg.Judge.BaseURL != "https://custom.openai.com" { | | 321 | + t.Errorf("judge.base_url: got %q, want %q", cfg.Judge.BaseURL, "https://custom.openai.com") | | 322 | + } | | 323 | +} | | 324 | + | | 325 | +func TestParseJudgeConfig_Absent(t *testing.T) { | | 326 | + y := ` | | 327 | +listen: ":8090" | | 328 | +rules_dir: "./rules" | | 329 | +routes: | | 330 | + - scope: test | | 331 | + upstream: "https://example.com" | | 332 | +` | | 333 | + path := writeTempYAML(t, y) | | 334 | + cfg, err := Load(path) | | 335 | + if err != nil { | | 336 | + t.Fatalf("unexpected error: %v", err) | | 337 | + } | | 338 | + if cfg.Judge != nil { | | 339 | + t.Errorf("expected nil judge config, got %+v", cfg.Judge) | | 340 | + } | | 341 | +} | | 342 | + | 270 | 343 | // writeTempYAML writes content to a temp file and returns its path. | 271 | 344 | func writeTempYAML(t *testing.T, content string) string { | 272 | 345 | t.Helper() | |
|