-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathsystem.py
More file actions
1013 lines (866 loc) · 34.9 KB
/
system.py
File metadata and controls
1013 lines (866 loc) · 34.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# pylint: disable=too-many-lines
"""System configuration models."""
import os
from typing import Any, Optional
from pydantic import (
BaseModel,
ConfigDict,
Field,
ValidationError,
field_validator,
model_validator,
)
from lightspeed_evaluation.core.constants import (
DEFAULT_API_BASE,
DEFAULT_API_CACHE_DIR,
DEFAULT_API_TIMEOUT,
DEFAULT_API_VERSION,
DEFAULT_API_NUM_RETRIES,
DEFAULT_EMBEDDING_CACHE_DIR,
DEFAULT_EMBEDDING_MODEL,
DEFAULT_EMBEDDING_PROVIDER,
DEFAULT_ENDPOINT_TYPE,
DEFAULT_LLM_CACHE_DIR,
DEFAULT_LLM_MAX_TOKENS,
DEFAULT_LLM_MODEL,
DEFAULT_LLM_PROVIDER,
DEFAULT_SSL_VERIFY,
DEFAULT_SSL_CERT_FILE,
DEFAULT_LLM_RETRIES,
DEFAULT_LLM_TEMPERATURE,
DEFAULT_LOG_FORMAT,
DEFAULT_LOG_PACKAGE_LEVEL,
DEFAULT_LOG_SHOW_TIMESTAMPS,
DEFAULT_LOG_SOURCE_LEVEL,
DEFAULT_VISUALIZATION_DPI,
DEFAULT_VISUALIZATION_FIGSIZE,
SUPPORTED_ENDPOINT_TYPES,
SUPPORTED_GRAPH_TYPES,
)
from lightspeed_evaluation.core.storage.config import StorageBackendConfig
from lightspeed_evaluation.core.system.exceptions import ConfigurationError
# Keys not allowed in llm_pool.parameters
# These are either request-envelope fields or managed via dedicated config fields
FORBIDDEN_PARAMETER_KEYS = frozenset(
{
# Request envelope fields
"model",
"messages",
"n",
# Operational fields (set via dedicated config)
"timeout",
"num_retries",
"ssl_verify",
"ssl_cert_file",
"cache_enabled",
"cache_dir",
# Provider/client fields
"provider",
"client",
}
)
class LLMConfig(BaseModel):
"""LLM configuration from system configuration."""
model_config = ConfigDict(extra="forbid")
provider: str = Field(
default=DEFAULT_LLM_PROVIDER,
min_length=1,
description="Provider name, e.g., openai, azure, watsonx etc..",
)
model: str = Field(
default=DEFAULT_LLM_MODEL,
min_length=1,
description="Model identifier or deployment name",
)
ssl_verify: bool = Field(
default=DEFAULT_SSL_VERIFY,
description="Verify SSL certificates for HTTPS connections. Can be True/False",
)
ssl_cert_file: Optional[str] = Field(
default=DEFAULT_SSL_CERT_FILE,
description="Path to custom CA certificate file for SSL verification",
)
@model_validator(mode="after")
def validate_ssl_cert_file(self) -> "LLMConfig":
"""Validate SSL certificate file exists if provided."""
if self.ssl_cert_file is not None:
cert_path = self.ssl_cert_file
# Expand environment variables and user paths
cert_path = os.path.expandvars(os.path.expanduser(cert_path))
# Check if file exists
if not os.path.isfile(cert_path):
raise ConfigurationError(
f"SSL certificate file not found: '{cert_path}'. "
f"Original path: '{self.ssl_cert_file}'. "
"Please provide a valid path to a CA certificate file "
"or set ssl_cert_file to null."
)
# Update to absolute path for consistency
self.ssl_cert_file = os.path.abspath(cert_path)
return self
temperature: float = Field(
default=DEFAULT_LLM_TEMPERATURE,
ge=0.0,
le=2.0,
description="Sampling temperature",
)
max_tokens: int = Field(
default=DEFAULT_LLM_MAX_TOKENS, ge=1, description="Maximum tokens in response"
)
timeout: int = Field(
default=DEFAULT_API_TIMEOUT, ge=1, description="Request timeout in seconds"
)
num_retries: int = Field(
default=DEFAULT_LLM_RETRIES,
ge=0,
description="Retry attempts for failed requests",
)
cache_dir: str = Field(
default=DEFAULT_LLM_CACHE_DIR,
min_length=1,
description="Location of cached 'LLM as a judge' queries",
)
cache_enabled: bool = Field(
default=True, description="Is caching of 'LLM as a judge' queries enabled?"
)
parameters: dict[str, Any] = Field(
default_factory=dict,
description="Internal: dynamic LLM parameters for API calls",
)
@model_validator(mode="before")
@classmethod
def strip_user_parameters(cls, data: Any) -> Any:
"""Strip parameters from YAML/dict input.
Dynamic parameters via YAML are only supported through llm_pool.
For legacy llm:, parameters is always built from explicit fields.
"""
if isinstance(data, dict):
data.pop("parameters", None)
return data
@model_validator(mode="after")
def build_parameters_from_fields(self) -> "LLMConfig":
"""Build parameters dict from explicit temperature and max_tokens.
For the pool path, resolve_llm_config() overrides this via
model_copy(update=...) after construction.
"""
self.parameters = {
"temperature": self.temperature,
"max_completion_tokens": self.max_tokens,
}
return self
class EmbeddingConfig(BaseModel):
"""Embedding configuration."""
model_config = ConfigDict(extra="forbid")
provider: str = Field(
default=DEFAULT_EMBEDDING_PROVIDER,
min_length=1,
description="Provider name, e.g., huggingface, openai",
)
model: str = Field(
default=DEFAULT_EMBEDDING_MODEL,
min_length=1,
description="Embedding model identifier",
)
provider_kwargs: Optional[dict[str, Any]] = Field(
default=None,
description="Embedding provider arguments, e.g. model_kwargs: device:cpu",
)
cache_dir: str = Field(
default=DEFAULT_EMBEDDING_CACHE_DIR,
min_length=1,
description="Location of cached embedding queries",
)
cache_enabled: bool = Field(
default=True, description="Is caching of embedding queries enabled?"
)
@field_validator("provider")
@classmethod
def _validate_provider(cls, v: str) -> str:
allowed = {"openai", "huggingface", "gemini"}
if v not in allowed:
raise ValueError(
f"Unsupported embedding provider '{v}'. Allowed: {sorted(allowed)}"
)
return v
class MCPServerConfig(BaseModel):
"""Configuration for a single MCP server authentication."""
model_config = ConfigDict(extra="forbid")
env_var: str = Field(
...,
min_length=1,
description="Environment variable containing the token/key",
)
header_name: Optional[str] = Field(
default=None,
description="Custom header name (optional, defaults to 'Authorization')",
)
class MCPHeadersConfig(BaseModel):
"""Configuration for MCP headers functionality."""
model_config = ConfigDict(extra="forbid")
enabled: bool = Field(
default=True,
description="Enable MCP headers functionality",
)
servers: dict[str, MCPServerConfig] = Field(
default_factory=dict,
description="MCP server configurations",
)
@model_validator(mode="after")
def _validate_env_vars_when_enabled(self) -> "MCPHeadersConfig":
"""Validate that environment variables are set when MCP headers are enabled."""
if self.enabled and self.servers:
missing_vars = []
for server_name, server_config in self.servers.items():
if not os.getenv(server_config.env_var):
missing_vars.append(f"{server_name}: {server_config.env_var}")
if missing_vars:
missing_list = ", ".join(missing_vars)
msg = (
"MCP headers are enabled but required environment variables are not set: "
f"{missing_list}"
)
raise ValueError(msg)
return self
class APIConfig(BaseModel):
"""API configuration for dynamic data generation."""
model_config = ConfigDict(extra="forbid")
enabled: bool = Field(default=True, description="Enable API-based data generation")
api_base: str = Field(
default=DEFAULT_API_BASE,
description="Base URL for API requests (without version)",
)
version: str = Field(
default=DEFAULT_API_VERSION, description="API version (e.g., v1, v2)"
)
endpoint_type: str = Field(
default=DEFAULT_ENDPOINT_TYPE,
description="API endpoint type (streaming or query)",
)
timeout: int = Field(
default=DEFAULT_API_TIMEOUT, ge=1, description="Request timeout in seconds"
)
provider: Optional[str] = Field(default=None, description="LLM provider for API")
model: Optional[str] = Field(default=None, description="LLM model for API")
no_tools: Optional[bool] = Field(
default=None, description="Disable tool usage in API calls"
)
system_prompt: Optional[str] = Field(
default=None, description="System prompt for API calls"
)
extra_request_params: Optional[dict[str, Any]] = Field(default=None)
cache_dir: str = Field(
default=DEFAULT_API_CACHE_DIR,
min_length=1,
description="Location of cached lightspeed-stack queries",
)
cache_enabled: bool = Field(
default=True, description="Is caching of lightspeed-stack queries enabled?"
)
mcp_headers: Optional[MCPHeadersConfig] = Field(
default=None, description="MCP headers configuration for authentication"
)
num_retries: int = Field(
default=DEFAULT_API_NUM_RETRIES,
ge=0,
description=(
"Maximum number of retry attempts for API calls on "
"429 Too Many Requests errors"
),
)
@field_validator("endpoint_type")
@classmethod
def validate_endpoint_type(cls, v: str) -> str:
"""Validate endpoint type is supported."""
if v not in SUPPORTED_ENDPOINT_TYPES:
raise ValueError(f"Endpoint type must be one of {SUPPORTED_ENDPOINT_TYPES}")
return v
class LoggingConfig(BaseModel):
"""Logging configuration."""
model_config = ConfigDict(extra="forbid")
source_level: str = Field(
default=DEFAULT_LOG_SOURCE_LEVEL, description="Source code logging level"
)
package_level: str = Field(
default=DEFAULT_LOG_PACKAGE_LEVEL, description="Package logging level"
)
log_format: str = Field(
default=DEFAULT_LOG_FORMAT, description="Log message format"
)
show_timestamps: bool = Field(
default=DEFAULT_LOG_SHOW_TIMESTAMPS, description="Show timestamps in logs"
)
package_overrides: dict[str, str] = Field(
default_factory=dict, description="Package-specific log level overrides"
)
class VisualizationConfig(BaseModel):
"""Visualization configuration for graphs and charts."""
model_config = ConfigDict(extra="forbid")
figsize: list[int] = Field(
default=DEFAULT_VISUALIZATION_FIGSIZE, description="Figure size [width, height]"
)
dpi: int = Field(
default=DEFAULT_VISUALIZATION_DPI, ge=50, description="Resolution in DPI"
)
enabled_graphs: list[str] = Field(
default=[],
description="List of graph types to generate",
)
@field_validator("enabled_graphs")
@classmethod
def validate_enabled_graphs(cls, v: list[str]) -> list[str]:
"""Validate that all enabled graphs are supported."""
for graph_type in v:
if graph_type not in SUPPORTED_GRAPH_TYPES:
raise ValueError(
f"Unsupported graph type: {graph_type}. "
f"Supported types: {SUPPORTED_GRAPH_TYPES}"
)
return v
class CoreConfig(BaseModel):
"""Core evaluation configuration (e.g., concurrency limits)."""
model_config = ConfigDict(extra="forbid")
max_threads: Optional[int] = Field(
default=None,
description="Maximum threads for multithreading eval",
gt=0,
)
fail_on_invalid_data: bool = Field(
default=True,
description="If False don't fail on invalid conversations",
)
skip_on_failure: bool = Field(
default=False,
description="Skip remaining turns in conversation when a turn evaluation fails",
)
class LLMParametersConfig(BaseModel):
"""Dynamic parameters passed to LLM API calls.
These parameters are passed directly to the LLM provider.
All fields are optional - unset fields inherit from parent level.
Uses extra="allow" to pass through any provider-specific parameters.
"""
model_config = ConfigDict(extra="allow")
temperature: Optional[float] = Field(
default=None,
ge=0.0,
le=2.0,
description="Sampling temperature",
)
max_completion_tokens: Optional[int] = Field(
default=None,
ge=1,
description="Maximum tokens in response",
)
@model_validator(mode="before")
@classmethod
def reject_forbidden_keys(cls, data: Any) -> Any:
"""Reject keys that must be set via dedicated config fields, not parameters."""
if not isinstance(data, dict):
return data
forbidden_found = FORBIDDEN_PARAMETER_KEYS & data.keys()
if forbidden_found:
raise ConfigurationError(
f"Keys not allowed in parameters: {forbidden_found}. "
f"Use dedicated config fields instead."
)
return data
def to_dict(self, exclude_none: bool = True) -> dict[str, Any]:
"""Convert parameters to dict for passing to LLM.
Args:
exclude_none: If True, exclude None values from output.
If False, include only explicitly set fields (uses model_fields_set
to distinguish user-provided None from unset defaults).
Returns:
Dict of parameters ready for LLM API call
"""
if exclude_none:
return {k: v for k, v in self.model_dump().items() if v is not None}
# Include only fields the user explicitly set (including None overrides)
return {
k: v for k, v in self.model_dump().items() if k in self.model_fields_set
}
class LLMDefaultsConfig(BaseModel):
"""Global default settings for all LLMs in the pool.
These are shared defaults that apply to all LLMs unless overridden
at the provider or model level.
"""
model_config = ConfigDict(extra="forbid")
cache_enabled: bool = Field(
default=True,
description="Is caching of LLM queries enabled?",
)
cache_dir: str = Field(
default=DEFAULT_LLM_CACHE_DIR,
min_length=1,
description="Base cache directory",
)
timeout: int = Field(
default=DEFAULT_API_TIMEOUT,
ge=1,
description="Request timeout in seconds",
)
num_retries: int = Field(
default=DEFAULT_LLM_RETRIES,
ge=0,
description="Retry attempts for failed requests",
)
# Default dynamic parameters
parameters: LLMParametersConfig = Field(
default_factory=lambda: LLMParametersConfig(
temperature=DEFAULT_LLM_TEMPERATURE,
max_completion_tokens=DEFAULT_LLM_MAX_TOKENS,
),
description="Default dynamic parameters for LLM calls",
)
class LLMProviderConfig(BaseModel):
"""Configuration for a single LLM provider/model in the pool.
Contains model-specific settings. Cache and retry settings are managed
at the pool defaults level, not per-model.
The dict key is the unique model ID used for referencing.
"""
model_config = ConfigDict(extra="forbid")
# Required: Provider type
provider: str = Field(
min_length=1,
description="Provider type (e.g., openai, watsonx, gemini, hosted_vllm)",
)
# Model identity (optional - defaults to dict key)
model: Optional[str] = Field(
default=None,
min_length=1,
description="Actual model name. If not set, uses the dict key as model name.",
)
# SSL settings (optional - inherit from defaults or use system defaults)
ssl_verify: Optional[bool] = Field(
default=None,
description="Verify SSL certificates. Inherits from defaults if not set.",
)
ssl_cert_file: Optional[str] = Field(
default=None,
description="Path to custom CA certificate file",
)
# API endpoint/key configuration (optional - falls back to environment variable)
api_base: Optional[str] = Field(
default=None,
min_length=1,
description=(
"Base URL for the API endpoint. "
"If not set, falls back to provider-specific environment variable."
),
)
api_key_path: Optional[str] = Field(
default=None,
min_length=1,
description=(
"Path to text file containing the API key for this model. "
"If not set, falls back to provider-specific environment variable."
),
)
# Dynamic parameters (passed to LLM API)
parameters: LLMParametersConfig = Field(
default_factory=LLMParametersConfig,
description="Dynamic parameters for this model (merged with defaults)",
)
# Timeout can be model-specific (some models are slower)
timeout: Optional[int] = Field(
default=None,
ge=1,
description="Override timeout for this model",
)
class LLMPoolConfig(BaseModel):
"""Pool of LLM configurations for reuse across the system.
Provides a centralized place to define all LLM configurations,
which can be referenced by judge_panel, agents, or other components.
Cache and retry settings are managed at the defaults level only.
Model entries contain model-specific settings (provider, parameters, SSL).
"""
model_config = ConfigDict(extra="forbid")
defaults: LLMDefaultsConfig = Field(
default_factory=LLMDefaultsConfig,
description="Global default settings for all LLMs (cache, retry, parameters)",
)
models: dict[str, LLMProviderConfig] = Field(
default_factory=dict,
description="Model configurations. Key is unique model ID for referencing.",
)
def get_model_ids(self) -> list[str]:
"""Get all available model IDs."""
return list(self.models.keys())
def resolve_llm_config(
self, model_id: str, cache_suffix: Optional[str] = None
) -> LLMConfig:
"""Resolve a model ID to a fully configured LLMConfig.
Resolution order: defaults -> model entry (for model-specific fields)
Args:
model_id: Model identifier (key in models dict)
cache_suffix: Optional suffix for cache directory (e.g., "judge_0")
Returns:
Fully resolved LLMConfig
Raises:
ConfigurationError: If model_id not found
"""
if model_id not in self.models:
raise ConfigurationError(
f"Model '{model_id}' not found in llm_pool.models. "
f"Available: {list(self.models.keys())}"
)
entry = self.models[model_id]
# Merge parameters: defaults -> individual (individual overrides defaults).
# None in individual explicitly removes the default's value.
# Note: Forbidden keys are rejected at LLMParametersConfig load time.
merged_params: dict[str, Any] = {}
merged_params.update(self.defaults.parameters.to_dict(exclude_none=True))
merged_params.update(entry.parameters.to_dict(exclude_none=False))
merged_params = {k: v for k, v in merged_params.items() if v is not None}
# Build cache_dir from defaults with model-specific suffix
suffix = cache_suffix if cache_suffix else model_id
cache_dir = os.path.join(self.defaults.cache_dir, suffix)
config = LLMConfig(
provider=entry.provider,
model=entry.model or model_id,
temperature=merged_params.get("temperature", DEFAULT_LLM_TEMPERATURE),
max_tokens=merged_params.get(
"max_completion_tokens", DEFAULT_LLM_MAX_TOKENS
),
timeout=(
entry.timeout if entry.timeout is not None else self.defaults.timeout
),
num_retries=self.defaults.num_retries,
ssl_verify=(
entry.ssl_verify if entry.ssl_verify is not None else DEFAULT_SSL_VERIFY
),
ssl_cert_file=entry.ssl_cert_file,
cache_enabled=self.defaults.cache_enabled,
cache_dir=cache_dir,
# Note: api_base and api_key_path are not propagated yet - requires LLMConfig extension
)
return config.model_copy(update={"parameters": merged_params})
class JudgePanelConfig(BaseModel):
"""Judge panel configuration for multi-LLM evaluation.
References models from LLM pool by model ID (the key in llm_pool.models).
Each judge ID must correspond to a key in the llm_pool.models dictionary.
"""
model_config = ConfigDict(extra="forbid")
judges: list[str] = Field(
...,
min_length=1,
description="List of model IDs (keys from llm_pool.models). At least one required.",
)
enabled_metrics: Optional[list[str]] = Field(
default=None,
description=(
"Metrics that should use the judge panel. "
"If None, all metrics use the panel. "
"If empty list, no metrics use the panel."
),
)
aggregation_strategy: str = Field(
default="max",
description=(
"Strategy for aggregating scores: 'max', 'average', or "
"'majority_vote' (average reported; PASS if a strict majority vote)."
),
)
@field_validator("enabled_metrics")
@classmethod
def validate_enabled_metrics(cls, v: Optional[list[str]]) -> Optional[list[str]]:
"""Validate enabled_metrics format (framework:metric_name)."""
if v is not None:
for metric in v:
if not metric or ":" not in metric:
raise ValueError(
f'Metric "{metric}" must be in format "framework:metric_name"'
)
parts = metric.split(":", 1)
if len(parts) != 2 or not parts[0].strip() or not parts[1].strip():
raise ValueError(
f'Metric "{metric}" must be in format "framework:metric_name"'
)
return v
@field_validator("aggregation_strategy")
@classmethod
def validate_aggregation_strategy(cls, v: str) -> str:
"""Validate aggregation_strategy is a supported value."""
allowed = ["max", "average", "majority_vote"]
if v not in allowed:
raise ValueError(
f"Unsupported aggregation_strategy '{v}'. Allowed: {allowed}"
)
return v
class GEvalRubricConfig(BaseModel):
"""Single rubric entry: score range 0-10 and expected outcome text."""
model_config = ConfigDict(extra="forbid")
score_range: tuple[int, int] = Field(
...,
description="[min, max] score range (0-10); non-overlapping",
)
expected_outcome: str = Field(
...,
min_length=1,
description="Expected outcome for this score range",
)
@field_validator("score_range")
@classmethod
def validate_score_range(cls, v: tuple[int, int]) -> tuple[int, int]:
"""Ensure score_range is [min, max] with 0 <= min <= max <= 10."""
if not isinstance(v, (list, tuple)) or len(v) != 2:
raise ValueError("score_range must be [min, max] with two integers")
low, high = int(v[0]), int(v[1])
if low > high:
raise ValueError(f"score_range min must be <= max, got [{low}, {high}]")
if not (0 <= low <= 10 and 0 <= high <= 10):
raise ValueError(
f"score_range values must be between 0 and 10, got [{low}, {high}]"
)
return (low, high)
class GEvalConfig(BaseModel):
"""Validated GEval metric configuration (criteria required; rest optional)."""
model_config = ConfigDict(extra="forbid")
criteria: str = Field(..., min_length=1, description="Required evaluation criteria")
evaluation_params: list[str] = Field(
default_factory=list,
description="Field names to include (e.g. query, response, expected_response)",
)
evaluation_steps: list[str] | None = Field(
default=None,
description="Optional step-by-step evaluation instructions",
)
rubrics: list[GEvalRubricConfig] | None = Field(
default=None,
description="Optional score ranges (0-10) with expected_outcome",
)
threshold: float = Field(
default=0.5,
ge=0.0,
le=1.0,
description="Minimum score threshold for pass/fail",
)
@model_validator(mode="after")
def validate_rubrics_non_overlapping(self) -> "GEvalConfig":
"""Ensure rubric score ranges do not overlap."""
rubs: list[GEvalRubricConfig] = self.rubrics if self.rubrics else []
if len(rubs) <= 1:
return self
ranges = [r.score_range for r in rubs]
for i, (a, b) in enumerate(ranges):
for j, (c, d) in enumerate(ranges):
if i >= j:
continue
# Overlap if not (b < c or d < a)
if not (b < c or d < a):
raise ConfigurationError(
f"Rubric score ranges must not overlap: "
f"[{a}, {b}] and [{c}, {d}] overlap"
)
return self
@classmethod
def from_metadata(cls, raw: dict[str, Any]) -> "GEvalConfig":
"""Build GEvalConfig from raw metadata dict.
Args:
raw: Metadata dict with at least "criteria" (required). May include
evaluation_params, evaluation_steps, rubrics, threshold.
Returns:
Validated GEvalConfig instance.
Raises:
ValueError: If raw is not a dict or criteria is missing/empty
(only these pre-model_validate checks raise bare ValueError).
ValidationError: If rubric or config fields fail Pydantic validation:
wrong types (e.g. score_range, expected_outcome), invalid structure.
ConfigurationError: If rubric score ranges overlap (model validator
raises ConfigurationError directly, bypassing Pydantic wrapping).
"""
if not isinstance(raw, dict):
raise ValueError("GEval config must be a dict")
criteria = raw.get("criteria")
if not criteria or not isinstance(criteria, str) or not criteria.strip():
raise ValueError("GEval requires non-empty 'criteria' in configuration")
data: dict[str, Any] = {
"criteria": criteria.strip(),
"evaluation_params": raw.get("evaluation_params") or [],
"evaluation_steps": raw.get("evaluation_steps"),
"threshold": raw.get("threshold", 0.5),
}
raw_rubrics = raw.get("rubrics")
if raw_rubrics and isinstance(raw_rubrics, list):
data["rubrics"] = [
GEvalRubricConfig.model_validate(item) for item in raw_rubrics
]
else:
data["rubrics"] = None
return cls.model_validate(data)
class QualityScoreConfig(BaseModel):
"""Quality score configuration."""
model_config = ConfigDict(extra="forbid")
metrics: list[str] = Field(
default_factory=list,
description="List of metric identifiers to use for quality score computation",
)
default: bool = Field(
default=False,
description="If true, set default: true for all metrics in the list",
)
@field_validator("metrics")
@classmethod
def validate_metrics(cls, v: list[str]) -> list[str]:
"""Ensure metrics list is not empty and contains no duplicates."""
if len(v) == 0:
raise ValueError(
"Quality score metrics list cannot be empty. "
"Either specify at least one metric or "
"remove the quality_score section from configuration."
)
if len(v) != len(set(v)):
duplicates = [m for m in v if v.count(m) > 1]
raise ValueError(
f"Quality score metrics contains duplicates: {set(duplicates)}. "
"Each metric should appear only once."
)
return v
class SystemConfig(BaseModel):
"""System configuration using individual config models."""
model_config = ConfigDict(extra="forbid")
# Individual configuration models
core: CoreConfig = Field(
default_factory=CoreConfig, description="Core eval configuration"
)
llm: LLMConfig = Field(default_factory=LLMConfig, description="LLM configuration")
# LLM Pool - shared pool of LLM configurations
llm_pool: Optional[LLMPoolConfig] = Field(
default=None,
description=(
"Pool of LLM configurations. Define models once, "
"reference by ID in judge_panel or other components."
),
)
# Judge Panel - references models from llm_pool
judge_panel: Optional[JudgePanelConfig] = Field(
default=None,
description=(
"Optional judge panel configuration. "
"References models from 'llm_pool' by ID. "
"If not provided, the single 'llm' configuration is used."
),
)
embedding: EmbeddingConfig = Field(
default_factory=EmbeddingConfig, description="Embeddings configuration"
)
api: APIConfig = Field(default_factory=APIConfig, description="API configuration")
storage: list[StorageBackendConfig] = Field(
default_factory=list,
description="Storage backends for evaluation results (file and/or database)",
)
logging: LoggingConfig = Field(
default_factory=LoggingConfig, description="Logging configuration"
)
visualization: VisualizationConfig = Field(
default_factory=VisualizationConfig, description="Visualization configuration"
)
# Quality score configuration
quality_score: Optional[QualityScoreConfig] = Field(
default=None, description="Quality score configuration"
)
# Default metrics metadata from system config
default_turn_metrics_metadata: dict[str, dict[str, Any]] = Field(
default_factory=dict, description="Default turn metrics metadata"
)
default_conversation_metrics_metadata: dict[str, dict[str, Any]] = Field(
default_factory=dict, description="Default conversation metrics metadata"
)
@field_validator(
"default_turn_metrics_metadata", "default_conversation_metrics_metadata"
)
@classmethod
def validate_default_metrics_metadata_geval(
cls, v: dict[str, dict[str, Any]]
) -> dict[str, dict[str, Any]]:
"""Validate GEval entries at load; keep storing as dict (result discarded).
We call GEvalConfig.from_metadata(meta) only for its validation side
effect (fail fast on invalid system config). The returned config is
discarded; the raw dict is stored. At evaluation time the manager
may merge overrides with this dict, and the handler re-validates
via from_metadata on the merged result.
Raises:
ConfigurationError: When a geval:* entry has invalid config (e.g.
missing criteria, invalid rubric structure, overlapping rubrics).
Re-raised from ValueError, ValidationError, or ConfigurationError
for a consistent config-failure exception type with metric context.
"""
if not v:
return v
for metric_id, meta in v.items():
if metric_id.startswith("geval:") and isinstance(meta, dict):
try:
GEvalConfig.from_metadata(meta)
except (ValueError, ValidationError, ConfigurationError) as e:
raise ConfigurationError(
f"Invalid GEval config for '{metric_id}': {e!s}"
) from e
return v
@model_validator(mode="after")
def validate_quality_score_metrics(self) -> "SystemConfig":
"""Validate quality_score metrics exist in metrics_metadata.
Raises:
ConfigurationError: When quality_score contains metrics not defined
in turn or conversation level metrics_metadata.
"""
if not self.quality_score:
return self
# Combine all available metrics from both turn and conversation level metadata
all_metrics = set(self.default_turn_metrics_metadata.keys()) | set(
self.default_conversation_metrics_metadata.keys()
)
# Check for invalid metrics
invalid = [m for m in self.quality_score.metrics if m not in all_metrics]
if invalid:
raise ConfigurationError(
f"Invalid quality_score metrics: {invalid}. "
"Must be defined in default_turn_metrics_metadata or "
"default_conversation_metrics_metadata."
)
return self
@property
def turn_level_metric_names(self) -> set[str]:
"""Return turn-level metric names derived from metadata keys."""
return set(self.default_turn_metrics_metadata.keys())
@property
def conversation_level_metric_names(self) -> set[str]:
"""Return conversation-level metric names derived from metadata keys."""
return set(self.default_conversation_metrics_metadata.keys())
def get_judge_configs(self) -> list[tuple[str, LLMConfig]]:
"""Get resolved LLMConfig for all judges with their pool keys.
Returns:
List of (pool_key, LLMConfig) tuples for each judge.
If judge_panel is configured, resolves from llm_pool.
Otherwise, returns single entry with "primary" as key.
"""
if not self.judge_panel:
return [("primary", self.llm)]
if not self.llm_pool:
raise ConfigurationError(
"judge_panel is configured but 'llm_pool' is not defined. "
"Please define the llm_pool section with models."
)
configs = []
for idx, judge_id in enumerate(self.judge_panel.judges):
cache_suffix = f"judge_{idx}"
config = self.llm_pool.resolve_llm_config(
judge_id, cache_suffix=cache_suffix
)
configs.append((judge_id, config))
return configs
def get_llm_config(
self, model_id: str, cache_suffix: Optional[str] = None
) -> LLMConfig:
"""Get resolved LLMConfig for a specific model from the pool.
Args:
model_id: Model identifier (key in llm_pool.models)