55from ldclient .client import LDClient
66
77from ldai .models import (
8- AIConfig ,
9- LDAIAgent ,
10- LDAIAgentConfig ,
11- LDAIAgentDefaults ,
12- LDAIAgents ,
8+ AIAgentConfig ,
9+ AIAgentConfigDefault ,
10+ AIAgentConfigRequest ,
11+ AIAgents ,
12+ AICompletionConfig ,
13+ AICompletionConfigDefault ,
14+ AIJudgeConfig ,
15+ AIJudgeConfigDefault ,
16+ Judge ,
17+ JudgeConfiguration ,
1318 LDMessage ,
1419 ModelConfig ,
1520 ProviderConfig ,
@@ -23,40 +28,103 @@ class LDAIClient:
2328 def __init__ (self , client : LDClient ):
2429 self ._client = client
2530
31+ def completion_config (
32+ self ,
33+ key : str ,
34+ context : Context ,
35+ default_value : AICompletionConfigDefault ,
36+ variables : Optional [Dict [str , Any ]] = None ,
37+ ) -> AICompletionConfig :
38+ """
39+ Get the value of a completion configuration.
40+
41+ :param key: The key of the completion configuration.
42+ :param context: The context to evaluate the completion configuration in.
43+ :param default_value: The default value of the completion configuration.
44+ :param variables: Additional variables for the completion configuration.
45+ :return: The completion configuration with a tracker used for gathering metrics.
46+ """
47+ self ._client .track ('$ld:ai:config:function:single' , context , key , 1 )
48+
49+ model , provider , messages , instructions , tracker , enabled , judge_configuration = self .__evaluate (
50+ key , context , default_value .to_dict (), variables
51+ )
52+
53+ config = AICompletionConfig (
54+ enabled = bool (enabled ),
55+ model = model ,
56+ messages = messages ,
57+ provider = provider ,
58+ tracker = tracker ,
59+ judge_configuration = judge_configuration ,
60+ )
61+
62+ return config
63+
2664 def config (
2765 self ,
2866 key : str ,
2967 context : Context ,
30- default_value : AIConfig ,
68+ default_value : AICompletionConfigDefault ,
3169 variables : Optional [Dict [str , Any ]] = None ,
32- ) -> Tuple [ AIConfig , LDAIConfigTracker ] :
70+ ) -> AICompletionConfig :
3371 """
3472 Get the value of a model configuration.
3573
74+ .. deprecated:: Use :meth:`completion_config` instead. This method will be removed in a future version.
75+
3676 :param key: The key of the model configuration.
3777 :param context: The context to evaluate the model configuration in.
3878 :param default_value: The default value of the model configuration.
3979 :param variables: Additional variables for the model configuration.
4080 :return: The value of the model configuration along with a tracker used for gathering metrics.
4181 """
42- self ._client . track ( '$ld:ai:config:function:single' , context , key , 1 )
82+ return self .completion_config ( key , context , default_value , variables )
4383
44- model , provider , messages , instructions , tracker , enabled = self .__evaluate (key , context , default_value .to_dict (), variables )
84+ def judge_config (
85+ self ,
86+ key : str ,
87+ context : Context ,
88+ default_value : AIJudgeConfigDefault ,
89+ variables : Optional [Dict [str , Any ]] = None ,
90+ ) -> AIJudgeConfig :
91+ """
92+ Get the value of a judge configuration.
93+
94+ :param key: The key of the judge configuration.
95+ :param context: The context to evaluate the judge configuration in.
96+ :param default_value: The default value of the judge configuration.
97+ :param variables: Additional variables for the judge configuration.
98+ :return: The judge configuration with a tracker used for gathering metrics.
99+ """
100+ self ._client .track ('$ld:ai:judge:function:single' , context , key , 1 )
101+
102+ model , provider , messages , instructions , tracker , enabled , judge_configuration = self .__evaluate (
103+ key , context , default_value .to_dict (), variables
104+ )
45105
46- config = AIConfig (
106+ # Extract evaluation_metric_keys from the variation
107+ variation = self ._client .variation (key , context , default_value .to_dict ())
108+ evaluation_metric_keys = variation .get ('evaluationMetricKeys' , default_value .evaluation_metric_keys or [])
109+
110+ config = AIJudgeConfig (
47111 enabled = bool (enabled ),
112+ evaluation_metric_keys = evaluation_metric_keys ,
48113 model = model ,
49114 messages = messages ,
50115 provider = provider ,
116+ tracker = tracker ,
51117 )
52118
53- return config , tracker
119+ return config
54120
55- def agent (
121+ def agent_config (
56122 self ,
57- config : LDAIAgentConfig ,
123+ key : str ,
58124 context : Context ,
59- ) -> LDAIAgent :
125+ default_value : AIAgentConfigDefault ,
126+ variables : Optional [Dict [str , Any ]] = None ,
127+ ) -> AIAgentConfig :
60128 """
61129 Retrieve a single AI Config agent.
62130
@@ -65,39 +133,58 @@ def agent(
65133
66134 Example::
67135
68- agent = client.agent(LDAIAgentConfig(
69- key='research_agent',
70- default_value=LDAIAgentDefaults(
136+ agent = client.agent_config(
137+ 'research_agent',
138+ context,
139+ AIAgentConfigDefault(
71140 enabled=True,
72141 model=ModelConfig('gpt-4'),
73142 instructions="You are a research assistant specializing in {{topic}}."
74143 ),
75144 variables={'topic': 'climate change'}
76- ), context)
145+ )
77146
78147 if agent.enabled:
79148 research_result = agent.instructions # Interpolated instructions
80149 agent.tracker.track_success()
81150
82- :param config : The agent configuration to use .
151+ :param key : The agent configuration key .
83152 :param context: The context to evaluate the agent configuration in.
84- :return: Configured LDAIAgent instance.
153+ :param default_value: Default agent values.
154+ :param variables: Variables for interpolation.
155+ :return: Configured AIAgentConfig instance.
85156 """
86157 # Track single agent usage
87158 self ._client .track (
88159 "$ld:ai:agent:function:single" ,
89160 context ,
90- config . key ,
161+ key ,
91162 1
92163 )
93164
94- return self .__evaluate_agent (config . key , context , config . default_value , config . variables )
165+ return self .__evaluate_agent (key , context , default_value , variables )
95166
96- def agents (
167+ def agent (
97168 self ,
98- agent_configs : List [ LDAIAgentConfig ] ,
169+ config : AIAgentConfigRequest ,
99170 context : Context ,
100- ) -> LDAIAgents :
171+ ) -> AIAgentConfig :
172+ """
173+ Retrieve a single AI Config agent.
174+
175+ .. deprecated:: Use :meth:`agent_config` instead. This method will be removed in a future version.
176+
177+ :param config: The agent configuration to use.
178+ :param context: The context to evaluate the agent configuration in.
179+ :return: Configured AIAgentConfig instance.
180+ """
181+ return self .agent_config (config .key , context , config .default_value , config .variables )
182+
183+ def agent_configs (
184+ self ,
185+ agent_configs : List [AIAgentConfigRequest ],
186+ context : Context ,
187+ ) -> AIAgents :
101188 """
102189 Retrieve multiple AI agent configurations.
103190
@@ -107,18 +194,18 @@ def agents(
107194
108195 Example::
109196
110- agents = client.agents ([
111- LDAIAgentConfig (
197+ agents = client.agent_configs ([
198+ AIAgentConfigRequest (
112199 key='research_agent',
113- default_value=LDAIAgentDefaults (
200+ default_value=AIAgentConfigDefault (
114201 enabled=True,
115202 instructions='You are a research assistant.'
116203 ),
117204 variables={'topic': 'climate change'}
118205 ),
119- LDAIAgentConfig (
206+ AIAgentConfigRequest (
120207 key='writing_agent',
121- default_value=LDAIAgentDefaults (
208+ default_value=AIAgentConfigDefault (
122209 enabled=True,
123210 instructions='You are a writing assistant.'
124211 ),
@@ -131,7 +218,7 @@ def agents(
131218
132219 :param agent_configs: List of agent configurations to retrieve.
133220 :param context: The context to evaluate the agent configurations in.
134- :return: Dictionary mapping agent keys to their LDAIAgent configurations.
221+ :return: Dictionary mapping agent keys to their AIAgentConfig configurations.
135222 """
136223 # Track multiple agents usage
137224 agent_count = len (agent_configs )
@@ -142,7 +229,7 @@ def agents(
142229 agent_count
143230 )
144231
145- result : LDAIAgents = {}
232+ result : AIAgents = {}
146233
147234 for config in agent_configs :
148235 agent = self .__evaluate_agent (
@@ -155,13 +242,29 @@ def agents(
155242
156243 return result
157244
245+ def agents (
246+ self ,
247+ agent_configs : List [AIAgentConfigRequest ],
248+ context : Context ,
249+ ) -> AIAgents :
250+ """
251+ Retrieve multiple AI agent configurations.
252+
253+ .. deprecated:: Use :meth:`agent_configs` instead. This method will be removed in a future version.
254+
255+ :param agent_configs: List of agent configurations to retrieve.
256+ :param context: The context to evaluate the agent configurations in.
257+ :return: Dictionary mapping agent keys to their AIAgentConfig configurations.
258+ """
259+ return self .agent_configs (agent_configs , context )
260+
158261 def __evaluate (
159262 self ,
160263 key : str ,
161264 context : Context ,
162265 default_dict : Dict [str , Any ],
163266 variables : Optional [Dict [str , Any ]] = None ,
164- ) -> Tuple [Optional [ModelConfig ], Optional [ProviderConfig ], Optional [List [LDMessage ]], Optional [str ], LDAIConfigTracker , bool ]:
267+ ) -> Tuple [Optional [ModelConfig ], Optional [ProviderConfig ], Optional [List [LDMessage ]], Optional [str ], LDAIConfigTracker , bool , Optional [ Any ] ]:
165268 """
166269 Internal method to evaluate a configuration and extract components.
167270
@@ -228,37 +331,54 @@ def __evaluate(
228331
229332 enabled = variation .get ('_ldMeta' , {}).get ('enabled' , False )
230333
231- return model , provider_config , messages , instructions , tracker , enabled
334+ # Extract judge configuration
335+ judge_configuration = None
336+ if 'judgeConfiguration' in variation and isinstance (variation ['judgeConfiguration' ], dict ):
337+ judge_config = variation ['judgeConfiguration' ]
338+ if 'judges' in judge_config and isinstance (judge_config ['judges' ], list ):
339+ judges = [
340+ Judge (
341+ key = judge ['key' ],
342+ sampling_rate = judge ['samplingRate' ]
343+ )
344+ for judge in judge_config ['judges' ]
345+ if isinstance (judge , dict ) and 'key' in judge and 'samplingRate' in judge
346+ ]
347+ if judges :
348+ judge_configuration = JudgeConfiguration (judges = judges )
349+
350+ return model , provider_config , messages , instructions , tracker , enabled , judge_configuration
232351
233352 def __evaluate_agent (
234353 self ,
235354 key : str ,
236355 context : Context ,
237- default_value : LDAIAgentDefaults ,
356+ default_value : AIAgentConfigDefault ,
238357 variables : Optional [Dict [str , Any ]] = None ,
239- ) -> LDAIAgent :
358+ ) -> AIAgentConfig :
240359 """
241360 Internal method to evaluate an agent configuration.
242361
243362 :param key: The agent configuration key.
244363 :param context: The evaluation context.
245364 :param default_value: Default agent values.
246365 :param variables: Variables for interpolation.
247- :return: Configured LDAIAgent instance.
366+ :return: Configured AIAgentConfig instance.
248367 """
249- model , provider , messages , instructions , tracker , enabled = self .__evaluate (
368+ model , provider , messages , instructions , tracker , enabled , judge_configuration = self .__evaluate (
250369 key , context , default_value .to_dict (), variables
251370 )
252371
253372 # For agents, prioritize instructions over messages
254373 final_instructions = instructions if instructions is not None else default_value .instructions
255374
256- return LDAIAgent (
257- enabled = bool (enabled ) if enabled is not None else default_value .enabled ,
375+ return AIAgentConfig (
376+ enabled = bool (enabled ) if enabled is not None else ( default_value .enabled or False ) ,
258377 model = model or default_value .model ,
259378 provider = provider or default_value .provider ,
260379 instructions = final_instructions ,
261380 tracker = tracker ,
381+ judge_configuration = judge_configuration or default_value .judge_configuration ,
262382 )
263383
264384 def __interpolate_template (self , template : str , variables : Dict [str , Any ]) -> str :
0 commit comments