|
29 | 29 | get_llm_request_attributes, |
30 | 30 | get_llm_url, |
31 | 31 | get_langtrace_attributes, |
| 32 | + set_event_completion, |
32 | 33 | set_usage_attributes, |
33 | 34 | ) |
34 | 35 | from langtrace_python_sdk.constants.instrumentation.common import ( |
@@ -139,18 +140,8 @@ def traced_method(wrapped, instance, args, kwargs): |
139 | 140 | } |
140 | 141 | for choice in result.choices |
141 | 142 | ] |
142 | | - set_span_attribute( |
143 | | - span, |
144 | | - SpanAttributes.LLM_COMPLETIONS, |
145 | | - json.dumps(responses), |
146 | | - ) |
147 | | - else: |
148 | | - responses = [] |
149 | | - set_span_attribute( |
150 | | - span, |
151 | | - SpanAttributes.LLM_COMPLETIONS, |
152 | | - json.dumps(responses), |
153 | | - ) |
| 143 | + set_event_completion(span, responses) |
| 144 | + |
154 | 145 | if ( |
155 | 146 | hasattr(result, "system_fingerprint") |
156 | 147 | and result.system_fingerprint is not None |
@@ -270,11 +261,8 @@ def handle_streaming_response( |
270 | 261 | span, |
271 | 262 | {"input_tokens": prompt_tokens, "output_tokens": completion_tokens}, |
272 | 263 | ) |
273 | | - |
274 | | - set_span_attribute( |
275 | | - span, |
276 | | - SpanAttributes.LLM_COMPLETIONS, |
277 | | - json.dumps([{"role": "assistant", "content": "".join(result_content)}]), |
| 264 | + set_event_completion( |
| 265 | + span, [{"role": "assistant", "content": "".join(result_content)}] |
278 | 266 | ) |
279 | 267 |
|
280 | 268 | span.set_status(StatusCode.OK) |
@@ -379,18 +367,9 @@ async def traced_method(wrapped, instance, args, kwargs): |
379 | 367 | } |
380 | 368 | for choice in result.choices |
381 | 369 | ] |
382 | | - set_span_attribute( |
383 | | - span, |
384 | | - SpanAttributes.LLM_COMPLETIONS, |
385 | | - json.dumps(responses), |
386 | | - ) |
387 | | - else: |
388 | | - responses = [] |
389 | | - set_span_attribute( |
390 | | - span, |
391 | | - SpanAttributes.LLM_COMPLETIONS, |
392 | | - json.dumps(responses), |
393 | | - ) |
| 370 | + |
| 371 | + set_event_completion(span, responses) |
| 372 | + |
394 | 373 | if ( |
395 | 374 | hasattr(result, "system_fingerprint") |
396 | 375 | and result.system_fingerprint is not None |
@@ -515,17 +494,15 @@ async def ahandle_streaming_response( |
515 | 494 | span, |
516 | 495 | {"input_tokens": prompt_tokens, "output_tokens": completion_tokens}, |
517 | 496 | ) |
518 | | - set_span_attribute( |
| 497 | + |
| 498 | + set_event_completion( |
519 | 499 | span, |
520 | | - SpanAttributes.LLM_COMPLETIONS, |
521 | | - json.dumps( |
522 | | - [ |
523 | | - { |
524 | | - "role": "assistant", |
525 | | - "content": "".join(result_content), |
526 | | - } |
527 | | - ] |
528 | | - ), |
| 500 | + [ |
| 501 | + { |
| 502 | + "role": "assistant", |
| 503 | + "content": "".join(result_content), |
| 504 | + } |
| 505 | + ], |
529 | 506 | ) |
530 | 507 |
|
531 | 508 | span.set_status(StatusCode.OK) |
|
0 commit comments