judgeval 0.16.4__py3-none-any.whl → 0.16.6__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of judgeval might be problematic. Click here for more details.
- judgeval/__init__.py +7 -2
- judgeval/scorers/judgeval_scorers/api_scorers/prompt_scorer.py +15 -4
- judgeval/tracer/__init__.py +9 -1
- judgeval/tracer/llm/llm_anthropic/wrapper.py +160 -130
- judgeval/tracer/llm/llm_google/wrapper.py +137 -98
- judgeval/tracer/llm/llm_groq/wrapper.py +137 -116
- judgeval/tracer/llm/llm_openai/wrapper.py +130 -106
- judgeval/tracer/llm/llm_together/wrapper.py +145 -120
- judgeval/tracer/utils.py +1 -1
- judgeval/utils/decorators/dont_throw.py +1 -1
- judgeval/version.py +1 -1
- {judgeval-0.16.4.dist-info → judgeval-0.16.6.dist-info}/METADATA +2 -2
- {judgeval-0.16.4.dist-info → judgeval-0.16.6.dist-info}/RECORD +16 -16
- {judgeval-0.16.4.dist-info → judgeval-0.16.6.dist-info}/WHEEL +0 -0
- {judgeval-0.16.4.dist-info → judgeval-0.16.6.dist-info}/entry_points.txt +0 -0
- {judgeval-0.16.4.dist-info → judgeval-0.16.6.dist-info}/licenses/LICENSE.md +0 -0
|
@@ -18,6 +18,7 @@ from judgeval.tracer.llm.llm_google.config import (
|
|
|
18
18
|
google_genai_AsyncClient,
|
|
19
19
|
)
|
|
20
20
|
from judgeval.tracer.managers import sync_span_context, async_span_context
|
|
21
|
+
from judgeval.logger import judgeval_logger
|
|
21
22
|
from judgeval.tracer.keys import AttributeKeys
|
|
22
23
|
from judgeval.tracer.utils import set_span_attribute
|
|
23
24
|
from judgeval.utils.serialize import safe_serialize
|
|
@@ -263,23 +264,10 @@ def wrap_google_client(tracer: Tracer, client: GoogleClientType) -> GoogleClient
|
|
|
263
264
|
@functools.wraps(function)
|
|
264
265
|
def wrapper(*args, **kwargs):
|
|
265
266
|
if kwargs.get("stream", False):
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
set_span_attribute(
|
|
271
|
-
span, AttributeKeys.GEN_AI_PROMPT, safe_serialize(kwargs)
|
|
272
|
-
)
|
|
273
|
-
model_name = kwargs.get("model", "")
|
|
274
|
-
set_span_attribute(span, AttributeKeys.GEN_AI_REQUEST_MODEL, model_name)
|
|
275
|
-
stream_response = function(*args, **kwargs)
|
|
276
|
-
return TracedGoogleGenerator(
|
|
277
|
-
tracer, stream_response, client, span, model_name
|
|
278
|
-
)
|
|
279
|
-
else:
|
|
280
|
-
with sync_span_context(
|
|
281
|
-
tracer, span_name, {AttributeKeys.JUDGMENT_SPAN_KIND: "llm"}
|
|
282
|
-
) as span:
|
|
267
|
+
try:
|
|
268
|
+
span = tracer.get_tracer().start_span(
|
|
269
|
+
span_name, attributes={AttributeKeys.JUDGMENT_SPAN_KIND: "llm"}
|
|
270
|
+
)
|
|
283
271
|
tracer.add_agent_attributes_to_span(span)
|
|
284
272
|
set_span_attribute(
|
|
285
273
|
span, AttributeKeys.GEN_AI_PROMPT, safe_serialize(kwargs)
|
|
@@ -288,46 +276,78 @@ def wrap_google_client(tracer: Tracer, client: GoogleClientType) -> GoogleClient
|
|
|
288
276
|
set_span_attribute(
|
|
289
277
|
span, AttributeKeys.GEN_AI_REQUEST_MODEL, model_name
|
|
290
278
|
)
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
279
|
+
except Exception as e:
|
|
280
|
+
judgeval_logger.error(
|
|
281
|
+
f"[google wrapped] Error adding span metadata: {e}"
|
|
282
|
+
)
|
|
283
|
+
stream_response = function(*args, **kwargs)
|
|
284
|
+
return TracedGoogleGenerator(
|
|
285
|
+
tracer, stream_response, client, span, model_name
|
|
286
|
+
)
|
|
287
|
+
else:
|
|
288
|
+
with sync_span_context(
|
|
289
|
+
tracer, span_name, {AttributeKeys.JUDGMENT_SPAN_KIND: "llm"}
|
|
290
|
+
) as span:
|
|
291
|
+
try:
|
|
292
|
+
tracer.add_agent_attributes_to_span(span)
|
|
295
293
|
set_span_attribute(
|
|
296
|
-
span, AttributeKeys.
|
|
294
|
+
span, AttributeKeys.GEN_AI_PROMPT, safe_serialize(kwargs)
|
|
297
295
|
)
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
AttributeKeys.GEN_AI_USAGE_OUTPUT_TOKENS,
|
|
313
|
-
completion_tokens,
|
|
314
|
-
)
|
|
296
|
+
model_name = kwargs.get("model", "")
|
|
297
|
+
set_span_attribute(
|
|
298
|
+
span, AttributeKeys.GEN_AI_REQUEST_MODEL, model_name
|
|
299
|
+
)
|
|
300
|
+
except Exception as e:
|
|
301
|
+
judgeval_logger.error(
|
|
302
|
+
f"[google wrapped] Error adding span metadata: {e}"
|
|
303
|
+
)
|
|
304
|
+
|
|
305
|
+
response = function(*args, **kwargs)
|
|
306
|
+
|
|
307
|
+
try:
|
|
308
|
+
if isinstance(response, GoogleGenerateContentResponse):
|
|
309
|
+
output, usage_data = _format_google_output(response)
|
|
315
310
|
set_span_attribute(
|
|
316
|
-
span,
|
|
317
|
-
AttributeKeys.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS,
|
|
318
|
-
cache_read,
|
|
311
|
+
span, AttributeKeys.GEN_AI_COMPLETION, output
|
|
319
312
|
)
|
|
313
|
+
if usage_data:
|
|
314
|
+
(
|
|
315
|
+
prompt_tokens,
|
|
316
|
+
completion_tokens,
|
|
317
|
+
cache_read,
|
|
318
|
+
cache_creation,
|
|
319
|
+
) = _extract_google_tokens(usage_data)
|
|
320
|
+
set_span_attribute(
|
|
321
|
+
span,
|
|
322
|
+
AttributeKeys.GEN_AI_USAGE_INPUT_TOKENS,
|
|
323
|
+
prompt_tokens,
|
|
324
|
+
)
|
|
325
|
+
set_span_attribute(
|
|
326
|
+
span,
|
|
327
|
+
AttributeKeys.GEN_AI_USAGE_OUTPUT_TOKENS,
|
|
328
|
+
completion_tokens,
|
|
329
|
+
)
|
|
330
|
+
set_span_attribute(
|
|
331
|
+
span,
|
|
332
|
+
AttributeKeys.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS,
|
|
333
|
+
cache_read,
|
|
334
|
+
)
|
|
335
|
+
set_span_attribute(
|
|
336
|
+
span,
|
|
337
|
+
AttributeKeys.JUDGMENT_USAGE_METADATA,
|
|
338
|
+
safe_serialize(usage_data),
|
|
339
|
+
)
|
|
320
340
|
set_span_attribute(
|
|
321
341
|
span,
|
|
322
|
-
AttributeKeys.
|
|
323
|
-
|
|
342
|
+
AttributeKeys.GEN_AI_RESPONSE_MODEL,
|
|
343
|
+
getattr(response, "model_version", model_name),
|
|
324
344
|
)
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
getattr(response, "model_version", model_name),
|
|
345
|
+
except Exception as e:
|
|
346
|
+
judgeval_logger.error(
|
|
347
|
+
f"[google wrapped] Error adding span metadata: {e}"
|
|
329
348
|
)
|
|
330
|
-
|
|
349
|
+
finally:
|
|
350
|
+
return response
|
|
331
351
|
|
|
332
352
|
return wrapper
|
|
333
353
|
|
|
@@ -335,23 +355,10 @@ def wrap_google_client(tracer: Tracer, client: GoogleClientType) -> GoogleClient
|
|
|
335
355
|
@functools.wraps(function)
|
|
336
356
|
async def wrapper(*args, **kwargs):
|
|
337
357
|
if kwargs.get("stream", False):
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
set_span_attribute(
|
|
343
|
-
span, AttributeKeys.GEN_AI_PROMPT, safe_serialize(kwargs)
|
|
344
|
-
)
|
|
345
|
-
model_name = kwargs.get("model", "")
|
|
346
|
-
set_span_attribute(span, AttributeKeys.GEN_AI_REQUEST_MODEL, model_name)
|
|
347
|
-
stream_response = await function(*args, **kwargs)
|
|
348
|
-
return TracedGoogleAsyncGenerator(
|
|
349
|
-
tracer, stream_response, client, span, model_name
|
|
350
|
-
)
|
|
351
|
-
else:
|
|
352
|
-
async with async_span_context(
|
|
353
|
-
tracer, span_name, {AttributeKeys.JUDGMENT_SPAN_KIND: "llm"}
|
|
354
|
-
) as span:
|
|
358
|
+
try:
|
|
359
|
+
span = tracer.get_tracer().start_span(
|
|
360
|
+
span_name, attributes={AttributeKeys.JUDGMENT_SPAN_KIND: "llm"}
|
|
361
|
+
)
|
|
355
362
|
tracer.add_agent_attributes_to_span(span)
|
|
356
363
|
set_span_attribute(
|
|
357
364
|
span, AttributeKeys.GEN_AI_PROMPT, safe_serialize(kwargs)
|
|
@@ -360,46 +367,78 @@ def wrap_google_client(tracer: Tracer, client: GoogleClientType) -> GoogleClient
|
|
|
360
367
|
set_span_attribute(
|
|
361
368
|
span, AttributeKeys.GEN_AI_REQUEST_MODEL, model_name
|
|
362
369
|
)
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
370
|
+
except Exception as e:
|
|
371
|
+
judgeval_logger.error(
|
|
372
|
+
f"[google wrapped_async] Error adding span metadata: {e}"
|
|
373
|
+
)
|
|
374
|
+
stream_response = await function(*args, **kwargs)
|
|
375
|
+
return TracedGoogleAsyncGenerator(
|
|
376
|
+
tracer, stream_response, client, span, model_name
|
|
377
|
+
)
|
|
378
|
+
else:
|
|
379
|
+
async with async_span_context(
|
|
380
|
+
tracer, span_name, {AttributeKeys.JUDGMENT_SPAN_KIND: "llm"}
|
|
381
|
+
) as span:
|
|
382
|
+
try:
|
|
383
|
+
tracer.add_agent_attributes_to_span(span)
|
|
367
384
|
set_span_attribute(
|
|
368
|
-
span, AttributeKeys.
|
|
385
|
+
span, AttributeKeys.GEN_AI_PROMPT, safe_serialize(kwargs)
|
|
369
386
|
)
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
AttributeKeys.GEN_AI_USAGE_OUTPUT_TOKENS,
|
|
385
|
-
completion_tokens,
|
|
386
|
-
)
|
|
387
|
+
model_name = kwargs.get("model", "")
|
|
388
|
+
set_span_attribute(
|
|
389
|
+
span, AttributeKeys.GEN_AI_REQUEST_MODEL, model_name
|
|
390
|
+
)
|
|
391
|
+
except Exception as e:
|
|
392
|
+
judgeval_logger.error(
|
|
393
|
+
f"[google wrapped_async] Error adding span metadata: {e}"
|
|
394
|
+
)
|
|
395
|
+
|
|
396
|
+
response = await function(*args, **kwargs)
|
|
397
|
+
|
|
398
|
+
try:
|
|
399
|
+
if isinstance(response, GoogleGenerateContentResponse):
|
|
400
|
+
output, usage_data = _format_google_output(response)
|
|
387
401
|
set_span_attribute(
|
|
388
|
-
span,
|
|
389
|
-
AttributeKeys.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS,
|
|
390
|
-
cache_read,
|
|
402
|
+
span, AttributeKeys.GEN_AI_COMPLETION, output
|
|
391
403
|
)
|
|
404
|
+
if usage_data:
|
|
405
|
+
(
|
|
406
|
+
prompt_tokens,
|
|
407
|
+
completion_tokens,
|
|
408
|
+
cache_read,
|
|
409
|
+
cache_creation,
|
|
410
|
+
) = _extract_google_tokens(usage_data)
|
|
411
|
+
set_span_attribute(
|
|
412
|
+
span,
|
|
413
|
+
AttributeKeys.GEN_AI_USAGE_INPUT_TOKENS,
|
|
414
|
+
prompt_tokens,
|
|
415
|
+
)
|
|
416
|
+
set_span_attribute(
|
|
417
|
+
span,
|
|
418
|
+
AttributeKeys.GEN_AI_USAGE_OUTPUT_TOKENS,
|
|
419
|
+
completion_tokens,
|
|
420
|
+
)
|
|
421
|
+
set_span_attribute(
|
|
422
|
+
span,
|
|
423
|
+
AttributeKeys.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS,
|
|
424
|
+
cache_read,
|
|
425
|
+
)
|
|
426
|
+
set_span_attribute(
|
|
427
|
+
span,
|
|
428
|
+
AttributeKeys.JUDGMENT_USAGE_METADATA,
|
|
429
|
+
safe_serialize(usage_data),
|
|
430
|
+
)
|
|
392
431
|
set_span_attribute(
|
|
393
432
|
span,
|
|
394
|
-
AttributeKeys.
|
|
395
|
-
|
|
433
|
+
AttributeKeys.GEN_AI_RESPONSE_MODEL,
|
|
434
|
+
getattr(response, "model_version", model_name),
|
|
396
435
|
)
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
getattr(response, "model_version", model_name),
|
|
436
|
+
except Exception as e:
|
|
437
|
+
judgeval_logger.error(
|
|
438
|
+
f"[google wrapped_async] Error adding span metadata: {e}"
|
|
401
439
|
)
|
|
402
|
-
|
|
440
|
+
finally:
|
|
441
|
+
return response
|
|
403
442
|
|
|
404
443
|
return wrapper
|
|
405
444
|
|
|
@@ -19,6 +19,7 @@ from judgeval.tracer.llm.llm_groq.config import (
|
|
|
19
19
|
groq_AsyncGroq,
|
|
20
20
|
)
|
|
21
21
|
from judgeval.tracer.managers import sync_span_context, async_span_context
|
|
22
|
+
from judgeval.logger import judgeval_logger
|
|
22
23
|
from judgeval.tracer.keys import AttributeKeys
|
|
23
24
|
from judgeval.tracer.utils import set_span_attribute
|
|
24
25
|
from judgeval.utils.serialize import safe_serialize
|
|
@@ -295,71 +296,81 @@ def wrap_groq_client(tracer: Tracer, client: GroqClientType) -> GroqClientType:
|
|
|
295
296
|
with sync_span_context(
|
|
296
297
|
tracer, span_name, {AttributeKeys.JUDGMENT_SPAN_KIND: "llm"}
|
|
297
298
|
) as span:
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
299
|
+
try:
|
|
300
|
+
tracer.add_agent_attributes_to_span(span)
|
|
301
|
+
set_span_attribute(
|
|
302
|
+
span, AttributeKeys.GEN_AI_PROMPT, safe_serialize(kwargs)
|
|
303
|
+
)
|
|
304
|
+
model_name = kwargs.get("model", "")
|
|
305
|
+
# Add groq/ prefix for server-side cost calculation
|
|
306
|
+
prefixed_model_name = f"groq/{model_name}" if model_name else ""
|
|
307
|
+
set_span_attribute(
|
|
308
|
+
span,
|
|
309
|
+
AttributeKeys.GEN_AI_REQUEST_MODEL,
|
|
310
|
+
prefixed_model_name,
|
|
311
|
+
)
|
|
312
|
+
except Exception as e:
|
|
313
|
+
judgeval_logger.error(
|
|
314
|
+
f"[groq wrapped] Error adding span metadata: {e}"
|
|
315
|
+
)
|
|
316
|
+
|
|
308
317
|
response = function(*args, **kwargs)
|
|
309
318
|
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
if
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
319
|
+
try:
|
|
320
|
+
if isinstance(response, GroqChatCompletion):
|
|
321
|
+
output, usage_data = _format_groq_output(response)
|
|
322
|
+
# Serialize structured data to JSON for span attribute
|
|
323
|
+
if output:
|
|
324
|
+
if isinstance(output, list):
|
|
325
|
+
output_str = safe_serialize(output)
|
|
326
|
+
else:
|
|
327
|
+
output_str = str(output)
|
|
328
|
+
set_span_attribute(
|
|
329
|
+
span, AttributeKeys.GEN_AI_COMPLETION, output_str
|
|
330
|
+
)
|
|
331
|
+
if usage_data:
|
|
332
|
+
(
|
|
333
|
+
prompt_tokens,
|
|
334
|
+
completion_tokens,
|
|
335
|
+
cache_read,
|
|
336
|
+
cache_creation,
|
|
337
|
+
) = _extract_groq_tokens(usage_data)
|
|
338
|
+
set_span_attribute(
|
|
339
|
+
span,
|
|
340
|
+
AttributeKeys.GEN_AI_USAGE_INPUT_TOKENS,
|
|
341
|
+
prompt_tokens,
|
|
342
|
+
)
|
|
343
|
+
set_span_attribute(
|
|
344
|
+
span,
|
|
345
|
+
AttributeKeys.GEN_AI_USAGE_OUTPUT_TOKENS,
|
|
346
|
+
completion_tokens,
|
|
347
|
+
)
|
|
348
|
+
set_span_attribute(
|
|
349
|
+
span,
|
|
350
|
+
AttributeKeys.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS,
|
|
351
|
+
cache_read,
|
|
352
|
+
)
|
|
353
|
+
set_span_attribute(
|
|
354
|
+
span,
|
|
355
|
+
AttributeKeys.JUDGMENT_USAGE_METADATA,
|
|
356
|
+
safe_serialize(usage_data),
|
|
357
|
+
)
|
|
358
|
+
# Add groq/ prefix to response model for server-side cost calculation
|
|
359
|
+
response_model = getattr(response, "model", model_name)
|
|
360
|
+
prefixed_response_model = (
|
|
361
|
+
f"groq/{response_model}" if response_model else ""
|
|
346
362
|
)
|
|
347
363
|
set_span_attribute(
|
|
348
364
|
span,
|
|
349
|
-
AttributeKeys.
|
|
350
|
-
|
|
365
|
+
AttributeKeys.GEN_AI_RESPONSE_MODEL,
|
|
366
|
+
prefixed_response_model,
|
|
351
367
|
)
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
f"groq/{response_model}" if response_model else ""
|
|
356
|
-
)
|
|
357
|
-
set_span_attribute(
|
|
358
|
-
span,
|
|
359
|
-
AttributeKeys.GEN_AI_RESPONSE_MODEL,
|
|
360
|
-
prefixed_response_model,
|
|
368
|
+
except Exception as e:
|
|
369
|
+
judgeval_logger.error(
|
|
370
|
+
f"[groq wrapped] Error adding span metadata: {e}"
|
|
361
371
|
)
|
|
362
|
-
|
|
372
|
+
finally:
|
|
373
|
+
return response
|
|
363
374
|
|
|
364
375
|
return wrapper
|
|
365
376
|
|
|
@@ -388,71 +399,81 @@ def wrap_groq_client(tracer: Tracer, client: GroqClientType) -> GroqClientType:
|
|
|
388
399
|
async with async_span_context(
|
|
389
400
|
tracer, span_name, {AttributeKeys.JUDGMENT_SPAN_KIND: "llm"}
|
|
390
401
|
) as span:
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
402
|
+
try:
|
|
403
|
+
tracer.add_agent_attributes_to_span(span)
|
|
404
|
+
set_span_attribute(
|
|
405
|
+
span, AttributeKeys.GEN_AI_PROMPT, safe_serialize(kwargs)
|
|
406
|
+
)
|
|
407
|
+
model_name = kwargs.get("model", "")
|
|
408
|
+
# Add groq/ prefix for server-side cost calculation
|
|
409
|
+
prefixed_model_name = f"groq/{model_name}" if model_name else ""
|
|
410
|
+
set_span_attribute(
|
|
411
|
+
span,
|
|
412
|
+
AttributeKeys.GEN_AI_REQUEST_MODEL,
|
|
413
|
+
prefixed_model_name,
|
|
414
|
+
)
|
|
415
|
+
except Exception as e:
|
|
416
|
+
judgeval_logger.error(
|
|
417
|
+
f"[groq wrapped_async] Error adding span metadata: {e}"
|
|
418
|
+
)
|
|
419
|
+
|
|
401
420
|
response = await function(*args, **kwargs)
|
|
402
421
|
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
if
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
422
|
+
try:
|
|
423
|
+
if isinstance(response, GroqChatCompletion):
|
|
424
|
+
output, usage_data = _format_groq_output(response)
|
|
425
|
+
# Serialize structured data to JSON for span attribute
|
|
426
|
+
if output:
|
|
427
|
+
if isinstance(output, list):
|
|
428
|
+
output_str = safe_serialize(output)
|
|
429
|
+
else:
|
|
430
|
+
output_str = str(output)
|
|
431
|
+
set_span_attribute(
|
|
432
|
+
span, AttributeKeys.GEN_AI_COMPLETION, output_str
|
|
433
|
+
)
|
|
434
|
+
if usage_data:
|
|
435
|
+
(
|
|
436
|
+
prompt_tokens,
|
|
437
|
+
completion_tokens,
|
|
438
|
+
cache_read,
|
|
439
|
+
cache_creation,
|
|
440
|
+
) = _extract_groq_tokens(usage_data)
|
|
441
|
+
set_span_attribute(
|
|
442
|
+
span,
|
|
443
|
+
AttributeKeys.GEN_AI_USAGE_INPUT_TOKENS,
|
|
444
|
+
prompt_tokens,
|
|
445
|
+
)
|
|
446
|
+
set_span_attribute(
|
|
447
|
+
span,
|
|
448
|
+
AttributeKeys.GEN_AI_USAGE_OUTPUT_TOKENS,
|
|
449
|
+
completion_tokens,
|
|
450
|
+
)
|
|
451
|
+
set_span_attribute(
|
|
452
|
+
span,
|
|
453
|
+
AttributeKeys.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS,
|
|
454
|
+
cache_read,
|
|
455
|
+
)
|
|
456
|
+
set_span_attribute(
|
|
457
|
+
span,
|
|
458
|
+
AttributeKeys.JUDGMENT_USAGE_METADATA,
|
|
459
|
+
safe_serialize(usage_data),
|
|
460
|
+
)
|
|
461
|
+
# Add groq/ prefix to response model for server-side cost calculation
|
|
462
|
+
response_model = getattr(response, "model", model_name)
|
|
463
|
+
prefixed_response_model = (
|
|
464
|
+
f"groq/{response_model}" if response_model else ""
|
|
439
465
|
)
|
|
440
466
|
set_span_attribute(
|
|
441
467
|
span,
|
|
442
|
-
AttributeKeys.
|
|
443
|
-
|
|
468
|
+
AttributeKeys.GEN_AI_RESPONSE_MODEL,
|
|
469
|
+
prefixed_response_model,
|
|
444
470
|
)
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
f"groq/{response_model}" if response_model else ""
|
|
449
|
-
)
|
|
450
|
-
set_span_attribute(
|
|
451
|
-
span,
|
|
452
|
-
AttributeKeys.GEN_AI_RESPONSE_MODEL,
|
|
453
|
-
prefixed_response_model,
|
|
471
|
+
except Exception as e:
|
|
472
|
+
judgeval_logger.error(
|
|
473
|
+
f"[groq wrapped_async] Error adding span metadata: {e}"
|
|
454
474
|
)
|
|
455
|
-
|
|
475
|
+
finally:
|
|
476
|
+
return response
|
|
456
477
|
|
|
457
478
|
return wrapper
|
|
458
479
|
|