-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathasync_client.py
More file actions
376 lines (319 loc) · 12.9 KB
/
async_client.py
File metadata and controls
376 lines (319 loc) · 12.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
import os
import random
import json
import time
from pathlib import Path
import jwt
from typing import Any, Dict, List, Optional, Union
import traceback
import httpx
from quotientai import resources
from quotientai.exceptions import handle_async_errors, logger
from quotientai.resources.auth import AsyncAuthResource
from quotientai.resources.logs import LogDocument
class _AsyncQuotientClient(httpx.AsyncClient):
def __init__(self, api_key: str):
try:
token_dir = Path.home()
except Exception:
if Path("/root/").exists():
token_dir = Path("/root")
else:
token_dir = Path.cwd()
self.api_key = api_key
self.token = None
self.token_expiry = 0
self.token_api_key = None
self._token_path = (
token_dir
/ ".quotient"
/ f"{api_key[-6:]+'_' if api_key else ''}auth_token.json"
)
# Try to load existing token
self._load_token()
# Set initial authorization header (token if valid, otherwise API key)
auth_header = (
f"Bearer {self.token}" if self._is_token_valid() else f"Bearer {api_key}"
)
super().__init__(
base_url="https://api.quotientai.co/api/v1",
headers={"Authorization": auth_header},
)
def _save_token(self, token: str, expiry: int):
"""Save token to memory and disk"""
self.token = token
self.token_expiry = expiry
# Create directory if it doesn't exist
try:
self._token_path.parent.mkdir(parents=True, exist_ok=True)
except Exception:
logger.error(
f"could not create directory for token. if you see this error please notify us at contact@quotientai.co"
)
return None
# Save to disk
with open(self._token_path, "w") as f:
json.dump(
{"token": token, "expires_at": expiry, "api_key": self.api_key}, f
)
def _load_token(self):
"""Load token from disk if available"""
if not self._token_path.exists():
return
try:
with open(self._token_path, "r") as f:
data = json.load(f)
self.token = data.get("token")
self.token_expiry = data.get("expires_at", 0)
self.token_api_key = data.get("api_key")
except Exception:
# If loading fails, token remains None
pass
def _is_token_valid(self):
"""Check if token exists and is not expired"""
self._load_token()
if not self.token:
return False
if self.token_api_key != self.api_key:
return False
# With 5-minute buffer
return time.time() < (self.token_expiry - 300)
def _update_auth_header(self):
"""Update authorization header with token or API key"""
if self._is_token_valid():
self.headers["Authorization"] = f"Bearer {self.token}"
else:
self.headers["Authorization"] = f"Bearer {self.api_key}"
def _handle_response(self, response):
"""Check response for JWT token and save if present"""
# Look for JWT token in response headers
jwt_token = response.headers.get("X-JWT-Token")
if jwt_token:
try:
# Parse token to get expiry (assuming token is a standard JWT)
decoded = jwt.decode(jwt_token, options={"verify_signature": False})
expiry = decoded.get("exp", time.time() + 3600) # Default 1h if no exp
# Save the token
self._save_token(jwt_token, expiry)
# Update auth header for future requests
self.headers["Authorization"] = f"Bearer {jwt_token}"
except Exception:
# If token parsing fails, continue with current auth
pass
return response
@handle_async_errors
async def _get(
self, path: str, params: Optional[Dict[str, Any]] = None, timeout: int = None
) -> dict:
"""
Send an async GET request to the specified path.
Args:
path: API endpoint path
params: Optional query parameters
timeout: Optional request timeout in seconds
"""
self._update_auth_header()
response = await self.get(path, params=params, timeout=timeout)
return self._handle_response(response)
@handle_async_errors
async def _post(self, path: str, data: dict = {}, timeout: int = None) -> dict:
self._update_auth_header()
if isinstance(data, dict):
data = {k: v for k, v in data.items() if v is not None}
elif isinstance(data, list):
data = [v for v in data if v is not None]
response = await self.post(
url=path,
json=data,
timeout=timeout,
)
return self._handle_response(response)
@handle_async_errors
async def _patch(self, path: str, data: dict = {}, timeout: int = None) -> dict:
self._update_auth_header()
data = {k: v for k, v in data.items() if v is not None}
response = await self.patch(
url=path,
json=data,
timeout=timeout,
)
return self._handle_response(response)
@handle_async_errors
async def _delete(self, path: str, timeout: int = None) -> dict:
self._update_auth_header()
response = await self.delete(path, timeout=timeout)
return self._handle_response(response)
class AsyncQuotientLogger:
"""
Logger interface that wraps the underlying logs resource for asynchronous operations.
This class handles both configuration (via init) and logging.
"""
def __init__(self, logs_resource):
self.logs_resource = logs_resource
self.app_name: Optional[str] = None
self.environment: Optional[str] = None
self.tags: Dict[str, Any] = {}
self.sample_rate: float = 1.0
self.hallucination_detection: bool = False
self.inconsistency_detection: bool = False
self._configured = False
self.hallucination_detection_sample_rate = 0
def init(
self,
*,
app_name: str,
environment: str,
tags: Optional[Dict[str, Any]] = {},
sample_rate: float = 1.0,
hallucination_detection: bool = False,
inconsistency_detection: bool = False,
hallucination_detection_sample_rate: float = 0,
) -> "AsyncQuotientLogger":
"""
Configure the logger with the provided parameters and return self.
This method must be called before using log().
"""
self.app_name = app_name
self.environment = environment
self.tags = tags or {}
self.sample_rate = sample_rate
if not (0.0 <= self.sample_rate <= 1.0):
logger.error(f"sample_rate must be between 0.0 and 1.0")
return None
self.hallucination_detection = hallucination_detection
self.inconsistency_detection = inconsistency_detection
self._configured = True
self.hallucination_detection_sample_rate = hallucination_detection_sample_rate
return self
def _should_sample(self) -> bool:
"""
Determine if the log should be sampled based on the sample rate.
"""
return random.random() < self.sample_rate
async def log(
self,
*,
user_query: str,
model_output: str,
documents: Optional[List[Union[str, LogDocument]]] = None,
message_history: Optional[List[Dict[str, Any]]] = None,
instructions: Optional[List[str]] = None,
tags: Optional[Dict[str, Any]] = {},
hallucination_detection: Optional[bool] = None,
inconsistency_detection: Optional[bool] = None,
):
"""
Log the model interaction asynchronously.
Merges the default tags (set via init) with any runtime-supplied tags and calls the
underlying non_blocking_create function.
"""
if not self._configured:
logger.error(
f"Logger is not configured. Please call init() before logging."
)
return None
# Merge default tags with any tags provided at log time.
merged_tags = {**self.tags, **(tags or {})}
# Use the instance variable as the default if not provided
hallucination_detection = (
hallucination_detection
if hallucination_detection is not None
else self.hallucination_detection
)
inconsistency_detection = (
inconsistency_detection
if inconsistency_detection is not None
else self.inconsistency_detection
)
# Validate documents format
if documents:
for doc in documents:
if isinstance(doc, str):
continue
elif isinstance(doc, dict):
try:
LogDocument(**doc)
except Exception as e:
logger.error(
f"Invalid document format: Documents must include 'page_content' field and optional 'metadata' object with string keys."
)
return None
else:
actual_type = type(doc).__name__
logger.error(
f"Invalid document type: Received {actual_type}, but documents must be strings or dictionaries."
)
return None
if self._should_sample():
log_id = await self.logs_resource.create(
app_name=self.app_name,
environment=self.environment,
user_query=user_query,
model_output=model_output,
documents=documents,
message_history=message_history,
instructions=instructions,
tags=merged_tags,
hallucination_detection=hallucination_detection,
inconsistency_detection=inconsistency_detection,
hallucination_detection_sample_rate=self.hallucination_detection_sample_rate,
)
return log_id
return None
async def poll_for_detection(
self, log_id: str, timeout: int = 300, poll_interval: float = 2.0
):
"""
Get Detection results for a log asynchronously.
This method polls the Detection endpoint until the results are ready or the timeout is reached.
Args:
log_id: The ID of the log to get Detection results for
timeout: Maximum time to wait for results in seconds (default: 300s/5min)
poll_interval: How often to poll the API in seconds (default: 2s)
Returns:
Log object with Detection results if successful, None otherwise
"""
if not self._configured:
logger.error(
f"Logger is not configured. Please call init() before getting Detection results."
)
return None
if not log_id:
logger.error("Log ID is required for Detection")
return None
# Call the underlying resource method
return await self.logs_resource.poll_for_detection(
log_id, timeout, poll_interval
)
class AsyncQuotientAI:
"""
An async client that provides access to the QuotientAI API.
The AsyncQuotientAI class provides asynchronous methods to interact with the QuotientAI API,
including logging data to Quotient and running detections.
Args:
api_key (Optional[str]): The API key to use for authentication. If not provided,
will attempt to read from QUOTIENT_API_KEY environment variable.
"""
def __init__(self, api_key: Optional[str] = None):
self.api_key = api_key or os.environ.get("QUOTIENT_API_KEY")
if not self.api_key:
logger.error(
"could not find API key. either pass api_key to AsyncQuotientAI() or "
"set the QUOTIENT_API_KEY environment variable. "
f"if you do not have an API key, you can create one at https://app.quotientai.co in your settings page"
)
self._client = _AsyncQuotientClient(self.api_key)
self.auth = AsyncAuthResource(self._client)
self.logs = resources.AsyncLogsResource(self._client)
self.tracing = resources.AsyncTracingResource(self._client)
self.trace = self.tracing.trace
# Create an unconfigured logger instance.
self.logger = AsyncQuotientLogger(self.logs)
try:
self.auth.authenticate()
except Exception as e:
logger.error(
"If you are seeing this error, please check that your API key is correct.\n"
f"If the issue persists, please contact support@quotientai.co\n{traceback.format_exc()}"
)
return None