compache 0.14.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.
- compache/__init__.py +54 -0
- compache/__main__.py +3 -0
- compache/_tls.py +47 -0
- compache/api.py +761 -0
- compache/cli.py +665 -0
- compache/defaults.py +113 -0
- compache/demo/labels.jsonl +71 -0
- compache/demo/threads.jsonl.gz +0 -0
- compache/errors.py +5 -0
- compache/hermes.py +284 -0
- compache/labels.py +336 -0
- compache/langchain.py +65 -0
- compache/langfuse.py +738 -0
- compache/litellm.py +306 -0
- compache/live.py +1443 -0
- compache/mlflow.py +439 -0
- compache/models.json +156 -0
- compache/models.py +156 -0
- compache/notes.py +365 -0
- compache/optimizer/__init__.py +51 -0
- compache/optimizer/cache_breaks.py +755 -0
- compache/optimizer/contracts.py +196 -0
- compache/optimizer/distribution.py +1384 -0
- compache/optimizer/edit_accounting.py +324 -0
- compache/optimizer/header_traffic.py +225 -0
- compache/optimizer/host_ledger.py +318 -0
- compache/optimizer/parts.py +435 -0
- compache/optimizer/recovery.py +250 -0
- compache/optimizer/replay.py +638 -0
- compache/optimizer/retention.py +996 -0
- compache/optimizer/session.py +1269 -0
- compache/optimizer/shared_prefix.py +465 -0
- compache/optimizer/solve.py +382 -0
- compache/optimizer/usage.py +49 -0
- compache/otel.py +390 -0
- compache/profile.py +643 -0
- compache/proxy.py +352 -0
- compache/py.typed +0 -0
- compache/record.py +238 -0
- compache/threads.py +892 -0
- compache/wire.py +1548 -0
- compache/wrap.py +417 -0
- compache-0.14.0.dist-info/METADATA +174 -0
- compache-0.14.0.dist-info/RECORD +47 -0
- compache-0.14.0.dist-info/WHEEL +4 -0
- compache-0.14.0.dist-info/entry_points.txt +6 -0
- compache-0.14.0.dist-info/licenses/LICENCE.md +202 -0
compache/__init__.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"""compache: fit context compaction and prompt-cache keep-warm to your own conversations.
|
|
2
|
+
|
|
3
|
+
policy = compache.fit("threads.jsonl", "labels.jsonl", model="claude-sonnet-5-5")
|
|
4
|
+
client = compache.wrap(client, policy) # or: compache proxy --policy policy.json
|
|
5
|
+
with compache.thread("conversation-17"):
|
|
6
|
+
client.messages.create(...) # the policy is applied on the way out
|
|
7
|
+
|
|
8
|
+
Without past traces to fit on, record going forward and fit on the record:
|
|
9
|
+
|
|
10
|
+
client = compache.wrap(client, record="compache-record", sample=0.1)
|
|
11
|
+
policy = compache.fit("compache-record")
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
__version__ = "0.14.0"
|
|
15
|
+
|
|
16
|
+
from . import langchain, langfuse, litellm, mlflow, otel
|
|
17
|
+
from .api import Policy, Session, Turn, fit, replay
|
|
18
|
+
from .defaults import default_policy
|
|
19
|
+
from .errors import ContractError
|
|
20
|
+
from .live import Live
|
|
21
|
+
from .models import known_models, model_spec
|
|
22
|
+
from .optimizer.contracts import ModelSpec
|
|
23
|
+
from .optimizer.recovery import REOPEN_INSTRUCTIONS, REOPEN_TOOL
|
|
24
|
+
from .profile import profile
|
|
25
|
+
from .threads import Thread, load_threads
|
|
26
|
+
from .wrap import async_http_client, http_client, thread, wrap
|
|
27
|
+
|
|
28
|
+
__all__ = [
|
|
29
|
+
"REOPEN_INSTRUCTIONS",
|
|
30
|
+
"REOPEN_TOOL",
|
|
31
|
+
"ContractError",
|
|
32
|
+
"Live",
|
|
33
|
+
"ModelSpec",
|
|
34
|
+
"Policy",
|
|
35
|
+
"Session",
|
|
36
|
+
"Thread",
|
|
37
|
+
"Turn",
|
|
38
|
+
"async_http_client",
|
|
39
|
+
"default_policy",
|
|
40
|
+
"fit",
|
|
41
|
+
"http_client",
|
|
42
|
+
"known_models",
|
|
43
|
+
"langchain",
|
|
44
|
+
"langfuse",
|
|
45
|
+
"litellm",
|
|
46
|
+
"load_threads",
|
|
47
|
+
"mlflow",
|
|
48
|
+
"model_spec",
|
|
49
|
+
"otel",
|
|
50
|
+
"profile",
|
|
51
|
+
"replay",
|
|
52
|
+
"thread",
|
|
53
|
+
"wrap",
|
|
54
|
+
]
|
compache/__main__.py
ADDED
compache/_tls.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"""Root certificates for a Python that ships none, as the python.org builds for macOS do.
|
|
2
|
+
|
|
3
|
+
Every HTTPS call here first goes out with Python's own certificates. When the
|
|
4
|
+
host cannot be verified with those, `fall_back()` switches the process to
|
|
5
|
+
certifi's bundle or the system's, and the call is made once more.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import os
|
|
11
|
+
import ssl
|
|
12
|
+
|
|
13
|
+
__all__ = ["FOUND", "fall_back", "unverified"]
|
|
14
|
+
|
|
15
|
+
# The context HTTPS calls are made with. None stands for Python's own default.
|
|
16
|
+
FOUND: dict = {"context": None}
|
|
17
|
+
_BUNDLES = (
|
|
18
|
+
"/etc/ssl/cert.pem",
|
|
19
|
+
"/etc/ssl/certs/ca-certificates.crt",
|
|
20
|
+
"/etc/pki/tls/certs/ca-bundle.crt",
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def unverified(error: BaseException) -> bool:
|
|
25
|
+
"""Whether a call failed because the host's certificate could not be checked."""
|
|
26
|
+
return "CERTIFICATE_VERIFY_FAILED" in f"{error} {getattr(error, 'reason', '')}"
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def _other() -> ssl.SSLContext | None:
|
|
30
|
+
try:
|
|
31
|
+
import certifi
|
|
32
|
+
|
|
33
|
+
return ssl.create_default_context(cafile=certifi.where())
|
|
34
|
+
except ImportError:
|
|
35
|
+
pass
|
|
36
|
+
for bundle in _BUNDLES:
|
|
37
|
+
if os.path.exists(bundle):
|
|
38
|
+
return ssl.create_default_context(cafile=bundle)
|
|
39
|
+
return None
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def fall_back() -> bool:
|
|
43
|
+
"""Switch to other root certificates. False when that was done before, or there are none."""
|
|
44
|
+
if FOUND["context"] is not None:
|
|
45
|
+
return False
|
|
46
|
+
FOUND["context"] = _other()
|
|
47
|
+
return FOUND["context"] is not None
|