poliora 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.
Files changed (38) hide show
  1. poliora-0.2.0/LICENSE +21 -0
  2. poliora-0.2.0/PKG-INFO +415 -0
  3. poliora-0.2.0/README.md +377 -0
  4. poliora-0.2.0/poliora/__init__.py +7 -0
  5. poliora-0.2.0/poliora/app_launcher.py +83 -0
  6. poliora-0.2.0/poliora/config.py +103 -0
  7. poliora-0.2.0/poliora/cost/__init__.py +134 -0
  8. poliora-0.2.0/poliora/cost/antigravity.py +182 -0
  9. poliora-0.2.0/poliora/cost/budget.py +52 -0
  10. poliora-0.2.0/poliora/cost/capture.py +287 -0
  11. poliora-0.2.0/poliora/cost/catalog.py +196 -0
  12. poliora-0.2.0/poliora/cost/codex_exec.py +177 -0
  13. poliora-0.2.0/poliora/cost/companion.py +171 -0
  14. poliora-0.2.0/poliora/cost/dashboard.py +416 -0
  15. poliora-0.2.0/poliora/cost/decisions.py +226 -0
  16. poliora-0.2.0/poliora/cost/detection.py +108 -0
  17. poliora-0.2.0/poliora/cost/importer.py +415 -0
  18. poliora-0.2.0/poliora/cost/local_usage.py +482 -0
  19. poliora-0.2.0/poliora/cost/pricing.py +487 -0
  20. poliora-0.2.0/poliora/cost/recommendations.py +141 -0
  21. poliora-0.2.0/poliora/cost/reports.py +306 -0
  22. poliora-0.2.0/poliora/cost/scanner.py +92 -0
  23. poliora-0.2.0/poliora/cost/scenarios.py +95 -0
  24. poliora-0.2.0/poliora/cost/sdk.py +202 -0
  25. poliora-0.2.0/poliora/cost/simulation.py +144 -0
  26. poliora-0.2.0/poliora/cost/sync.py +164 -0
  27. poliora-0.2.0/poliora/cost/usage.py +183 -0
  28. poliora-0.2.0/poliora/cost/workspace.py +133 -0
  29. poliora-0.2.0/poliora/main.py +1062 -0
  30. poliora-0.2.0/poliora/trainer/__init__.py +5 -0
  31. poliora-0.2.0/poliora/trainer/core.py +637 -0
  32. poliora-0.2.0/poliora/utils/__init__.py +20 -0
  33. poliora-0.2.0/poliora/utils/benchmark.py +365 -0
  34. poliora-0.2.0/poliora/utils/carbon.py +257 -0
  35. poliora-0.2.0/poliora/utils/eco_tips.py +131 -0
  36. poliora-0.2.0/poliora/utils/electricity.py +146 -0
  37. poliora-0.2.0/poliora/web.py +1517 -0
  38. poliora-0.2.0/pyproject.toml +67 -0
poliora-0.2.0/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Poliora Contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
poliora-0.2.0/PKG-INFO ADDED
@@ -0,0 +1,415 @@
1
+ Metadata-Version: 2.4
2
+ Name: poliora
3
+ Version: 0.2.0
4
+ Summary: AI cost, carbon, and fine-tuning efficiency toolkit.
5
+ License-Expression: MIT
6
+ License-File: LICENSE
7
+ Keywords: llm,ai-finops,cost-tracking,sustainability,carbon-tracking,lora,quantization
8
+ Author: Poliora Contributors
9
+ Requires-Python: >=3.11,<4.0
10
+ Classifier: Development Status :: 3 - Alpha
11
+ Classifier: Intended Audience :: Developers
12
+ Classifier: Intended Audience :: Information Technology
13
+ Classifier: Operating System :: OS Independent
14
+ Classifier: Programming Language :: Python :: 3
15
+ Classifier: Programming Language :: Python :: 3.11
16
+ Classifier: Programming Language :: Python :: 3.12
17
+ Classifier: Programming Language :: Python :: 3.13
18
+ Classifier: Programming Language :: Python :: 3.14
19
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
20
+ Classifier: Topic :: System :: Monitoring
21
+ Provides-Extra: training
22
+ Requires-Dist: accelerate (>=0.25) ; extra == "training"
23
+ Requires-Dist: bitsandbytes (>=0.41) ; extra == "training"
24
+ Requires-Dist: codecarbon (>=2.3) ; extra == "training"
25
+ Requires-Dist: datasets (>=2.16) ; extra == "training"
26
+ Requires-Dist: httpx (>=0.25)
27
+ Requires-Dist: peft (>=0.7) ; extra == "training"
28
+ Requires-Dist: psutil (>=5.9) ; extra == "training"
29
+ Requires-Dist: pydantic (>=2.5) ; extra == "training"
30
+ Requires-Dist: pydantic-settings (>=2.1) ; extra == "training"
31
+ Requires-Dist: rich (>=13.0)
32
+ Requires-Dist: sentencepiece (>=0.1.99) ; extra == "training"
33
+ Requires-Dist: torch (>=2.1) ; extra == "training"
34
+ Requires-Dist: transformers (>=4.36) ; extra == "training"
35
+ Requires-Dist: typer (>=0.9,<1.0)
36
+ Description-Content-Type: text/markdown
37
+
38
+ # Poliora
39
+
40
+ Poliora is an AI cost, carbon, and fine-tuning efficiency toolkit.
41
+
42
+ [Laptop testing](docs/LAPTOP_TESTING.md) | [Release gate](docs/LAUNCH_CHECKLIST.md) | [Hosting](docs/HOSTING_AND_LAUNCH.md) | [Private pilot](docs/PILOT_GUIDE.md) | [Privacy](docs/PRIVACY.md) | [Security](SECURITY.md) | [Changelog](CHANGELOG.md) | [License](LICENSE)
43
+
44
+ The Cloudflare Pages-ready product site is in [`site/`](site/).
45
+
46
+ The original project focused on sustainable LLM fine-tuning. The new direction
47
+ keeps that foundation, but expands the product into a practical "AI FinOps"
48
+ toolkit: track token usage, estimate spend, project monthly cost, and get
49
+ recommendations for reducing waste.
50
+
51
+ ## What Poliora Does
52
+
53
+ - Records AI usage events from the CLI or Python SDK.
54
+ - Estimates token cost by provider and model.
55
+ - Produces local AI spend reports with model and workflow breakdowns.
56
+ - Projects monthly spend against a configured budget.
57
+ - Suggests savings actions like cheaper model routing, prompt trimming, and caching.
58
+ - Tracks optimization decisions from modeled opportunity through quality testing
59
+ and measured rollout, keeping projected and realized savings separate.
60
+ - Keeps the existing LoRA, quantization, carbon tracking, and training benchmark tools.
61
+
62
+ ## Install And Open
63
+
64
+ The launch install is deliberately small. Install the cost-control product, move
65
+ to the folder where its local data should live, and run one command:
66
+
67
+ ```bash
68
+ pip install poliora
69
+ poliora dashboard
70
+ ```
71
+
72
+ On first run, Poliora creates a `.poliora` workspace and opens
73
+ `http://127.0.0.1:8787` automatically. The same command opens the existing
74
+ workspace on future runs. Stop the local app with `Ctrl+C`.
75
+
76
+ The legacy fine-tuning toolkit is optional because its machine-learning
77
+ dependencies are much larger:
78
+
79
+ ```bash
80
+ pip install "poliora[training]"
81
+ ```
82
+
83
+ ## Install For Development
84
+
85
+ ```bash
86
+ poetry install
87
+ ```
88
+
89
+ Or, with the existing virtual environment:
90
+
91
+ ```bash
92
+ .venv\Scripts\python.exe -m poliora.main --help
93
+ ```
94
+
95
+ ## Quickstart: AI Spend Tracking
96
+
97
+ Creating a workspace explicitly is useful when you want to set its name and
98
+ budget before opening the dashboard:
99
+
100
+ ```bash
101
+ poliora init --project demo-agency --monthly-budget 1000
102
+ ```
103
+
104
+ Record usage:
105
+
106
+ ```bash
107
+ poliora record \
108
+ --provider openai \
109
+ --model gpt-4o \
110
+ --input-tokens 8000 \
111
+ --output-tokens 2000 \
112
+ --cached-input-tokens 3000 \
113
+ --reasoning-tokens 800 \
114
+ --operation agent
115
+ ```
116
+
117
+ `input_tokens` includes cached tokens when your provider reports them. Poliora applies
118
+ the matching cache-read price where known, records reasoning-token usage as a separate
119
+ signal, and can also track fixed provider tool charges with `--tool-cost-usd`.
120
+
121
+ Generate a report:
122
+
123
+ ```bash
124
+ poliora report --json .poliora/report.json --csv .poliora/models.csv --html poliora-report.html
125
+ ```
126
+
127
+ Fail a CI/deployment check if projected spend is too high:
128
+
129
+ ```bash
130
+ poliora check --max-monthly 600
131
+ ```
132
+
133
+ Get recommendations:
134
+
135
+ ```bash
136
+ poliora recommend --monthly-spend 1000 --target-savings 40
137
+ ```
138
+
139
+ `--html` creates a polished, standalone executive report. It can be opened locally or attached to a client update; it needs no server, account, or tracking script.
140
+
141
+ ## Local Dashboard
142
+
143
+ Start a browser-based control room for the current workspace:
144
+
145
+ ```bash
146
+ poliora dashboard
147
+ ```
148
+
149
+ Then open `http://127.0.0.1:8787`. The dashboard reads local data only, includes the routing simulator, and can export a client-ready HTML report. It binds to your computer by default; do not expose it to the public internet yet.
150
+
151
+ The catalog can also save local contract rates, while the routing simulator can
152
+ save named scenarios. Those assumptions stay in `.poliora` with the project;
153
+ they are not sent to Poliora or an AI provider.
154
+
155
+ The **Connection center** lists the AI tools Poliora can observe. It explains
156
+ the exact metrics and permission required for each connector before you approve
157
+ setup. Approval is local and does not save any provider key.
158
+
159
+ Install the supported Antigravity workspace plugin with:
160
+
161
+ ```bash
162
+ poliora antigravity-install
163
+ ```
164
+
165
+ The plugin records Antigravity invocation activity through its documented hook.
166
+ Google's hook payload does not expose model or token totals, so Poliora keeps
167
+ this activity separate from measured Gemini API spend.
168
+
169
+ When the workspace is empty, select **Load guided sample data** on the dashboard
170
+ to explore the charts, filters, model catalog, routing simulator, scenarios, and
171
+ report export with fictional records. It never loads over existing usage data.
172
+
173
+ ## Test A Cheaper Model Route
174
+
175
+ Use recorded usage to estimate the impact of moving part of a workload to a different model:
176
+
177
+ ```bash
178
+ poliora simulate \
179
+ --source-provider openai --source-model gpt-4o \
180
+ --target-provider openai --target-model gpt-4o-mini \
181
+ --percentage 35
182
+ ```
183
+
184
+ Poliora keeps the current recorded cost and prices the proposed route from the editable `.poliora/pricing.json` registry. That makes the assumptions visible before anyone acts on the result.
185
+
186
+ ## Import Existing Usage
187
+
188
+ For a quick client pilot, import a CSV instead of changing application code first:
189
+
190
+ ```bash
191
+ poliora import-csv client-usage.csv --provider openai --project client-demo
192
+ ```
193
+
194
+ Poliora accepts common columns such as `model`, `prompt_tokens`, `completion_tokens`,
195
+ `cached_tokens`, `reasoning_tokens`, `tool_cost_usd`, `cost_usd`, `workflow`,
196
+ `customer`, and `timestamp`. It preserves a supplied `cost_usd`; otherwise it uses
197
+ the editable pricing registry.
198
+
199
+ Try the included example before importing a real client file:
200
+
201
+ ```bash
202
+ poliora import-csv examples/usage_import.csv --project demo-agency
203
+ ```
204
+
205
+ ## Python SDK
206
+
207
+ ```python
208
+ from poliora.cost import log_usage
209
+
210
+ event = log_usage(
211
+ provider="openai",
212
+ model="gpt-4o-mini",
213
+ input_tokens=1200,
214
+ output_tokens=400,
215
+ cached_input_tokens=300,
216
+ trace_id="support-ticket-1842",
217
+ operation="support-chat",
218
+ )
219
+
220
+ print(event.cost_usd)
221
+ ```
222
+
223
+ ## Track A Codex CLI Task
224
+
225
+ Run a Codex task through Poliora's wrapper to collect the documented JSON usage
226
+ event without storing the task, agent reply, commands, or file changes:
227
+
228
+ ```bash
229
+ poliora codex --model gpt-5.4 --sandbox workspace-write "review this repository"
230
+ ```
231
+
232
+ `codex --version` must identify itself as `codex-cli`. Poliora rejects unrelated
233
+ executables that happen to use the same command name and prints the repair steps.
234
+
235
+ The default assumes Codex is using a ChatGPT subscription, so Poliora records
236
+ tokens but does not invent an API-equivalent dollar charge. Use `--api-billed`
237
+ only when Codex is authenticated for usage-based API billing and the selected
238
+ model has the correct rate in your workspace.
239
+
240
+ Wrap an OpenAI-style call:
241
+
242
+ ```python
243
+ from poliora.cost import track_openai_call
244
+
245
+ captured = track_openai_call(
246
+ client.chat.completions.create,
247
+ model="gpt-4o-mini",
248
+ messages=[{"role": "user", "content": "Summarize this ticket."}],
249
+ operation="support-chat",
250
+ )
251
+
252
+ response = captured.response
253
+ print(captured.event.cost_usd)
254
+ ```
255
+
256
+ Or proxy common SDK calls:
257
+
258
+ ```python
259
+ from poliora.cost import track_openai_client
260
+
261
+ client = track_openai_client(client, project="acme")
262
+ response = client.chat.completions.create(
263
+ model="gpt-4o-mini",
264
+ messages=[{"role": "user", "content": "Draft a reply."}],
265
+ )
266
+ ```
267
+
268
+ For a provider or gateway with an OpenAI-compatible client, keep the same
269
+ response parsing but label the event correctly:
270
+
271
+ ```python
272
+ from poliora.cost import track_openai_compatible_client
273
+
274
+ client = track_openai_compatible_client(client, provider="deepseek", project="acme")
275
+ ```
276
+
277
+ Gemini Generate Content responses can be wrapped directly as well:
278
+
279
+ ```python
280
+ from poliora.cost import track_gemini_client
281
+
282
+ client = track_gemini_client(client, project="acme")
283
+ response = client.models.generate_content(model="gemini-3.5-flash", contents="Summarize this ticket.")
284
+ ```
285
+
286
+ See `examples/gemini_tracking.py` for a runnable example. The provider key is
287
+ read by Google's client from the environment; Poliora does not save it.
288
+
289
+ For OpenAI-style response objects or dictionaries:
290
+
291
+ ```python
292
+ from poliora.cost import log_openai_response
293
+
294
+ response = {
295
+ "model": "gpt-4o-mini",
296
+ "usage": {
297
+ "prompt_tokens": 300,
298
+ "completion_tokens": 200,
299
+ },
300
+ }
301
+
302
+ log_openai_response(response)
303
+ ```
304
+
305
+ Anthropic-style response dictionaries are supported too:
306
+
307
+ ```python
308
+ from poliora.cost import log_anthropic_response
309
+
310
+ log_anthropic_response({
311
+ "model": "claude-3-5-haiku",
312
+ "usage": {"input_tokens": 800, "output_tokens": 250},
313
+ })
314
+ ```
315
+
316
+ ## Pricing
317
+
318
+ Poliora ships with an editable starter pricing registry:
319
+
320
+ ```bash
321
+ poliora pricing
322
+ ```
323
+
324
+ The file lives at:
325
+
326
+ ```text
327
+ .poliora/pricing.json
328
+ ```
329
+
330
+ These are estimates, not vendor billing truth. Teams should update this file
331
+ with their real provider rates, discounts, and enterprise contracts.
332
+
333
+ The model catalog is separate from pricing. It includes verified provider model
334
+ IDs and lifecycle metadata, while workspace prices take precedence over public
335
+ defaults:
336
+
337
+ ```bash
338
+ poliora models
339
+ poliora models --provider deepseek
340
+ poliora model-add --provider acme --model terra-v1 --name "Terra V1" --input-per-1m 1.2 --output-per-1m 4.8
341
+ ```
342
+
343
+ Custom models are first-class records. Poliora will never guess a price for them.
344
+
345
+ To refresh a provider catalog from the models available to your own account,
346
+ run a local sync. The API key is only used for that request and is never saved:
347
+
348
+ ```bash
349
+ poliora sync-models --provider openai --api-key YOUR_OPENAI_API_KEY
350
+ ```
351
+
352
+ Supported discovery providers are OpenAI, Anthropic, Google, Mistral, and xAI.
353
+ Model discovery adds account-visible models but does not overwrite pricing; set
354
+ contract rates in `.poliora/pricing.json` or with `poliora model-add`.
355
+
356
+ ## Fine-Tuning Tools
357
+
358
+ The original training workflow is still available:
359
+
360
+ ```bash
361
+ poliora train \
362
+ --model microsoft/phi-3-mini-4k-instruct \
363
+ --dataset examples/sample_data.csv \
364
+ --output tuned_model \
365
+ --epochs 3 \
366
+ --lora \
367
+ --quantize \
368
+ --carbon
369
+ ```
370
+
371
+ Benchmark eco-optimized training against a baseline:
372
+
373
+ ```bash
374
+ poliora benchmark \
375
+ --model microsoft/phi-3-mini-4k-instruct \
376
+ --dataset examples/sample_data.csv \
377
+ --epochs 1 \
378
+ --json benchmark_output/report.json \
379
+ --csv benchmark_output/report.csv
380
+ ```
381
+
382
+ ## Product Direction
383
+
384
+ Poliora should become the control room for AI operating cost:
385
+
386
+ - local CLI and SDK for developers
387
+ - hosted dashboard for teams
388
+ - alerts when projected spend crosses budget
389
+ - client-ready PDF reports for agencies
390
+ - CI/CD budget gates for AI features
391
+ - integrations with OpenAI, Anthropic, Gemini, Langfuse, LiteLLM, and cloud billing exports
392
+ - carbon reporting as a useful differentiator, not the only sales message
393
+
394
+ The commercial promise is simple:
395
+
396
+ > See where AI spend is going, then reduce waste without blindly hurting quality.
397
+
398
+ ## Development
399
+
400
+ Run tests:
401
+
402
+ ```bash
403
+ .venv\Scripts\python.exe -m pytest tests -q
404
+ ```
405
+
406
+ Run lint:
407
+
408
+ ```bash
409
+ .venv\Scripts\ruff.exe check poliora tests
410
+ ```
411
+
412
+ ## License
413
+
414
+ MIT
415
+