@@ -64,40 +64,45 @@ def set_prompt_attributes(span, message_history):
64
64
if not message_history :
65
65
return
66
66
67
- for msg in message_history :
67
+ for i , msg in enumerate ( message_history ) :
68
68
if isinstance (msg , dict ):
69
-
70
69
role = msg .get ("role" , "user" )
71
70
content = msg .get ("content" , "" )
72
-
73
- span .set_attribute (f"{ SpanAttributes .LLM_PROMPTS } .role" , role )
74
- span . set_attribute ( f" { SpanAttributes . LLM_PROMPTS } .content" , content )
71
+ span . set_attribute ( f" { SpanAttributes . LLM_PROMPTS } . { i } .role" , role )
72
+ span .set_attribute (f"{ SpanAttributes .LLM_PROMPTS } .{ i } .content" ,
73
+ content )
75
74
76
75
77
76
def set_response_content_span_attribute (response , span ):
78
-
79
77
if hasattr (response , "output" ) and isinstance (response .output , list ):
78
+ roles = []
79
+ types = []
80
+ contents = []
81
+
80
82
for output_message in response .output :
81
- # Extract role and type from output_message
82
83
role = getattr (output_message , "role" , None )
83
84
msg_type = getattr (output_message , "type" , None )
84
85
85
86
if role :
86
- span .set_attribute (
87
- f"{ SpanAttributes .LLM_COMPLETIONS } .role" , role )
87
+ roles .append (role )
88
88
if msg_type :
89
- span .set_attribute (
90
- f"{ SpanAttributes .LLM_COMPLETIONS } .type" , msg_type )
89
+ types .append (msg_type )
91
90
92
- if hasattr (output_message , "content" ) and isinstance (
93
- output_message .content , list
94
- ):
91
+ if hasattr (output_message , "content" ) and \
92
+ isinstance (output_message .content , list ):
95
93
for content_item in output_message .content :
96
94
if hasattr (content_item , "text" ):
97
- span .set_attribute (
98
- f"{ SpanAttributes .LLM_COMPLETIONS } .content" ,
99
- content_item .text ,
100
- )
95
+ contents .append (content_item .text )
96
+
97
+ if roles :
98
+ span .set_attribute (f"{ SpanAttributes .LLM_COMPLETIONS } .roles" ,
99
+ roles )
100
+ if types :
101
+ span .set_attribute (f"{ SpanAttributes .LLM_COMPLETIONS } .types" ,
102
+ types )
103
+ if contents :
104
+ span .set_attribute (f"{ SpanAttributes .LLM_COMPLETIONS } .contents" ,
105
+ contents )
101
106
102
107
103
108
def set_token_usage_span_attributes (response , span ):
0 commit comments