Skip to content

Commit 5a7d402

Browse files
committed
factor out body creation for gen ai events
1 parent f4e93d6 commit 5a7d402

File tree

1 file changed

+11
-33
lines changed
  • aws-opentelemetry-distro/src/amazon/opentelemetry/distro

1 file changed

+11
-33
lines changed

aws-opentelemetry-distro/src/amazon/opentelemetry/distro/llo_handler.py

Lines changed: 11 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -131,30 +131,32 @@ def _extract_gen_ai_prompt_events(self, span: ReadableSpan, attributes: Dict[str
131131
"original_attribute": key
132132
}
133133

134+
body = {
135+
"content": value,
136+
"role": role
137+
}
138+
134139
event = None
135140
if role == "system":
136141
event = self._get_gen_ai_system_message_event(
137142
span_ctx,
138143
prompt_timestamp,
139144
event_attributes,
140-
value,
141-
role
145+
body
142146
)
143147
elif role == "user":
144148
event = self._get_gen_ai_user_message_event(
145149
span_ctx,
146150
prompt_timestamp,
147151
event_attributes,
148-
value,
149-
role
152+
body
150153
)
151154
elif role == "assistant":
152155
event = self._get_gen_ai_assistant_message_event(
153156
span_ctx,
154157
prompt_timestamp,
155158
event_attributes,
156-
value,
157-
role,
159+
body
158160
)
159161
elif role in ["function", "unknown"]:
160162
# TODO: Need to define a custom event and emit
@@ -170,19 +172,11 @@ def _get_gen_ai_system_message_event(
170172
span_ctx,
171173
timestamp,
172174
event_attributes,
173-
content,
174-
role
175+
body
175176
):
176177
"""
177178
Create and return a `gen_ai.system.message` Event.
178179
"""
179-
body = {"content": content}
180-
181-
# According to OTel spec, this body field is only required if available and not equal to `system`.
182-
# ref: https://opentelemetry.io/docs/specs/semconv/gen-ai/gen-ai-events/#event-gen_aisystemmessage
183-
if role != "system":
184-
body["role"] = role
185-
186180
return Event(
187181
name="gen_ai.system.message",
188182
timestamp=timestamp,
@@ -198,19 +192,11 @@ def _get_gen_ai_user_message_event(
198192
span_ctx,
199193
timestamp,
200194
event_attributes,
201-
content,
202-
role
195+
body
203196
):
204197
"""
205198
Create and return a `gen_ai.user.message` Event.
206199
"""
207-
body = {"content": content}
208-
209-
# According to OTel spec, this body field is only required if available and not equal to `user`.
210-
# ref: https://opentelemetry.io/docs/specs/semconv/gen-ai/gen-ai-events/#event-gen_aiusermessage
211-
if role != "user":
212-
body["role"] = role
213-
214200
return Event(
215201
name="gen_ai.user.message",
216202
timestamp=timestamp,
@@ -226,8 +212,7 @@ def _get_gen_ai_assistant_message_event(
226212
span_ctx,
227213
timestamp,
228214
event_attributes,
229-
content,
230-
role,
215+
body
231216
):
232217
"""
233218
Create and return a `gen_ai.assistant.message` Event.
@@ -244,13 +229,6 @@ def _get_gen_ai_assistant_message_event(
244229
245230
ref: https://opentelemetry.io/docs/specs/semconv/gen-ai/gen-ai-events/#event-gen_aiassistantmessage
246231
"""
247-
body = {"content": content}
248-
249-
# According to the OTel spec, this body field is only required if available and not equal to `assistant`.
250-
# ref: https://opentelemetry.io/docs/specs/semconv/gen-ai/gen-ai-events/#event-gen_aiassistantmessage
251-
if role != "assistant":
252-
body["role"] = role
253-
254232
return Event(
255233
name="gen_ai.assistant.message",
256234
timestamp=timestamp,

0 commit comments

Comments
 (0)