1
+ import pytest
2
+ from ldclient import LDClient , Context , Config
3
+ from ldclient .integrations .test_data import TestData
4
+ from ldai .types import AIConfig
5
+ from ldai .client import LDAIClient
6
+ from ldclient .testing .builders import *
7
+
8
+ @pytest .fixture
9
+ def td () -> TestData :
10
+ td = TestData .data_source ()
11
+ td .update (td .flag ('model-config' ).variations ({
12
+ 'model' : { 'modelId' : 'fakeModel' },
13
+ 'prompt' : [{'role' : 'system' , 'content' : 'Hello, {{name}}!' }],
14
+ '_ldMeta' : {'enabled' : True , 'versionKey' : 'abcd' }
15
+ }, "green" ).variation_for_all (0 ))
16
+
17
+ td .update (td .flag ('multiple-prompt' ).variations ({
18
+ 'model' : { 'modelId' : 'fakeModel' },
19
+ 'prompt' : [{'role' : 'system' , 'content' : 'Hello, {{name}}!' }, {'role' : 'user' , 'content' : 'The day is, {{day}}!' }],
20
+ '_ldMeta' : {'enabled' : True , 'versionKey' : 'abcd' }
21
+ }, "green" ).variation_for_all (0 ))
22
+
23
+ td .update (td .flag ('ctx-interpolation' ).variations ({
24
+ 'model' : { 'modelId' : 'fakeModel' },
25
+ 'prompt' : [{'role' : 'system' , 'content' : 'Hello, {{ldctx.name}}!' }],
26
+ '_ldMeta' : {'enabled' : True , 'versionKey' : 'abcd' }
27
+ }).variation_for_all (0 ))
28
+
29
+ td .update (td .flag ('off-config' ).variations ({
30
+ 'model' : { 'modelId' : 'fakeModel' },
31
+ 'prompt' : [{'role' : 'system' , 'content' : 'Hello, {{name}}!' }],
32
+ '_ldMeta' : {'enabled' : False , 'versionKey' : 'abcd' }
33
+ }).variation_for_all (0 ))
34
+
35
+ return td
36
+
37
+ @pytest .fixture
38
+ def client (td : TestData ) -> LDClient :
39
+ config = Config ('sdk-key' , update_processor_class = td , send_events = False )
40
+ return LDClient (config = config )
41
+
42
+ @pytest .fixture
43
+ def ldai_client (client : LDClient ) -> LDAIClient :
44
+ return LDAIClient (client )
45
+
46
+ def test_model_config_interpolation (ldai_client : LDAIClient ):
47
+ context = Context .create ('user-key' )
48
+ default_value = AIConfig (config = {
49
+ 'model' : { 'modelId' : 'fakeModel' },
50
+ 'prompt' : [{'role' : 'system' , 'content' : 'Hello, {{name}}!' }],
51
+ '_ldMeta' : {'enabled' : True , 'versionKey' : 'abcd' }
52
+ }, tracker = None , enabled = True )
53
+ variables = {'name' : 'World' }
54
+
55
+ config = ldai_client .model_config ('model-config' , context , default_value , variables )
56
+
57
+ assert config .config ['prompt' ][0 ]['content' ] == 'Hello, World!'
58
+ assert config .enabled is True
59
+ assert config .tracker .version_key == 'abcd'
60
+
61
+ def test_model_config_no_variables (ldai_client : LDAIClient ):
62
+ context = Context .create ('user-key' )
63
+ default_value = AIConfig (config = {}, tracker = None , enabled = True )
64
+
65
+ config = ldai_client .model_config ('model-config' , context , default_value , {})
66
+
67
+ assert config .config ['prompt' ][0 ]['content' ] == 'Hello, !'
68
+ assert config .enabled is True
69
+ assert config .tracker .version_key == 'abcd'
70
+
71
+ def test_context_interpolation (ldai_client : LDAIClient ):
72
+ context = Context .builder ('user-key' ).name ("Sandy" ).build ()
73
+ default_value = AIConfig (config = {}, tracker = None , enabled = True )
74
+ variables = {'name' : 'World' }
75
+
76
+ config = ldai_client .model_config ('ctx-interpolation' , context , default_value , variables )
77
+
78
+ assert config .config ['prompt' ][0 ]['content' ] == 'Hello, Sandy!'
79
+ assert config .enabled is True
80
+ assert config .tracker .version_key == 'abcd'
81
+
82
+ def test_model_config_disabled (ldai_client : LDAIClient ):
83
+ context = Context .create ('user-key' )
84
+ default_value = AIConfig (config = {}, tracker = None , enabled = True )
85
+
86
+ config = ldai_client .model_config ('off-config' , context , default_value , {})
87
+
88
+ assert config .enabled is False
89
+ assert config .tracker .version_key == 'abcd'
90
+
91
+ def test_model_config_multiple (ldai_client : LDAIClient ):
92
+ context = Context .create ('user-key' )
93
+ default_value = AIConfig (config = {}, tracker = None , enabled = True )
94
+ variables = {'name' : 'World' , 'day' : 'Monday' }
95
+
96
+ config = ldai_client .model_config ('multiple-prompt' , context , default_value , variables )
97
+
98
+ assert config .config ['prompt' ][0 ]['content' ] == 'Hello, World!'
99
+ assert config .config ['prompt' ][1 ]['content' ] == 'The day is, Monday!'
100
+ assert config .enabled is True
101
+ assert config .tracker .version_key == 'abcd'
0 commit comments