generalcontext 0.2.0__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.
|
@@ -0,0 +1,807 @@
|
|
|
1
|
+
# Copyright 2026 General Context Labs, Inc.
|
|
2
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
"""Record your agent's model calls for General Context, without changing how it runs.
|
|
5
|
+
|
|
6
|
+
import generalcontext
|
|
7
|
+
generalcontext.initialize() # once per process, reads GENERALCONTEXT_API_KEY
|
|
8
|
+
|
|
9
|
+
with generalcontext.session(conversation_id, user_id="u_42"):
|
|
10
|
+
client.chat.completions.create(...) # any OpenAI or Anthropic client, anywhere
|
|
11
|
+
|
|
12
|
+
`initialize()` patches the installed `openai` and `anthropic` client classes, so every
|
|
13
|
+
client is recorded: chat.completions, responses and messages, sync and async, streamed or
|
|
14
|
+
not. Raw `httpx` and `requests` POSTs to `/chat/completions` are recorded only with
|
|
15
|
+
`instrument_http=True`. Calls are sent from a background thread. Your agent never waits on
|
|
16
|
+
General Context, and a failure on our side is logged and dropped, never raised.
|
|
17
|
+
|
|
18
|
+
`redact=True` replaces emails, phone numbers and card or SSN-like numbers with stable
|
|
19
|
+
placeholders (<phone:3f2a1c>) before anything leaves this process. Set
|
|
20
|
+
GENERALCONTEXT_REDACT_SALT to one secret in every process so placeholders match.
|
|
21
|
+
|
|
22
|
+
Standard library only. `wrap(client)` and `conversation(...)` from 0.1 still work.
|
|
23
|
+
"""
|
|
24
|
+
|
|
25
|
+
from contextlib import contextmanager
|
|
26
|
+
from contextvars import ContextVar
|
|
27
|
+
from datetime import datetime, timezone
|
|
28
|
+
import atexit
|
|
29
|
+
import functools
|
|
30
|
+
import hashlib
|
|
31
|
+
import hmac
|
|
32
|
+
import inspect
|
|
33
|
+
import json
|
|
34
|
+
import logging
|
|
35
|
+
import os
|
|
36
|
+
import queue
|
|
37
|
+
import re
|
|
38
|
+
import threading
|
|
39
|
+
import time
|
|
40
|
+
import urllib.parse
|
|
41
|
+
import urllib.request
|
|
42
|
+
import uuid
|
|
43
|
+
|
|
44
|
+
__all__ = ["initialize", "session", "with_session", "conversation", "task", "flush",
|
|
45
|
+
"report_outcome", "log_event", "wrap"]
|
|
46
|
+
__version__ = "0.2.0"
|
|
47
|
+
|
|
48
|
+
DEFAULT_BASE_URL = "https://generalcontextlabs.com"
|
|
49
|
+
BATCH = 20
|
|
50
|
+
log = logging.getLogger("generalcontext")
|
|
51
|
+
|
|
52
|
+
_session = ContextVar("generalcontext_session", default=None)
|
|
53
|
+
_task = ContextVar("generalcontext_task", default=None)
|
|
54
|
+
# Set while a patched SDK call runs, so the call is recorded once even when it is wrapped
|
|
55
|
+
# twice or its HTTP request passes through a patched transport.
|
|
56
|
+
_inside = ContextVar("generalcontext_inside", default=False)
|
|
57
|
+
|
|
58
|
+
_config = {"initialized": False, "api_key": None, "base_url": None, "app": None, "scrub": None, "batch": BATCH,
|
|
59
|
+
"before_send": None}
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def _api_key():
|
|
63
|
+
return _config["api_key"] or os.environ.get("GENERALCONTEXT_API_KEY")
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def _base_url():
|
|
67
|
+
return (_config["base_url"] or os.environ.get("GENERALCONTEXT_BASE_URL") or DEFAULT_BASE_URL).rstrip("/")
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def _now():
|
|
71
|
+
return datetime.now(timezone.utc).isoformat()
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def _disabled():
|
|
75
|
+
"""GENERALCONTEXT_DISABLED=1 turns the SDK off without a code change."""
|
|
76
|
+
return os.environ.get("GENERALCONTEXT_DISABLED", "").strip().lower() in ("1", "true", "yes")
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
# ---- Setup -------------------------------------------------------------------------------
|
|
80
|
+
|
|
81
|
+
def initialize(api_key=None, base_url=None, app_name=None, redact=False, before_send=None,
|
|
82
|
+
instrument_http=False, disable_batch=False):
|
|
83
|
+
"""Record every OpenAI and Anthropic call in this process. Call once, in every process.
|
|
84
|
+
|
|
85
|
+
SDK clients are always recorded. Raw httpx/requests POSTs to /chat/completions are
|
|
86
|
+
recorded only with instrument_http=True. before_send(call) runs on the sender thread and
|
|
87
|
+
returns the dict to send, or None to drop the call.
|
|
88
|
+
"""
|
|
89
|
+
if _disabled():
|
|
90
|
+
log.info("GENERALCONTEXT_DISABLED is set; General Context is off")
|
|
91
|
+
return
|
|
92
|
+
if _config["initialized"]:
|
|
93
|
+
log.warning("generalcontext.initialize() was already called; this call is ignored")
|
|
94
|
+
return
|
|
95
|
+
_config.update(initialized=True, api_key=api_key, base_url=base_url, app=app_name,
|
|
96
|
+
scrub=_redactor(redact), before_send=before_send, batch=1 if disable_batch else BATCH)
|
|
97
|
+
if not _api_key():
|
|
98
|
+
log.warning("GENERALCONTEXT_API_KEY is not set; model calls will not be recorded")
|
|
99
|
+
if not _secure(_base_url()):
|
|
100
|
+
_warn_insecure()
|
|
101
|
+
atexit.register(_flush_at_exit)
|
|
102
|
+
_patch_sdks()
|
|
103
|
+
if instrument_http:
|
|
104
|
+
_patch_http()
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def _patch_sdks():
|
|
108
|
+
targets = [
|
|
109
|
+
("openai.resources.chat.completions", "Completions", "AsyncCompletions", "openai-chat"),
|
|
110
|
+
("openai.resources.responses", "Responses", "AsyncResponses", "openai-responses"),
|
|
111
|
+
("anthropic.resources.messages", "Messages", "AsyncMessages", "anthropic"),
|
|
112
|
+
]
|
|
113
|
+
for module_name, sync_name, async_name, api in targets:
|
|
114
|
+
try:
|
|
115
|
+
module = __import__(module_name, fromlist=[sync_name])
|
|
116
|
+
except Exception:
|
|
117
|
+
continue # Package not installed.
|
|
118
|
+
for name, is_async in ((sync_name, False), (async_name, True)):
|
|
119
|
+
cls = getattr(module, name, None)
|
|
120
|
+
if cls is None or getattr(cls.create, "_generalcontext", False):
|
|
121
|
+
continue
|
|
122
|
+
cls.create = _recording(cls.create, api, is_async)
|
|
123
|
+
if api == "anthropic" and hasattr(cls, "stream"):
|
|
124
|
+
cls.stream = _recording_stream_manager(cls.stream, is_async)
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
def wrap(client, *, redact=False):
|
|
128
|
+
"""Record every model call `client` makes. Returns the same client. Prefer initialize()."""
|
|
129
|
+
if _disabled():
|
|
130
|
+
return client
|
|
131
|
+
scrub = _redactor(redact)
|
|
132
|
+
targets = []
|
|
133
|
+
if hasattr(client, "chat") and hasattr(getattr(client.chat, "completions", None), "create"):
|
|
134
|
+
targets.append((client.chat.completions, "openai-chat"))
|
|
135
|
+
if hasattr(getattr(client, "responses", None), "create"):
|
|
136
|
+
targets.append((client.responses, "openai-responses"))
|
|
137
|
+
if hasattr(getattr(client, "messages", None), "create") and not hasattr(client, "chat"):
|
|
138
|
+
targets.append((client.messages, "anthropic"))
|
|
139
|
+
if not targets:
|
|
140
|
+
raise TypeError("generalcontext.wrap expects an OpenAI or Anthropic client")
|
|
141
|
+
for owner, api in targets:
|
|
142
|
+
create = owner.create
|
|
143
|
+
if not getattr(create, "_generalcontext_instance", False):
|
|
144
|
+
owner.create = _recording(create, api, inspect.iscoroutinefunction(inspect.unwrap(create)), scrub)
|
|
145
|
+
owner.create._generalcontext_instance = True
|
|
146
|
+
return client
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
# ---- Sessions and tasks --------------------------------------------------------------------
|
|
150
|
+
|
|
151
|
+
@contextmanager
|
|
152
|
+
def session(session_id, user_id=None, channel=None, product=None, metadata=None):
|
|
153
|
+
"""Tag every call inside the block as part of conversation `session_id`. Safe across threads and asyncio."""
|
|
154
|
+
tags = {key: value for key, value in dict(id=str(session_id), user_id=user_id, channel=channel,
|
|
155
|
+
product=product, metadata=metadata).items() if value is not None}
|
|
156
|
+
token = _session.set(tags)
|
|
157
|
+
try:
|
|
158
|
+
yield
|
|
159
|
+
finally:
|
|
160
|
+
_session.reset(token)
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
with_session = session
|
|
164
|
+
conversation = session
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
@contextmanager
|
|
168
|
+
def task(name):
|
|
169
|
+
"""Mark every call inside the block as part of the unit of work `name`."""
|
|
170
|
+
token = _task.set(str(name))
|
|
171
|
+
try:
|
|
172
|
+
yield
|
|
173
|
+
finally:
|
|
174
|
+
_task.reset(token)
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
# ---- Sending -----------------------------------------------------------------------------
|
|
178
|
+
|
|
179
|
+
MAX_CALL_BYTES = 4_000_000 # The server rejects larger calls.
|
|
180
|
+
MAX_QUEUE_BYTES = 64_000_000
|
|
181
|
+
MAX_BATCH_BYTES = 4_000_000
|
|
182
|
+
MAX_QUEUE_CALLS = 5000
|
|
183
|
+
_warned_at = {}
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
def _warn(key, message, *args):
|
|
187
|
+
"""Log `message` at most once per 60 seconds per `key`."""
|
|
188
|
+
now = time.monotonic()
|
|
189
|
+
if now - _warned_at.get(key, -1e9) >= 60:
|
|
190
|
+
_warned_at[key] = now
|
|
191
|
+
log.warning(message, *args)
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
def _secure(url):
|
|
195
|
+
"""HTTPS, or plain HTTP to this machine for local development."""
|
|
196
|
+
parts = urllib.parse.urlsplit(url)
|
|
197
|
+
return parts.scheme == "https" or (parts.scheme == "http" and parts.hostname in ("localhost", "127.0.0.1", "::1"))
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
def _warn_insecure():
|
|
201
|
+
_warn("http", "General Context base_url %s is not https; nothing will be sent", _base_url())
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
def _size(value, limit):
|
|
205
|
+
"""Cheap size estimate of a snapshot; stops counting past `limit`."""
|
|
206
|
+
total, stack = 0, [value]
|
|
207
|
+
while stack and total <= limit:
|
|
208
|
+
item = stack.pop()
|
|
209
|
+
total += 8
|
|
210
|
+
if isinstance(item, str):
|
|
211
|
+
total += len(item)
|
|
212
|
+
elif isinstance(item, dict):
|
|
213
|
+
stack.extend(item.keys())
|
|
214
|
+
stack.extend(item.values())
|
|
215
|
+
elif isinstance(item, list):
|
|
216
|
+
stack.extend(item)
|
|
217
|
+
return total
|
|
218
|
+
|
|
219
|
+
|
|
220
|
+
class _Sender:
|
|
221
|
+
"""One daemon thread that redacts, encodes, batches and posts recorded calls."""
|
|
222
|
+
|
|
223
|
+
def __init__(self):
|
|
224
|
+
self._reset()
|
|
225
|
+
self.warned = False
|
|
226
|
+
|
|
227
|
+
def _reset(self):
|
|
228
|
+
# Also run in a forked child: the parent's thread and lock do not exist there.
|
|
229
|
+
self.queue = queue.Queue(maxsize=MAX_QUEUE_CALLS)
|
|
230
|
+
self.lock = threading.Lock()
|
|
231
|
+
self.bytes = 0
|
|
232
|
+
self.thread = None
|
|
233
|
+
|
|
234
|
+
def put(self, call, scrub):
|
|
235
|
+
"""Runs on the caller's thread, so it only measures and enqueues."""
|
|
236
|
+
if not _api_key():
|
|
237
|
+
if not self.warned:
|
|
238
|
+
log.warning("GENERALCONTEXT_API_KEY is not set; model calls are not recorded")
|
|
239
|
+
self.warned = True
|
|
240
|
+
return
|
|
241
|
+
if not _secure(_base_url()):
|
|
242
|
+
return _warn_insecure()
|
|
243
|
+
size = _size(call, MAX_CALL_BYTES)
|
|
244
|
+
if size > MAX_CALL_BYTES:
|
|
245
|
+
return _warn("oversized", "General Context dropped a model call larger than %d bytes", MAX_CALL_BYTES)
|
|
246
|
+
with self.lock:
|
|
247
|
+
if self.bytes + size > MAX_QUEUE_BYTES:
|
|
248
|
+
return _warn("full", "General Context queue is full; a model call was dropped")
|
|
249
|
+
try:
|
|
250
|
+
self.queue.put_nowait((call, scrub, size))
|
|
251
|
+
except queue.Full:
|
|
252
|
+
return _warn("full", "General Context queue is full; a model call was dropped")
|
|
253
|
+
self.bytes += size
|
|
254
|
+
if self.thread is None or not self.thread.is_alive():
|
|
255
|
+
self.thread = threading.Thread(target=self._run, name="generalcontext", daemon=True)
|
|
256
|
+
self.thread.start()
|
|
257
|
+
|
|
258
|
+
def _take(self, pending, timeout):
|
|
259
|
+
"""Move one call from the queue into `pending` as JSON bytes. False when the queue stayed empty."""
|
|
260
|
+
try:
|
|
261
|
+
call, scrub, size = self.queue.get(timeout=timeout)
|
|
262
|
+
except queue.Empty:
|
|
263
|
+
return False
|
|
264
|
+
with self.lock:
|
|
265
|
+
self.bytes -= size
|
|
266
|
+
data = _encode(call, scrub)
|
|
267
|
+
if data is None:
|
|
268
|
+
self.queue.task_done()
|
|
269
|
+
else:
|
|
270
|
+
pending.append(data)
|
|
271
|
+
return True
|
|
272
|
+
|
|
273
|
+
def _run(self):
|
|
274
|
+
pending = [] # Encoded calls taken from the queue; each still owes one task_done.
|
|
275
|
+
while True:
|
|
276
|
+
if not pending:
|
|
277
|
+
self._take(pending, None)
|
|
278
|
+
continue
|
|
279
|
+
while (len(pending) < _config["batch"] and sum(map(len, pending)) < MAX_BATCH_BYTES
|
|
280
|
+
and self._take(pending, 0.5)):
|
|
281
|
+
pass
|
|
282
|
+
count, size = 0, 0
|
|
283
|
+
for data in pending:
|
|
284
|
+
if count and (count >= _config["batch"] or size + len(data) + 1 > MAX_BATCH_BYTES):
|
|
285
|
+
break
|
|
286
|
+
count, size = count + 1, size + len(data) + 1
|
|
287
|
+
batch, pending = pending[:count], pending[count:]
|
|
288
|
+
try:
|
|
289
|
+
head = json.dumps({"sdk": f"python/{__version__}", "app": _config["app"]})[:-1]
|
|
290
|
+
_post("/api/platform-collect/sdk", (head + ', "calls": [').encode() + b",".join(batch) + b"]}", 15)
|
|
291
|
+
except Exception as error: # Never let our failure reach the agent.
|
|
292
|
+
_warn("send", "General Context did not accept %d calls: %s", len(batch), error)
|
|
293
|
+
finally:
|
|
294
|
+
for _ in batch:
|
|
295
|
+
self.queue.task_done()
|
|
296
|
+
|
|
297
|
+
|
|
298
|
+
def _encode(call, scrub):
|
|
299
|
+
"""before_send, redaction and JSON encoding, on the sender thread. None drops the call."""
|
|
300
|
+
try:
|
|
301
|
+
before_send = _config["before_send"]
|
|
302
|
+
if before_send is not None:
|
|
303
|
+
try:
|
|
304
|
+
call = before_send(call)
|
|
305
|
+
except Exception as error:
|
|
306
|
+
_warn("before_send", "General Context before_send raised; a call was dropped: %s", error)
|
|
307
|
+
return None
|
|
308
|
+
if call is None:
|
|
309
|
+
return None
|
|
310
|
+
data = json.dumps(_scrubbed(call, scrub), default=str).encode()
|
|
311
|
+
except Exception as error:
|
|
312
|
+
_warn("encode", "General Context could not encode a model call: %s", error)
|
|
313
|
+
return None
|
|
314
|
+
if len(data) > MAX_CALL_BYTES:
|
|
315
|
+
_warn("oversized", "General Context dropped a model call larger than %d bytes", MAX_CALL_BYTES)
|
|
316
|
+
return None
|
|
317
|
+
return data
|
|
318
|
+
|
|
319
|
+
|
|
320
|
+
class _NoRedirect(urllib.request.HTTPRedirectHandler):
|
|
321
|
+
"""Refuse redirects, so the Authorization header is never forwarded anywhere else."""
|
|
322
|
+
|
|
323
|
+
def redirect_request(self, *args, **kwargs):
|
|
324
|
+
return None # urllib then raises HTTPError for the 3xx.
|
|
325
|
+
|
|
326
|
+
|
|
327
|
+
_opener = urllib.request.build_opener(_NoRedirect) # Honors HTTPS_PROXY and SSL_CERT_FILE.
|
|
328
|
+
|
|
329
|
+
|
|
330
|
+
def _post(path, body, timeout):
|
|
331
|
+
url = _base_url() + path
|
|
332
|
+
if not _secure(url):
|
|
333
|
+
_warn_insecure()
|
|
334
|
+
raise ValueError("refusing to send to a non-https base_url")
|
|
335
|
+
data = body if isinstance(body, bytes) else json.dumps(body, default=str).encode()
|
|
336
|
+
request = urllib.request.Request(url, data=data, headers={
|
|
337
|
+
"Authorization": "Bearer " + (_api_key() or ""), "Content-Type": "application/json"})
|
|
338
|
+
with _opener.open(request, timeout=timeout) as response:
|
|
339
|
+
response.read()
|
|
340
|
+
|
|
341
|
+
|
|
342
|
+
_sender = _Sender()
|
|
343
|
+
if hasattr(os, "register_at_fork"):
|
|
344
|
+
os.register_at_fork(after_in_child=_sender._reset)
|
|
345
|
+
|
|
346
|
+
|
|
347
|
+
def flush(timeout=10):
|
|
348
|
+
"""Wait until recorded calls are sent. Call before a short-lived process exits. True if all were sent."""
|
|
349
|
+
try:
|
|
350
|
+
done = threading.Event()
|
|
351
|
+
threading.Thread(target=lambda: (_sender.queue.join(), done.set()), daemon=True).start()
|
|
352
|
+
return done.wait(timeout)
|
|
353
|
+
except Exception as error:
|
|
354
|
+
log.warning("General Context flush failed: %s", error)
|
|
355
|
+
return False
|
|
356
|
+
|
|
357
|
+
|
|
358
|
+
def _flush_at_exit():
|
|
359
|
+
try:
|
|
360
|
+
flush(2)
|
|
361
|
+
except Exception:
|
|
362
|
+
pass
|
|
363
|
+
|
|
364
|
+
|
|
365
|
+
def _send_after_flush(path, body, what):
|
|
366
|
+
"""Flush first, so the conversation exists when the server receives `body`."""
|
|
367
|
+
try:
|
|
368
|
+
if not _api_key():
|
|
369
|
+
return False
|
|
370
|
+
flush()
|
|
371
|
+
_post(path, body, 10)
|
|
372
|
+
return True
|
|
373
|
+
except Exception as error:
|
|
374
|
+
log.warning("General Context did not accept the %s: %s", what, error)
|
|
375
|
+
return False
|
|
376
|
+
|
|
377
|
+
|
|
378
|
+
def _scrub_text(value):
|
|
379
|
+
scrub = _config["scrub"]
|
|
380
|
+
return scrub(value) if scrub is not None and isinstance(value, str) else value
|
|
381
|
+
|
|
382
|
+
|
|
383
|
+
def report_outcome(session_id, outcome, note=None):
|
|
384
|
+
"""Mark a conversation as a success or a failure. True if General Context accepted it."""
|
|
385
|
+
try:
|
|
386
|
+
if outcome not in ("success", "fail"):
|
|
387
|
+
log.warning("report_outcome expects 'success' or 'fail', got %r", outcome)
|
|
388
|
+
return False
|
|
389
|
+
body = {"session_id": _scrub_text(str(session_id)), "outcome": outcome, "note": _scrub_text(note)}
|
|
390
|
+
except Exception as error:
|
|
391
|
+
log.warning("General Context could not build the outcome: %s", error)
|
|
392
|
+
return False
|
|
393
|
+
return _send_after_flush("/api/platform-collect/outcomes", body, "outcome")
|
|
394
|
+
|
|
395
|
+
|
|
396
|
+
def log_event(session_id, event_type, properties=None, event_id=None, timestamp=None):
|
|
397
|
+
"""Record something that happened in your systems (order.refunded, ticket.escalated). Pass a stable event_id so retries dedupe."""
|
|
398
|
+
try:
|
|
399
|
+
event = {"event_id": event_id or str(uuid.uuid4()), "event_type": _scrub_text(event_type),
|
|
400
|
+
"timestamp": timestamp or _now(), "session_id": _scrub_text(str(session_id)),
|
|
401
|
+
"properties": {key: _scrub_text(value) for key, value in (properties or {}).items()}}
|
|
402
|
+
except Exception as error:
|
|
403
|
+
log.warning("General Context could not build the event: %s", error)
|
|
404
|
+
return False
|
|
405
|
+
return _send_after_flush("/api/platform-collect/events", {"events": [event]}, "event")
|
|
406
|
+
|
|
407
|
+
|
|
408
|
+
def _record(meta, response=None, error=None):
|
|
409
|
+
"""Snapshot and enqueue one finished call. Never raises."""
|
|
410
|
+
try:
|
|
411
|
+
call = dict(meta["call"], response=_plain(response) if response is not None else None,
|
|
412
|
+
ended_at=_now(), error=f"{type(error).__name__}: {error}" if error is not None else None)
|
|
413
|
+
_sender.put(call, meta["scrub"])
|
|
414
|
+
except Exception as failure:
|
|
415
|
+
log.warning("General Context could not record a model call: %s", failure)
|
|
416
|
+
|
|
417
|
+
|
|
418
|
+
def _start(api, request, scrub=None):
|
|
419
|
+
return {"scrub": scrub or _config["scrub"], "call": {
|
|
420
|
+
"api": api, "conversation": _session.get(), "task": _task.get(),
|
|
421
|
+
"request": _plain(request), "started_at": _now()}}
|
|
422
|
+
|
|
423
|
+
|
|
424
|
+
# ---- SDK patching --------------------------------------------------------------------------
|
|
425
|
+
|
|
426
|
+
def _safe_start(api, request, scrub=None):
|
|
427
|
+
try:
|
|
428
|
+
return _start(api, request, scrub)
|
|
429
|
+
except Exception as error:
|
|
430
|
+
_warn("start", "General Context could not snapshot a model call; it runs unrecorded: %s", error)
|
|
431
|
+
return None
|
|
432
|
+
|
|
433
|
+
|
|
434
|
+
def _recording(create, api, is_async, scrub=None):
|
|
435
|
+
"""Wrap a `create` method so each call is recorded. The caller gets exactly what `create` returns."""
|
|
436
|
+
if is_async:
|
|
437
|
+
@functools.wraps(create)
|
|
438
|
+
async def wrapped(*args, **kwargs):
|
|
439
|
+
if _inside.get():
|
|
440
|
+
return await create(*args, **kwargs)
|
|
441
|
+
meta = _safe_start(api, kwargs, scrub)
|
|
442
|
+
if meta is None:
|
|
443
|
+
return await create(*args, **kwargs)
|
|
444
|
+
token = _inside.set(True)
|
|
445
|
+
try:
|
|
446
|
+
reply = await create(*args, **kwargs)
|
|
447
|
+
except Exception as error:
|
|
448
|
+
_record(meta, error=error)
|
|
449
|
+
raise
|
|
450
|
+
finally:
|
|
451
|
+
_inside.reset(token)
|
|
452
|
+
if kwargs.get("stream") is True:
|
|
453
|
+
return _AsyncStream(reply, meta)
|
|
454
|
+
_record(meta, reply)
|
|
455
|
+
return reply
|
|
456
|
+
else:
|
|
457
|
+
@functools.wraps(create)
|
|
458
|
+
def wrapped(*args, **kwargs):
|
|
459
|
+
if _inside.get():
|
|
460
|
+
return create(*args, **kwargs)
|
|
461
|
+
meta = _safe_start(api, kwargs, scrub)
|
|
462
|
+
if meta is None:
|
|
463
|
+
return create(*args, **kwargs)
|
|
464
|
+
token = _inside.set(True)
|
|
465
|
+
try:
|
|
466
|
+
reply = create(*args, **kwargs)
|
|
467
|
+
except Exception as error:
|
|
468
|
+
_record(meta, error=error)
|
|
469
|
+
raise
|
|
470
|
+
finally:
|
|
471
|
+
_inside.reset(token)
|
|
472
|
+
if kwargs.get("stream") is True:
|
|
473
|
+
return _SyncStream(reply, meta)
|
|
474
|
+
_record(meta, reply)
|
|
475
|
+
return reply
|
|
476
|
+
wrapped._generalcontext = True
|
|
477
|
+
return wrapped
|
|
478
|
+
|
|
479
|
+
|
|
480
|
+
class _StreamBase:
|
|
481
|
+
"""Passes chunks through unchanged and records the assembled reply once the stream ends."""
|
|
482
|
+
|
|
483
|
+
def __init__(self, inner, meta):
|
|
484
|
+
self._inner, self._meta, self._chunks, self._done = inner, meta, [], False
|
|
485
|
+
|
|
486
|
+
def _finish(self, error=None):
|
|
487
|
+
if not self._done:
|
|
488
|
+
self._done = True
|
|
489
|
+
_record(self._meta, None if error else _assemble(self._meta["call"]["api"], self._chunks), error)
|
|
490
|
+
|
|
491
|
+
def _keep(self, chunk):
|
|
492
|
+
try:
|
|
493
|
+
self._chunks.append(_plain(chunk))
|
|
494
|
+
except Exception:
|
|
495
|
+
pass
|
|
496
|
+
return chunk
|
|
497
|
+
|
|
498
|
+
def __getattr__(self, name):
|
|
499
|
+
return getattr(self._inner, name)
|
|
500
|
+
|
|
501
|
+
|
|
502
|
+
class _SyncStream(_StreamBase):
|
|
503
|
+
def __iter__(self):
|
|
504
|
+
return self
|
|
505
|
+
|
|
506
|
+
def __next__(self):
|
|
507
|
+
if not hasattr(self, "_iterator"):
|
|
508
|
+
self._iterator = iter(self._inner)
|
|
509
|
+
try:
|
|
510
|
+
return self._keep(next(self._iterator))
|
|
511
|
+
except StopIteration:
|
|
512
|
+
self._finish()
|
|
513
|
+
raise
|
|
514
|
+
except Exception as error:
|
|
515
|
+
self._finish(error)
|
|
516
|
+
raise
|
|
517
|
+
|
|
518
|
+
def close(self):
|
|
519
|
+
try:
|
|
520
|
+
self._inner.close()
|
|
521
|
+
finally:
|
|
522
|
+
self._finish()
|
|
523
|
+
|
|
524
|
+
def __enter__(self):
|
|
525
|
+
return self
|
|
526
|
+
|
|
527
|
+
def __exit__(self, *exc):
|
|
528
|
+
self.close()
|
|
529
|
+
|
|
530
|
+
|
|
531
|
+
class _AsyncStream(_StreamBase):
|
|
532
|
+
def __aiter__(self):
|
|
533
|
+
return self
|
|
534
|
+
|
|
535
|
+
async def __anext__(self):
|
|
536
|
+
if not hasattr(self, "_iterator"):
|
|
537
|
+
self._iterator = self._inner.__aiter__()
|
|
538
|
+
try:
|
|
539
|
+
return self._keep(await self._iterator.__anext__())
|
|
540
|
+
except StopAsyncIteration:
|
|
541
|
+
self._finish()
|
|
542
|
+
raise
|
|
543
|
+
except Exception as error:
|
|
544
|
+
self._finish(error)
|
|
545
|
+
raise
|
|
546
|
+
|
|
547
|
+
async def close(self):
|
|
548
|
+
try:
|
|
549
|
+
await self._inner.close()
|
|
550
|
+
finally:
|
|
551
|
+
self._finish()
|
|
552
|
+
|
|
553
|
+
async def __aenter__(self):
|
|
554
|
+
return self
|
|
555
|
+
|
|
556
|
+
async def __aexit__(self, *exc):
|
|
557
|
+
await self.close()
|
|
558
|
+
|
|
559
|
+
|
|
560
|
+
def _recording_stream_manager(stream, is_async):
|
|
561
|
+
"""Wrap Anthropic `messages.stream(...)`: record the SDK's final message snapshot on exit."""
|
|
562
|
+
|
|
563
|
+
@functools.wraps(stream)
|
|
564
|
+
def wrapped(*args, **kwargs):
|
|
565
|
+
meta = _safe_start("anthropic", dict(kwargs, stream=True))
|
|
566
|
+
inner = stream(*args, **kwargs)
|
|
567
|
+
return inner if meta is None else _StreamManager(inner, meta)
|
|
568
|
+
wrapped._generalcontext = True
|
|
569
|
+
return wrapped
|
|
570
|
+
|
|
571
|
+
|
|
572
|
+
class _StreamManager:
|
|
573
|
+
def __init__(self, inner, meta):
|
|
574
|
+
self._inner, self._meta, self._stream = inner, meta, None
|
|
575
|
+
|
|
576
|
+
def _finish(self, error):
|
|
577
|
+
try:
|
|
578
|
+
snapshot = self._stream.current_message_snapshot if self._stream is not None else None
|
|
579
|
+
except Exception:
|
|
580
|
+
snapshot = None
|
|
581
|
+
_record(self._meta, snapshot, error)
|
|
582
|
+
|
|
583
|
+
def __enter__(self):
|
|
584
|
+
self._stream = self._inner.__enter__()
|
|
585
|
+
return self._stream
|
|
586
|
+
|
|
587
|
+
def __exit__(self, kind, error, trace):
|
|
588
|
+
self._finish(error)
|
|
589
|
+
return self._inner.__exit__(kind, error, trace)
|
|
590
|
+
|
|
591
|
+
async def __aenter__(self):
|
|
592
|
+
self._stream = await self._inner.__aenter__()
|
|
593
|
+
return self._stream
|
|
594
|
+
|
|
595
|
+
async def __aexit__(self, kind, error, trace):
|
|
596
|
+
self._finish(error)
|
|
597
|
+
return await self._inner.__aexit__(kind, error, trace)
|
|
598
|
+
|
|
599
|
+
|
|
600
|
+
def _assemble(api, chunks):
|
|
601
|
+
"""Turn streamed chunks into the reply a non-streamed call would have returned."""
|
|
602
|
+
if api == "openai-chat":
|
|
603
|
+
return _assemble_chat(chunks)
|
|
604
|
+
if api == "anthropic":
|
|
605
|
+
return _assemble_anthropic(chunks)
|
|
606
|
+
finals = [c["response"] for c in chunks if isinstance(c, dict) and c.get("type") in
|
|
607
|
+
("response.completed", "response.incomplete", "response.failed")]
|
|
608
|
+
return finals[-1] if finals else {"events": chunks}
|
|
609
|
+
|
|
610
|
+
|
|
611
|
+
def _assemble_chat(chunks):
|
|
612
|
+
text, tools, model, usage, finish = [], {}, None, None, None
|
|
613
|
+
for chunk in chunks:
|
|
614
|
+
model = chunk.get("model") or model
|
|
615
|
+
usage = chunk.get("usage") or usage
|
|
616
|
+
for choice in chunk.get("choices") or []:
|
|
617
|
+
if choice.get("index", 0) != 0:
|
|
618
|
+
continue
|
|
619
|
+
delta = choice.get("delta") or {}
|
|
620
|
+
text.append(delta.get("content") or "")
|
|
621
|
+
for call in delta.get("tool_calls") or []:
|
|
622
|
+
slot = tools.setdefault(call.get("index", 0), {"id": None, "type": "function",
|
|
623
|
+
"function": {"name": "", "arguments": ""}})
|
|
624
|
+
slot["id"] = call.get("id") or slot["id"]
|
|
625
|
+
function = call.get("function") or {}
|
|
626
|
+
slot["function"]["name"] += function.get("name") or ""
|
|
627
|
+
slot["function"]["arguments"] += function.get("arguments") or ""
|
|
628
|
+
finish = choice.get("finish_reason") or finish
|
|
629
|
+
message = {"role": "assistant", "content": "".join(text) or None}
|
|
630
|
+
if tools:
|
|
631
|
+
message["tool_calls"] = [tools[index] for index in sorted(tools)]
|
|
632
|
+
return {"model": model, "choices": [{"index": 0, "message": message, "finish_reason": finish}], "usage": usage}
|
|
633
|
+
|
|
634
|
+
|
|
635
|
+
def _assemble_anthropic(events):
|
|
636
|
+
message, blocks = {}, {}
|
|
637
|
+
for event in events:
|
|
638
|
+
kind = event.get("type")
|
|
639
|
+
if kind == "message_start":
|
|
640
|
+
message = dict(event.get("message") or {})
|
|
641
|
+
elif kind == "content_block_start":
|
|
642
|
+
blocks[event["index"]] = dict(event.get("content_block") or {})
|
|
643
|
+
elif kind == "content_block_delta":
|
|
644
|
+
block, delta = blocks.setdefault(event["index"], {}), event.get("delta") or {}
|
|
645
|
+
for field, key in (("text", "text"), ("partial_json", "_json"), ("thinking", "thinking")):
|
|
646
|
+
if field in delta:
|
|
647
|
+
block[key] = block.get(key, "") + delta[field]
|
|
648
|
+
if "signature" in delta:
|
|
649
|
+
block["signature"] = delta["signature"]
|
|
650
|
+
elif kind == "message_delta":
|
|
651
|
+
message.update(event.get("delta") or {})
|
|
652
|
+
message["usage"] = {**(message.get("usage") or {}), **(event.get("usage") or {})}
|
|
653
|
+
for block in blocks.values():
|
|
654
|
+
if "_json" in block:
|
|
655
|
+
raw = block.pop("_json")
|
|
656
|
+
try:
|
|
657
|
+
block["input"] = json.loads(raw) if raw else {}
|
|
658
|
+
except ValueError:
|
|
659
|
+
block["input"] = raw
|
|
660
|
+
message["content"] = [blocks[index] for index in sorted(blocks)]
|
|
661
|
+
return message
|
|
662
|
+
|
|
663
|
+
|
|
664
|
+
# ---- Raw HTTP capture ----------------------------------------------------------------------
|
|
665
|
+
|
|
666
|
+
def _patch_http():
|
|
667
|
+
try:
|
|
668
|
+
import httpx
|
|
669
|
+
except ImportError:
|
|
670
|
+
httpx = None
|
|
671
|
+
if httpx is not None and not getattr(httpx.Client.send, "_generalcontext", False):
|
|
672
|
+
sync_send, async_send = httpx.Client.send, httpx.AsyncClient.send
|
|
673
|
+
|
|
674
|
+
def send(self, request, *args, **kwargs):
|
|
675
|
+
meta = _http_start(request.url, lambda: request.content)
|
|
676
|
+
if meta is None:
|
|
677
|
+
return sync_send(self, request, *args, **kwargs)
|
|
678
|
+
return _http_finish(meta, lambda: sync_send(self, request, *args, **kwargs), kwargs.get("stream"),
|
|
679
|
+
lambda r: r.text)
|
|
680
|
+
|
|
681
|
+
async def asend(self, request, *args, **kwargs):
|
|
682
|
+
meta = _http_start(request.url, lambda: request.content)
|
|
683
|
+
if meta is None:
|
|
684
|
+
return await async_send(self, request, *args, **kwargs)
|
|
685
|
+
try:
|
|
686
|
+
response = await async_send(self, request, *args, **kwargs)
|
|
687
|
+
except Exception as error:
|
|
688
|
+
_record(meta, error=error)
|
|
689
|
+
raise
|
|
690
|
+
return _http_finish(meta, lambda: response, kwargs.get("stream"), lambda r: r.text)
|
|
691
|
+
|
|
692
|
+
send._generalcontext = asend._generalcontext = True
|
|
693
|
+
httpx.Client.send, httpx.AsyncClient.send = send, asend
|
|
694
|
+
try:
|
|
695
|
+
import requests
|
|
696
|
+
except ImportError:
|
|
697
|
+
return
|
|
698
|
+
if not getattr(requests.Session.send, "_generalcontext", False):
|
|
699
|
+
requests_send = requests.Session.send
|
|
700
|
+
|
|
701
|
+
def rsend(self, request, **kwargs):
|
|
702
|
+
meta = _http_start(request.url, lambda: request.body)
|
|
703
|
+
if meta is None:
|
|
704
|
+
return requests_send(self, request, **kwargs)
|
|
705
|
+
return _http_finish(meta, lambda: requests_send(self, request, **kwargs), kwargs.get("stream"),
|
|
706
|
+
lambda r: r.text)
|
|
707
|
+
|
|
708
|
+
rsend._generalcontext = True
|
|
709
|
+
requests.Session.send = rsend
|
|
710
|
+
|
|
711
|
+
|
|
712
|
+
def _http_start(url, body):
|
|
713
|
+
"""Meta for a raw chat completions POST, or None when it is not one to record."""
|
|
714
|
+
try:
|
|
715
|
+
url = str(url).split("?")[0]
|
|
716
|
+
if _inside.get() or not url.endswith("/chat/completions") or url.startswith(_base_url() + "/api/platform-collect"):
|
|
717
|
+
return None
|
|
718
|
+
raw = body()
|
|
719
|
+
return _start("openai-chat", json.loads(raw.decode() if isinstance(raw, bytes) else raw))
|
|
720
|
+
except Exception:
|
|
721
|
+
return None
|
|
722
|
+
|
|
723
|
+
|
|
724
|
+
def _http_finish(meta, send, streamed, text):
|
|
725
|
+
try:
|
|
726
|
+
response = send()
|
|
727
|
+
except Exception as error:
|
|
728
|
+
_record(meta, error=error)
|
|
729
|
+
raise
|
|
730
|
+
if streamed:
|
|
731
|
+
return response # The body is still unread; reading it here would change what the caller gets.
|
|
732
|
+
try:
|
|
733
|
+
body = text(response)
|
|
734
|
+
if body.lstrip().startswith("data:"):
|
|
735
|
+
chunks = [json.loads(line[5:]) for line in body.splitlines()
|
|
736
|
+
if line.startswith("data:") and line[5:].strip() != "[DONE]"]
|
|
737
|
+
_record(meta, _assemble_chat(chunks))
|
|
738
|
+
else:
|
|
739
|
+
_record(meta, json.loads(body))
|
|
740
|
+
except Exception as error:
|
|
741
|
+
log.warning("General Context could not read a chat completions response: %s", error)
|
|
742
|
+
return response
|
|
743
|
+
|
|
744
|
+
|
|
745
|
+
# ---- Redaction -----------------------------------------------------------------------------
|
|
746
|
+
|
|
747
|
+
PATTERNS = [
|
|
748
|
+
("email", re.compile(r"[A-Za-z0-9._%+-]{1,64}@[A-Za-z0-9-]{1,63}(?:\.[A-Za-z0-9-]{1,63}){0,8}\.[A-Za-z]{2,24}")),
|
|
749
|
+
("card", re.compile(r"\b(?:\d[ -]?){13,16}\b")),
|
|
750
|
+
("ssn", re.compile(r"\b\d{3}-\d{2}-\d{4}\b")),
|
|
751
|
+
("phone", re.compile(r"(?<![\w])\+?1?[\s.-]?\(?\d{3}\)?[\s.-]?\d{3}[\s.-]?\d{4}(?![\w])")),
|
|
752
|
+
]
|
|
753
|
+
|
|
754
|
+
|
|
755
|
+
def _redactor(redact):
|
|
756
|
+
if callable(redact):
|
|
757
|
+
return redact
|
|
758
|
+
if not redact:
|
|
759
|
+
return None
|
|
760
|
+
# A secret salt, the same across your processes, keeps placeholders stable everywhere while
|
|
761
|
+
# making them unlinkable to the raw value for anyone without it.
|
|
762
|
+
salt = os.environ.get("GENERALCONTEXT_REDACT_SALT", "").encode()
|
|
763
|
+
if not salt:
|
|
764
|
+
log.warning("GENERALCONTEXT_REDACT_SALT is not set; placeholders will differ between processes")
|
|
765
|
+
salt = os.urandom(16)
|
|
766
|
+
|
|
767
|
+
def placeholder(kind, value):
|
|
768
|
+
# Hash the normalized value, so (612) 555-0177 and +1 612-555-0177 read as the same number.
|
|
769
|
+
digits = "".join(ch for ch in value if ch.isdigit())
|
|
770
|
+
normal = value.lower() if kind == "email" else digits[-10:] if kind == "phone" else digits
|
|
771
|
+
return f"<{kind}:{hmac.new(salt, normal.encode(), hashlib.sha256).hexdigest()[:6]}>"
|
|
772
|
+
|
|
773
|
+
def scrub(text):
|
|
774
|
+
for kind, pattern in PATTERNS:
|
|
775
|
+
text = pattern.sub(lambda match: placeholder(kind, match.group()), text)
|
|
776
|
+
return text
|
|
777
|
+
return scrub
|
|
778
|
+
|
|
779
|
+
|
|
780
|
+
def _scrubbed(value, scrub):
|
|
781
|
+
if scrub is None:
|
|
782
|
+
return value
|
|
783
|
+
if isinstance(value, str):
|
|
784
|
+
return scrub(value)
|
|
785
|
+
if isinstance(value, dict):
|
|
786
|
+
return {key: _scrubbed(item, scrub) for key, item in value.items()}
|
|
787
|
+
if isinstance(value, list):
|
|
788
|
+
return [_scrubbed(item, scrub) for item in value]
|
|
789
|
+
return value
|
|
790
|
+
|
|
791
|
+
|
|
792
|
+
def _plain(value, depth=0):
|
|
793
|
+
"""SDK objects (pydantic models) as plain JSON. Request bodies may hold anything; keep what serializes."""
|
|
794
|
+
if depth > 40:
|
|
795
|
+
return None
|
|
796
|
+
if inspect.isclass(value): # e.g. response_format=SomeModel
|
|
797
|
+
return f"{value.__module__}.{value.__qualname__}"
|
|
798
|
+
if hasattr(value, "model_dump"):
|
|
799
|
+
return value.model_dump(mode="json", exclude_none=True)
|
|
800
|
+
if isinstance(value, dict):
|
|
801
|
+
return {str(key): _plain(item, depth + 1) for key, item in value.items()
|
|
802
|
+
if not str(key).startswith(("extra_headers", "timeout"))}
|
|
803
|
+
if isinstance(value, (list, tuple)):
|
|
804
|
+
return [_plain(item, depth + 1) for item in value]
|
|
805
|
+
if isinstance(value, (str, int, float, bool)) or value is None:
|
|
806
|
+
return value
|
|
807
|
+
return str(value)
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: generalcontext
|
|
3
|
+
Version: 0.2.0
|
|
4
|
+
Summary: Record your agent's OpenAI and Anthropic calls for General Context review and testing.
|
|
5
|
+
Project-URL: Homepage, https://generalcontextlabs.com/platform
|
|
6
|
+
Project-URL: Documentation, https://generalcontextlabs.com/platform/docs/sdk
|
|
7
|
+
Project-URL: Source, https://github.com/GeneralContext/sdk/tree/main/python
|
|
8
|
+
Project-URL: Issues, https://github.com/GeneralContext/sdk/issues
|
|
9
|
+
Author-email: General Context Labs <research@generalcontextlabs.com>
|
|
10
|
+
License-Expression: Apache-2.0
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
License-File: NOTICE
|
|
13
|
+
Keywords: agents,anthropic,evaluation,llm,observability,openai,opentelemetry
|
|
14
|
+
Classifier: Development Status :: 4 - Beta
|
|
15
|
+
Classifier: Intended Audience :: Developers
|
|
16
|
+
Classifier: Operating System :: OS Independent
|
|
17
|
+
Classifier: Programming Language :: Python :: 3
|
|
18
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
19
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
20
|
+
Classifier: Topic :: System :: Monitoring
|
|
21
|
+
Requires-Python: >=3.9
|
|
22
|
+
Description-Content-Type: text/markdown
|
|
23
|
+
|
|
24
|
+
# generalcontext
|
|
25
|
+
|
|
26
|
+
Record your agent's model calls for the General Context platform. No dependencies, and never
|
|
27
|
+
in your agent's way: calls are sent from a background thread, and our failures are logged, not raised.
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
pip install generalcontext
|
|
31
|
+
export GENERALCONTEXT_API_KEY=gck_... # from your workspace's Connect page
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
```python
|
|
35
|
+
import generalcontext
|
|
36
|
+
generalcontext.initialize(app_name="support-agent") # once per process, at startup
|
|
37
|
+
|
|
38
|
+
# Optional: group every call in one conversation under your own id
|
|
39
|
+
with generalcontext.session(conversation_id, user_id=customer_id, channel="chat"):
|
|
40
|
+
with generalcontext.task("triage"):
|
|
41
|
+
reply = client.chat.completions.create(...) # any OpenAI or Anthropic client
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
`initialize()` records every OpenAI (`chat.completions`, `responses`) and Anthropic (`messages`)
|
|
45
|
+
call in the process, sync and async, streamed or not, from clients created before or after it.
|
|
46
|
+
SDK clients are always recorded. Raw `httpx` and `requests` POSTs to `/chat/completions`
|
|
47
|
+
are recorded only with `instrument_http=True`. Call it in every process: queue workers and forked
|
|
48
|
+
children do not inherit it.
|
|
49
|
+
|
|
50
|
+
## Flush before exit
|
|
51
|
+
|
|
52
|
+
On serverless, in jobs, and before `os._exit()`, send what is queued:
|
|
53
|
+
|
|
54
|
+
```python
|
|
55
|
+
generalcontext.flush() # synchronous, waits up to 10s, never raises
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
## Outcomes and events
|
|
59
|
+
|
|
60
|
+
```python
|
|
61
|
+
generalcontext.report_outcome(conversation_id, "fail", note="customer reopened the ticket") # or "success"
|
|
62
|
+
generalcontext.log_event(conversation_id, "order.refunded", {"amount": 42.5}, event_id=f"refund:{refund.id}")
|
|
63
|
+
generalcontext.log_event(conversation_id, "handoff.to_human", {"reason": "billing"})
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
Both need the conversation to exist on our side first, so they flush pending calls before
|
|
67
|
+
sending. Both return `True` when accepted and never raise. Pass a stable `event_id` so a
|
|
68
|
+
retried event is counted once. Properties are flat strings, numbers and booleans.
|
|
69
|
+
|
|
70
|
+
## Redaction
|
|
71
|
+
|
|
72
|
+
```python
|
|
73
|
+
generalcontext.initialize(redact=True) # emails, phones, card- and SSN-like numbers
|
|
74
|
+
generalcontext.initialize(redact=fn) # or your own: str -> str, applied to every string
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
Values become stable placeholders (`<phone:3f2a1c>`) before anything leaves your process. Set
|
|
78
|
+
`GENERALCONTEXT_REDACT_SALT` to one secret in every process so placeholders match.
|
|
79
|
+
|
|
80
|
+
## Configuration
|
|
81
|
+
|
|
82
|
+
| Option | Default | Meaning |
|
|
83
|
+
| --- | --- | --- |
|
|
84
|
+
| `api_key` | `GENERALCONTEXT_API_KEY` | Workspace key. Without it nothing is sent. |
|
|
85
|
+
| `base_url` | `GENERALCONTEXT_BASE_URL`, else `https://generalcontextlabs.com` | Must be `https://` (plain `http://` only for localhost). |
|
|
86
|
+
| `app_name` | `None` | Label for this app. |
|
|
87
|
+
| `redact` | `False` | `True` for built-in PII redaction, or a `str -> str` function. Also applied to outcomes and events. |
|
|
88
|
+
| `before_send` | `None` | `fn(call) -> dict or None`, runs on the sender thread before redaction. `None` or an exception drops the call. |
|
|
89
|
+
| `instrument_http` | `False` | Also record raw `httpx`/`requests` POSTs to `/chat/completions`. |
|
|
90
|
+
| `disable_batch` | `False` | Send each call on its own. |
|
|
91
|
+
|
|
92
|
+
| Env var | Meaning |
|
|
93
|
+
| --- | --- |
|
|
94
|
+
| `GENERALCONTEXT_API_KEY` | Default `api_key`. |
|
|
95
|
+
| `GENERALCONTEXT_BASE_URL` | Default `base_url`. |
|
|
96
|
+
| `GENERALCONTEXT_REDACT_SALT` | Shared secret so redaction placeholders match across processes. |
|
|
97
|
+
| `GENERALCONTEXT_DISABLED` | `1`, `true` or `yes` makes `initialize()` do nothing. |
|
|
98
|
+
|
|
99
|
+
`HTTPS_PROXY` and `SSL_CERT_FILE` are honored via `urllib`. Redirects are never followed.
|
|
100
|
+
Calls over 4 MB are dropped, and the in-memory queue is capped at 64 MB. A second `initialize()`
|
|
101
|
+
is ignored. `wrap(client)` and `conversation(...)` from 0.1 still work.
|
|
102
|
+
|
|
103
|
+
Tests: `cd python && python -m unittest test_sdk -v`.
|
|
104
|
+
|
|
105
|
+
## License
|
|
106
|
+
|
|
107
|
+
Apache License 2.0. See [LICENSE](LICENSE) and [NOTICE](NOTICE).
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
generalcontext/__init__.py,sha256=Jbo8umNRoqpy-DsAOI-RsBzMCHt789Km6maTYxSdtQY,30837
|
|
2
|
+
generalcontext-0.2.0.dist-info/METADATA,sha256=jC8nIvVQtA69O3wyK8kZmSWOzH7vUfw5S9dK2Bz5-gY,4845
|
|
3
|
+
generalcontext-0.2.0.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
|
|
4
|
+
generalcontext-0.2.0.dist-info/licenses/LICENSE,sha256=z8d0m5b2O9McPEK1xHG_dWgUBT6EfBDz6wA0F7xSPTA,11358
|
|
5
|
+
generalcontext-0.2.0.dist-info/licenses/NOTICE,sha256=rlm5IZ6r_Swmm_FTUUzDLv9q8yyg7Z3rIiedmH4f8jE,168
|
|
6
|
+
generalcontext-0.2.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|