1
1
import pytest
2
2
from ldclient import LDClient , Context , Config
3
3
from ldclient .integrations .test_data import TestData
4
- from ldai .types import AIConfig
4
+ from ldai .types import AIConfig , AIConfigData , LDMessage
5
5
from ldai .client import LDAIClient
6
+ from ldai .tracker import LDAIConfigTracker
6
7
from ldclient .testing .builders import *
7
8
9
+
10
+
8
11
@pytest .fixture
9
12
def td () -> TestData :
10
13
td = TestData .data_source ()
@@ -45,57 +48,56 @@ def ldai_client(client: LDClient) -> LDAIClient:
45
48
46
49
def test_model_config_interpolation (ldai_client : LDAIClient ):
47
50
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 )
51
+ default_value = AIConfig (config = AIConfigData (model = { 'modelId' : 'fakeModel' }, prompt = [LDMessage (role = 'system' , content = 'Hello, {{name}}!' )], _ldMeta = {'enabled' : True , 'versionKey' : 'abcd' }), tracker = LDAIConfigTracker (), enabled = True )
53
52
variables = {'name' : 'World' }
54
53
55
54
config = ldai_client .model_config ('model-config' , context , default_value , variables )
56
-
57
- assert config .config ['prompt' ][0 ]['content' ] == 'Hello, World!'
55
+
56
+ assert config .config .prompt is not None
57
+ assert len (config .config .prompt ) > 0
58
+ assert config .config .prompt [0 ].content == 'Hello, World!'
58
59
assert config .enabled is True
59
- assert config .tracker .version_key == 'abcd'
60
60
61
61
def test_model_config_no_variables (ldai_client : LDAIClient ):
62
62
context = Context .create ('user-key' )
63
- default_value = AIConfig (config = {}, tracker = None , enabled = True )
63
+ default_value = AIConfig (config = AIConfigData ( model = {}, prompt = [], _ldMeta = { 'enabled' : True , 'versionKey' : 'abcd' }), tracker = LDAIConfigTracker () , enabled = True )
64
64
65
65
config = ldai_client .model_config ('model-config' , context , default_value , {})
66
66
67
- assert config .config ['prompt' ][0 ]['content' ] == 'Hello, !'
67
+ assert config .config .prompt is not None
68
+ assert len (config .config .prompt ) > 0
69
+ assert config .config .prompt [0 ].content == 'Hello, !'
68
70
assert config .enabled is True
69
- assert config .tracker .version_key == 'abcd'
70
71
71
72
def test_context_interpolation (ldai_client : LDAIClient ):
72
73
context = Context .builder ('user-key' ).name ("Sandy" ).build ()
73
- default_value = AIConfig (config = {}, tracker = None , enabled = True )
74
+ default_value = AIConfig (config = AIConfigData ( model = {}, prompt = [], _ldMeta = { 'enabled' : True , 'versionKey' : 'abcd' }), tracker = LDAIConfigTracker () , enabled = True )
74
75
variables = {'name' : 'World' }
75
76
76
77
config = ldai_client .model_config ('ctx-interpolation' , context , default_value , variables )
77
78
78
- assert config .config ['prompt' ][0 ]['content' ] == 'Hello, Sandy!'
79
+ assert config .config .prompt is not None
80
+ assert len (config .config .prompt ) > 0
81
+ assert config .config .prompt [0 ].content == 'Hello, Sandy!'
79
82
assert config .enabled is True
80
- assert config .tracker .version_key == 'abcd'
81
-
83
+
82
84
def test_model_config_disabled (ldai_client : LDAIClient ):
83
85
context = Context .create ('user-key' )
84
- default_value = AIConfig (config = {}, tracker = None , enabled = True )
86
+ default_value = AIConfig (config = AIConfigData ( model = {}, prompt = [], _ldMeta = { 'enabled' : True , 'versionKey' : 'abcd' }), tracker = LDAIConfigTracker () , enabled = True )
85
87
86
88
config = ldai_client .model_config ('off-config' , context , default_value , {})
87
89
88
90
assert config .enabled is False
89
- assert config .tracker .version_key == 'abcd'
90
91
91
92
def test_model_config_multiple (ldai_client : LDAIClient ):
92
93
context = Context .create ('user-key' )
93
- default_value = AIConfig (config = {}, tracker = None , enabled = True )
94
+ default_value = AIConfig (config = AIConfigData ( model = {}, prompt = [], _ldMeta = { 'enabled' : True , 'versionKey' : 'abcd' }), tracker = LDAIConfigTracker () , enabled = True )
94
95
variables = {'name' : 'World' , 'day' : 'Monday' }
95
96
96
97
config = ldai_client .model_config ('multiple-prompt' , context , default_value , variables )
97
98
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'
99
+ assert config .config .prompt is not None
100
+ assert len (config .config .prompt ) > 0
101
+ assert config .config .prompt [0 ].content == 'Hello, World!'
102
+ assert config .config .prompt [1 ].content == 'The day is, Monday!'
103
+ assert config .enabled is True
0 commit comments