Skip to content

Commit 512bc40

Browse files
committed
Update clients
1 parent cee3617 commit 512bc40

File tree

2 files changed

+99
-5
lines changed

2 files changed

+99
-5
lines changed

quotientai/async_client.py

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,46 @@ async def poll_for_detection(
333333
return await self.logs_resource.poll_for_detection(
334334
log_id, timeout, poll_interval
335335
)
336+
337+
class AsyncQuotientTracer:
338+
"""
339+
Tracer interface that wraps the underlying tracing resource for asynchronous operations.
340+
"""
341+
342+
def __init__(self, tracing_resource):
343+
self.tracing_resource = tracing_resource
344+
345+
self.app_name: Optional[str] = None
346+
self.environment: Optional[str] = None
347+
self.instruments: Optional[list] = None
348+
self._configured = False
349+
350+
def init(self, app_name: str, environment: str, instruments: Optional[list] = None):
351+
"""
352+
Configure the tracer with the provided parameters and return self.
353+
This method must be called before using trace().
354+
"""
355+
self.app_name = app_name
356+
self.environment = environment
357+
self.instruments = instruments
358+
359+
self.tracing_resource.configure(app_name=app_name, environment=environment, instruments=instruments)
360+
361+
self._configured = True
362+
363+
return self
364+
365+
def trace(self):
366+
"""
367+
Trace a function asynchronously.
368+
"""
369+
if not self._configured:
370+
logger.error(
371+
"Tracer is not configured. Please call init() before using trace()."
372+
)
373+
return None
374+
375+
return self.tracing_resource.trace()
336376

337377

338378
class AsyncQuotientAI:
@@ -359,11 +399,11 @@ def __init__(self, api_key: Optional[str] = None):
359399
self._client = _AsyncQuotientClient(self.api_key)
360400
self.auth = AsyncAuthResource(self._client)
361401
self.logs = resources.AsyncLogsResource(self._client)
362-
self.tracing = resources.AsyncTracingResource(self._client)
363-
self.trace = self.tracing.trace
402+
self.tracing = resources.TracingResource(self._client)
364403

365404
# Create an unconfigured logger instance.
366405
self.logger = AsyncQuotientLogger(self.logs)
406+
self.tracer = AsyncQuotientTracer(self.tracing)
367407

368408
try:
369409
self.auth.authenticate()
@@ -419,3 +459,28 @@ async def log(
419459
inconsistency_detection=inconsistency_detection,
420460
)
421461
return result
462+
463+
def trace(self):
464+
"""
465+
Trace a function asynchronously.
466+
"""
467+
return self.tracer.trace()
468+
469+
async def poll_for_detection(
470+
self, log_id: str, timeout: int = 300, poll_interval: float = 2.0
471+
):
472+
"""
473+
Get Detection results for a log asynchronously.
474+
"""
475+
if not self.logger._configured:
476+
logger.error(
477+
"Logger is not configured. Please call init() before using poll_for_detection()."
478+
)
479+
return None
480+
481+
if not log_id:
482+
logger.error("Log ID is required for Detection")
483+
return None
484+
485+
detection = await self.logger.poll_for_detection(log_id=log_id, timeout=timeout, poll_interval=poll_interval)
486+
return detection

quotientai/client.py

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,13 @@ def poll_for_detection(
335335
Returns:
336336
Log object with Detection results if successful, None otherwise
337337
"""
338+
warnings.warn(
339+
"quotient.logger.poll_for_detection() is deprecated and will be removed in a future version. "
340+
"Please use quotient.poll_for_detection() instead.",
341+
DeprecationWarning,
342+
stacklevel=2
343+
)
344+
338345
if not self._configured:
339346
logger.error(
340347
f"Logger is not configured. Please call init() before getting Detection results."
@@ -408,7 +415,7 @@ async def my_async_function():
408415
logger.error(
409416
f"Tracer is not configured. Please call init() before tracing."
410417
)
411-
return lambda func: func
418+
return None
412419

413420
# Call the tracing resource without parameters since it's now configured
414421
return self.tracing_resource.trace()
@@ -487,7 +494,7 @@ def log(
487494
)
488495
return None
489496

490-
result = self.logger.log(
497+
log_id = self.logger.log(
491498
user_query=user_query,
492499
model_output=model_output,
493500
documents=documents,
@@ -497,9 +504,31 @@ def log(
497504
hallucination_detection=hallucination_detection,
498505
inconsistency_detection=inconsistency_detection,
499506
)
500-
return result
507+
return log_id
501508

502509
def trace(self):
503510
"""Direct access to the tracer's trace decorator."""
504511
return self.tracer.trace()
512+
513+
def poll_for_detection(self, log_id: str, timeout: int = 300, poll_interval: float = 2.0):
514+
"""
515+
Direct access to the logger's poll_for_detection method.
516+
517+
Args:
518+
log_id: The ID of the log to get Detection results for
519+
timeout: Maximum time to wait for results in seconds (default: 300s/5min)
520+
poll_interval: How often to poll the API in seconds (default: 2s)
521+
"""
522+
if not self.logger._configured:
523+
logger.error(
524+
"Logger is not configured. Please call quotient.logger.init() before using poll_for_detection()."
525+
)
526+
return None
527+
528+
if not log_id:
529+
logger.error("Log ID is required for Detection")
530+
return None
531+
532+
detection = self.logger.poll_for_detection(log_id=log_id, timeout=timeout, poll_interval=poll_interval)
533+
return detection
505534

0 commit comments

Comments
 (0)