@@ -184,6 +184,18 @@ def to_dict(self) -> Dict[str, Any]:
184
184
return result
185
185
186
186
187
+ @dataclass
188
+ class LDAIAgentConfig :
189
+ """
190
+ Configuration for individual agent in batch requests.
191
+
192
+ Combines agent key with its specific default configuration and variables.
193
+ """
194
+ agent_key : str
195
+ default_config : LDAIAgentDefaults
196
+ variables : Optional [Dict [str , Any ]] = None
197
+
198
+
187
199
# Type alias for multiple agents
188
200
LDAIAgents = Dict [str , LDAIAgent ]
189
201
@@ -221,49 +233,111 @@ def config(
221
233
222
234
return config , tracker
223
235
224
- def agents (
236
+ def agent (
225
237
self ,
226
- keys : List [ str ] ,
238
+ key : str ,
227
239
context : Context ,
228
240
default_value : LDAIAgentDefaults ,
229
241
variables : Optional [Dict [str , Any ]] = None ,
230
- ) -> LDAIAgents :
242
+ ) -> LDAIAgent :
231
243
"""
232
- Get multiple AI agent configurations .
244
+ Retrieve a single AI Config agent .
233
245
234
- This method allows you to retrieve multiple agent configurations in a single call,
235
- with each agent having its instructions dynamically interpolated with the provided
236
- variables and context data.
246
+ This method retrieves a single agent configuration with instructions
247
+ dynamically interpolated using the provided variables and context data.
237
248
238
249
Example::
239
250
240
- agents = client.agents (
241
- ['customer-support', 'sales-assistant'] ,
251
+ agent = client.agent (
252
+ 'research_agent' ,
242
253
context,
243
254
LDAIAgentDefaults(
244
255
enabled=True,
245
256
model=ModelConfig('gpt-4'),
246
- instructions="You are a helpful assistant."
257
+ instructions="You are a research assistant specializing in {{topic}} ."
247
258
),
248
- {'company_name ': 'Acme Corp '}
259
+ {'topic ': 'climate change '}
249
260
)
250
261
251
- support_agent = agents['customer-support']
252
- if support_agent.enabled:
253
- print(support_agent.instructions) # Instructions with interpolated variables
254
- # Use support_agent.tracker for metrics tracking
262
+ if agent.enabled:
263
+ research_result = agent.instructions # Interpolated instructions
264
+ agent.tracker.track_success()
255
265
256
- :param keys: List of agent configuration keys to retrieve.
257
- :param context: The context to evaluate the agent configurations in.
266
+ :param key: The agent configuration key to retrieve.
267
+ :param context: The context to evaluate the agent configuration in.
258
268
:param default_value: Default agent configuration values to use as fallback.
259
269
:param variables: Additional variables for template interpolation in instructions.
270
+ :return: Configured LDAIAgent instance.
271
+ """
272
+ # Track single agent usage
273
+ self ._client .track (
274
+ "$ld:ai:agent:function:single" ,
275
+ context ,
276
+ key ,
277
+ 1
278
+ )
279
+
280
+ return self .__evaluate_agent (key , context , default_value , variables )
281
+
282
+ def agents (
283
+ self ,
284
+ agent_configs : List [LDAIAgentConfig ],
285
+ context : Context ,
286
+ ) -> LDAIAgents :
287
+ """
288
+ Retrieve multiple AI agent configurations.
289
+
290
+ This method allows you to retrieve multiple agent configurations in a single call,
291
+ with each agent having its own default configuration and variables for instruction
292
+ interpolation.
293
+
294
+ Example::
295
+
296
+ agents = client.agents([
297
+ LDAIAgentConfig(
298
+ agent_key='research_agent',
299
+ default_config=LDAIAgentDefaults(
300
+ enabled=True,
301
+ instructions='You are a research assistant.'
302
+ ),
303
+ variables={'topic': 'climate change'}
304
+ ),
305
+ LDAIAgentConfig(
306
+ agent_key='writing_agent',
307
+ default_config=LDAIAgentDefaults(
308
+ enabled=True,
309
+ instructions='You are a writing assistant.'
310
+ ),
311
+ variables={'style': 'academic'}
312
+ )
313
+ ], context)
314
+
315
+ research_result = agents["research_agent"].instructions
316
+ agents["research_agent"].tracker.track_success()
317
+
318
+ :param agent_configs: List of agent configurations to retrieve.
319
+ :param context: The context to evaluate the agent configurations in.
260
320
:return: Dictionary mapping agent keys to their LDAIAgent configurations.
261
321
"""
322
+ # Track multiple agents usage
323
+ agent_count = len (agent_configs )
324
+ self ._client .track (
325
+ "$ld:ai:agent:function:multiple" ,
326
+ context ,
327
+ agent_count ,
328
+ agent_count
329
+ )
330
+
262
331
result : LDAIAgents = {}
263
332
264
- for key in keys :
265
- agent = self .__evaluate_agent (key , context , default_value , variables )
266
- result [key ] = agent
333
+ for config in agent_configs :
334
+ agent = self .__evaluate_agent (
335
+ config .agent_key ,
336
+ context ,
337
+ config .default_config ,
338
+ config .variables
339
+ )
340
+ result [config .agent_key ] = agent
267
341
268
342
return result
269
343
@@ -360,20 +434,23 @@ def __evaluate_agent(
360
434
key , context , default_value .to_dict (), variables
361
435
)
362
436
437
+ # For agents, prioritize instructions over messages
438
+ final_instructions = instructions if instructions is not None else default_value .instructions
439
+
363
440
return LDAIAgent (
364
- enabled = bool (enabled ) if enabled is not None else None ,
441
+ enabled = bool (enabled ) if enabled is not None else default_value . enabled ,
365
442
model = model or default_value .model ,
366
443
provider = provider or default_value .provider ,
367
- instructions = instructions ,
444
+ instructions = final_instructions ,
368
445
tracker = tracker ,
369
446
)
370
447
371
448
def __interpolate_template (self , template : str , variables : Dict [str , Any ]) -> str :
372
449
"""
373
- Interpolate the template with the given variables.
450
+ Interpolate the template with the given variables using Mustache format .
374
451
375
- :template: The template string.
376
- :variables: The variables to interpolate into the template.
452
+ :param template: The template string.
453
+ :param variables: The variables to interpolate into the template.
377
454
:return: The interpolated string.
378
455
"""
379
456
return chevron .render (template , variables )
0 commit comments