@@ -18,7 +18,7 @@ def td() -> TestData:
18
18
'model' : {'name' : 'fakeModel' , 'parameters' : {'temperature' : 0.5 , 'maxTokens' : 4096 }, 'custom' : {'extra-attribute' : 'value' }},
19
19
'provider' : {'name' : 'fakeProvider' },
20
20
'messages' : [{'role' : 'system' , 'content' : 'Hello, {{name}}!' }],
21
- '_ldMeta' : {'enabled' : True , 'variationKey' : 'abcd' },
21
+ '_ldMeta' : {'enabled' : True , 'variationKey' : 'abcd' , 'version' : 1 },
22
22
},
23
23
"green" ,
24
24
)
@@ -38,7 +38,7 @@ def client(td: TestData) -> LDClient:
38
38
39
39
def test_summary_starts_empty (client : LDClient ):
40
40
context = Context .create ('user-key' )
41
- tracker = LDAIConfigTracker (client , "variation-key" , "config-key" , context )
41
+ tracker = LDAIConfigTracker (client , "variation-key" , "config-key" , 1 , context )
42
42
43
43
assert tracker .get_summary ().duration is None
44
44
assert tracker .get_summary ().feedback is None
@@ -48,13 +48,13 @@ def test_summary_starts_empty(client: LDClient):
48
48
49
49
def test_tracks_duration (client : LDClient ):
50
50
context = Context .create ('user-key' )
51
- tracker = LDAIConfigTracker (client , "variation-key" , "config-key" , context )
51
+ tracker = LDAIConfigTracker (client , "variation-key" , "config-key" , 3 , context )
52
52
tracker .track_duration (100 )
53
53
54
54
client .track .assert_called_with ( # type: ignore
55
55
'$ld:ai:duration:total' ,
56
56
context ,
57
- {'variationKey' : 'variation-key' , 'configKey' : 'config-key' },
57
+ {'variationKey' : 'variation-key' , 'configKey' : 'config-key' , 'version' : 3 },
58
58
100
59
59
)
60
60
@@ -63,27 +63,27 @@ def test_tracks_duration(client: LDClient):
63
63
64
64
def test_tracks_duration_of (client : LDClient ):
65
65
context = Context .create ('user-key' )
66
- tracker = LDAIConfigTracker (client , "variation-key" , "config-key" , context )
66
+ tracker = LDAIConfigTracker (client , "variation-key" , "config-key" , 3 , context )
67
67
tracker .track_duration_of (lambda : sleep (0.01 ))
68
68
69
69
calls = client .track .mock_calls # type: ignore
70
70
71
71
assert len (calls ) == 1
72
72
assert calls [0 ].args [0 ] == '$ld:ai:duration:total'
73
73
assert calls [0 ].args [1 ] == context
74
- assert calls [0 ].args [2 ] == {'variationKey' : 'variation-key' , 'configKey' : 'config-key' }
74
+ assert calls [0 ].args [2 ] == {'variationKey' : 'variation-key' , 'configKey' : 'config-key' , 'version' : 3 }
75
75
assert calls [0 ].args [3 ] == pytest .approx (10 , rel = 10 )
76
76
77
77
78
78
def test_tracks_time_to_first_token (client : LDClient ):
79
79
context = Context .create ('user-key' )
80
- tracker = LDAIConfigTracker (client , "variation-key" , "config-key" , context )
80
+ tracker = LDAIConfigTracker (client , "variation-key" , "config-key" , 3 , context )
81
81
tracker .track_time_to_first_token (100 )
82
82
83
83
client .track .assert_called_with ( # type: ignore
84
84
'$ld:ai:tokens:ttf' ,
85
85
context ,
86
- {'variationKey' : 'variation-key' , 'configKey' : 'config-key' },
86
+ {'variationKey' : 'variation-key' , 'configKey' : 'config-key' , 'version' : 3 },
87
87
100
88
88
)
89
89
@@ -92,7 +92,7 @@ def test_tracks_time_to_first_token(client: LDClient):
92
92
93
93
def test_tracks_duration_of_with_exception (client : LDClient ):
94
94
context = Context .create ('user-key' )
95
- tracker = LDAIConfigTracker (client , "variation-key" , "config-key" , context )
95
+ tracker = LDAIConfigTracker (client , "variation-key" , "config-key" , 3 , context )
96
96
97
97
def sleep_and_throw ():
98
98
sleep (0.01 )
@@ -109,21 +109,21 @@ def sleep_and_throw():
109
109
assert len (calls ) == 1
110
110
assert calls [0 ].args [0 ] == '$ld:ai:duration:total'
111
111
assert calls [0 ].args [1 ] == context
112
- assert calls [0 ].args [2 ] == {'variationKey' : 'variation-key' , 'configKey' : 'config-key' }
112
+ assert calls [0 ].args [2 ] == {'variationKey' : 'variation-key' , 'configKey' : 'config-key' , 'version' : 3 }
113
113
assert calls [0 ].args [3 ] == pytest .approx (10 , rel = 10 )
114
114
115
115
116
116
def test_tracks_token_usage (client : LDClient ):
117
117
context = Context .create ('user-key' )
118
- tracker = LDAIConfigTracker (client , "variation-key" , "config-key" , context )
118
+ tracker = LDAIConfigTracker (client , "variation-key" , "config-key" , 3 , context )
119
119
120
120
tokens = TokenUsage (300 , 200 , 100 )
121
121
tracker .track_tokens (tokens )
122
122
123
123
calls = [
124
- call ('$ld:ai:tokens:total' , context , {'variationKey' : 'variation-key' , 'configKey' : 'config-key' }, 300 ),
125
- call ('$ld:ai:tokens:input' , context , {'variationKey' : 'variation-key' , 'configKey' : 'config-key' }, 200 ),
126
- call ('$ld:ai:tokens:output' , context , {'variationKey' : 'variation-key' , 'configKey' : 'config-key' }, 100 ),
124
+ call ('$ld:ai:tokens:total' , context , {'variationKey' : 'variation-key' , 'configKey' : 'config-key' , 'version' : 3 }, 300 ),
125
+ call ('$ld:ai:tokens:input' , context , {'variationKey' : 'variation-key' , 'configKey' : 'config-key' , 'version' : 3 }, 200 ),
126
+ call ('$ld:ai:tokens:output' , context , {'variationKey' : 'variation-key' , 'configKey' : 'config-key' , 'version' : 3 }, 100 ),
127
127
]
128
128
129
129
client .track .assert_has_calls (calls ) # type: ignore
@@ -133,7 +133,7 @@ def test_tracks_token_usage(client: LDClient):
133
133
134
134
def test_tracks_bedrock_metrics (client : LDClient ):
135
135
context = Context .create ('user-key' )
136
- tracker = LDAIConfigTracker (client , "variation-key" , "config-key" , context )
136
+ tracker = LDAIConfigTracker (client , "variation-key" , "config-key" , 3 , context )
137
137
138
138
bedrock_result = {
139
139
'$metadata' : {'httpStatusCode' : 200 },
@@ -149,12 +149,12 @@ def test_tracks_bedrock_metrics(client: LDClient):
149
149
tracker .track_bedrock_converse_metrics (bedrock_result )
150
150
151
151
calls = [
152
- call ('$ld:ai:generation' , context , {'variationKey' : 'variation-key' , 'configKey' : 'config-key' }, 1 ),
153
- call ('$ld:ai:generation:success' , context , {'variationKey' : 'variation-key' , 'configKey' : 'config-key' }, 1 ),
154
- call ('$ld:ai:duration:total' , context , {'variationKey' : 'variation-key' , 'configKey' : 'config-key' }, 50 ),
155
- call ('$ld:ai:tokens:total' , context , {'variationKey' : 'variation-key' , 'configKey' : 'config-key' }, 330 ),
156
- call ('$ld:ai:tokens:input' , context , {'variationKey' : 'variation-key' , 'configKey' : 'config-key' }, 220 ),
157
- call ('$ld:ai:tokens:output' , context , {'variationKey' : 'variation-key' , 'configKey' : 'config-key' }, 110 ),
152
+ call ('$ld:ai:generation' , context , {'variationKey' : 'variation-key' , 'configKey' : 'config-key' , 'version' : 3 }, 1 ),
153
+ call ('$ld:ai:generation:success' , context , {'variationKey' : 'variation-key' , 'configKey' : 'config-key' , 'version' : 3 }, 1 ),
154
+ call ('$ld:ai:duration:total' , context , {'variationKey' : 'variation-key' , 'configKey' : 'config-key' , 'version' : 3 }, 50 ),
155
+ call ('$ld:ai:tokens:total' , context , {'variationKey' : 'variation-key' , 'configKey' : 'config-key' , 'version' : 3 }, 330 ),
156
+ call ('$ld:ai:tokens:input' , context , {'variationKey' : 'variation-key' , 'configKey' : 'config-key' , 'version' : 3 }, 220 ),
157
+ call ('$ld:ai:tokens:output' , context , {'variationKey' : 'variation-key' , 'configKey' : 'config-key' , 'version' : 3 }, 110 ),
158
158
]
159
159
160
160
client .track .assert_has_calls (calls ) # type: ignore
@@ -166,7 +166,7 @@ def test_tracks_bedrock_metrics(client: LDClient):
166
166
167
167
def test_tracks_bedrock_metrics_with_error (client : LDClient ):
168
168
context = Context .create ('user-key' )
169
- tracker = LDAIConfigTracker (client , "variation-key" , "config-key" , context )
169
+ tracker = LDAIConfigTracker (client , "variation-key" , "config-key" , 3 , context )
170
170
171
171
bedrock_result = {
172
172
'$metadata' : {'httpStatusCode' : 500 },
@@ -182,12 +182,12 @@ def test_tracks_bedrock_metrics_with_error(client: LDClient):
182
182
tracker .track_bedrock_converse_metrics (bedrock_result )
183
183
184
184
calls = [
185
- call ('$ld:ai:generation' , context , {'variationKey' : 'variation-key' , 'configKey' : 'config-key' }, 1 ),
186
- call ('$ld:ai:generation:error' , context , {'variationKey' : 'variation-key' , 'configKey' : 'config-key' }, 1 ),
187
- call ('$ld:ai:duration:total' , context , {'variationKey' : 'variation-key' , 'configKey' : 'config-key' }, 50 ),
188
- call ('$ld:ai:tokens:total' , context , {'variationKey' : 'variation-key' , 'configKey' : 'config-key' }, 330 ),
189
- call ('$ld:ai:tokens:input' , context , {'variationKey' : 'variation-key' , 'configKey' : 'config-key' }, 220 ),
190
- call ('$ld:ai:tokens:output' , context , {'variationKey' : 'variation-key' , 'configKey' : 'config-key' }, 110 ),
185
+ call ('$ld:ai:generation' , context , {'variationKey' : 'variation-key' , 'configKey' : 'config-key' , 'version' : 3 }, 1 ),
186
+ call ('$ld:ai:generation:error' , context , {'variationKey' : 'variation-key' , 'configKey' : 'config-key' , 'version' : 3 }, 1 ),
187
+ call ('$ld:ai:duration:total' , context , {'variationKey' : 'variation-key' , 'configKey' : 'config-key' , 'version' : 3 }, 50 ),
188
+ call ('$ld:ai:tokens:total' , context , {'variationKey' : 'variation-key' , 'configKey' : 'config-key' , 'version' : 3 }, 330 ),
189
+ call ('$ld:ai:tokens:input' , context , {'variationKey' : 'variation-key' , 'configKey' : 'config-key' , 'version' : 3 }, 220 ),
190
+ call ('$ld:ai:tokens:output' , context , {'variationKey' : 'variation-key' , 'configKey' : 'config-key' , 'version' : 3 }, 110 ),
191
191
]
192
192
193
193
client .track .assert_has_calls (calls ) # type: ignore
@@ -199,7 +199,7 @@ def test_tracks_bedrock_metrics_with_error(client: LDClient):
199
199
200
200
def test_tracks_openai_metrics (client : LDClient ):
201
201
context = Context .create ('user-key' )
202
- tracker = LDAIConfigTracker (client , "variation-key" , "config-key" , context )
202
+ tracker = LDAIConfigTracker (client , "variation-key" , "config-key" , 3 , context )
203
203
204
204
class Result :
205
205
def __init__ (self ):
@@ -216,11 +216,11 @@ def to_dict(self):
216
216
tracker .track_openai_metrics (lambda : Result ())
217
217
218
218
calls = [
219
- call ('$ld:ai:generation' , context , {'variationKey' : 'variation-key' , 'configKey' : 'config-key' }, 1 ),
220
- call ('$ld:ai:generation:success' , context , {'variationKey' : 'variation-key' , 'configKey' : 'config-key' }, 1 ),
221
- call ('$ld:ai:tokens:total' , context , {'variationKey' : 'variation-key' , 'configKey' : 'config-key' }, 330 ),
222
- call ('$ld:ai:tokens:input' , context , {'variationKey' : 'variation-key' , 'configKey' : 'config-key' }, 220 ),
223
- call ('$ld:ai:tokens:output' , context , {'variationKey' : 'variation-key' , 'configKey' : 'config-key' }, 110 ),
219
+ call ('$ld:ai:generation' , context , {'variationKey' : 'variation-key' , 'configKey' : 'config-key' , 'version' : 3 }, 1 ),
220
+ call ('$ld:ai:generation:success' , context , {'variationKey' : 'variation-key' , 'configKey' : 'config-key' , 'version' : 3 }, 1 ),
221
+ call ('$ld:ai:tokens:total' , context , {'variationKey' : 'variation-key' , 'configKey' : 'config-key' , 'version' : 3 }, 330 ),
222
+ call ('$ld:ai:tokens:input' , context , {'variationKey' : 'variation-key' , 'configKey' : 'config-key' , 'version' : 3 }, 220 ),
223
+ call ('$ld:ai:tokens:output' , context , {'variationKey' : 'variation-key' , 'configKey' : 'config-key' , 'version' : 3 }, 110 ),
224
224
]
225
225
226
226
client .track .assert_has_calls (calls , any_order = False ) # type: ignore
@@ -230,7 +230,7 @@ def to_dict(self):
230
230
231
231
def test_tracks_openai_metrics_with_exception (client : LDClient ):
232
232
context = Context .create ('user-key' )
233
- tracker = LDAIConfigTracker (client , "variation-key" , "config-key" , context )
233
+ tracker = LDAIConfigTracker (client , "variation-key" , "config-key" , 3 , context )
234
234
235
235
def raise_exception ():
236
236
raise ValueError ("Something went wrong" )
@@ -242,8 +242,8 @@ def raise_exception():
242
242
pass
243
243
244
244
calls = [
245
- call ('$ld:ai:generation' , context , {'variationKey' : 'variation-key' , 'configKey' : 'config-key' }, 1 ),
246
- call ('$ld:ai:generation:error' , context , {'variationKey' : 'variation-key' , 'configKey' : 'config-key' }, 1 ),
245
+ call ('$ld:ai:generation' , context , {'variationKey' : 'variation-key' , 'configKey' : 'config-key' , 'version' : 3 }, 1 ),
246
+ call ('$ld:ai:generation:error' , context , {'variationKey' : 'variation-key' , 'configKey' : 'config-key' , 'version' : 3 }, 1 ),
247
247
]
248
248
249
249
client .track .assert_has_calls (calls , any_order = False ) # type: ignore
@@ -260,27 +260,27 @@ def raise_exception():
260
260
)
261
261
def test_tracks_feedback (client : LDClient , kind : FeedbackKind , label : str ):
262
262
context = Context .create ('user-key' )
263
- tracker = LDAIConfigTracker (client , "variation-key" , "config-key" , context )
263
+ tracker = LDAIConfigTracker (client , "variation-key" , "config-key" , 3 , context )
264
264
265
265
tracker .track_feedback ({'kind' : kind })
266
266
267
267
client .track .assert_called_with ( # type: ignore
268
268
f'$ld:ai:feedback:user:{ label } ' ,
269
269
context ,
270
- {'variationKey' : 'variation-key' , 'configKey' : 'config-key' },
270
+ {'variationKey' : 'variation-key' , 'configKey' : 'config-key' , 'version' : 3 },
271
271
1
272
272
)
273
273
assert tracker .get_summary ().feedback == {'kind' : kind }
274
274
275
275
276
276
def test_tracks_success (client : LDClient ):
277
277
context = Context .create ('user-key' )
278
- tracker = LDAIConfigTracker (client , "variation-key" , "config-key" , context )
278
+ tracker = LDAIConfigTracker (client , "variation-key" , "config-key" , 3 , context )
279
279
tracker .track_success ()
280
280
281
281
calls = [
282
- call ('$ld:ai:generation' , context , {'variationKey' : 'variation-key' , 'configKey' : 'config-key' }, 1 ),
283
- call ('$ld:ai:generation:success' , context , {'variationKey' : 'variation-key' , 'configKey' : 'config-key' }, 1 ),
282
+ call ('$ld:ai:generation' , context , {'variationKey' : 'variation-key' , 'configKey' : 'config-key' , 'version' : 3 }, 1 ),
283
+ call ('$ld:ai:generation:success' , context , {'variationKey' : 'variation-key' , 'configKey' : 'config-key' , 'version' : 3 }, 1 ),
284
284
]
285
285
286
286
client .track .assert_has_calls (calls ) # type: ignore
@@ -290,12 +290,12 @@ def test_tracks_success(client: LDClient):
290
290
291
291
def test_tracks_error (client : LDClient ):
292
292
context = Context .create ('user-key' )
293
- tracker = LDAIConfigTracker (client , "variation-key" , "config-key" , context )
293
+ tracker = LDAIConfigTracker (client , "variation-key" , "config-key" , 3 , context )
294
294
tracker .track_error ()
295
295
296
296
calls = [
297
- call ('$ld:ai:generation' , context , {'variationKey' : 'variation-key' , 'configKey' : 'config-key' }, 1 ),
298
- call ('$ld:ai:generation:error' , context , {'variationKey' : 'variation-key' , 'configKey' : 'config-key' }, 1 ),
297
+ call ('$ld:ai:generation' , context , {'variationKey' : 'variation-key' , 'configKey' : 'config-key' , 'version' : 3 }, 1 ),
298
+ call ('$ld:ai:generation:error' , context , {'variationKey' : 'variation-key' , 'configKey' : 'config-key' , 'version' : 3 }, 1 ),
299
299
]
300
300
301
301
client .track .assert_has_calls (calls ) # type: ignore
@@ -305,15 +305,15 @@ def test_tracks_error(client: LDClient):
305
305
306
306
def test_error_overwrites_success (client : LDClient ):
307
307
context = Context .create ('user-key' )
308
- tracker = LDAIConfigTracker (client , "variation-key" , "config-key" , context )
308
+ tracker = LDAIConfigTracker (client , "variation-key" , "config-key" , 3 , context )
309
309
tracker .track_success ()
310
310
tracker .track_error ()
311
311
312
312
calls = [
313
- call ('$ld:ai:generation' , context , {'variationKey' : 'variation-key' , 'configKey' : 'config-key' }, 1 ),
314
- call ('$ld:ai:generation:success' , context , {'variationKey' : 'variation-key' , 'configKey' : 'config-key' }, 1 ),
315
- call ('$ld:ai:generation' , context , {'variationKey' : 'variation-key' , 'configKey' : 'config-key' }, 1 ),
316
- call ('$ld:ai:generation:error' , context , {'variationKey' : 'variation-key' , 'configKey' : 'config-key' }, 1 ),
313
+ call ('$ld:ai:generation' , context , {'variationKey' : 'variation-key' , 'configKey' : 'config-key' , 'version' : 3 }, 1 ),
314
+ call ('$ld:ai:generation:success' , context , {'variationKey' : 'variation-key' , 'configKey' : 'config-key' , 'version' : 3 }, 1 ),
315
+ call ('$ld:ai:generation' , context , {'variationKey' : 'variation-key' , 'configKey' : 'config-key' , 'version' : 3 }, 1 ),
316
+ call ('$ld:ai:generation:error' , context , {'variationKey' : 'variation-key' , 'configKey' : 'config-key' , 'version' : 3 }, 1 ),
317
317
]
318
318
319
319
client .track .assert_has_calls (calls ) # type: ignore
0 commit comments