contextpress 0.5.1__tar.gz → 0.5.2__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.
Files changed (55) hide show
  1. {contextpress-0.5.1 → contextpress-0.5.2}/CHANGELOG.md +4 -0
  2. {contextpress-0.5.1 → contextpress-0.5.2}/PKG-INFO +2 -3
  3. {contextpress-0.5.1 → contextpress-0.5.2}/README.md +1 -2
  4. {contextpress-0.5.1 → contextpress-0.5.2}/contextpress/__init__.py +1 -1
  5. {contextpress-0.5.1 → contextpress-0.5.2}/contextpress/llm/adapters.py +34 -9
  6. {contextpress-0.5.1 → contextpress-0.5.2}/examples/llm_tier_openai.py +1 -3
  7. {contextpress-0.5.1 → contextpress-0.5.2}/pyproject.toml +1 -1
  8. contextpress-0.5.2/tests/test_v052.py +32 -0
  9. {contextpress-0.5.1 → contextpress-0.5.2}/.gitignore +0 -0
  10. {contextpress-0.5.1 → contextpress-0.5.2}/CITATION.cff +0 -0
  11. {contextpress-0.5.1 → contextpress-0.5.2}/CONTRIBUTING.md +0 -0
  12. {contextpress-0.5.1 → contextpress-0.5.2}/LICENSE +0 -0
  13. {contextpress-0.5.1 → contextpress-0.5.2}/NOTICE +0 -0
  14. {contextpress-0.5.1 → contextpress-0.5.2}/contextpress/_bootstrap.py +0 -0
  15. {contextpress-0.5.1 → contextpress-0.5.2}/contextpress/compression.py +0 -0
  16. {contextpress-0.5.1 → contextpress-0.5.2}/contextpress/core.py +0 -0
  17. {contextpress-0.5.1 → contextpress-0.5.2}/contextpress/llm/__init__.py +0 -0
  18. {contextpress-0.5.1 → contextpress-0.5.2}/contextpress/llm/_helpers.py +0 -0
  19. {contextpress-0.5.1 → contextpress-0.5.2}/contextpress/llm/base.py +0 -0
  20. {contextpress-0.5.1 → contextpress-0.5.2}/contextpress/models.py +0 -0
  21. {contextpress-0.5.1 → contextpress-0.5.2}/contextpress/normalizer.py +0 -0
  22. {contextpress-0.5.1 → contextpress-0.5.2}/contextpress/pipeline.py +0 -0
  23. {contextpress-0.5.1 → contextpress-0.5.2}/contextpress/profiles.py +0 -0
  24. {contextpress-0.5.1 → contextpress-0.5.2}/contextpress/py.typed +0 -0
  25. {contextpress-0.5.1 → contextpress-0.5.2}/contextpress/registry.py +0 -0
  26. {contextpress-0.5.1 → contextpress-0.5.2}/contextpress/stats.py +0 -0
  27. {contextpress-0.5.1 → contextpress-0.5.2}/contextpress/strategies/__init__.py +0 -0
  28. {contextpress-0.5.1 → contextpress-0.5.2}/contextpress/strategies/base.py +0 -0
  29. {contextpress-0.5.1 → contextpress-0.5.2}/contextpress/strategies/budget.py +0 -0
  30. {contextpress-0.5.1 → contextpress-0.5.2}/contextpress/strategies/filler.py +0 -0
  31. {contextpress-0.5.1 → contextpress-0.5.2}/contextpress/strategies/recency.py +0 -0
  32. {contextpress-0.5.1 → contextpress-0.5.2}/contextpress/strategies/repetition.py +0 -0
  33. {contextpress-0.5.1 → contextpress-0.5.2}/contextpress/strategies/resolution.py +0 -0
  34. {contextpress-0.5.1 → contextpress-0.5.2}/contextpress/warnings_capture.py +0 -0
  35. {contextpress-0.5.1 → contextpress-0.5.2}/examples/benchmark_presets.py +0 -0
  36. {contextpress-0.5.1 → contextpress-0.5.2}/examples/dry_run_preview.py +0 -0
  37. {contextpress-0.5.1 → contextpress-0.5.2}/examples/estimate_and_stats.py +0 -0
  38. {contextpress-0.5.1 → contextpress-0.5.2}/examples/llm_tier_claude.py +0 -0
  39. {contextpress-0.5.1 → contextpress-0.5.2}/examples/llm_tier_gemini.py +0 -0
  40. {contextpress-0.5.1 → contextpress-0.5.2}/examples/llm_tier_ollama.py +0 -0
  41. {contextpress-0.5.1 → contextpress-0.5.2}/tests/__init__.py +0 -0
  42. {contextpress-0.5.1 → contextpress-0.5.2}/tests/test_budget.py +0 -0
  43. {contextpress-0.5.1 → contextpress-0.5.2}/tests/test_filler.py +0 -0
  44. {contextpress-0.5.1 → contextpress-0.5.2}/tests/test_llm_helpers.py +0 -0
  45. {contextpress-0.5.1 → contextpress-0.5.2}/tests/test_models.py +0 -0
  46. {contextpress-0.5.1 → contextpress-0.5.2}/tests/test_normalizer.py +0 -0
  47. {contextpress-0.5.1 → contextpress-0.5.2}/tests/test_pipeline.py +0 -0
  48. {contextpress-0.5.1 → contextpress-0.5.2}/tests/test_recency.py +0 -0
  49. {contextpress-0.5.1 → contextpress-0.5.2}/tests/test_repetition.py +0 -0
  50. {contextpress-0.5.1 → contextpress-0.5.2}/tests/test_resolution.py +0 -0
  51. {contextpress-0.5.1 → contextpress-0.5.2}/tests/test_stats.py +0 -0
  52. {contextpress-0.5.1 → contextpress-0.5.2}/tests/test_v03.py +0 -0
  53. {contextpress-0.5.1 → contextpress-0.5.2}/tests/test_v04.py +0 -0
  54. {contextpress-0.5.1 → contextpress-0.5.2}/tests/test_v05.py +0 -0
  55. {contextpress-0.5.1 → contextpress-0.5.2}/tests/test_v051.py +0 -0
@@ -4,6 +4,10 @@ All notable changes to `contextpress` are recorded here.
4
4
  The format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/)
5
5
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
6
 
7
+ ## [0.5.2] - 2026-07-22
8
+
9
+ - **`OpenAIBackend`** — optional `api_key` / env (`OPENAI_API_KEY`); no manual client required.
10
+
7
11
  ## [0.5.1] - 2026-07-22
8
12
 
9
13
  - **`ClaudeBackend`** — shortcut for Anthropic Claude (`ANTHROPIC_API_KEY` or `api_key=`).
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: contextpress
3
- Version: 0.5.1
3
+ Version: 0.5.2
4
4
  Summary: Deterministic context compression for LLM chat, RAG, and agent pipelines
5
5
  Project-URL: Homepage, https://github.com/Taha-azizi/contextpress
6
6
  Project-URL: Documentation, https://github.com/Taha-azizi/contextpress#readme
@@ -459,11 +459,10 @@ Optional constructor knobs: **`llm_min_input_chars`**, **`llm_max_summary_tokens
459
459
  **Install SDKs** (not bundled): `pip install openai`, `anthropic`, and/or **`ollama`** (for local Ollama), or `pip install "contextpress[llm]"` from this repo’s `pyproject.toml` to pull all optional LLM clients.
460
460
 
461
461
  ```python
462
- from openai import OpenAI
463
462
  from contextpress import ContextManager
464
463
  from contextpress.llm.adapters import OpenAIBackend
465
464
 
466
- backend = OpenAIBackend(client=OpenAI(), model="gpt-4o-mini")
465
+ backend = OpenAIBackend(model="gpt-4o-mini") # uses OPENAI_API_KEY
467
466
  cm = ContextManager(
468
467
  type="chat",
469
468
  llm_backend=backend,
@@ -218,11 +218,10 @@ Optional constructor knobs: **`llm_min_input_chars`**, **`llm_max_summary_tokens
218
218
  **Install SDKs** (not bundled): `pip install openai`, `anthropic`, and/or **`ollama`** (for local Ollama), or `pip install "contextpress[llm]"` from this repo’s `pyproject.toml` to pull all optional LLM clients.
219
219
 
220
220
  ```python
221
- from openai import OpenAI
222
221
  from contextpress import ContextManager
223
222
  from contextpress.llm.adapters import OpenAIBackend
224
223
 
225
- backend = OpenAIBackend(client=OpenAI(), model="gpt-4o-mini")
224
+ backend = OpenAIBackend(model="gpt-4o-mini") # uses OPENAI_API_KEY
226
225
  cm = ContextManager(
227
226
  type="chat",
228
227
  llm_backend=backend,
@@ -16,7 +16,7 @@ __all__ = [
16
16
  "CompressionResult",
17
17
  "CompressionStats",
18
18
  ]
19
- __version__ = "0.5.1"
19
+ __version__ = "0.5.2"
20
20
 
21
21
 
22
22
  def __getattr__(name: str) -> Any:
@@ -27,20 +27,45 @@ def _ollama_response_text(resp: Any) -> str:
27
27
 
28
28
  class OpenAIBackend(LLMBackend):
29
29
  """
30
- Adapter for OpenAI-compatible APIs.
31
- Requires: pip install openai
32
- User must pass their own client instance.
30
+ Adapter for **OpenAI** chat APIs.
31
+
32
+ Requires: ``pip install openai``
33
+
34
+ Usage (API key from env ``OPENAI_API_KEY`` or ``api_key=``)::
33
35
 
34
- Usage:
35
- from openai import OpenAI
36
36
  from contextpress.llm.adapters import OpenAIBackend
37
37
 
38
- backend = OpenAIBackend(client=OpenAI(), model="gpt-4o-mini")
39
- cm = ContextManager(type="chat", llm_backend=backend)
38
+ backend = OpenAIBackend(model=\"gpt-4o-mini\")
39
+ cm = ContextManager(type=\"chat\", llm_backend=backend)
40
+
41
+ Or pass your own client::
42
+
43
+ from openai import OpenAI
44
+
45
+ backend = OpenAIBackend(client=OpenAI(), model=\"gpt-4o-mini\")
40
46
  """
41
47
 
42
- def __init__(self, client: Any, model: str = "gpt-4o-mini"):
43
- self.client = client
48
+ def __init__(
49
+ self,
50
+ client: Any | None = None,
51
+ model: str = "gpt-4o-mini",
52
+ *,
53
+ api_key: str | None = None,
54
+ ):
55
+ if client is not None:
56
+ self.client = client
57
+ self.model = model
58
+ return
59
+ try:
60
+ from openai import OpenAI
61
+ except ImportError as exc:
62
+ raise ImportError(
63
+ "OpenAIBackend requires the 'openai' package. " "Install with: pip install openai"
64
+ ) from exc
65
+ key = api_key or os.environ.get("OPENAI_API_KEY")
66
+ if not key:
67
+ raise ValueError("Set OPENAI_API_KEY or pass api_key= for OpenAIBackend")
68
+ self.client = OpenAI(api_key=key)
44
69
  self.model = model
45
70
 
46
71
  def summarize(self, text: str, max_tokens: int) -> str:
@@ -17,8 +17,6 @@ from __future__ import annotations
17
17
  import os
18
18
  import sys
19
19
 
20
- from openai import OpenAI
21
-
22
20
  from contextpress import ContextManager
23
21
  from contextpress.llm.adapters import OpenAIBackend
24
22
 
@@ -35,7 +33,7 @@ def main() -> None:
35
33
  {"role": "user", "content": "And numpy? " * 80},
36
34
  ]
37
35
 
38
- backend = OpenAIBackend(client=OpenAI(), model="gpt-4o-mini")
36
+ backend = OpenAIBackend(model="gpt-4o-mini")
39
37
  cm = ContextManager(
40
38
  type="chat",
41
39
  llm_backend=backend,
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "contextpress"
7
- version = "0.5.1"
7
+ version = "0.5.2"
8
8
  description = "Deterministic context compression for LLM chat, RAG, and agent pipelines"
9
9
  readme = "README.md"
10
10
  license = { file = "LICENSE" }
@@ -0,0 +1,32 @@
1
+ import os
2
+ from unittest.mock import MagicMock, patch
3
+
4
+ import pytest
5
+
6
+ from contextpress.llm.adapters import OpenAIBackend
7
+
8
+
9
+ def test_openai_backend_with_client():
10
+ client = MagicMock()
11
+ backend = OpenAIBackend(client=client, model="gpt-4o-mini")
12
+ assert backend.client is client
13
+ assert backend.model == "gpt-4o-mini"
14
+
15
+
16
+ def test_openai_backend_requires_api_key():
17
+ mock_openai = MagicMock()
18
+ with patch.dict(os.environ, {}, clear=True):
19
+ with patch.dict("sys.modules", {"openai": mock_openai}):
20
+ with pytest.raises(ValueError, match="OPENAI_API_KEY"):
21
+ OpenAIBackend()
22
+
23
+
24
+ def test_openai_backend_from_api_key():
25
+ mock_module = MagicMock()
26
+ mock_client = MagicMock()
27
+ mock_module.OpenAI.return_value = mock_client
28
+ with patch.dict(os.environ, {"OPENAI_API_KEY": "sk-test"}):
29
+ with patch.dict("sys.modules", {"openai": mock_module}):
30
+ backend = OpenAIBackend(model="gpt-4o-mini")
31
+ assert backend.client is mock_client
32
+ mock_module.OpenAI.assert_called_once_with(api_key="sk-test")
File without changes
File without changes
File without changes
File without changes