11import pytest
2- from ldclient import LDClient , Context , Config
2+ from ldclient import Config , Context , LDClient
33from ldclient .integrations .test_data import TestData
4+ from ldclient .testing .builders import *
5+
46from ldai .client import AIConfig , AIConfigData , LDAIClient , LDMessage
57from ldai .tracker import LDAIConfigTracker
6- from ldclient .testing .builders import *
78
89
910@pytest .fixture
1011def td () -> TestData :
1112 td = TestData .data_source ()
12- td .update (td .flag ('model-config' ).variations ({
13- 'model' : { 'modelId' : 'fakeModel' },
14- 'prompt' : [{'role' : 'system' , 'content' : 'Hello, {{name}}!' }],
15- '_ldMeta' : {'enabled' : True , 'versionKey' : 'abcd' }
16- }, "green" ).variation_for_all (0 ))
17-
18- td .update (td .flag ('multiple-prompt' ).variations ({
19- 'model' : { 'modelId' : 'fakeModel' },
20- 'prompt' : [{'role' : 'system' , 'content' : 'Hello, {{name}}!' }, {'role' : 'user' , 'content' : 'The day is, {{day}}!' }],
21- '_ldMeta' : {'enabled' : True , 'versionKey' : 'abcd' }
22- }, "green" ).variation_for_all (0 ))
23-
24- td .update (td .flag ('ctx-interpolation' ).variations ({
25- 'model' : { 'modelId' : 'fakeModel' },
26- 'prompt' : [{'role' : 'system' , 'content' : 'Hello, {{ldctx.name}}!' }],
27- '_ldMeta' : {'enabled' : True , 'versionKey' : 'abcd' }
28- }).variation_for_all (0 ))
29-
30- td .update (td .flag ('off-config' ).variations ({
31- 'model' : { 'modelId' : 'fakeModel' },
32- 'prompt' : [{'role' : 'system' , 'content' : 'Hello, {{name}}!' }],
33- '_ldMeta' : {'enabled' : False , 'versionKey' : 'abcd' }
34- }).variation_for_all (0 ))
13+ td .update (
14+ td .flag ('model-config' )
15+ .variations (
16+ {
17+ 'model' : {'modelId' : 'fakeModel' },
18+ 'prompt' : [{'role' : 'system' , 'content' : 'Hello, {{name}}!' }],
19+ '_ldMeta' : {'enabled' : True , 'versionKey' : 'abcd' },
20+ },
21+ "green" ,
22+ )
23+ .variation_for_all (0 )
24+ )
25+
26+ td .update (
27+ td .flag ('multiple-prompt' )
28+ .variations (
29+ {
30+ 'model' : {'modelId' : 'fakeModel' },
31+ 'prompt' : [
32+ {'role' : 'system' , 'content' : 'Hello, {{name}}!' },
33+ {'role' : 'user' , 'content' : 'The day is, {{day}}!' },
34+ ],
35+ '_ldMeta' : {'enabled' : True , 'versionKey' : 'abcd' },
36+ },
37+ "green" ,
38+ )
39+ .variation_for_all (0 )
40+ )
41+
42+ td .update (
43+ td .flag ('ctx-interpolation' )
44+ .variations (
45+ {
46+ 'model' : {'modelId' : 'fakeModel' },
47+ 'prompt' : [{'role' : 'system' , 'content' : 'Hello, {{ldctx.name}}!' }],
48+ '_ldMeta' : {'enabled' : True , 'versionKey' : 'abcd' },
49+ }
50+ )
51+ .variation_for_all (0 )
52+ )
53+
54+ td .update (
55+ td .flag ('off-config' )
56+ .variations (
57+ {
58+ 'model' : {'modelId' : 'fakeModel' },
59+ 'prompt' : [{'role' : 'system' , 'content' : 'Hello, {{name}}!' }],
60+ '_ldMeta' : {'enabled' : False , 'versionKey' : 'abcd' },
61+ }
62+ )
63+ .variation_for_all (0 )
64+ )
3565
3666 return td
3767
68+
3869@pytest .fixture
3970def client (td : TestData ) -> LDClient :
4071 config = Config ('sdk-key' , update_processor_class = td , send_events = False )
4172 return LDClient (config = config )
4273
74+
4375@pytest .fixture
4476def tracker (client : LDClient ) -> LDAIConfigTracker :
4577 return LDAIConfigTracker (client , 'abcd' , 'model-config' , Context .create ('user-key' ))
4678
79+
4780@pytest .fixture
4881def ldai_client (client : LDClient ) -> LDAIClient :
4982 return LDAIClient (client )
5083
84+
5185def test_model_config_interpolation (ldai_client : LDAIClient , tracker ):
5286 context = Context .create ('user-key' )
53- default_value = AIConfig (config = AIConfigData (model = { 'modelId' : 'fakeModel' }, prompt = [LDMessage (role = 'system' , content = 'Hello, {{name}}!' )]), tracker = tracker , enabled = True )
87+ default_value = AIConfig (
88+ config = AIConfigData (
89+ model = {'modelId' : 'fakeModel' },
90+ prompt = [LDMessage (role = 'system' , content = 'Hello, {{name}}!' )],
91+ ),
92+ tracker = tracker ,
93+ enabled = True ,
94+ )
5495 variables = {'name' : 'World' }
5596
5697 config = ldai_client .model_config ('model-config' , context , default_value , variables )
@@ -60,9 +101,12 @@ def test_model_config_interpolation(ldai_client: LDAIClient, tracker):
60101 assert config .config .prompt [0 ].content == 'Hello, World!'
61102 assert config .enabled is True
62103
104+
63105def test_model_config_no_variables (ldai_client : LDAIClient , tracker ):
64106 context = Context .create ('user-key' )
65- default_value = AIConfig (config = AIConfigData (model = {}, prompt = []), tracker = tracker , enabled = True )
107+ default_value = AIConfig (
108+ config = AIConfigData (model = {}, prompt = []), tracker = tracker , enabled = True
109+ )
66110
67111 config = ldai_client .model_config ('model-config' , context , default_value , {})
68112
@@ -71,34 +115,47 @@ def test_model_config_no_variables(ldai_client: LDAIClient, tracker):
71115 assert config .config .prompt [0 ].content == 'Hello, !'
72116 assert config .enabled is True
73117
118+
74119def test_context_interpolation (ldai_client : LDAIClient , tracker ):
75120 context = Context .builder ('user-key' ).name ("Sandy" ).build ()
76- default_value = AIConfig (config = AIConfigData (model = {}, prompt = []), tracker = tracker , enabled = True )
121+ default_value = AIConfig (
122+ config = AIConfigData (model = {}, prompt = []), tracker = tracker , enabled = True
123+ )
77124 variables = {'name' : 'World' }
78125
79- config = ldai_client .model_config ('ctx-interpolation' , context , default_value , variables )
126+ config = ldai_client .model_config (
127+ 'ctx-interpolation' , context , default_value , variables
128+ )
80129
81130 assert config .config .prompt is not None
82131 assert len (config .config .prompt ) > 0
83132 assert config .config .prompt [0 ].content == 'Hello, Sandy!'
84133 assert config .enabled is True
85134
135+
86136def test_model_config_multiple (ldai_client : LDAIClient , tracker ):
87137 context = Context .create ('user-key' )
88- default_value = AIConfig (config = AIConfigData (model = {}, prompt = []), tracker = tracker , enabled = True )
138+ default_value = AIConfig (
139+ config = AIConfigData (model = {}, prompt = []), tracker = tracker , enabled = True
140+ )
89141 variables = {'name' : 'World' , 'day' : 'Monday' }
90142
91- config = ldai_client .model_config ('multiple-prompt' , context , default_value , variables )
143+ config = ldai_client .model_config (
144+ 'multiple-prompt' , context , default_value , variables
145+ )
92146
93147 assert config .config .prompt is not None
94148 assert len (config .config .prompt ) > 0
95149 assert config .config .prompt [0 ].content == 'Hello, World!'
96150 assert config .config .prompt [1 ].content == 'The day is, Monday!'
97151 assert config .enabled is True
98152
153+
99154def test_model_config_disabled (ldai_client : LDAIClient , tracker ):
100155 context = Context .create ('user-key' )
101- default_value = AIConfig (config = AIConfigData (model = {}, prompt = []), tracker = tracker , enabled = False )
156+ default_value = AIConfig (
157+ config = AIConfigData (model = {}, prompt = []), tracker = tracker , enabled = False
158+ )
102159
103160 config = ldai_client .model_config ('off-config' , context , default_value , {})
104161
0 commit comments