Skip to content

Commit 39e09c6

Browse files
authored
fix: Make default optional with a disabled config default (#97)
fix!: Rename default_value args to default
1 parent ea0c109 commit 39e09c6

7 files changed

Lines changed: 201 additions & 88 deletions

File tree

packages/ai-providers/server-ai-openai/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ async def main():
3131
ai_config = ai_client.config(
3232
"my-ai-config-key",
3333
context,
34-
default_value
34+
default
3535
)
3636

3737
# Create an OpenAI provider from the config

packages/sdk/server-ai/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ from ldai.providers.types import LDAIMetrics, TokenUsage
145145
from ldai_langchain import LangChainProvider
146146

147147
async def main():
148-
ai_config = ai_client.completion_config(ai_config_key, context, default_value)
148+
ai_config = ai_client.completion_config(ai_config_key, context, default)
149149

150150
# Create LangChain model from configuration
151151
llm = await LangChainProvider.create_langchain_model(ai_config)
@@ -169,7 +169,7 @@ from ldai import LDAIClient, AICompletionConfigDefault, ModelConfig
169169
from ldai.providers.types import LDAIMetrics, TokenUsage
170170

171171
async def main():
172-
ai_config = ai_client.completion_config(ai_config_key, context, default_value)
172+
ai_config = ai_client.completion_config(ai_config_key, context, default)
173173

174174
# Define custom metrics mapping for your provider
175175
def map_custom_provider_metrics(response):

packages/sdk/server-ai/src/ldai/client.py

Lines changed: 46 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ def _completion_config(
4949
self,
5050
key: str,
5151
context: Context,
52-
default_value: AICompletionConfigDefault,
52+
default: AICompletionConfigDefault,
5353
variables: Optional[Dict[str, Any]] = None,
5454
) -> AICompletionConfig:
5555
model, provider, messages, instructions, tracker, enabled, judge_configuration, _ = self.__evaluate(
56-
key, context, default_value.to_dict(), variables
56+
key, context, default.to_dict(), variables
5757
)
5858

5959
config = AICompletionConfig(
@@ -72,27 +72,30 @@ def completion_config(
7272
self,
7373
key: str,
7474
context: Context,
75-
default_value: AICompletionConfigDefault,
75+
default: Optional[AICompletionConfigDefault] = None,
7676
variables: Optional[Dict[str, Any]] = None,
7777
) -> AICompletionConfig:
7878
"""
7979
Get the value of a completion configuration.
8080
8181
:param key: The key of the completion configuration.
8282
:param context: The context to evaluate the completion configuration in.
83-
:param default_value: The default value of the completion configuration.
83+
:param default: The default value of the completion configuration. When not provided,
84+
a disabled config is used as the fallback.
8485
:param variables: Additional variables for the completion configuration.
8586
:return: The completion configuration with a tracker used for gathering metrics.
8687
"""
8788
self._client.track(_TRACK_USAGE_COMPLETION_CONFIG, context, key, 1)
8889

89-
return self._completion_config(key, context, default_value, variables)
90+
return self._completion_config(
91+
key, context, default or AICompletionConfigDefault.disabled(), variables
92+
)
9093

9194
def config(
9295
self,
9396
key: str,
9497
context: Context,
95-
default_value: AICompletionConfigDefault,
98+
default: Optional[AICompletionConfigDefault] = None,
9699
variables: Optional[Dict[str, Any]] = None,
97100
) -> AICompletionConfig:
98101
"""
@@ -102,21 +105,21 @@ def config(
102105
103106
:param key: The key of the model configuration.
104107
:param context: The context to evaluate the model configuration in.
105-
:param default_value: The default value of the model configuration.
108+
:param default: The default value of the model configuration.
106109
:param variables: Additional variables for the model configuration.
107110
:return: The value of the model configuration along with a tracker used for gathering metrics.
108111
"""
109-
return self.completion_config(key, context, default_value, variables)
112+
return self.completion_config(key, context, default, variables)
110113

111114
def _judge_config(
112115
self,
113116
key: str,
114117
context: Context,
115-
default_value: AIJudgeConfigDefault,
118+
default: AIJudgeConfigDefault,
116119
variables: Optional[Dict[str, Any]] = None,
117120
) -> AIJudgeConfig:
118121
model, provider, messages, instructions, tracker, enabled, judge_configuration, variation = self.__evaluate(
119-
key, context, default_value.to_dict(), variables
122+
key, context, default.to_dict(), variables
120123
)
121124

122125
def _extract_evaluation_metric_key(variation: Dict[str, Any]) -> Optional[str]:
@@ -152,27 +155,30 @@ def judge_config(
152155
self,
153156
key: str,
154157
context: Context,
155-
default_value: AIJudgeConfigDefault,
158+
default: Optional[AIJudgeConfigDefault] = None,
156159
variables: Optional[Dict[str, Any]] = None,
157160
) -> AIJudgeConfig:
158161
"""
159162
Get the value of a judge configuration.
160163
161164
:param key: The key of the judge configuration.
162165
:param context: The context to evaluate the judge configuration in.
163-
:param default_value: The default value of the judge configuration.
166+
:param default: The default value of the judge configuration. When not provided,
167+
a disabled config is used as the fallback.
164168
:param variables: Additional variables for the judge configuration.
165169
:return: The judge configuration with a tracker used for gathering metrics.
166170
"""
167171
self._client.track(_TRACK_USAGE_JUDGE_CONFIG, context, key, 1)
168172

169-
return self._judge_config(key, context, default_value, variables)
173+
return self._judge_config(
174+
key, context, default or AIJudgeConfigDefault.disabled(), variables
175+
)
170176

171177
async def create_judge(
172178
self,
173179
key: str,
174180
context: Context,
175-
default_value: AIJudgeConfigDefault,
181+
default: Optional[AIJudgeConfigDefault] = None,
176182
variables: Optional[Dict[str, Any]] = None,
177183
default_ai_provider: Optional[str] = None,
178184
) -> Optional[Judge]:
@@ -181,7 +187,7 @@ async def create_judge(
181187
182188
:param key: The key identifying the AI judge configuration to use
183189
:param context: Standard Context used when evaluating flags
184-
:param default_value: A default value representing a standard AI config result
190+
:param default: A default value representing a standard AI config result
185191
:param variables: Dictionary of values for instruction interpolation.
186192
The variables `message_history` and `response_to_evaluate` are reserved for the judge and will be ignored.
187193
:param default_ai_provider: Optional default AI provider to use.
@@ -222,7 +228,9 @@ async def create_judge(
222228
extended_variables['message_history'] = '{{message_history}}'
223229
extended_variables['response_to_evaluate'] = '{{response_to_evaluate}}'
224230

225-
judge_config = self._judge_config(key, context, default_value, extended_variables)
231+
judge_config = self._judge_config(
232+
key, context, default or AIJudgeConfigDefault.disabled(), extended_variables
233+
)
226234

227235
if not judge_config.enabled or not judge_config.tracker:
228236
return None
@@ -284,7 +292,7 @@ async def create_chat(
284292
self,
285293
key: str,
286294
context: Context,
287-
default_value: AICompletionConfigDefault,
295+
default: Optional[AICompletionConfigDefault] = None,
288296
variables: Optional[Dict[str, Any]] = None,
289297
default_ai_provider: Optional[str] = None,
290298
) -> Optional[Chat]:
@@ -293,7 +301,8 @@ async def create_chat(
293301
294302
:param key: The key identifying the AI completion configuration to use
295303
:param context: Standard Context used when evaluating flags
296-
:param default_value: A default value representing a standard AI config result
304+
:param default: A default value representing a standard AI config result. When not provided,
305+
a disabled config is used as the fallback.
297306
:param variables: Dictionary of values for instruction interpolation
298307
:param default_ai_provider: Optional default AI provider to use
299308
:return: Chat instance or None if disabled/unsupported
@@ -322,7 +331,7 @@ async def create_chat(
322331
"""
323332
self._client.track(_TRACK_USAGE_CREATE_CHAT, context, key, 1)
324333
log.debug(f"Creating chat for key: {key}")
325-
config = self._completion_config(key, context, default_value, variables)
334+
config = self._completion_config(key, context, default or AICompletionConfigDefault.disabled(), variables)
326335

327336
if not config.enabled or not config.tracker:
328337
return None
@@ -346,7 +355,7 @@ def agent_config(
346355
self,
347356
key: str,
348357
context: Context,
349-
default_value: AIAgentConfigDefault,
358+
default: Optional[AIAgentConfigDefault] = None,
350359
variables: Optional[Dict[str, Any]] = None,
351360
) -> AIAgentConfig:
352361
"""
@@ -374,7 +383,8 @@ def agent_config(
374383
375384
:param key: The agent configuration key.
376385
:param context: The context to evaluate the agent configuration in.
377-
:param default_value: Default agent values.
386+
:param default: Default agent values. When not provided, a disabled config is used
387+
as the fallback.
378388
:param variables: Variables for interpolation.
379389
:return: Configured AIAgentConfig instance.
380390
"""
@@ -385,7 +395,9 @@ def agent_config(
385395
1
386396
)
387397

388-
return self.__evaluate_agent(key, context, default_value, variables)
398+
return self.__evaluate_agent(
399+
key, context, default or AIAgentConfigDefault.disabled(), variables
400+
)
389401

390402
def agent(
391403
self,
@@ -401,7 +413,7 @@ def agent(
401413
:param context: The context to evaluate the agent configuration in.
402414
:return: Configured AIAgentConfig instance.
403415
"""
404-
return self.agent_config(config.key, context, config.default_value, config.variables)
416+
return self.agent_config(config.key, context, config.default, config.variables)
405417

406418
def agent_configs(
407419
self,
@@ -420,15 +432,15 @@ def agent_configs(
420432
agents = client.agent_configs([
421433
AIAgentConfigRequest(
422434
key='research_agent',
423-
default_value=AIAgentConfigDefault(
435+
default=AIAgentConfigDefault(
424436
enabled=True,
425437
instructions='You are a research assistant.'
426438
),
427439
variables={'topic': 'climate change'}
428440
),
429441
AIAgentConfigRequest(
430442
key='writing_agent',
431-
default_value=AIAgentConfigDefault(
443+
default=AIAgentConfigDefault(
432444
enabled=True,
433445
instructions='You are a writing assistant.'
434446
),
@@ -457,7 +469,7 @@ def agent_configs(
457469
agent = self.__evaluate_agent(
458470
config.key,
459471
context,
460-
config.default_value,
472+
config.default or AIAgentConfigDefault.disabled(),
461473
config.variables
462474
)
463475
result[config.key] = agent
@@ -682,33 +694,33 @@ def __evaluate_agent(
682694
self,
683695
key: str,
684696
context: Context,
685-
default_value: AIAgentConfigDefault,
697+
default: AIAgentConfigDefault,
686698
variables: Optional[Dict[str, Any]] = None,
687699
) -> AIAgentConfig:
688700
"""
689701
Internal method to evaluate an agent configuration.
690702
691703
:param key: The agent configuration key.
692704
:param context: The evaluation context.
693-
:param default_value: Default agent values.
705+
:param default: Default agent values.
694706
:param variables: Variables for interpolation.
695707
:return: Configured AIAgentConfig instance.
696708
"""
697709
model, provider, messages, instructions, tracker, enabled, judge_configuration, _ = self.__evaluate(
698-
key, context, default_value.to_dict(), variables
710+
key, context, default.to_dict(), variables
699711
)
700712

701713
# For agents, prioritize instructions over messages
702-
final_instructions = instructions if instructions is not None else default_value.instructions
714+
final_instructions = instructions if instructions is not None else default.instructions
703715

704716
return AIAgentConfig(
705717
key=key,
706-
enabled=bool(enabled) if enabled is not None else (default_value.enabled or False),
707-
model=model or default_value.model,
708-
provider=provider or default_value.provider,
718+
enabled=bool(enabled) if enabled is not None else (default.enabled or False),
719+
model=model or default.model,
720+
provider=provider or default.provider,
709721
instructions=final_instructions,
710722
tracker=tracker,
711-
judge_configuration=judge_configuration or default_value.judge_configuration,
723+
judge_configuration=judge_configuration or default.judge_configuration,
712724
)
713725

714726
def __interpolate_template(self, template: str, variables: Dict[str, Any]) -> str:

packages/sdk/server-ai/src/ldai/models.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,14 @@ class AIConfigDefault:
152152
model: Optional[ModelConfig] = None
153153
provider: Optional[ProviderConfig] = None
154154

155+
@classmethod
156+
def disabled(cls):
157+
"""
158+
Returns a new disabled config default with enabled set to false.
159+
When called on a subclass, returns an instance of that subclass.
160+
"""
161+
return cls(enabled=False)
162+
155163
def _base_to_dict(self) -> Dict[str, Any]:
156164
"""
157165
Render the base config fields as a dictionary object.
@@ -334,7 +342,7 @@ class AIAgentConfigRequest:
334342
Combines agent key with its specific default configuration and variables.
335343
"""
336344
key: str
337-
default_value: AIAgentConfigDefault
345+
default: Optional[AIAgentConfigDefault] = None
338346
variables: Optional[Dict[str, Any]] = None
339347

340348

0 commit comments

Comments
 (0)