Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/ai-providers/server-ai-openai/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ async def main():
ai_config = ai_client.config(
"my-ai-config-key",
context,
default_value
default
)

# Create an OpenAI provider from the config
Expand Down
4 changes: 2 additions & 2 deletions packages/sdk/server-ai/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ from ldai.providers.types import LDAIMetrics, TokenUsage
from ldai_langchain import LangChainProvider

async def main():
ai_config = ai_client.completion_config(ai_config_key, context, default_value)
ai_config = ai_client.completion_config(ai_config_key, context, default)

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

async def main():
ai_config = ai_client.completion_config(ai_config_key, context, default_value)
ai_config = ai_client.completion_config(ai_config_key, context, default)

# Define custom metrics mapping for your provider
def map_custom_provider_metrics(response):
Expand Down
79 changes: 45 additions & 34 deletions packages/sdk/server-ai/src/ldai/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ def _completion_config(
self,
key: str,
context: Context,
default_value: AICompletionConfigDefault,
default: AICompletionConfigDefault,
variables: Optional[Dict[str, Any]] = None,
) -> AICompletionConfig:
model, provider, messages, instructions, tracker, enabled, judge_configuration, _ = self.__evaluate(
key, context, default_value.to_dict(), variables
key, context, default.to_dict(), variables
)

config = AICompletionConfig(
Expand All @@ -72,27 +72,30 @@ def completion_config(
self,
key: str,
context: Context,
default_value: AICompletionConfigDefault,
default: Optional[AICompletionConfigDefault] = None,
variables: Optional[Dict[str, Any]] = None,
) -> AICompletionConfig:
"""
Get the value of a completion configuration.

:param key: The key of the completion configuration.
:param context: The context to evaluate the completion configuration in.
:param default_value: The default value of the completion configuration.
:param default: The default value of the completion configuration. When not provided,
a disabled config is used as the fallback.
:param variables: Additional variables for the completion configuration.
:return: The completion configuration with a tracker used for gathering metrics.
"""
self._client.track(_TRACK_USAGE_COMPLETION_CONFIG, context, key, 1)

return self._completion_config(key, context, default_value, variables)
return self._completion_config(
key, context, default or AICompletionConfigDefault.disabled(), variables
)

def config(
self,
key: str,
context: Context,
default_value: AICompletionConfigDefault,
default: Optional[AICompletionConfigDefault] = None,
variables: Optional[Dict[str, Any]] = None,
) -> AICompletionConfig:
"""
Expand All @@ -102,21 +105,21 @@ def config(

:param key: The key of the model configuration.
:param context: The context to evaluate the model configuration in.
:param default_value: The default value of the model configuration.
:param default: The default value of the model configuration.
:param variables: Additional variables for the model configuration.
:return: The value of the model configuration along with a tracker used for gathering metrics.
"""
return self.completion_config(key, context, default_value, variables)
return self.completion_config(key, context, default, variables)

def _judge_config(
self,
key: str,
context: Context,
default_value: AIJudgeConfigDefault,
default: AIJudgeConfigDefault,
variables: Optional[Dict[str, Any]] = None,
) -> AIJudgeConfig:
model, provider, messages, instructions, tracker, enabled, judge_configuration, variation = self.__evaluate(
key, context, default_value.to_dict(), variables
key, context, default.to_dict(), variables
)

def _extract_evaluation_metric_key(variation: Dict[str, Any]) -> Optional[str]:
Expand Down Expand Up @@ -152,27 +155,30 @@ def judge_config(
self,
key: str,
context: Context,
default_value: AIJudgeConfigDefault,
default: Optional[AIJudgeConfigDefault] = None,
variables: Optional[Dict[str, Any]] = None,
) -> AIJudgeConfig:
"""
Get the value of a judge configuration.

:param key: The key of the judge configuration.
:param context: The context to evaluate the judge configuration in.
:param default_value: The default value of the judge configuration.
:param default: The default value of the judge configuration. When not provided,
a disabled config is used as the fallback.
:param variables: Additional variables for the judge configuration.
:return: The judge configuration with a tracker used for gathering metrics.
"""
self._client.track(_TRACK_USAGE_JUDGE_CONFIG, context, key, 1)

return self._judge_config(key, context, default_value, variables)
return self._judge_config(
key, context, default or AIJudgeConfigDefault.disabled(), variables
)

async def create_judge(
self,
key: str,
context: Context,
default_value: AIJudgeConfigDefault,
default: Optional[AIJudgeConfigDefault] = None,
variables: Optional[Dict[str, Any]] = None,
default_ai_provider: Optional[str] = None,
) -> Optional[Judge]:
Expand All @@ -181,7 +187,7 @@ async def create_judge(

:param key: The key identifying the AI judge configuration to use
:param context: Standard Context used when evaluating flags
:param default_value: A default value representing a standard AI config result
:param default: A default value representing a standard AI config result
:param variables: Dictionary of values for instruction interpolation.
The variables `message_history` and `response_to_evaluate` are reserved for the judge and will be ignored.
:param default_ai_provider: Optional default AI provider to use.
Expand Down Expand Up @@ -222,7 +228,9 @@ async def create_judge(
extended_variables['message_history'] = '{{message_history}}'
extended_variables['response_to_evaluate'] = '{{response_to_evaluate}}'

judge_config = self._judge_config(key, context, default_value, extended_variables)
judge_config = self._judge_config(
key, context, default or AIJudgeConfigDefault.disabled(), extended_variables
)

if not judge_config.enabled or not judge_config.tracker:
return None
Expand Down Expand Up @@ -284,7 +292,7 @@ async def create_chat(
self,
key: str,
context: Context,
default_value: AICompletionConfigDefault,
default: AICompletionConfigDefault,
Comment thread
cursor[bot] marked this conversation as resolved.
Outdated
variables: Optional[Dict[str, Any]] = None,
default_ai_provider: Optional[str] = None,
) -> Optional[Chat]:
Expand All @@ -293,7 +301,7 @@ async def create_chat(

:param key: The key identifying the AI completion configuration to use
:param context: Standard Context used when evaluating flags
:param default_value: A default value representing a standard AI config result
:param default: A default value representing a standard AI config result
:param variables: Dictionary of values for instruction interpolation
:param default_ai_provider: Optional default AI provider to use
:return: Chat instance or None if disabled/unsupported
Expand Down Expand Up @@ -322,7 +330,7 @@ async def create_chat(
"""
self._client.track(_TRACK_USAGE_CREATE_CHAT, context, key, 1)
log.debug(f"Creating chat for key: {key}")
config = self._completion_config(key, context, default_value, variables)
config = self._completion_config(key, context, default, variables)

if not config.enabled or not config.tracker:
return None
Expand All @@ -346,7 +354,7 @@ def agent_config(
self,
key: str,
context: Context,
default_value: AIAgentConfigDefault,
default: Optional[AIAgentConfigDefault] = None,
variables: Optional[Dict[str, Any]] = None,
) -> AIAgentConfig:
"""
Expand Down Expand Up @@ -374,7 +382,8 @@ def agent_config(

:param key: The agent configuration key.
:param context: The context to evaluate the agent configuration in.
:param default_value: Default agent values.
:param default: Default agent values. When not provided, a disabled config is used
as the fallback.
:param variables: Variables for interpolation.
:return: Configured AIAgentConfig instance.
"""
Expand All @@ -385,7 +394,9 @@ def agent_config(
1
)

return self.__evaluate_agent(key, context, default_value, variables)
return self.__evaluate_agent(
key, context, default or AIAgentConfigDefault.disabled(), variables
)

def agent(
self,
Expand All @@ -401,7 +412,7 @@ def agent(
:param context: The context to evaluate the agent configuration in.
:return: Configured AIAgentConfig instance.
"""
return self.agent_config(config.key, context, config.default_value, config.variables)
return self.agent_config(config.key, context, config.default, config.variables)

def agent_configs(
self,
Expand All @@ -420,15 +431,15 @@ def agent_configs(
agents = client.agent_configs([
AIAgentConfigRequest(
key='research_agent',
default_value=AIAgentConfigDefault(
default=AIAgentConfigDefault(
enabled=True,
instructions='You are a research assistant.'
),
variables={'topic': 'climate change'}
),
AIAgentConfigRequest(
key='writing_agent',
default_value=AIAgentConfigDefault(
default=AIAgentConfigDefault(
enabled=True,
instructions='You are a writing assistant.'
),
Expand Down Expand Up @@ -457,7 +468,7 @@ def agent_configs(
agent = self.__evaluate_agent(
config.key,
context,
config.default_value,
config.default or AIAgentConfigDefault.disabled(),
config.variables
)
result[config.key] = agent
Expand Down Expand Up @@ -682,33 +693,33 @@ def __evaluate_agent(
self,
key: str,
context: Context,
default_value: AIAgentConfigDefault,
default: AIAgentConfigDefault,
variables: Optional[Dict[str, Any]] = None,
) -> AIAgentConfig:
"""
Internal method to evaluate an agent configuration.

:param key: The agent configuration key.
:param context: The evaluation context.
:param default_value: Default agent values.
:param default: Default agent values.
:param variables: Variables for interpolation.
:return: Configured AIAgentConfig instance.
"""
model, provider, messages, instructions, tracker, enabled, judge_configuration, _ = self.__evaluate(
key, context, default_value.to_dict(), variables
key, context, default.to_dict(), variables
)

# For agents, prioritize instructions over messages
final_instructions = instructions if instructions is not None else default_value.instructions
final_instructions = instructions if instructions is not None else default.instructions

return AIAgentConfig(
key=key,
enabled=bool(enabled) if enabled is not None else (default_value.enabled or False),
model=model or default_value.model,
provider=provider or default_value.provider,
enabled=bool(enabled) if enabled is not None else (default.enabled or False),
model=model or default.model,
provider=provider or default.provider,
instructions=final_instructions,
tracker=tracker,
judge_configuration=judge_configuration or default_value.judge_configuration,
judge_configuration=judge_configuration or default.judge_configuration,
)

def __interpolate_template(self, template: str, variables: Dict[str, Any]) -> str:
Expand Down
10 changes: 9 additions & 1 deletion packages/sdk/server-ai/src/ldai/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,14 @@ class AIConfigDefault:
model: Optional[ModelConfig] = None
provider: Optional[ProviderConfig] = None

@classmethod
def disabled(cls):
"""
Returns a new disabled config default with enabled set to false.
When called on a subclass, returns an instance of that subclass.
"""
return cls(enabled=False)

def _base_to_dict(self) -> Dict[str, Any]:
"""
Render the base config fields as a dictionary object.
Expand Down Expand Up @@ -334,7 +342,7 @@ class AIAgentConfigRequest:
Combines agent key with its specific default configuration and variables.
"""
key: str
default_value: AIAgentConfigDefault
default: Optional[AIAgentConfigDefault] = None
variables: Optional[Dict[str, Any]] = None


Expand Down
Loading