llm-optimizer 0.4.2__tar.gz → 0.4.4__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.
- llm_optimizer-0.4.4/LICENSE +21 -0
- {llm_optimizer-0.4.2/llm_optimizer.egg-info → llm_optimizer-0.4.4}/PKG-INFO +379 -4
- llm_optimizer-0.4.2/PKG-INFO → llm_optimizer-0.4.4/README.md +1291 -957
- {llm_optimizer-0.4.2 → llm_optimizer-0.4.4}/llm_optimizer/__init__.py +3 -2
- llm_optimizer-0.4.4/llm_optimizer/bedrock_converse.py +348 -0
- {llm_optimizer-0.4.2 → llm_optimizer-0.4.4}/llm_optimizer/caching.py +9 -4
- {llm_optimizer-0.4.2 → llm_optimizer-0.4.4}/llm_optimizer/client.py +37 -15
- llm_optimizer-0.4.4/llm_optimizer/document_parser.py +467 -0
- llm_optimizer-0.4.4/llm_optimizer/local.py +455 -0
- {llm_optimizer-0.4.2 → llm_optimizer-0.4.4}/llm_optimizer/models.py +2 -0
- {llm_optimizer-0.4.2 → llm_optimizer-0.4.4}/llm_optimizer/pricing.json +58 -21
- {llm_optimizer-0.4.2 → llm_optimizer-0.4.4}/llm_optimizer/tracker.py +12 -4
- llm_optimizer-0.4.2/README.md → llm_optimizer-0.4.4/llm_optimizer.egg-info/PKG-INFO +418 -4
- {llm_optimizer-0.4.2 → llm_optimizer-0.4.4}/llm_optimizer.egg-info/SOURCES.txt +8 -1
- {llm_optimizer-0.4.2 → llm_optimizer-0.4.4}/pyproject.toml +1 -1
- {llm_optimizer-0.4.2 → llm_optimizer-0.4.4}/tests/test_llm_optimizer.py +2 -2
- llm_optimizer-0.4.4/tests/test_local.py +265 -0
- llm_optimizer-0.4.4/tests/test_v041.py +342 -0
- llm_optimizer-0.4.4/tests/test_v044_fixes.py +78 -0
- {llm_optimizer-0.4.2 → llm_optimizer-0.4.4}/llm_optimizer/analytics.py +0 -0
- {llm_optimizer-0.4.2 → llm_optimizer-0.4.4}/llm_optimizer/batcher.py +0 -0
- {llm_optimizer-0.4.2 → llm_optimizer-0.4.4}/llm_optimizer/bedrock.py +0 -0
- {llm_optimizer-0.4.2 → llm_optimizer-0.4.4}/llm_optimizer/bedrock_cache.py +0 -0
- {llm_optimizer-0.4.2 → llm_optimizer-0.4.4}/llm_optimizer/budget.py +0 -0
- {llm_optimizer-0.4.2 → llm_optimizer-0.4.4}/llm_optimizer/cache.py +0 -0
- {llm_optimizer-0.4.2 → llm_optimizer-0.4.4}/llm_optimizer/cross_provider_cache.py +0 -0
- {llm_optimizer-0.4.2 → llm_optimizer-0.4.4}/llm_optimizer/few_shot.py +0 -0
- {llm_optimizer-0.4.2 → llm_optimizer-0.4.4}/llm_optimizer/history.py +0 -0
- {llm_optimizer-0.4.2 → llm_optimizer-0.4.4}/llm_optimizer/optimizer.py +0 -0
- {llm_optimizer-0.4.2 → llm_optimizer-0.4.4}/llm_optimizer/output_repair.py +0 -0
- {llm_optimizer-0.4.2 → llm_optimizer-0.4.4}/llm_optimizer/pii.py +0 -0
- {llm_optimizer-0.4.2 → llm_optimizer-0.4.4}/llm_optimizer/prefill.py +0 -0
- {llm_optimizer-0.4.2 → llm_optimizer-0.4.4}/llm_optimizer/router.py +0 -0
- {llm_optimizer-0.4.2 → llm_optimizer-0.4.4}/llm_optimizer/semantic_cache.py +0 -0
- {llm_optimizer-0.4.2 → llm_optimizer-0.4.4}/llm_optimizer/streaming.py +0 -0
- {llm_optimizer-0.4.2 → llm_optimizer-0.4.4}/llm_optimizer.egg-info/dependency_links.txt +0 -0
- {llm_optimizer-0.4.2 → llm_optimizer-0.4.4}/llm_optimizer.egg-info/entry_points.txt +0 -0
- {llm_optimizer-0.4.2 → llm_optimizer-0.4.4}/llm_optimizer.egg-info/requires.txt +0 -0
- {llm_optimizer-0.4.2 → llm_optimizer-0.4.4}/llm_optimizer.egg-info/top_level.txt +0 -0
- {llm_optimizer-0.4.2 → llm_optimizer-0.4.4}/setup.cfg +0 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Somnath Banerjee
|
|
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.
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: llm-optimizer
|
|
3
|
-
Version: 0.4.
|
|
3
|
+
Version: 0.4.4
|
|
4
4
|
Summary: Reduce LLM API costs by up to 90% — prompt caching, smart routing, batch processing, and more
|
|
5
5
|
Author-email: Somnath Banerjee <banerjso30@gmail.com>
|
|
6
6
|
License: MIT
|
|
@@ -18,6 +18,7 @@ Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
|
18
18
|
Classifier: Intended Audience :: Developers
|
|
19
19
|
Requires-Python: >=3.10
|
|
20
20
|
Description-Content-Type: text/markdown
|
|
21
|
+
License-File: LICENSE
|
|
21
22
|
Requires-Dist: anthropic>=0.40.0
|
|
22
23
|
Provides-Extra: openai
|
|
23
24
|
Requires-Dist: openai>=1.0.0; extra == "openai"
|
|
@@ -36,6 +37,7 @@ Provides-Extra: dev
|
|
|
36
37
|
Requires-Dist: pytest>=8.0; extra == "dev"
|
|
37
38
|
Requires-Dist: build; extra == "dev"
|
|
38
39
|
Requires-Dist: twine; extra == "dev"
|
|
40
|
+
Dynamic: license-file
|
|
39
41
|
|
|
40
42
|
# llm-optimizer
|
|
41
43
|
|
|
@@ -56,10 +58,9 @@ client.print_summary()
|
|
|
56
58
|
# 💰 Total cost: $0.000842 | Total saved: $0.007580 (90% savings)
|
|
57
59
|
```
|
|
58
60
|
|
|
59
|
-
  
|
|
60
62
|
|
|
61
|
-
> **v0.4.
|
|
62
|
-
> few-shot selector, JSON auto-correction, streaming analytics. See [CHANGELOG](#changelog).
|
|
63
|
+
> **v0.4.3** — Bedrock Converse API (Nova, Mistral, Llama support), Document Parser (PDF/DOCX/XLSX to Markdown), tracker cost_saved fix. See [CHANGELOG](#changelog).
|
|
63
64
|
|
|
64
65
|
---
|
|
65
66
|
|
|
@@ -76,6 +77,8 @@ client.print_summary()
|
|
|
76
77
|
- [Available Models](#available-models)
|
|
77
78
|
- [Test Suite](#test-suite)
|
|
78
79
|
- [Project Structure](#project-structure)
|
|
80
|
+
- [v0.4.3 — What's New](#v0.4.3--whats-new)
|
|
81
|
+
- [Local Model Support](#19--local-model-support--ollama-lm-studio-vllm-llamacpp)
|
|
79
82
|
- [Changelog](#changelog)
|
|
80
83
|
|
|
81
84
|
---
|
|
@@ -866,6 +869,10 @@ tests/
|
|
|
866
869
|
|
|
867
870
|
## Changelog
|
|
868
871
|
|
|
872
|
+
### v0.4.3
|
|
873
|
+
|
|
874
|
+
- Bedrock Converse API (Nova, Mistral, Llama support), Document Parser (PDF/DOCX/XLSX to Markdown), tracker cost_saved fix
|
|
875
|
+
|
|
869
876
|
### v0.4.2
|
|
870
877
|
|
|
871
878
|
- Sanitize docs, examples, and tests — replace org-specific AWS profile names with generic placeholders; Bedrock integration tests read `AWS_PROFILE` from the environment
|
|
@@ -874,6 +881,16 @@ tests/
|
|
|
874
881
|
|
|
875
882
|
- BUG FIX
|
|
876
883
|
|
|
884
|
+
### v0.4.1
|
|
885
|
+
|
|
886
|
+
- **Bedrock Converse API** — unified format for Claude, Nova, Titan, Llama, Mistral on Bedrock. `BedrockConverseClient` and `make_converse_client()` factory. Streaming supported. 11 model IDs in registry across 5 families.
|
|
887
|
+
- **Document Parser** — PDF, DOCX, XLSX, CSV, TXT to structured Markdown. Page limit, token limit, multi-document formatting, bytes input. Zero lossy compression — full content extraction.
|
|
888
|
+
- **Tracker fix** — `cost_saved` parameter correctly recorded for output cache hits. `Total Saved` in `print_summary()` now shows accurately.
|
|
889
|
+
- `[documents]` install extra added: `pip install llm-optimizer[documents]`
|
|
890
|
+
- 34 new unit tests (all mocked — no real AWS calls or file dependencies)
|
|
891
|
+
|
|
892
|
+
---
|
|
893
|
+
|
|
877
894
|
### v0.4.0 — Major Feature Release
|
|
878
895
|
|
|
879
896
|
**Cost Reduction**
|
|
@@ -950,6 +967,364 @@ tests/
|
|
|
950
967
|
- Cost tracking with JSONL persistence
|
|
951
968
|
- 36 unit tests
|
|
952
969
|
|
|
970
|
+
|
|
971
|
+
---
|
|
972
|
+
|
|
973
|
+
## v0.4.3 — What's New
|
|
974
|
+
|
|
975
|
+
### 17. 🔀 Bedrock Converse API — route across all model families
|
|
976
|
+
|
|
977
|
+
The standard Bedrock InvokeModel API requires a different request format for each model family. The Converse API solves this with one unified format that works across Claude, Nova, Titan, Llama, Mistral, and any future Bedrock model.
|
|
978
|
+
|
|
979
|
+
**What this enables:**
|
|
980
|
+
- Route cheap simple tasks to Nova Micro (Amazon's cheapest model)
|
|
981
|
+
- Route complex tasks to Claude Sonnet
|
|
982
|
+
- Switch model families with one config change — no code rewrite
|
|
983
|
+
|
|
984
|
+
```python
|
|
985
|
+
from llm_optimizer.bedrock_converse import make_converse_client, CONVERSE_MODELS
|
|
986
|
+
|
|
987
|
+
client = make_converse_client(
|
|
988
|
+
profile_name="xxxxxx",
|
|
989
|
+
region="xxxxxx",
|
|
990
|
+
discount_pct=16.0,
|
|
991
|
+
)
|
|
992
|
+
|
|
993
|
+
# Claude — same quality, via Converse API
|
|
994
|
+
response = client.complete(
|
|
995
|
+
messages=[{"role": "user", "content": "Summarize this contract"}],
|
|
996
|
+
model_id=CONVERSE_MODELS["claude-sonnet-4-6"],
|
|
997
|
+
system="You are a legal analyst.",
|
|
998
|
+
)
|
|
999
|
+
|
|
1000
|
+
# Nova Micro — Amazon's cheapest model for simple tasks
|
|
1001
|
+
response = client.complete(
|
|
1002
|
+
messages=[{"role": "user", "content": "Is this spam?"}],
|
|
1003
|
+
model_id=CONVERSE_MODELS["nova-micro"],
|
|
1004
|
+
)
|
|
1005
|
+
|
|
1006
|
+
# Mistral Small — significantly cheaper than Haiku for rote tasks
|
|
1007
|
+
response = client.complete(
|
|
1008
|
+
messages=[{"role": "user", "content": "Classify this email"}],
|
|
1009
|
+
model_id=CONVERSE_MODELS["mistral-small"],
|
|
1010
|
+
)
|
|
1011
|
+
|
|
1012
|
+
# Stream any model — same call
|
|
1013
|
+
for chunk in client.stream(messages=[...], model_id=CONVERSE_MODELS["nova-pro"]):
|
|
1014
|
+
print(chunk, end="", flush=True)
|
|
1015
|
+
```
|
|
1016
|
+
|
|
1017
|
+
**Supported models (all via Converse API):**
|
|
1018
|
+
|
|
1019
|
+
| Key | Model ID | Provider | Notes |
|
|
1020
|
+
|---|---|---|---|
|
|
1021
|
+
| `claude-haiku-4-5` | `us.anthropic.claude-haiku-4-5-20251001-v1:0` | Anthropic | Fast, cheap |
|
|
1022
|
+
| `claude-sonnet-4-5` | `us.anthropic.claude-sonnet-4-5-20250929-v1:0` | Anthropic | Best all-rounder |
|
|
1023
|
+
| `claude-sonnet-4-6` | `us.anthropic.claude-sonnet-4-6` | Anthropic | Org standard |
|
|
1024
|
+
| `claude-opus-4-5` | `us.anthropic.claude-opus-4-5-20251101-v1:0` | Anthropic | Expert tasks |
|
|
1025
|
+
| `nova-micro` | `us.amazon.nova-micro-v1:0` | Amazon | Cheapest — text only |
|
|
1026
|
+
| `nova-lite` | `us.amazon.nova-lite-v1:0` | Amazon | Multimodal |
|
|
1027
|
+
| `nova-pro` | `us.amazon.nova-pro-v1:0` | Amazon | Most capable Nova |
|
|
1028
|
+
| `mistral-small` | `us.mistral.mistral-small-2402-v1:0` | Mistral AI | Cheap rote tasks |
|
|
1029
|
+
| `mistral-large` | `us.mistral.mistral-large-2402-v1:0` | Mistral AI | Complex reasoning |
|
|
1030
|
+
| `llama-3-70b` | `us.meta.llama3-3-70b-instruct-v1:0` | Meta | Open weights |
|
|
1031
|
+
| `llama-3-8b` | `us.meta.llama3-1-8b-instruct-v1:0` | Meta | Lightweight |
|
|
1032
|
+
|
|
1033
|
+
**Converse API vs InvokeModel:**
|
|
1034
|
+
|
|
1035
|
+
| | `bedrock.py` (InvokeModel) | `bedrock_converse.py` (Converse) |
|
|
1036
|
+
|---|---|---|
|
|
1037
|
+
| Claude models | ✅ | ✅ |
|
|
1038
|
+
| Nova / Titan | ❌ | ✅ |
|
|
1039
|
+
| Mistral | ❌ | ✅ |
|
|
1040
|
+
| Llama | ❌ | ✅ |
|
|
1041
|
+
| Caching | ✅ cachePoint | ✅ cacheConfig |
|
|
1042
|
+
| Streaming | ✅ | ✅ |
|
|
1043
|
+
|
|
1044
|
+
**Note:** Before using non-Claude models, verify they are enabled in your Bedrock account:
|
|
1045
|
+
|
|
1046
|
+
```bash
|
|
1047
|
+
aws bedrock list-foundation-models \
|
|
1048
|
+
--profile xxxxxx \
|
|
1049
|
+
--region xxxxxxxx \
|
|
1050
|
+
--query "modelSummaries[?contains(modelId, 'nova')].[modelId]" \
|
|
1051
|
+
--output table
|
|
1052
|
+
```
|
|
1053
|
+
|
|
1054
|
+
---
|
|
1055
|
+
|
|
1056
|
+
### 18. 📄 Document Parser — PDF, DOCX, XLSX to Markdown
|
|
1057
|
+
|
|
1058
|
+
Converts documents to clean, structured Markdown before sending to any LLM. Raw PDFs and Word documents cannot be sent directly — this module extracts their content into a format the LLM can actually reason about, preserving headings, tables, lists, and code blocks.
|
|
1059
|
+
|
|
1060
|
+
**Install dependencies:**
|
|
1061
|
+
|
|
1062
|
+
```bash
|
|
1063
|
+
pip install pypdf python-docx openpyxl
|
|
1064
|
+
# or
|
|
1065
|
+
pip install llm-optimizer[documents]
|
|
1066
|
+
```
|
|
1067
|
+
|
|
1068
|
+
**Basic usage:**
|
|
1069
|
+
|
|
1070
|
+
```python
|
|
1071
|
+
from llm_optimizer.document_parser import parse_document, parse_documents_for_prompt
|
|
1072
|
+
|
|
1073
|
+
# Parse a single document
|
|
1074
|
+
doc = parse_document("contract.pdf")
|
|
1075
|
+
print(doc.markdown) # structured Markdown
|
|
1076
|
+
print(doc.token_estimate) # estimated tokens
|
|
1077
|
+
print(doc.page_count) # number of pages
|
|
1078
|
+
|
|
1079
|
+
# Parse multiple documents and combine for prompt injection
|
|
1080
|
+
context = parse_documents_for_prompt([
|
|
1081
|
+
"report.pdf",
|
|
1082
|
+
"data.xlsx",
|
|
1083
|
+
"notes.docx",
|
|
1084
|
+
])
|
|
1085
|
+
|
|
1086
|
+
# Inject into your request
|
|
1087
|
+
response = client.complete(
|
|
1088
|
+
messages=[{"role": "user", "content": "Summarize the key findings"}],
|
|
1089
|
+
system=context,
|
|
1090
|
+
)
|
|
1091
|
+
```
|
|
1092
|
+
|
|
1093
|
+
**Parse from bytes (S3, uploads, APIs):**
|
|
1094
|
+
|
|
1095
|
+
```python
|
|
1096
|
+
from llm_optimizer.document_parser import DocumentParser
|
|
1097
|
+
|
|
1098
|
+
parser = DocumentParser()
|
|
1099
|
+
|
|
1100
|
+
with open("contract.pdf", "rb") as f:
|
|
1101
|
+
doc = parser.parse_bytes(f.read(), file_type="pdf", source="contract.pdf")
|
|
1102
|
+
|
|
1103
|
+
print(doc.markdown)
|
|
1104
|
+
```
|
|
1105
|
+
|
|
1106
|
+
**What each format produces:**
|
|
1107
|
+
|
|
1108
|
+
| Format | What the parser preserves |
|
|
1109
|
+
|---|---|
|
|
1110
|
+
| PDF | Page-by-page text with `### Page N` headers. Hyphenated line breaks fixed. |
|
|
1111
|
+
| DOCX | `# ## ###` headings, `- bullet` lists, `\| Markdown \| tables \|` |
|
|
1112
|
+
| XLSX | One `## Sheet: name` section per sheet, full Markdown table per sheet |
|
|
1113
|
+
| CSV | Markdown table with header row and separator |
|
|
1114
|
+
| TXT / MD | Passthrough — returned as-is with light whitespace cleanup |
|
|
1115
|
+
|
|
1116
|
+
**Token budget and page limit:**
|
|
1117
|
+
|
|
1118
|
+
```python
|
|
1119
|
+
from llm_optimizer.document_parser import DocumentParser
|
|
1120
|
+
|
|
1121
|
+
# Limit output to 4,000 tokens
|
|
1122
|
+
parser = DocumentParser(max_tokens=4000)
|
|
1123
|
+
doc = parser.parse("large_report.pdf")
|
|
1124
|
+
|
|
1125
|
+
# Only first 20 pages of a long PDF
|
|
1126
|
+
parser = DocumentParser(max_pages=20)
|
|
1127
|
+
doc = parser.parse("annual_report.pdf")
|
|
1128
|
+
```
|
|
1129
|
+
|
|
1130
|
+
**Multiple documents with clear headers:**
|
|
1131
|
+
|
|
1132
|
+
```python
|
|
1133
|
+
parser = DocumentParser()
|
|
1134
|
+
context = parser.to_llm_content([
|
|
1135
|
+
"contract.pdf",
|
|
1136
|
+
"amendments.docx",
|
|
1137
|
+
"pricing.xlsx",
|
|
1138
|
+
])
|
|
1139
|
+
# Output:
|
|
1140
|
+
# ## Document: contract.pdf
|
|
1141
|
+
# # Master Services Agreement
|
|
1142
|
+
# ...
|
|
1143
|
+
# ---
|
|
1144
|
+
# ## Document: amendments.docx
|
|
1145
|
+
# ...
|
|
1146
|
+
```
|
|
1147
|
+
|
|
1148
|
+
---
|
|
1149
|
+
|
|
1150
|
+
## Updated Installation
|
|
1151
|
+
|
|
1152
|
+
```bash
|
|
1153
|
+
# Core library (Anthropic only)
|
|
1154
|
+
pip install llm-optimizer
|
|
1155
|
+
|
|
1156
|
+
# With AWS Bedrock + Converse API
|
|
1157
|
+
pip install llm-optimizer[bedrock]
|
|
1158
|
+
|
|
1159
|
+
# With document parsing (PDF, DOCX, XLSX)
|
|
1160
|
+
pip install llm-optimizer[documents]
|
|
1161
|
+
|
|
1162
|
+
# With exact token counting
|
|
1163
|
+
pip install llm-optimizer[tiktoken]
|
|
1164
|
+
|
|
1165
|
+
# Everything
|
|
1166
|
+
pip install llm-optimizer[all]
|
|
1167
|
+
```
|
|
1168
|
+
|
|
1169
|
+
---
|
|
1170
|
+
|
|
1171
|
+
---
|
|
1172
|
+
|
|
1173
|
+
### 19. 🖥️ Local Model Support — Ollama, LM Studio, vLLM, llama.cpp
|
|
1174
|
+
|
|
1175
|
+
Run any local LLM through the same wrapper. Works with any server that
|
|
1176
|
+
exposes an OpenAI-compatible endpoint — no cloud, no API keys, no billing.
|
|
1177
|
+
|
|
1178
|
+
**Install a local server first:**
|
|
1179
|
+
|
|
1180
|
+
```bash
|
|
1181
|
+
# Ollama (recommended — easiest setup)
|
|
1182
|
+
brew install ollama # macOS
|
|
1183
|
+
# or download from ollama.ai
|
|
1184
|
+
|
|
1185
|
+
ollama pull llama3 # download Llama 3
|
|
1186
|
+
ollama pull mistral # download Mistral
|
|
1187
|
+
ollama pull gemma2 # download Gemma 2
|
|
1188
|
+
ollama serve # start the server
|
|
1189
|
+
```
|
|
1190
|
+
|
|
1191
|
+
**Use with llm-optimizer:**
|
|
1192
|
+
|
|
1193
|
+
```python
|
|
1194
|
+
from llm_optimizer import OptimizedClient, Provider
|
|
1195
|
+
from llm_optimizer.local import make_local_client
|
|
1196
|
+
|
|
1197
|
+
# Ollama
|
|
1198
|
+
client = OptimizedClient(
|
|
1199
|
+
local_client=make_local_client(
|
|
1200
|
+
base_url="http://localhost:11434/v1",
|
|
1201
|
+
model="llama3",
|
|
1202
|
+
),
|
|
1203
|
+
preferred_provider=Provider.LOCAL,
|
|
1204
|
+
enable_output_cache=True, # cache hits still work
|
|
1205
|
+
enable_optimization=True, # prompt optimization still works
|
|
1206
|
+
)
|
|
1207
|
+
|
|
1208
|
+
response = client.complete(
|
|
1209
|
+
messages=[{"role": "user", "content": "Summarize this document"}],
|
|
1210
|
+
system="You are a helpful assistant.",
|
|
1211
|
+
)
|
|
1212
|
+
print(response.content[0].text)
|
|
1213
|
+
```
|
|
1214
|
+
|
|
1215
|
+
**Convenience factories for each server:**
|
|
1216
|
+
|
|
1217
|
+
```python
|
|
1218
|
+
from llm_optimizer.local import (
|
|
1219
|
+
make_ollama_client,
|
|
1220
|
+
make_lmstudio_client,
|
|
1221
|
+
make_vllm_client,
|
|
1222
|
+
make_local_client,
|
|
1223
|
+
)
|
|
1224
|
+
|
|
1225
|
+
# Ollama (localhost:11434)
|
|
1226
|
+
client = OptimizedClient(
|
|
1227
|
+
local_client=make_ollama_client(model="llama3"),
|
|
1228
|
+
preferred_provider=Provider.LOCAL,
|
|
1229
|
+
)
|
|
1230
|
+
|
|
1231
|
+
# LM Studio (localhost:1234)
|
|
1232
|
+
client = OptimizedClient(
|
|
1233
|
+
local_client=make_lmstudio_client(model="mistral-7b-instruct"),
|
|
1234
|
+
preferred_provider=Provider.LOCAL,
|
|
1235
|
+
)
|
|
1236
|
+
|
|
1237
|
+
# vLLM — self-hosted GPU server
|
|
1238
|
+
client = OptimizedClient(
|
|
1239
|
+
local_client=make_vllm_client(
|
|
1240
|
+
model="meta-llama/Llama-3-70b-instruct",
|
|
1241
|
+
host="your-gpu-server.internal",
|
|
1242
|
+
port=8000,
|
|
1243
|
+
),
|
|
1244
|
+
preferred_provider=Provider.LOCAL,
|
|
1245
|
+
)
|
|
1246
|
+
|
|
1247
|
+
# Any custom OpenAI-compatible endpoint
|
|
1248
|
+
client = OptimizedClient(
|
|
1249
|
+
local_client=make_local_client(
|
|
1250
|
+
base_url="http://your-server:8080/v1",
|
|
1251
|
+
model="your-model-name",
|
|
1252
|
+
api_key="optional-key",
|
|
1253
|
+
),
|
|
1254
|
+
preferred_provider=Provider.LOCAL,
|
|
1255
|
+
)
|
|
1256
|
+
```
|
|
1257
|
+
|
|
1258
|
+
**Supported local servers:**
|
|
1259
|
+
|
|
1260
|
+
| Server | Default Port | Notes |
|
|
1261
|
+
|---|---|---|
|
|
1262
|
+
| Ollama | 11434 | `ollama pull <model>` then `ollama serve` |
|
|
1263
|
+
| LM Studio | 1234 | Enable API server in LM Studio settings |
|
|
1264
|
+
| llama.cpp server | 8080 | `./server -m model.gguf --port 8080` |
|
|
1265
|
+
| vLLM | 8000 | `python -m vllm.entrypoints.openai.api_server` |
|
|
1266
|
+
| LocalAI | 8080 | `docker run -p 8080:8080 localai/localai` |
|
|
1267
|
+
| Jan | 1337 | Enable API server in Jan settings |
|
|
1268
|
+
|
|
1269
|
+
**Check available models and server health:**
|
|
1270
|
+
|
|
1271
|
+
```python
|
|
1272
|
+
from llm_optimizer.local import make_ollama_client
|
|
1273
|
+
|
|
1274
|
+
local = make_ollama_client(model="llama3")
|
|
1275
|
+
|
|
1276
|
+
# Check if server is running
|
|
1277
|
+
print(local.health_check()) # True or False
|
|
1278
|
+
|
|
1279
|
+
# List available models
|
|
1280
|
+
print(local.list_models()) # ["llama3", "mistral", "gemma2"]
|
|
1281
|
+
|
|
1282
|
+
# Usage stats
|
|
1283
|
+
print(local.stats())
|
|
1284
|
+
# {
|
|
1285
|
+
# "base_url": "http://localhost:11434/v1",
|
|
1286
|
+
# "model": "llama3",
|
|
1287
|
+
# "total_requests": 14,
|
|
1288
|
+
# "total_tokens": 2840,
|
|
1289
|
+
# "server_reachable": True
|
|
1290
|
+
# }
|
|
1291
|
+
```
|
|
1292
|
+
|
|
1293
|
+
**What works with local models:**
|
|
1294
|
+
|
|
1295
|
+
| Feature | Works locally | Notes |
|
|
1296
|
+
|---|---|---|
|
|
1297
|
+
| Output cache | ✅ | Cache hits at zero cost |
|
|
1298
|
+
| Semantic cache | ✅ | Fuzzy matching works |
|
|
1299
|
+
| Cross-provider cache | ✅ | Share cache with cloud providers |
|
|
1300
|
+
| Prompt optimization | ✅ | Filler stripping reduces tokens |
|
|
1301
|
+
| Document parser | ✅ | PDF/DOCX/XLSX to Markdown |
|
|
1302
|
+
| History summarization | ✅ | Keeps conversations manageable |
|
|
1303
|
+
| Few-shot selector | ✅ | Dynamic example selection |
|
|
1304
|
+
| JSON auto-correction | ✅ | Fixes broken output |
|
|
1305
|
+
| Cost tracking | ✅ | Records requests (cost: $0.00) |
|
|
1306
|
+
| Streaming | ✅ | `client.stream()` works |
|
|
1307
|
+
| Prompt caching | ⚪ | Requires Anthropic/Bedrock |
|
|
1308
|
+
| Model routing | ⚪ | Use `force_model` instead |
|
|
1309
|
+
| Budget enforcement | ⚪ | Cost is already $0 |
|
|
1310
|
+
|
|
1311
|
+
**Streaming with local models:**
|
|
1312
|
+
|
|
1313
|
+
```python
|
|
1314
|
+
client = OptimizedClient(
|
|
1315
|
+
local_client=make_ollama_client(model="llama3"),
|
|
1316
|
+
preferred_provider=Provider.LOCAL,
|
|
1317
|
+
)
|
|
1318
|
+
|
|
1319
|
+
with client.stream(
|
|
1320
|
+
messages=[{"role": "user", "content": "Write a short story about clouds"}],
|
|
1321
|
+
system="You are a creative writer.",
|
|
1322
|
+
) as stream:
|
|
1323
|
+
for chunk in stream:
|
|
1324
|
+
print(chunk, end="", flush=True)
|
|
1325
|
+
```
|
|
1326
|
+
|
|
1327
|
+
|
|
953
1328
|
---
|
|
954
1329
|
|
|
955
1330
|
## License
|