cap-shield 0.1.0__tar.gz → 0.2.0__tar.gz

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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: cap-shield
3
- Version: 0.1.0
3
+ Version: 0.2.0
4
4
  Summary: Context selection and compression for AI agents — measured, not estimated
5
5
  Project-URL: Homepage, https://cap-shield-robin.fly.dev
6
6
  Project-URL: Documentation, https://cap-shield-robin.fly.dev/docs/quickstart
@@ -59,15 +59,22 @@ itself.
59
59
 
60
60
  ## Measure before you commit
61
61
 
62
- No account needed:
62
+ **No account, no key:**
63
63
 
64
64
  ```python
65
- CapShield(api_key="anything").measure(
65
+ from cap_shield import measure, print_measurement
66
+
67
+ print_measurement(measure(
66
68
  texts=["...your actual messages..."],
67
- query="what your agent would search for")
69
+ query="what your agent would search for"))
68
70
  ```
69
71
 
70
- Runs on your own traffic and stores nothing.
72
+ Runs on your own traffic and stores nothing — the text is compressed in
73
+ memory, the numbers computed, and everything discarded. No dictionary is
74
+ trained on it.
75
+
76
+ Rate limited to 20 measurements per hour per IP. Enough to evaluate, not
77
+ enough to use the service for free.
71
78
 
72
79
  ## Two mechanisms, deliberately separate
73
80
 
@@ -41,15 +41,22 @@ itself.
41
41
 
42
42
  ## Measure before you commit
43
43
 
44
- No account needed:
44
+ **No account, no key:**
45
45
 
46
46
  ```python
47
- CapShield(api_key="anything").measure(
47
+ from cap_shield import measure, print_measurement
48
+
49
+ print_measurement(measure(
48
50
  texts=["...your actual messages..."],
49
- query="what your agent would search for")
51
+ query="what your agent would search for"))
50
52
  ```
51
53
 
52
- Runs on your own traffic and stores nothing.
54
+ Runs on your own traffic and stores nothing — the text is compressed in
55
+ memory, the numbers computed, and everything discarded. No dictionary is
56
+ trained on it.
57
+
58
+ Rate limited to 20 measurements per hour per IP. Enough to evaluate, not
59
+ enough to use the service for free.
53
60
 
54
61
  ## Two mechanisms, deliberately separate
55
62
 
@@ -34,7 +34,7 @@ import os
34
34
  from dataclasses import dataclass, field
35
35
  from typing import Any, Iterable
36
36
 
37
- __version__ = "0.1.0"
37
+ __version__ = "0.2.0"
38
38
 
39
39
  BAS = "https://cap-shield-robin.fly.dev"
40
40
  TIMEOUT = 30.0
@@ -174,7 +174,10 @@ class CapShield:
174
174
  if not self.api_key:
175
175
  raise CapShieldError(
176
176
  "Ingen API-nyckel. Ange api_key= eller sätt "
177
- "CAP_SHIELD_API_KEY. Hämta en på " + BAS
177
+ "CAP_SHIELD_API_KEY. Hämta en på " + BAS + "\n\n"
178
+ "Vill du bara mäta på din egen trafik behövs ingen nyckel:\n"
179
+ " from cap_shield import measure\n"
180
+ " measure(texts=[...], query='...')"
178
181
  )
179
182
  self.base_url = base_url.rstrip("/")
180
183
  self.timeout = timeout
@@ -398,5 +401,84 @@ class AsyncCapShield(CapShield):
398
401
  return iid
399
402
 
400
403
 
404
+ def measure(texts: Iterable[str], *, dict_id: str = "finance",
405
+ query: str | None = None, base_url: str = BAS,
406
+ timeout: float = TIMEOUT) -> dict:
407
+ """Mät på din egen trafik. INGEN NYCKEL BEHÖVS.
408
+
409
+ from cap_shield import measure
410
+ r = measure(texts=[...dina meddelanden...],
411
+ query="vad din agent skulle söka efter")
412
+
413
+ Anges query mäts även tokenbesparingen. Utan den mäts bara
414
+ komprimeringen.
415
+
416
+ INGET LAGRAS. Texten komprimeras i minnet, talen räknas, och allt
417
+ kastas. Ingen ordbok tränas på den.
418
+
419
+ VARFÖR DETTA GÅR UTAN NYCKEL:
420
+ Hela produktens argument är "mät själv, tro oss inte". Att kräva
421
+ ett konto före första mätningen vore att be om förtroende innan
422
+ man visat något.
423
+
424
+ Vägen är kvoterad på servern — tjugo mätningar per timme och
425
+ IP-adress, tak på 200 meddelanden och 512 kB per anrop. Det räcker
426
+ för att utvärdera, inte för att använda tjänsten gratis.
427
+
428
+ RESULTATET ÄR RÅTT från API:t: 'individual', 'batched',
429
+ 'token_saving' och 'recommendation'.
430
+ """
431
+ import httpx
432
+
433
+ kropp: dict = {"texts": list(texts), "dict_id": dict_id}
434
+ if query:
435
+ kropp["query"] = query
436
+ try:
437
+ r = httpx.post(f"{base_url.rstrip('/')}/api/v1/try", json=kropp,
438
+ timeout=timeout,
439
+ headers={"User-Agent":
440
+ f"cap-shield-python/{__version__}"})
441
+ except Exception as exc:
442
+ raise CapShieldError(f"Kunde inte nå {base_url}: {exc}") from None
443
+ if r.status_code >= 400:
444
+ try:
445
+ kropp_svar = r.json()
446
+ except Exception:
447
+ kropp_svar = r.text[:400]
448
+ raise _tolka_fel(r.status_code, kropp_svar)
449
+ return r.json()
450
+
451
+
452
+ def print_measurement(resultat: dict) -> None:
453
+ """Skriv en mätning som ett kvitto.
454
+
455
+ Samma form som Kontext.receipt, så att den som provar utan konto ser
456
+ samma sorts utskrift som den som har ett.
457
+ """
458
+ i = resultat.get("individual", {})
459
+ b = resultat.get("batched") or {}
460
+ t = resultat.get("token_saving") or {}
461
+
462
+ print(f"CAP-Shield {resultat.get('messages', 0)} messages, "
463
+ f"{resultat.get('raw_bytes', 0)} bytes raw")
464
+ print(f" {i.get('saving_pct', 0)} % over the wire, "
465
+ f"one at a time")
466
+ if b:
467
+ print(f" {b.get('saving_pct', 0)} % batched "
468
+ f"({b.get('bytes_per_message', 0)} B per message)")
469
+ if i.get("degraded"):
470
+ print(f" {i['degraded']} messages came out LARGER")
471
+
472
+ if t.get("measured"):
473
+ print(f" {t['saving_pct']} % of context not sent "
474
+ f"({t['entries_selected']} of {t['entries_total']} entries)")
475
+ print(f" NOTE: {t['caveat']}")
476
+ elif t:
477
+ print(f" token saving: {t.get('detail', '')}")
478
+
479
+ if resultat.get("recommendation"):
480
+ print(f" {resultat['recommendation']}")
481
+
482
+
401
483
  __all__ = ["CapShield", "AsyncCapShield", "Kontext", "CapShieldError",
402
- "__version__"]
484
+ "measure", "print_measurement", "__version__"]
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "cap-shield"
7
- version = "0.1.0"
7
+ version = "0.2.0"
8
8
  description = "Context selection and compression for AI agents — measured, not estimated"
9
9
  readme = "README_SDK.md"
10
10
  requires-python = ">=3.9"