@@ -47,12 +47,29 @@ class InputMessageModel(TypedDict):
4747 state : NotRequired [JSONContent ]
4848 questions : NotRequired [dict [str , Union [NoulModel , ChoiceModel , ScoreModel ]]]
4949
50+ class NoulEvaluationModel (TypedDict ):
51+ type : Literal ["noul" ]
52+ noul : float
53+
54+ class ChoiceEvaluationModel (TypedDict ):
55+ type : Literal ["choice" ]
56+ choice : str
57+ probabilities : dict [str , float ]
58+ confidence : float
59+
60+ class ScoreEvaluationModel (TypedDict ):
61+ type : Literal ["score" ]
62+ score : float
63+ probabilities : dict [int , float ]
64+ confidence : float
65+ legend : dict [int , Union [str , dict [str , Any ], list [Any ]]]
66+
5067
5168try :
52- from typesafe_sdk import Choice , Noul , Score
69+ from typesafe_sdk import Choice , ChoiceAnswer , Noul , NoulAnswer , Score , ScoreAnswer
5370 from typesafe_sdk ._core .client .aio .client import AsyncTypeSafeClient
5471 from typesafe_sdk ._core .client .sync .client import TypeSafeClient
55- from typesafe_sdk ._core .response_types import SystemOneResponse
72+ from typesafe_sdk ._core .response_types import Answer , SystemOneResponse
5673except ImportError :
5774 raise DidNotEnable ("typesafe-sdk not installed" )
5875
@@ -152,6 +169,42 @@ def _transform_questions(
152169 return transformed_questions
153170
154171
172+ def _transform_evaluation_answers (
173+ answers : "dict[str, Answer]" ,
174+ ) -> (
175+ "dict[str, Union[NoulEvaluationModel, ChoiceEvaluationModel, ScoreEvaluationModel]]"
176+ ):
177+ items : "dict[str, Union[NoulEvaluationModel, ChoiceEvaluationModel, ScoreEvaluationModel]]" = {}
178+ for name , answer in answers .items ():
179+ if isinstance (answer , NoulAnswer ):
180+ items [name ] = {
181+ "type" : "noul" ,
182+ "noul" : answer .noul ,
183+ }
184+ continue
185+
186+ if isinstance (answer , ChoiceAnswer ):
187+ items [name ] = {
188+ "type" : "choice" ,
189+ "choice" : answer .choice ,
190+ "probabilities" : answer .probabilities ,
191+ "confidence" : answer .confidence ,
192+ }
193+ continue
194+
195+ if isinstance (answer , ScoreAnswer ):
196+ items [name ] = {
197+ "type" : "score" ,
198+ "score" : answer .score ,
199+ "probabilities" : answer .probabilities ,
200+ "confidence" : answer .confidence ,
201+ "legend" : answer .legend ,
202+ }
203+ continue
204+
205+ return items
206+
207+
155208def _wrap_system_one (f : "Callable[..., Any]" ) -> "Callable[..., Any]" :
156209 @wraps (f )
157210 def wrap_system_one (self : "TypeSafeClient" , * args : "Any" , ** kwargs : "Any" ) -> "Any" :
@@ -231,6 +284,27 @@ def wrap_system_one(self: "TypeSafeClient", *args: "Any", **kwargs: "Any") -> "A
231284 SPANDATA .GEN_AI_USAGE_OUTPUT_TOKENS , response .usage .output_tokens
232285 )
233286
287+ if (
288+ has_data_collection_enabled (client .options )
289+ and client .options ["data_collection" ]["gen_ai" ]["outputs" ]
290+ ) or (
291+ not has_data_collection_enabled (client .options )
292+ and should_send_default_pii ()
293+ ):
294+ set_on_span (
295+ SPANDATA .GEN_AI_OUTPUT_MESSAGES ,
296+ json .dumps (
297+ [
298+ {
299+ "type" : "evaluation" ,
300+ "answers" : _transform_evaluation_answers (
301+ response .answers
302+ ),
303+ }
304+ ]
305+ ),
306+ )
307+
234308 return response
235309
236310 return wrap_system_one
@@ -317,6 +391,27 @@ async def wrap_system_one_async(
317391 SPANDATA .GEN_AI_USAGE_OUTPUT_TOKENS , response .usage .output_tokens
318392 )
319393
394+ if (
395+ has_data_collection_enabled (client .options )
396+ and client .options ["data_collection" ]["gen_ai" ]["outputs" ]
397+ ) or (
398+ not has_data_collection_enabled (client .options )
399+ and should_send_default_pii ()
400+ ):
401+ set_on_span (
402+ SPANDATA .GEN_AI_OUTPUT_MESSAGES ,
403+ json .dumps (
404+ [
405+ {
406+ "type" : "evaluation" ,
407+ "answers" : _transform_evaluation_answers (
408+ response .answers
409+ ),
410+ }
411+ ]
412+ ),
413+ )
414+
320415 return response
321416
322417 return wrap_system_one_async
0 commit comments