@@ -98,14 +98,35 @@ def judge_config(
9898 key , context , default_value .to_dict (), variables
9999 )
100100
101- # Extract evaluation_metric_keys from the variation
102101 variation = self ._client .variation (key , context , default_value .to_dict ())
103- evaluation_metric_keys = variation .get ('evaluationMetricKeys' , default_value .evaluation_metric_keys or [])
102+ def _extract_evaluation_metric_key (variation : Dict [str , Any ], default_value : AIJudgeConfigDefault ) -> Optional [str ]:
103+ """
104+ Extract evaluation_metric_key with backward compatibility.
105+
106+ Priority: 1) evaluationMetricKey from variation, 2) evaluation_metric_key from default,
107+ 3) first from evaluationMetricKeys in variation, 4) first from evaluation_metric_keys in default
108+ """
109+ if evaluation_metric_key := variation .get ('evaluationMetricKey' ):
110+ return evaluation_metric_key
111+
112+ if default_value .evaluation_metric_key :
113+ return default_value .evaluation_metric_key
114+
115+ variation_keys = variation .get ('evaluationMetricKeys' )
116+ if isinstance (variation_keys , list ) and variation_keys :
117+ return variation_keys [0 ]
118+
119+ if default_value .evaluation_metric_keys :
120+ return default_value .evaluation_metric_keys [0 ]
121+
122+ return None
123+
124+ evaluation_metric_key = _extract_evaluation_metric_key (variation , default_value )
104125
105126 config = AIJudgeConfig (
106127 key = key ,
107128 enabled = bool (enabled ),
108- evaluation_metric_keys = evaluation_metric_keys ,
129+ evaluation_metric_key = evaluation_metric_key ,
109130 model = model ,
110131 messages = messages ,
111132 provider = provider ,
@@ -142,7 +163,7 @@ async def create_judge(
142163 enabled=True,
143164 model=ModelConfig("gpt-4"),
144165 provider=ProviderConfig("openai"),
145- evaluation_metric_keys=[ '$ld:ai:judge:relevance'] ,
166+ evaluation_metric_key= '$ld:ai:judge:relevance',
146167 messages=[LDMessage(role='system', content='You are a relevance judge.')]
147168 ),
148169 variables={'metric': "relevance"}
@@ -158,33 +179,27 @@ async def create_judge(
158179 self ._client .track ('$ld:ai:judge:function:createJudge' , context , key , 1 )
159180
160181 try :
161- # Warn if reserved variables are provided
162182 if variables :
163183 if 'message_history' in variables :
164- # Note: Python doesn't have a logger on the client, but we could add one
165- pass # Would log warning if logger available
184+ pass
166185 if 'response_to_evaluate' in variables :
167- pass # Would log warning if logger available
186+ pass
168187
169- # Overwrite reserved variables to ensure they remain as placeholders for judge evaluation
170188 extended_variables = dict (variables ) if variables else {}
171189 extended_variables ['message_history' ] = '{{message_history}}'
172190 extended_variables ['response_to_evaluate' ] = '{{response_to_evaluate}}'
173191
174192 judge_config = self .judge_config (key , context , default_value , extended_variables )
175193
176194 if not judge_config .enabled or not judge_config .tracker :
177- # Would log info if logger available
178195 return None
179196
180- # Create AI provider for the judge
181197 provider = await AIProviderFactory .create (judge_config , default_ai_provider )
182198 if not provider :
183199 return None
184200
185201 return Judge (judge_config , judge_config .tracker , provider )
186202 except Exception as error :
187- # Would log error if logger available
188203 return None
189204
190205 async def _initialize_judges (
@@ -277,7 +292,6 @@ async def create_chat(
277292 config = self .completion_config (key , context , default_value , variables )
278293
279294 if not config .enabled or not config .tracker :
280- # Would log info if logger available
281295 return None
282296
283297 provider = await AIProviderFactory .create (config , default_ai_provider )
@@ -331,7 +345,6 @@ def agent_config(
331345 :param variables: Variables for interpolation.
332346 :return: Configured AIAgentConfig instance.
333347 """
334- # Track single agent usage
335348 self ._client .track (
336349 "$ld:ai:agent:function:single" ,
337350 context ,
@@ -397,7 +410,6 @@ def agent_configs(
397410 :param context: The context to evaluate the agent configurations in.
398411 :return: Dictionary mapping agent keys to their AIAgentConfig configurations.
399412 """
400- # Track multiple agents usage
401413 agent_count = len (agent_configs )
402414 self ._client .track (
403415 "$ld:ai:agent:function:multiple" ,
@@ -461,7 +473,6 @@ def __evaluate(
461473 all_variables .update (variables )
462474 all_variables ['ldctx' ] = context .to_dict ()
463475
464- # Extract messages
465476 messages = None
466477 if 'messages' in variation and isinstance (variation ['messages' ], list ) and all (
467478 isinstance (entry , dict ) for entry in variation ['messages' ]
@@ -476,18 +487,15 @@ def __evaluate(
476487 for entry in variation ['messages' ]
477488 ]
478489
479- # Extract instructions
480490 instructions = None
481491 if 'instructions' in variation and isinstance (variation ['instructions' ], str ):
482492 instructions = self .__interpolate_template (variation ['instructions' ], all_variables )
483493
484- # Extract provider config
485494 provider_config = None
486495 if 'provider' in variation and isinstance (variation ['provider' ], dict ):
487496 provider = variation ['provider' ]
488497 provider_config = ProviderConfig (provider .get ('name' , '' ))
489498
490- # Extract model config
491499 model = None
492500 if 'model' in variation and isinstance (variation ['model' ], dict ):
493501 parameters = variation ['model' ].get ('parameters' , None )
@@ -498,7 +506,6 @@ def __evaluate(
498506 custom = custom
499507 )
500508
501- # Create tracker
502509 tracker = LDAIConfigTracker (
503510 self ._client ,
504511 variation .get ('_ldMeta' , {}).get ('variationKey' , '' ),
@@ -511,7 +518,6 @@ def __evaluate(
511518
512519 enabled = variation .get ('_ldMeta' , {}).get ('enabled' , False )
513520
514- # Extract judge configuration
515521 judge_configuration = None
516522 if 'judgeConfiguration' in variation and isinstance (variation ['judgeConfiguration' ], dict ):
517523 judge_config = variation ['judgeConfiguration' ]
0 commit comments