feedsummary-core 1.0.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 (31) hide show
  1. feedsummary_core-1.0.2/.github/workflows/release.yaml +101 -0
  2. feedsummary_core-1.0.2/.gitignore +207 -0
  3. feedsummary_core-1.0.2/LICENSE +28 -0
  4. feedsummary_core-1.0.2/PKG-INFO +26 -0
  5. feedsummary_core-1.0.2/README.md +2 -0
  6. feedsummary_core-1.0.2/pyproject.toml +42 -0
  7. feedsummary_core-1.0.2/setup.cfg +4 -0
  8. feedsummary_core-1.0.2/src/feedsummary_core/__init__.py +38 -0
  9. feedsummary_core-1.0.2/src/feedsummary_core/_version.py +34 -0
  10. feedsummary_core-1.0.2/src/feedsummary_core/llm_client/__init__.py +94 -0
  11. feedsummary_core-1.0.2/src/feedsummary_core/llm_client/fallback_client.py +113 -0
  12. feedsummary_core-1.0.2/src/feedsummary_core/llm_client/ollama_cloud.py +209 -0
  13. feedsummary_core-1.0.2/src/feedsummary_core/llm_client/ollama_local.py +214 -0
  14. feedsummary_core-1.0.2/src/feedsummary_core/persistence/SqliteStore.py +679 -0
  15. feedsummary_core-1.0.2/src/feedsummary_core/persistence/TinyDbStore.py +292 -0
  16. feedsummary_core-1.0.2/src/feedsummary_core/persistence/__init__.py +111 -0
  17. feedsummary_core-1.0.2/src/feedsummary_core/summarizer/__init__.py +32 -0
  18. feedsummary_core-1.0.2/src/feedsummary_core/summarizer/batching.py +378 -0
  19. feedsummary_core-1.0.2/src/feedsummary_core/summarizer/chat.py +113 -0
  20. feedsummary_core-1.0.2/src/feedsummary_core/summarizer/helpers.py +490 -0
  21. feedsummary_core-1.0.2/src/feedsummary_core/summarizer/ingest.py +326 -0
  22. feedsummary_core-1.0.2/src/feedsummary_core/summarizer/main.py +900 -0
  23. feedsummary_core-1.0.2/src/feedsummary_core/summarizer/prompt_lab.py +424 -0
  24. feedsummary_core-1.0.2/src/feedsummary_core/summarizer/prompt_replay.py +507 -0
  25. feedsummary_core-1.0.2/src/feedsummary_core/summarizer/summarizer.py +941 -0
  26. feedsummary_core-1.0.2/src/feedsummary_core/summarizer/token_budget.py +96 -0
  27. feedsummary_core-1.0.2/src/feedsummary_core.egg-info/PKG-INFO +26 -0
  28. feedsummary_core-1.0.2/src/feedsummary_core.egg-info/SOURCES.txt +29 -0
  29. feedsummary_core-1.0.2/src/feedsummary_core.egg-info/dependency_links.txt +1 -0
  30. feedsummary_core-1.0.2/src/feedsummary_core.egg-info/requires.txt +10 -0
  31. feedsummary_core-1.0.2/src/feedsummary_core.egg-info/top_level.txt +1 -0
@@ -0,0 +1,101 @@
1
+ name: Release to PyPI + GitHub Releases
2
+
3
+ on:
4
+ push:
5
+ tags:
6
+ - "v*"
7
+
8
+ permissions:
9
+ contents: write
10
+ id-token: write
11
+
12
+ jobs:
13
+ gate:
14
+ name: Validate tag is semver vX.Y.Z
15
+ runs-on: ubuntu-latest
16
+ outputs:
17
+ is_semver: ${{ steps.check.outputs.is_semver }}
18
+ steps:
19
+ - name: Check tag format
20
+ id: check
21
+ shell: bash
22
+ run: |
23
+ TAG="${GITHUB_REF_NAME}"
24
+ if [[ "$TAG" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
25
+ echo "is_semver=true" >> "$GITHUB_OUTPUT"
26
+ else
27
+ echo "is_semver=false" >> "$GITHUB_OUTPUT"
28
+ fi
29
+
30
+ build:
31
+ name: Build distributions
32
+ needs: gate
33
+ if: needs.gate.outputs.is_semver == 'true'
34
+ runs-on: ubuntu-latest
35
+
36
+ steps:
37
+ - uses: actions/checkout@v4
38
+
39
+ - name: Set up Python
40
+ uses: actions/setup-python@v5
41
+ with:
42
+ python-version: "3.11"
43
+
44
+ - name: Install build tools
45
+ run: |
46
+ python -m pip install --upgrade pip
47
+ python -m pip install build twine
48
+
49
+ - name: Build sdist + wheel
50
+ run: python -m build
51
+
52
+ - name: Twine check
53
+ run: python -m twine check dist/*
54
+
55
+ - name: Upload dist artifacts
56
+ uses: actions/upload-artifact@v4
57
+ with:
58
+ name: dist
59
+ path: dist/*
60
+
61
+ publish_pypi:
62
+ name: Publish to PyPI
63
+ needs: [gate, build]
64
+ if: needs.gate.outputs.is_semver == 'true'
65
+ runs-on: ubuntu-latest
66
+ environment:
67
+ name: pypi
68
+ url: https://pypi.org/project/feedsummary_core/
69
+
70
+ steps:
71
+ - name: Download dist artifacts
72
+ uses: actions/download-artifact@v4
73
+ with:
74
+ name: dist
75
+ path: dist
76
+
77
+ - name: Publish to PyPI (Trusted Publishing)
78
+ uses: pypa/gh-action-pypi-publish@release/v1
79
+ with:
80
+ packages-dir: dist
81
+ github_release:
82
+ name: Create GitHub Release
83
+ needs: [gate, build]
84
+ if: needs.gate.outputs.is_semver == 'true'
85
+ runs-on: ubuntu-latest
86
+
87
+ steps:
88
+ - name: Download dist artifacts
89
+ uses: actions/download-artifact@v4
90
+ with:
91
+ name: dist
92
+ path: dist
93
+
94
+ - name: Create GitHub Release and upload artifacts
95
+ uses: softprops/action-gh-release@v2
96
+ with:
97
+ tag_name: ${{ github.ref_name }}
98
+ name: ${{ github.ref_name }}
99
+ generate_release_notes: true
100
+ files: |
101
+ dist/*
@@ -0,0 +1,207 @@
1
+ # Byte-compiled / optimized / DLL files
2
+ __pycache__/
3
+ *.py[codz]
4
+ *$py.class
5
+
6
+ # C extensions
7
+ *.so
8
+
9
+ # Distribution / packaging
10
+ .Python
11
+ build/
12
+ develop-eggs/
13
+ dist/
14
+ downloads/
15
+ eggs/
16
+ .eggs/
17
+ lib/
18
+ lib64/
19
+ parts/
20
+ sdist/
21
+ var/
22
+ wheels/
23
+ share/python-wheels/
24
+ *.egg-info/
25
+ .installed.cfg
26
+ *.egg
27
+ MANIFEST
28
+
29
+ # PyInstaller
30
+ # Usually these files are written by a python script from a template
31
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
32
+ *.manifest
33
+ *.spec
34
+
35
+ # Installer logs
36
+ pip-log.txt
37
+ pip-delete-this-directory.txt
38
+
39
+ # Unit test / coverage reports
40
+ htmlcov/
41
+ .tox/
42
+ .nox/
43
+ .coverage
44
+ .coverage.*
45
+ .cache
46
+ nosetests.xml
47
+ coverage.xml
48
+ *.cover
49
+ *.py.cover
50
+ .hypothesis/
51
+ .pytest_cache/
52
+ cover/
53
+
54
+ # Translations
55
+ *.mo
56
+ *.pot
57
+
58
+ # Django stuff:
59
+ *.log
60
+ local_settings.py
61
+ db.sqlite3
62
+ db.sqlite3-journal
63
+
64
+ # Flask stuff:
65
+ instance/
66
+ .webassets-cache
67
+
68
+ # Scrapy stuff:
69
+ .scrapy
70
+
71
+ # Sphinx documentation
72
+ docs/_build/
73
+
74
+ # PyBuilder
75
+ .pybuilder/
76
+ target/
77
+
78
+ # Jupyter Notebook
79
+ .ipynb_checkpoints
80
+
81
+ # IPython
82
+ profile_default/
83
+ ipython_config.py
84
+
85
+ # pyenv
86
+ # For a library or package, you might want to ignore these files since the code is
87
+ # intended to run in multiple environments; otherwise, check them in:
88
+ # .python-version
89
+
90
+ # pipenv
91
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
93
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
94
+ # install all needed dependencies.
95
+ #Pipfile.lock
96
+
97
+ # UV
98
+ # Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
99
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
100
+ # commonly ignored for libraries.
101
+ #uv.lock
102
+
103
+ # poetry
104
+ # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
105
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
106
+ # commonly ignored for libraries.
107
+ # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
108
+ #poetry.lock
109
+ #poetry.toml
110
+
111
+ # pdm
112
+ # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
113
+ # pdm recommends including project-wide configuration in pdm.toml, but excluding .pdm-python.
114
+ # https://pdm-project.org/en/latest/usage/project/#working-with-version-control
115
+ #pdm.lock
116
+ #pdm.toml
117
+ .pdm-python
118
+ .pdm-build/
119
+
120
+ # pixi
121
+ # Similar to Pipfile.lock, it is generally recommended to include pixi.lock in version control.
122
+ #pixi.lock
123
+ # Pixi creates a virtual environment in the .pixi directory, just like venv module creates one
124
+ # in the .venv directory. It is recommended not to include this directory in version control.
125
+ .pixi
126
+
127
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
128
+ __pypackages__/
129
+
130
+ # Celery stuff
131
+ celerybeat-schedule
132
+ celerybeat.pid
133
+
134
+ # SageMath parsed files
135
+ *.sage.py
136
+
137
+ # Environments
138
+ .env
139
+ .envrc
140
+ .venv
141
+ env/
142
+ venv/
143
+ ENV/
144
+ env.bak/
145
+ venv.bak/
146
+
147
+ # Spyder project settings
148
+ .spyderproject
149
+ .spyproject
150
+
151
+ # Rope project settings
152
+ .ropeproject
153
+
154
+ # mkdocs documentation
155
+ /site
156
+
157
+ # mypy
158
+ .mypy_cache/
159
+ .dmypy.json
160
+ dmypy.json
161
+
162
+ # Pyre type checker
163
+ .pyre/
164
+
165
+ # pytype static type analyzer
166
+ .pytype/
167
+
168
+ # Cython debug symbols
169
+ cython_debug/
170
+
171
+ # PyCharm
172
+ # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
173
+ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
174
+ # and can be added to the global gitignore or merged into this file. For a more nuclear
175
+ # option (not recommended) you can uncomment the following to ignore the entire idea folder.
176
+ #.idea/
177
+
178
+ # Abstra
179
+ # Abstra is an AI-powered process automation framework.
180
+ # Ignore directories containing user credentials, local state, and settings.
181
+ # Learn more at https://abstra.io/docs
182
+ .abstra/
183
+
184
+ # Visual Studio Code
185
+ # Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore
186
+ # that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
187
+ # and can be added to the global gitignore or merged into this file. However, if you prefer,
188
+ # you could uncomment the following to ignore the entire vscode folder
189
+ # .vscode/
190
+
191
+ # Ruff stuff:
192
+ .ruff_cache/
193
+
194
+ # PyPI configuration file
195
+ .pypirc
196
+
197
+ # Cursor
198
+ # Cursor is an AI-powered code editor. `.cursorignore` specifies files/directories to
199
+ # exclude from AI features like autocomplete and code analysis. Recommended for sensitive data
200
+ # refer to https://docs.cursor.com/context/ignore-files
201
+ .cursorignore
202
+ .cursorindexingignore
203
+
204
+ # Marimo
205
+ marimo/_static/
206
+ marimo/_lsp/
207
+ __marimo__/
@@ -0,0 +1,28 @@
1
+ BSD 3-Clause License
2
+
3
+ Copyright (c) 2026, Martin Vesterlund
4
+
5
+ Redistribution and use in source and binary forms, with or without
6
+ modification, are permitted provided that the following conditions are met:
7
+
8
+ 1. Redistributions of source code must retain the above copyright notice, this
9
+ list of conditions and the following disclaimer.
10
+
11
+ 2. Redistributions in binary form must reproduce the above copyright notice,
12
+ this list of conditions and the following disclaimer in the documentation
13
+ and/or other materials provided with the distribution.
14
+
15
+ 3. Neither the name of the copyright holder nor the names of its
16
+ contributors may be used to endorse or promote products derived from
17
+ this software without specific prior written permission.
18
+
19
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -0,0 +1,26 @@
1
+ Metadata-Version: 2.4
2
+ Name: feedsummary-core
3
+ Version: 1.0.2
4
+ Summary: Core library for FeedSummary.
5
+ Author: Martin Vesterlund
6
+ License-Expression: BSD-3-Clause
7
+ Keywords: rss,summarization,llm
8
+ Classifier: Programming Language :: Python :: 3
9
+ Classifier: Operating System :: OS Independent
10
+ Requires-Python: >=3.10
11
+ Description-Content-Type: text/markdown
12
+ License-File: LICENSE
13
+ Requires-Dist: aiohttp>=3.13.3
14
+ Requires-Dist: aiolimiter>=1.2.1
15
+ Requires-Dist: feedparser>=6.0.12
16
+ Requires-Dist: tenacity>=9.1.4
17
+ Requires-Dist: tinydb>=4.8.2
18
+ Requires-Dist: tld>=0.13.1
19
+ Requires-Dist: PyYAML>=6.0.3
20
+ Requires-Dist: trafilatura>=2.0.0
21
+ Requires-Dist: ollama>=0.6.1
22
+ Requires-Dist: lxml_html_clean>=0.4.3
23
+ Dynamic: license-file
24
+
25
+ # feedSummaryBase
26
+ Common functionality for the feedSummary
@@ -0,0 +1,2 @@
1
+ # feedSummaryBase
2
+ Common functionality for the feedSummary
@@ -0,0 +1,42 @@
1
+ [build-system]
2
+ requires = ["setuptools>=69", "wheel", "setuptools-scm>=8"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "feedsummary-core"
7
+ dynamic = ["version"]
8
+ description = "Core library for FeedSummary."
9
+ readme = "README.md"
10
+ requires-python = ">=3.10"
11
+ license = "BSD-3-Clause"
12
+ license-files = [ "LICENSE" ]
13
+ authors = [{ name = "Martin Vesterlund" }]
14
+ keywords = ["rss", "summarization", "llm"]
15
+ classifiers = [
16
+ "Programming Language :: Python :: 3",
17
+ "Operating System :: OS Independent",
18
+ ]
19
+
20
+ dependencies = [
21
+ "aiohttp>=3.13.3", # :contentReference[oaicite:1]{index=1}
22
+ "aiolimiter>=1.2.1", # :contentReference[oaicite:2]{index=2}
23
+ "feedparser>=6.0.12", # :contentReference[oaicite:3]{index=3}
24
+ "tenacity>=9.1.4", # :contentReference[oaicite:4]{index=4}
25
+ "tinydb>=4.8.2", # :contentReference[oaicite:5]{index=5}
26
+ "tld>=0.13.1", # :contentReference[oaicite:6]{index=6}
27
+ "PyYAML>=6.0.3", # :contentReference[oaicite:7]{index=7}
28
+ "trafilatura>=2.0.0", # :contentReference[oaicite:8]{index=8}
29
+ "ollama>=0.6.1", # :contentReference[oaicite:9]{index=9}
30
+ "lxml_html_clean>=0.4.3", # :contentReference[oaicite:10]{index=10}
31
+ ]
32
+ [tool.setuptools]
33
+ package-dir = {"" = "src"}
34
+
35
+ [tool.setuptools.packages.find]
36
+ where = ["src"]
37
+
38
+ [tool.setuptools_scm]
39
+ version_file = "src/feedsummary_core/_version.py"
40
+
41
+ [tool.ruff]
42
+ line-length = 100
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,38 @@
1
+ # LICENSE HEADER MANAGED BY add-license-header
2
+ #
3
+ # BSD 3-Clause License
4
+ #
5
+ # Copyright (c) 2026, Martin Vesterlund
6
+ #
7
+ # Redistribution and use in source and binary forms, with or without
8
+ # modification, are permitted provided that the following conditions are met:
9
+ #
10
+ # 1. Redistributions of source code must retain the above copyright notice, this
11
+ # list of conditions and the following disclaimer.
12
+ #
13
+ # 2. Redistributions in binary form must reproduce the above copyright notice,
14
+ # this list of conditions and the following disclaimer in the documentation
15
+ # and/or other materials provided with the distribution.
16
+ #
17
+ # 3. Neither the name of the copyright holder nor the names of its
18
+ # contributors may be used to endorse or promote products derived from
19
+ # this software without specific prior written permission.
20
+ #
21
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24
+ # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25
+ # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26
+ # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27
+ # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28
+ # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29
+ # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30
+ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
+ #
32
+
33
+ try:
34
+ from ._version import version as __version__
35
+ except Exception:
36
+ __version__ = "0+unknown"
37
+
38
+ __all__ = ["__version__"]
@@ -0,0 +1,34 @@
1
+ # file generated by setuptools-scm
2
+ # don't change, don't track in version control
3
+
4
+ __all__ = [
5
+ "__version__",
6
+ "__version_tuple__",
7
+ "version",
8
+ "version_tuple",
9
+ "__commit_id__",
10
+ "commit_id",
11
+ ]
12
+
13
+ TYPE_CHECKING = False
14
+ if TYPE_CHECKING:
15
+ from typing import Tuple
16
+ from typing import Union
17
+
18
+ VERSION_TUPLE = Tuple[Union[int, str], ...]
19
+ COMMIT_ID = Union[str, None]
20
+ else:
21
+ VERSION_TUPLE = object
22
+ COMMIT_ID = object
23
+
24
+ version: str
25
+ __version__: str
26
+ __version_tuple__: VERSION_TUPLE
27
+ version_tuple: VERSION_TUPLE
28
+ commit_id: COMMIT_ID
29
+ __commit_id__: COMMIT_ID
30
+
31
+ __version__ = version = '1.0.2'
32
+ __version_tuple__ = version_tuple = (1, 0, 2)
33
+
34
+ __commit_id__ = commit_id = 'g1ddc4d235'
@@ -0,0 +1,94 @@
1
+ # LICENSE HEADER MANAGED BY add-license-header
2
+ #
3
+ # BSD 3-Clause License
4
+ #
5
+ # Copyright (c) 2026, Martin Vesterlund
6
+ #
7
+ # Redistribution and use in source and binary forms, with or without
8
+ # modification, are permitted provided that the following conditions are met:
9
+ #
10
+ # 1. Redistributions of source code must retain the above copyright notice, this
11
+ # list of conditions and the following disclaimer.
12
+ #
13
+ # 2. Redistributions in binary form must reproduce the above copyright notice,
14
+ # this list of conditions and the following disclaimer in the documentation
15
+ # and/or other materials provided with the distribution.
16
+ #
17
+ # 3. Neither the name of the copyright holder nor the names of its
18
+ # contributors may be used to endorse or promote products derived from
19
+ # this software without specific prior written permission.
20
+ #
21
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24
+ # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25
+ # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26
+ # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27
+ # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28
+ # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29
+ # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30
+ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
+ #
32
+ from __future__ import annotations
33
+
34
+ from llmClient.fallback_client import FallbackLLMClient, FallbackPolicy
35
+
36
+ from typing import Any, Dict, List, Optional, Protocol
37
+
38
+
39
+ class LLMError(Exception):
40
+ pass
41
+
42
+
43
+ class LLMClient(Protocol):
44
+ async def chat(self, messages: List[Dict[str, str]], *, temperature: float = 0.2) -> str: ...
45
+
46
+
47
+ def _create_single_llm(llm_cfg: Dict[str, Any]):
48
+ provider = (llm_cfg.get("provider") or "ollama").lower()
49
+
50
+ if provider == "ollama_cloud":
51
+ from llmClient.ollama_cloud import OllamaCloudClient
52
+
53
+ return OllamaCloudClient(llm_cfg)
54
+
55
+ if provider == "ollama_local":
56
+ from llmClient.ollama_local import OllamaLocalClient, OllamaConfig
57
+
58
+ cfg = OllamaConfig(
59
+ base_url=str(llm_cfg.get("base_url", "http://localhost:11434")),
60
+ model=str(llm_cfg.get("model", "llama3.1:latest")),
61
+ max_rps=float(llm_cfg.get("max_rps", 1.0)),
62
+ first_byte_timeout_s=int(llm_cfg.get("first_byte_timeout_s", 900)),
63
+ sock_read_timeout_s=int(llm_cfg.get("sock_read_timeout_s", 300)),
64
+ progress_log_every_s=float(llm_cfg.get("progress_log_every_s", 2.0)),
65
+ max_retries=int(llm_cfg.get("max_retries", 3)),
66
+ )
67
+ return OllamaLocalClient(cfg)
68
+
69
+ raise ValueError(f"Unsupported LLM provider: {provider}")
70
+
71
+
72
+ def create_llm_client(config: Dict[str, Any]):
73
+ """
74
+ Bygger primary från config['llm'] och (om finns) fallback från config['llm_fallback'].
75
+
76
+ Fallback triggas när cloud slår i quota igen efter retry.
77
+ """
78
+ llm_cfg = config.get("llm") or {}
79
+ primary = _create_single_llm(llm_cfg)
80
+
81
+ fallback_cfg: Optional[Dict[str, Any]] = config.get("llm_fallback")
82
+ fallback = _create_single_llm(fallback_cfg) if isinstance(fallback_cfg, dict) else None
83
+
84
+ # policy kan ligga under llm.quota eller llm_fallback_policy – välj det du gillar
85
+ quota_cfg = llm_cfg.get("quota") or {}
86
+ policy = FallbackPolicy(
87
+ max_quota_retries=int(quota_cfg.get("max_quota_retries", 1)),
88
+ default_wait_s=int(quota_cfg.get("default_wait_s", 30)),
89
+ )
90
+
91
+ if fallback:
92
+ return FallbackLLMClient(primary=primary, fallback=fallback, policy=policy)
93
+
94
+ return primary
@@ -0,0 +1,113 @@
1
+ # LICENSE HEADER MANAGED BY add-license-header
2
+ #
3
+ # BSD 3-Clause License
4
+ #
5
+ # Copyright (c) 2026, Martin Vesterlund
6
+ #
7
+ # Redistribution and use in source and binary forms, with or without
8
+ # modification, are permitted provided that the following conditions are met:
9
+ #
10
+ # 1. Redistributions of source code must retain the above copyright notice, this
11
+ # list of conditions and the following disclaimer.
12
+ #
13
+ # 2. Redistributions in binary form must reproduce the above copyright notice,
14
+ # this list of conditions and the following disclaimer in the documentation
15
+ # and/or other materials provided with the distribution.
16
+ #
17
+ # 3. Neither the name of the copyright holder nor the names of its
18
+ # contributors may be used to endorse or promote products derived from
19
+ # this software without specific prior written permission.
20
+ #
21
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24
+ # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25
+ # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26
+ # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27
+ # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28
+ # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29
+ # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30
+ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
+ #
32
+
33
+ from __future__ import annotations
34
+
35
+ import asyncio
36
+ import logging
37
+ from dataclasses import dataclass
38
+ from typing import Dict, List, Optional, Protocol
39
+
40
+ from llm_client import LLMRateLimitError
41
+
42
+ log = logging.getLogger(__name__)
43
+
44
+
45
+ class LLMClient(Protocol):
46
+ async def chat(self, messages: List[Dict[str, str]], *, temperature: float = 0.2) -> str: ...
47
+
48
+
49
+ @dataclass
50
+ class FallbackPolicy:
51
+ max_quota_retries: int = 1 # "vid retry" -> om det slår i igen efter 1 retry: fallback
52
+ default_wait_s: int = 30 # om retry_after saknas
53
+ jitter_s: float = 0.0 # håll 0 om du vill vara deterministisk
54
+
55
+
56
+ class FallbackLLMClient:
57
+ """
58
+ Wrapper som kör primary, men om quota/rate-limit kvarstår efter retry -> fallback.
59
+
60
+ Beteende:
61
+ - Får vi LLMRateLimitError:
62
+ * vänta retry_after (eller default_wait_s)
63
+ * försök igen upp till max_quota_retries
64
+ - Om fortfarande rate limited:
65
+ * växla permanent till fallback (för resten av processen)
66
+ """
67
+
68
+ def __init__(
69
+ self,
70
+ primary: LLMClient,
71
+ fallback: Optional[LLMClient],
72
+ policy: Optional[FallbackPolicy] = None,
73
+ ):
74
+ self.primary = primary
75
+ self.fallback = fallback
76
+ self.policy = policy or FallbackPolicy()
77
+ self._force_fallback = False
78
+
79
+ async def chat(self, messages: List[Dict[str, str]], *, temperature: float = 0.2) -> str:
80
+ # Om vi redan växlat till fallback
81
+ if self._force_fallback:
82
+ if not self.fallback:
83
+ raise RuntimeError("Fallback aktiverad men llm_fallback saknas i config.")
84
+ return await self.fallback.chat(messages, temperature=temperature)
85
+
86
+ # Försök primary med quota-retries
87
+ attempt = 0
88
+ while True:
89
+ try:
90
+ return await self.primary.chat(messages, temperature=temperature)
91
+ except LLMRateLimitError as e:
92
+ attempt += 1
93
+ wait_s = int(e.retry_after_seconds or self.policy.default_wait_s)
94
+
95
+ # Om vi redan testat retry och slår i igen -> fallback
96
+ if attempt > self.policy.max_quota_retries:
97
+ if not self.fallback:
98
+ # ingen fallback konfigurerad
99
+ raise
100
+ log.warning(
101
+ "LLM quota/rate-limit kvarstår efter %d retry(s). Växlar till fallback.",
102
+ self.policy.max_quota_retries,
103
+ )
104
+ self._force_fallback = True
105
+ return await self.fallback.chat(messages, temperature=temperature)
106
+
107
+ log.warning(
108
+ "LLM rate-limited (attempt %d/%d). Väntar %ss och retry...",
109
+ attempt,
110
+ self.policy.max_quota_retries,
111
+ wait_s,
112
+ )
113
+ await asyncio.sleep(wait_s)