|
35 | 35 | AgentCreation, |
36 | 36 | AgentInvocation, |
37 | 37 | Error, |
38 | | - GenAIInvocation, |
39 | 38 | InputMessage, |
40 | 39 | LLMInvocation, |
41 | 40 | MessagePart, |
@@ -72,17 +71,9 @@ def _get_llm_common_attributes( |
72 | 71 | } |
73 | 72 |
|
74 | 73 |
|
75 | | -def _get_span_name( |
76 | | - invocation: GenAIInvocation, |
77 | | -) -> str: |
78 | | - """Get the span name for a GenAI invocation.""" |
79 | | - request_model = getattr(invocation, "request_model", None) or "" |
80 | | - return f"{invocation.operation_name} {request_model}".strip() |
81 | | - |
82 | | - |
83 | 74 | def _get_llm_span_name(invocation: LLMInvocation) -> str: |
84 | 75 | """Get the span name for an LLM invocation.""" |
85 | | - return _get_span_name(invocation) |
| 76 | + return f"{invocation.operation_name} {invocation.request_model}".strip() |
86 | 77 |
|
87 | 78 |
|
88 | 79 | def _get_llm_messages_attributes_for_span( |
@@ -266,8 +257,18 @@ def _get_request_attributes( |
266 | 257 | def _get_llm_request_attributes( |
267 | 258 | invocation: LLMInvocation, |
268 | 259 | ) -> dict[str, Any]: |
269 | | - """Get GenAI request semantic convention attributes for LLM invocations.""" |
270 | | - return _get_request_attributes(invocation) |
| 260 | + """Get GenAI request semantic convention attributes.""" |
| 261 | + optional_attrs = ( |
| 262 | + (GenAI.GEN_AI_REQUEST_TEMPERATURE, invocation.temperature), |
| 263 | + (GenAI.GEN_AI_REQUEST_TOP_P, invocation.top_p), |
| 264 | + (GenAI.GEN_AI_REQUEST_FREQUENCY_PENALTY, invocation.frequency_penalty), |
| 265 | + (GenAI.GEN_AI_REQUEST_PRESENCE_PENALTY, invocation.presence_penalty), |
| 266 | + (GenAI.GEN_AI_REQUEST_MAX_TOKENS, invocation.max_tokens), |
| 267 | + (GenAI.GEN_AI_REQUEST_STOP_SEQUENCES, invocation.stop_sequences), |
| 268 | + (GenAI.GEN_AI_REQUEST_SEED, invocation.seed), |
| 269 | + ) |
| 270 | + |
| 271 | + return {key: value for key, value in optional_attrs if value is not None} |
271 | 272 |
|
272 | 273 |
|
273 | 274 | def _get_response_attributes( |
@@ -305,8 +306,36 @@ def _get_response_attributes( |
305 | 306 | def _get_llm_response_attributes( |
306 | 307 | invocation: LLMInvocation, |
307 | 308 | ) -> dict[str, Any]: |
308 | | - """Get GenAI response semantic convention attributes for LLM invocations.""" |
309 | | - return _get_response_attributes(invocation) |
| 309 | + """Get GenAI response semantic convention attributes.""" |
| 310 | + finish_reasons: list[str] | None |
| 311 | + if invocation.finish_reasons is not None: |
| 312 | + finish_reasons = invocation.finish_reasons |
| 313 | + elif invocation.output_messages: |
| 314 | + finish_reasons = [ |
| 315 | + message.finish_reason |
| 316 | + for message in invocation.output_messages |
| 317 | + if message.finish_reason |
| 318 | + ] |
| 319 | + else: |
| 320 | + finish_reasons = None |
| 321 | + |
| 322 | + # De-duplicate finish reasons |
| 323 | + unique_finish_reasons = ( |
| 324 | + sorted(set(finish_reasons)) if finish_reasons else None |
| 325 | + ) |
| 326 | + |
| 327 | + optional_attrs = ( |
| 328 | + ( |
| 329 | + GenAI.GEN_AI_RESPONSE_FINISH_REASONS, |
| 330 | + unique_finish_reasons if unique_finish_reasons else None, |
| 331 | + ), |
| 332 | + (GenAI.GEN_AI_RESPONSE_MODEL, invocation.response_model_name), |
| 333 | + (GenAI.GEN_AI_RESPONSE_ID, invocation.response_id), |
| 334 | + (GenAI.GEN_AI_USAGE_INPUT_TOKENS, invocation.input_tokens), |
| 335 | + (GenAI.GEN_AI_USAGE_OUTPUT_TOKENS, invocation.output_tokens), |
| 336 | + ) |
| 337 | + |
| 338 | + return {key: value for key, value in optional_attrs if value is not None} |
310 | 339 |
|
311 | 340 |
|
312 | 341 | def _get_base_agent_common_attributes( |
@@ -440,7 +469,6 @@ def _apply_creation_finish_attributes( |
440 | 469 | "_apply_llm_finish_attributes", |
441 | 470 | "_apply_error_attributes", |
442 | 471 | "_get_llm_common_attributes", |
443 | | - "_get_span_name", |
444 | 472 | "_get_request_attributes", |
445 | 473 | "_get_llm_request_attributes", |
446 | 474 | "_get_response_attributes", |
|
0 commit comments