-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathclient.py
More file actions
946 lines (811 loc) · 34.5 KB
/
client.py
File metadata and controls
946 lines (811 loc) · 34.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
import uuid
from typing import Any, Callable, Dict, List, Optional, Tuple
import chevron
from ldclient import Context, Result
from ldclient.client import LDClient
from ldai import log
from ldai.agent_graph import AgentGraphDefinition
from ldai.judge import Judge
from ldai.managed_agent import ManagedAgent
from ldai.managed_agent_graph import ManagedAgentGraph
from ldai.managed_model import ManagedModel
from ldai.models import (
AIAgentConfig,
AIAgentConfigDefault,
AIAgentConfigRequest,
AIAgentGraphConfig,
AIAgents,
AICompletionConfig,
AICompletionConfigDefault,
AIJudgeConfig,
AIJudgeConfigDefault,
Edge,
JudgeConfiguration,
LDMessage,
LDTool,
ModelConfig,
ProviderConfig,
)
from ldai.providers import ToolRegistry
from ldai.providers.runner_factory import RunnerFactory
from ldai.sdk_info import AI_SDK_LANGUAGE, AI_SDK_NAME, AI_SDK_VERSION
from ldai.tracker import AIGraphTracker, LDAIConfigTracker
_TRACK_SDK_INFO = '$ld:ai:sdk:info'
_TRACK_USAGE_AGENT_CONFIG = '$ld:ai:usage:agent-config'
_TRACK_USAGE_AGENT_CONFIGS = '$ld:ai:usage:agent-configs'
_TRACK_USAGE_COMPLETION_CONFIG = '$ld:ai:usage:completion-config'
_TRACK_USAGE_CREATE_AGENT = '$ld:ai:usage:create-agent'
_TRACK_USAGE_CREATE_AGENT_GRAPH = '$ld:ai:usage:create-agent-graph'
_TRACK_USAGE_CREATE_JUDGE = '$ld:ai:usage:create-judge'
_TRACK_USAGE_CREATE_MODEL = '$ld:ai:usage:create-model'
_TRACK_USAGE_JUDGE_CONFIG = '$ld:ai:usage:judge-config'
_INIT_TRACK_CONTEXT = Context.builder('ld-internal-tracking').kind('ld_ai').anonymous(True).build()
_DISABLED_COMPLETION_DEFAULT = AICompletionConfigDefault.disabled()
_DISABLED_AGENT_DEFAULT = AIAgentConfigDefault.disabled()
_DISABLED_JUDGE_DEFAULT = AIJudgeConfigDefault.disabled()
def _parse_tools(tools_data: Optional[Dict[str, Any]]) -> Optional[Dict[str, LDTool]]:
"""Parse the root-level tools map from a flag variation dict."""
if not isinstance(tools_data, dict):
return None
result = {}
for tool_name, tool_dict in tools_data.items():
if not isinstance(tool_dict, dict):
log.warning('Skipping tool "%s": expected a dict, got %s', tool_name, type(tool_dict).__name__)
continue
result[tool_name] = LDTool(
name=tool_dict.get('name', tool_name),
type=tool_dict.get('type'),
parameters=tool_dict.get('parameters'),
custom_parameters=tool_dict.get('customParameters'),
)
return result or None
class LDAIClient:
"""The LaunchDarkly AI SDK client object."""
def __init__(self, client: LDClient):
self._client = client
self._client.track(
_TRACK_SDK_INFO,
_INIT_TRACK_CONTEXT,
{
'aiSdkName': AI_SDK_NAME,
'aiSdkVersion': AI_SDK_VERSION,
'aiSdkLanguage': AI_SDK_LANGUAGE,
},
1,
)
def create_tracker(self, token: str, context: Context) -> Result:
"""
Reconstruct a tracker from a resumption token.
Delegates to :meth:`LDAIConfigTracker.from_resumption_token`.
:param token: A URL-safe Base64-encoded resumption token obtained from
:attr:`LDAIConfigTracker.resumption_token`.
:param context: The context to use for track events.
:return: A :class:`Result` whose ``value`` is a new
:class:`LDAIConfigTracker` on success, or whose ``error`` describes
the problem on failure.
"""
return LDAIConfigTracker.from_resumption_token(token, self._client, context)
def _completion_config(
self,
key: str,
context: Context,
default: AICompletionConfigDefault,
variables: Optional[Dict[str, Any]] = None,
) -> AICompletionConfig:
(model, provider, messages, instructions,
tracker_factory, enabled, judge_configuration, variation) = self.__evaluate(
key, context, default.to_dict(), variables
)
tools_data = variation.get('tools')
tools = _parse_tools(tools_data) if tools_data is not None else default.tools
config = AICompletionConfig(
key=key,
enabled=bool(enabled),
model=model,
messages=messages,
provider=provider,
create_tracker=tracker_factory,
judge_configuration=judge_configuration,
tools=tools,
)
return config
def completion_config(
self,
key: str,
context: Context,
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: 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 or _DISABLED_COMPLETION_DEFAULT, variables
)
def config(
self,
key: str,
context: Context,
default: Optional[AICompletionConfigDefault] = None,
variables: Optional[Dict[str, Any]] = None,
) -> AICompletionConfig:
"""
Get the value of a model configuration.
.. deprecated:: Use :meth:`completion_config` instead. This method will be removed in a future version.
:param key: The key of the model configuration.
:param context: The context to evaluate the model configuration in.
: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, variables)
def _judge_config(
self,
key: str,
context: Context,
default: AIJudgeConfigDefault,
variables: Optional[Dict[str, Any]] = None,
) -> AIJudgeConfig:
(model, provider, messages, instructions,
tracker_factory, enabled, judge_configuration, variation) = self.__evaluate(
key, context, default.to_dict(), variables
)
def _extract_evaluation_metric_key(variation: Dict[str, Any]) -> Optional[str]:
"""
Extract evaluation_metric_key with backward compatibility.
Priority: 1) evaluationMetricKey from variation, 2) first from evaluationMetricKeys in variation
"""
if evaluation_metric_key := variation.get('evaluationMetricKey'):
return evaluation_metric_key
variation_keys = variation.get('evaluationMetricKeys')
if isinstance(variation_keys, list) and variation_keys:
return variation_keys[0]
return None
evaluation_metric_key = _extract_evaluation_metric_key(variation)
config = AIJudgeConfig(
key=key,
enabled=bool(enabled),
evaluation_metric_key=evaluation_metric_key,
model=model,
messages=messages,
provider=provider,
create_tracker=tracker_factory,
)
return config
def judge_config(
self,
key: str,
context: Context,
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: 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 or _DISABLED_JUDGE_DEFAULT, variables
)
async def create_judge(
self,
key: str,
context: Context,
default: Optional[AIJudgeConfigDefault] = None,
variables: Optional[Dict[str, Any]] = None,
default_ai_provider: Optional[str] = None,
) -> Optional[Judge]:
"""
Creates and returns a new Judge instance for AI evaluation.
:param key: The key identifying the AI judge configuration to use
:param context: Standard Context used when evaluating flags
: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.
:return: Judge instance or None if disabled/unsupported
Example::
judge = client.create_judge(
"relevance-judge",
context,
AIJudgeConfigDefault(
enabled=True,
model=ModelConfig("gpt-4"),
provider=ProviderConfig("openai"),
evaluation_metric_key='$ld:ai:judge:relevance',
messages=[LDMessage(role='system', content='You are a relevance judge.')]
),
variables={'metric': "relevance"}
)
if judge:
result = await judge.evaluate("User question", "AI response")
if result and result.evals:
relevance_eval = result.evals.get('$ld:ai:judge:relevance')
if relevance_eval:
print('Relevance score:', relevance_eval.score)
"""
self._client.track(_TRACK_USAGE_CREATE_JUDGE, context, key, 1)
try:
if variables:
if 'message_history' in variables:
pass
if 'response_to_evaluate' in variables:
pass
extended_variables = dict(variables) if variables else {}
extended_variables['message_history'] = '{{message_history}}'
extended_variables['response_to_evaluate'] = '{{response_to_evaluate}}'
judge_config = self._judge_config(
key, context, default or _DISABLED_JUDGE_DEFAULT, extended_variables
)
if not judge_config.enabled:
return None
provider = RunnerFactory.create_model(judge_config, default_ai_provider)
if not provider:
return None
return Judge(judge_config, provider)
except Exception as error:
return None
async def _initialize_judges(
self,
judge_configs: List[JudgeConfiguration.Judge],
context: Context,
variables: Optional[Dict[str, Any]] = None,
default_ai_provider: Optional[str] = None,
) -> Dict[str, Judge]:
"""
Initialize judges from judge configurations.
:param judge_configs: List of judge configurations
:param context: Standard Context used when evaluating flags
:param variables: Dictionary of values for instruction interpolation
:param default_ai_provider: Optional default AI provider to use
:return: Dictionary of judge instances keyed by their configuration keys
"""
judges: Dict[str, Judge] = {}
async def create_judge_for_config(judge_key: str):
judge = await self.create_judge(
judge_key,
context,
AIJudgeConfigDefault.disabled(),
variables,
default_ai_provider,
)
return judge_key, judge
judge_promises = [
create_judge_for_config(judge_config.key)
for judge_config in judge_configs
]
import asyncio
results = await asyncio.gather(*judge_promises, return_exceptions=True)
for result in results:
if isinstance(result, Exception):
continue
judge_key, judge = result # type: ignore[misc]
if judge:
judges[judge_key] = judge
return judges
async def create_model(
self,
key: str,
context: Context,
default: Optional[AICompletionConfigDefault] = None,
variables: Optional[Dict[str, Any]] = None,
default_ai_provider: Optional[str] = None,
) -> Optional[ManagedModel]:
"""
Creates and returns a new ManagedModel for AI conversations.
:param key: The key identifying the AI completion configuration to use
:param context: Standard Context used when evaluating flags
:param default: A default value representing a standard AI config result. When not provided,
a disabled config is used as the fallback.
:param variables: Dictionary of values for instruction interpolation
:param default_ai_provider: Optional default AI provider to use
:return: ManagedModel instance or None if disabled/unsupported
Example::
model = await client.create_model(
"customer-support-chat",
context,
AICompletionConfigDefault(
enabled=True,
model=ModelConfig("gpt-4"),
provider=ProviderConfig("openai"),
messages=[LDMessage(role='system', content='You are a helpful assistant.')]
),
variables={'customerName': 'John'}
)
if model:
response = await model.invoke("I need help with my order")
print(response.message.content)
"""
self._client.track(_TRACK_USAGE_CREATE_MODEL, context, key, 1)
log.debug(f"Creating managed model for key: {key}")
config = self._completion_config(key, context, default or _DISABLED_COMPLETION_DEFAULT, variables)
if not config.enabled:
return None
runner = RunnerFactory.create_model(config, default_ai_provider)
if not runner:
return None
judges = {}
if config.judge_configuration and config.judge_configuration.judges:
judges = await self._initialize_judges(
config.judge_configuration.judges,
context,
variables,
default_ai_provider,
)
return ManagedModel(config, runner, judges)
async def create_chat(
self,
key: str,
context: Context,
default: Optional[AICompletionConfigDefault] = None,
variables: Optional[Dict[str, Any]] = None,
default_ai_provider: Optional[str] = None,
) -> Optional[ManagedModel]:
"""
.. deprecated:: Use :meth:`create_model` instead.
Creates and returns a ManagedModel for AI conversations.
This method is a deprecated alias for :meth:`create_model`.
"""
log.warning('create_chat() is deprecated, use create_model() instead')
return await self.create_model(key, context, default, variables, default_ai_provider)
async def create_agent(
self,
key: str,
context: Context,
tools: Optional[ToolRegistry] = None,
default: Optional[AIAgentConfigDefault] = None,
variables: Optional[Dict[str, Any]] = None,
default_ai_provider: Optional[str] = None,
) -> Optional[ManagedAgent]:
"""
CAUTION:
This feature is experimental and should NOT be considered ready for production use.
It may change or be removed without notice and is not subject to backwards
compatibility guarantees.
Creates and returns a new ManagedAgent for AI agent invocations.
:param key: The key identifying the AI agent configuration to use
:param context: Standard Context used when evaluating flags
:param tools: ToolRegistry mapping tool names to callable implementations
:param default: A default value representing a standard AI agent config result.
When not provided, a disabled config is used as the fallback.
:param variables: Dictionary of values for instruction interpolation
:param default_ai_provider: Optional default AI provider to use
:return: ManagedAgent instance or None if disabled/unsupported
Example::
agent = await client.create_agent(
"customer-support-agent",
context,
tools={"get-order": fetch_order_fn},
default=AIAgentConfigDefault(
enabled=True,
model=ModelConfig("gpt-4"),
provider=ProviderConfig("openai"),
instructions="You are a helpful customer support agent."
),
)
if agent:
result = await agent.run("Where is my order?")
print(result.output)
"""
self._client.track(_TRACK_USAGE_CREATE_AGENT, context, key, 1)
log.debug(f"Creating managed agent for key: {key}")
config = self.__evaluate_agent(key, context, default or _DISABLED_AGENT_DEFAULT, variables)
if not config.enabled:
return None
runner = RunnerFactory.create_agent(config, tools or {}, default_ai_provider)
if not runner:
return None
return ManagedAgent(config, runner)
def agent_config(
self,
key: str,
context: Context,
default: Optional[AIAgentConfigDefault] = None,
variables: Optional[Dict[str, Any]] = None,
) -> AIAgentConfig:
"""
Retrieve a single AI Config agent.
This method retrieves a single agent configuration with instructions
dynamically interpolated using the provided variables and context data.
Example::
agent = client.agent_config(
'research_agent',
context,
AIAgentConfigDefault(
enabled=True,
model=ModelConfig('gpt-4'),
instructions="You are a research assistant specializing in {{topic}}."
),
variables={'topic': 'climate change'}
)
if agent.enabled:
research_result = agent.instructions # Interpolated instructions
tracker = agent.create_tracker()
tracker.track_success()
:param key: The agent configuration key.
:param context: The context to evaluate the agent configuration in.
: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.
"""
self._client.track(
_TRACK_USAGE_AGENT_CONFIG,
context,
key,
1
)
return self.__evaluate_agent(
key, context, default or _DISABLED_AGENT_DEFAULT, variables
)
def agent(
self,
config: AIAgentConfigRequest,
context: Context,
) -> AIAgentConfig:
"""
Retrieve a single AI Config agent.
.. deprecated:: Use :meth:`agent_config` instead. This method will be removed in a future version.
:param config: The agent configuration to use.
:param context: The context to evaluate the agent configuration in.
:return: Configured AIAgentConfig instance.
"""
return self.agent_config(config.key, context, config.default, config.variables)
def agent_configs(
self,
agent_configs: List[AIAgentConfigRequest],
context: Context,
) -> AIAgents:
"""
Retrieve multiple AI agent configurations.
This method allows you to retrieve multiple agent configurations in a single call,
with each agent having its own default configuration and variables for instruction
interpolation.
Example::
agents = client.agent_configs([
AIAgentConfigRequest(
key='research_agent',
default=AIAgentConfigDefault(
enabled=True,
instructions='You are a research assistant.'
),
variables={'topic': 'climate change'}
),
AIAgentConfigRequest(
key='writing_agent',
default=AIAgentConfigDefault(
enabled=True,
instructions='You are a writing assistant.'
),
variables={'style': 'academic'}
)
], context)
research_result = agents["research_agent"].instructions
tracker = agents["research_agent"].create_tracker()
tracker.track_success()
:param agent_configs: List of agent configurations to retrieve.
:param context: The context to evaluate the agent configurations in.
:return: Dictionary mapping agent keys to their AIAgentConfig configurations.
"""
agent_count = len(agent_configs)
self._client.track(
_TRACK_USAGE_AGENT_CONFIGS,
context,
agent_count,
agent_count
)
result: AIAgents = {}
for config in agent_configs:
agent = self.__evaluate_agent(
config.key,
context,
config.default or _DISABLED_AGENT_DEFAULT,
config.variables
)
result[config.key] = agent
return result
def agent_graph(
self,
key: str,
context: Context,
) -> AgentGraphDefinition:
"""`
Retrieve an AI agent graph.
"""
variation = self._client.variation(key, context, {})
# Extract variation metadata for tracker
variation_key = variation.get("_ldMeta", {}).get("variationKey", "")
version = int(variation.get("_ldMeta", {}).get("version", 1))
# Create graph tracker factory
def graph_tracker_factory() -> AIGraphTracker:
return AIGraphTracker(
self._client,
variation_key,
key,
version,
context,
)
if not variation.get("root"):
log.debug(f"Agent graph {key} is disabled, no root config key found")
return AgentGraphDefinition(
AIAgentGraphConfig(
key=key,
root_config_key="",
edges=[],
enabled=False,
),
nodes={},
context=context,
enabled=False,
create_tracker=graph_tracker_factory,
)
edge_keys = list[str](variation.get("edges", {}).keys())
all_agent_keys = set[str]([variation.get("root")])
for edge_key in edge_keys:
for single_edge in variation.get("edges", {}).get(edge_key, []):
all_agent_keys.add(single_edge.get("key", ""))
graph_key_value = key
agent_configs = {
agent_key: self.__evaluate_agent(
agent_key, context, AIAgentConfigDefault.disabled(), graph_key=graph_key_value
)
for agent_key in all_agent_keys
}
if not all(config.enabled for config in agent_configs.values()):
log.debug(
f"Agent graph {key} is disabled, not all agent configs are enabled"
)
return AgentGraphDefinition(
AIAgentGraphConfig(
key=key,
root_config_key="",
edges=[],
enabled=False,
),
nodes={},
context=context,
enabled=False,
create_tracker=graph_tracker_factory,
)
try:
edges: list[Edge] = []
for edge_key in edge_keys:
for single_edge in variation.get("edges", {}).get(edge_key, []):
edges.append(Edge(
key=edge_key + "-" + single_edge.get("key", ""),
source_config=edge_key,
target_config=single_edge.get("key", ""),
handoff=single_edge.get("handoff", {}),
))
agent_graph_config = AIAgentGraphConfig(
key=key,
root_config_key=variation.get("root"),
edges=edges,
)
except Exception as e:
log.debug(f"Agent graph {key} is disabled, invalid agent graph config")
return AgentGraphDefinition(
AIAgentGraphConfig(
key=key,
root_config_key="",
edges=[],
enabled=False,
),
nodes={},
context=context,
enabled=False,
create_tracker=graph_tracker_factory,
)
nodes = AgentGraphDefinition.build_nodes(
agent_graph_config,
agent_configs,
)
return AgentGraphDefinition(
agent_graph=agent_graph_config,
nodes=nodes,
context=context,
enabled=agent_graph_config.enabled,
create_tracker=graph_tracker_factory,
)
async def create_agent_graph(
self,
key: str,
context: Context,
tools: Optional[ToolRegistry] = None,
default_ai_provider: Optional[str] = None,
) -> Optional[ManagedAgentGraph]:
"""
CAUTION:
This feature is experimental and should NOT be considered ready for production use.
It may change or be removed without notice and is not subject to backwards
compatibility guarantees.
Creates and returns a new ManagedAgentGraph for AI agent graph execution.
Resolves the graph configuration via ``agent_graph()``, creates a
provider-specific runner, and wraps it in a ``ManagedAgentGraph``.
:param key: The key identifying the agent graph configuration
:param context: Standard Context used when evaluating flags
:param tools: Registry mapping tool names to callables
:param default_ai_provider: Optional provider override ('openai', 'langchain', …)
:return: ManagedAgentGraph instance, or None if the graph is disabled or unsupported
Example::
graph = await client.create_agent_graph(
"travel-assistant-graph",
context,
tools={
"web_search_tool": my_search_fn,
"get_weather": my_weather_fn,
}
)
if graph:
result = await graph.run("Find me restaurants in Seattle")
print(result.output)
"""
self._client.track(_TRACK_USAGE_CREATE_AGENT_GRAPH, context, key, 1)
log.debug(f"Creating managed agent graph for key: {key}")
graph = self.agent_graph(key, context)
if not graph.enabled:
return None
runner = RunnerFactory.create_agent_graph(
graph, tools or {}, default_ai_provider
)
if not runner:
return None
return ManagedAgentGraph(runner)
def agents(
self,
agent_configs: List[AIAgentConfigRequest],
context: Context,
) -> AIAgents:
"""
Retrieve multiple AI agent configurations.
.. deprecated:: Use :meth:`agent_configs` instead. This method will be removed in a future version.
:param agent_configs: List of agent configurations to retrieve.
:param context: The context to evaluate the agent configurations in.
:return: Dictionary mapping agent keys to their AIAgentConfig configurations.
"""
return self.agent_configs(agent_configs, context)
def __evaluate(
self,
key: str,
context: Context,
default_dict: Dict[str, Any],
variables: Optional[Dict[str, Any]] = None,
graph_key: Optional[str] = None,
) -> Tuple[
Optional[ModelConfig], Optional[ProviderConfig], Optional[List[LDMessage]],
Optional[str], Callable[[], LDAIConfigTracker], bool, Optional[Any], Dict[str, Any]
]:
"""
Internal method to evaluate a configuration and extract components.
:param key: The configuration key.
:param context: The evaluation context.
:param default_dict: Default configuration as dictionary.
:param variables: Variables for interpolation.
:param graph_key: When set, passed to the tracker so all events include ``graphKey``.
:return: Tuple of (model, provider, messages, instructions,
tracker_factory, enabled, judge_configuration, variation).
"""
variation = self._client.variation(key, context, default_dict)
all_variables = {}
if variables:
all_variables.update(variables)
all_variables['ldctx'] = context.to_dict()
messages = None
if 'messages' in variation and isinstance(variation['messages'], list) and all(
isinstance(entry, dict) for entry in variation['messages']
):
messages = [
LDMessage(
role=entry['role'],
content=self.__interpolate_template(
entry['content'], all_variables
),
)
for entry in variation['messages']
]
instructions = None
if 'instructions' in variation and isinstance(variation['instructions'], str):
instructions = self.__interpolate_template(variation['instructions'], all_variables)
provider_config = None
if 'provider' in variation and isinstance(variation['provider'], dict):
provider = variation['provider']
provider_config = ProviderConfig(provider.get('name', ''))
model = None
if 'model' in variation and isinstance(variation['model'], dict):
parameters = variation['model'].get('parameters', None)
custom = variation['model'].get('custom', None)
model = ModelConfig(
name=variation['model']['name'],
parameters=parameters,
custom=custom
)
variation_key = variation.get('_ldMeta', {}).get('variationKey', '')
version = int(variation.get('_ldMeta', {}).get('version', 1))
model_name = model.name if model else ''
provider_name = provider_config.name if provider_config else ''
def tracker_factory() -> LDAIConfigTracker:
return LDAIConfigTracker(
ld_client=self._client,
run_id=str(uuid.uuid4()),
config_key=key,
variation_key=variation_key,
version=version,
context=context,
model_name=model_name,
provider_name=provider_name,
graph_key=graph_key,
)
enabled = variation.get('_ldMeta', {}).get('enabled', False)
judge_configuration = None
if 'judgeConfiguration' in variation and isinstance(variation['judgeConfiguration'], dict):
judge_config = variation['judgeConfiguration']
if 'judges' in judge_config and isinstance(judge_config['judges'], list):
judges = [
JudgeConfiguration.Judge(
key=judge['key'],
sampling_rate=judge['samplingRate']
)
for judge in judge_config['judges']
if isinstance(judge, dict) and 'key' in judge and 'samplingRate' in judge
]
if judges:
judge_configuration = JudgeConfiguration(judges=judges)
return (
model, provider_config, messages, instructions,
tracker_factory, enabled, judge_configuration, variation,
)
def __evaluate_agent(
self,
key: str,
context: Context,
default: AIAgentConfigDefault,
variables: Optional[Dict[str, Any]] = None,
graph_key: Optional[str] = None,
) -> AIAgentConfig:
"""
Internal method to evaluate an agent configuration.
:param key: The agent configuration key.
:param context: The evaluation context.
:param default: Default agent values.
:param variables: Variables for interpolation.
:param graph_key: When set, passed to the tracker so all events include ``graphKey``.
:return: Configured AIAgentConfig instance.
"""
(model, provider, messages, instructions,
tracker_factory, enabled, judge_configuration, variation) = self.__evaluate(
key, context, default.to_dict(), variables, graph_key=graph_key
)
# For agents, prioritize instructions over messages
final_instructions = instructions if instructions is not None else default.instructions
tools_data = variation.get('tools')
tools = _parse_tools(tools_data) if tools_data is not None else default.tools
return AIAgentConfig(
key=key,
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,
create_tracker=tracker_factory,
judge_configuration=judge_configuration or default.judge_configuration,
tools=tools,
)
def __interpolate_template(self, template: str, variables: Dict[str, Any]) -> str:
"""
Interpolate the template with the given variables using Mustache format.
:param template: The template string.
:param variables: The variables to interpolate into the template.
:return: The interpolated string.
"""
return chevron.render(template, variables)