Skip to content

Commit 7b111f3

Browse files
committed
Updated with .agent method from spec
1 parent 8940ea3 commit 7b111f3

File tree

3 files changed

+330
-151
lines changed

3 files changed

+330
-151
lines changed

ldai/client.py

Lines changed: 102 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,18 @@ def to_dict(self) -> Dict[str, Any]:
184184
return result
185185

186186

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+
187199
# Type alias for multiple agents
188200
LDAIAgents = Dict[str, LDAIAgent]
189201

@@ -221,49 +233,111 @@ def config(
221233

222234
return config, tracker
223235

224-
def agents(
236+
def agent(
225237
self,
226-
keys: List[str],
238+
key: str,
227239
context: Context,
228240
default_value: LDAIAgentDefaults,
229241
variables: Optional[Dict[str, Any]] = None,
230-
) -> LDAIAgents:
242+
) -> LDAIAgent:
231243
"""
232-
Get multiple AI agent configurations.
244+
Retrieve a single AI Config agent.
233245
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.
237248
238249
Example::
239250
240-
agents = client.agents(
241-
['customer-support', 'sales-assistant'],
251+
agent = client.agent(
252+
'research_agent',
242253
context,
243254
LDAIAgentDefaults(
244255
enabled=True,
245256
model=ModelConfig('gpt-4'),
246-
instructions="You are a helpful assistant."
257+
instructions="You are a research assistant specializing in {{topic}}."
247258
),
248-
{'company_name': 'Acme Corp'}
259+
{'topic': 'climate change'}
249260
)
250261
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()
255265
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.
258268
:param default_value: Default agent configuration values to use as fallback.
259269
: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.
260320
:return: Dictionary mapping agent keys to their LDAIAgent configurations.
261321
"""
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+
262331
result: LDAIAgents = {}
263332

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
267341

268342
return result
269343

@@ -360,20 +434,23 @@ def __evaluate_agent(
360434
key, context, default_value.to_dict(), variables
361435
)
362436

437+
# For agents, prioritize instructions over messages
438+
final_instructions = instructions if instructions is not None else default_value.instructions
439+
363440
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,
365442
model=model or default_value.model,
366443
provider=provider or default_value.provider,
367-
instructions=instructions,
444+
instructions=final_instructions,
368445
tracker=tracker,
369446
)
370447

371448
def __interpolate_template(self, template: str, variables: Dict[str, Any]) -> str:
372449
"""
373-
Interpolate the template with the given variables.
450+
Interpolate the template with the given variables using Mustache format.
374451
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.
377454
:return: The interpolated string.
378455
"""
379456
return chevron.render(template, variables)

0 commit comments

Comments
 (0)