runapprentice 0.1.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.
- runapprentice/__init__.py +26 -0
- runapprentice/_capture.py +117 -0
- runapprentice/client.py +391 -0
- runapprentice/langchain.py +403 -0
- runapprentice/models.py +62 -0
- runapprentice/openai.py +126 -0
- runapprentice/py.typed +0 -0
- runapprentice-0.1.0.dist-info/METADATA +219 -0
- runapprentice-0.1.0.dist-info/RECORD +11 -0
- runapprentice-0.1.0.dist-info/WHEEL +4 -0
- runapprentice-0.1.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: runapprentice
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Cut LLM cost without losing quality: capture traffic, build golden datasets, and optimize prompts.
|
|
5
|
+
Project-URL: Homepage, https://runapprentice.com
|
|
6
|
+
Project-URL: Documentation, https://docs.runapprentice.com
|
|
7
|
+
Author: Abhishek Singh
|
|
8
|
+
License-Expression: MIT
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Keywords: dspy,evaluation,gepa,langchain,llm,prompt-optimization,rag
|
|
11
|
+
Classifier: Development Status :: 3 - Alpha
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
16
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
17
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
18
|
+
Classifier: Typing :: Typed
|
|
19
|
+
Requires-Python: >=3.11
|
|
20
|
+
Requires-Dist: httpx>=0.27
|
|
21
|
+
Requires-Dist: pydantic>=2.0
|
|
22
|
+
Provides-Extra: langchain
|
|
23
|
+
Requires-Dist: langchain-core>=0.3; extra == 'langchain'
|
|
24
|
+
Description-Content-Type: text/markdown
|
|
25
|
+
|
|
26
|
+
# runapprentice
|
|
27
|
+
|
|
28
|
+
Capture your LLM traffic, build a golden dataset, and optimize your prompt against it. Training a small model to take over from the frontier model is planned for a later release.
|
|
29
|
+
|
|
30
|
+
This README documents the **implemented surface** (v0.1). Anything not shown here does not exist yet.
|
|
31
|
+
|
|
32
|
+
## Install
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
uv add runapprentice[langchain] # [langchain] extra enables zero-code capture
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Quickstart (the whole trial journey)
|
|
39
|
+
|
|
40
|
+
```python
|
|
41
|
+
from runapprentice import Apprentice
|
|
42
|
+
|
|
43
|
+
client = Apprentice(
|
|
44
|
+
api_key="ap_live_...", # create once in the console, or env APPRENTICE_API_KEY
|
|
45
|
+
) # talks to the hosted API; set base_url only to self-host
|
|
46
|
+
|
|
47
|
+
# 1. Create a task (a "task" = one repeatable LLM job you want to make cheaper)
|
|
48
|
+
client.tasks.create("ticket-triage")
|
|
49
|
+
|
|
50
|
+
# 2. Point us at the data you already have (CSV with input/output columns)
|
|
51
|
+
client.datasets.upload(
|
|
52
|
+
"ticket-triage",
|
|
53
|
+
path="golden.csv",
|
|
54
|
+
input_col="input",
|
|
55
|
+
output_col="output",
|
|
56
|
+
prompt="...your current system prompt...", # the baseline to beat
|
|
57
|
+
)
|
|
58
|
+
|
|
59
|
+
# 3. Optimize the prompt against your data (GEPA, held-out eval)
|
|
60
|
+
job = client.optimize("ticket-triage")
|
|
61
|
+
report = job.wait().report()
|
|
62
|
+
print(report.baseline_score, "->", report.optimized_score)
|
|
63
|
+
|
|
64
|
+
# 4. Pull the optimized prompt back into your code (versioned)
|
|
65
|
+
best = client.prompts.get("ticket-triage")
|
|
66
|
+
print(best.version, best.text)
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
## Live capture: LangChain (one line, zero code changes)
|
|
70
|
+
|
|
71
|
+
```python
|
|
72
|
+
# init_chat_model comes from your LangChain model package (for example
|
|
73
|
+
# langchain[openai]); runapprentice[langchain] only adds langchain-core for
|
|
74
|
+
# the callback.
|
|
75
|
+
from langchain.chat_models import init_chat_model
|
|
76
|
+
from runapprentice.langchain import ApprenticeCallback
|
|
77
|
+
|
|
78
|
+
model = init_chat_model(
|
|
79
|
+
"gpt-5.5", # whatever YOU already use. We observe, we don't choose
|
|
80
|
+
callbacks=[ApprenticeCallback("ticket-triage", client)],
|
|
81
|
+
)
|
|
82
|
+
# Every call now logs input/output/model/latency/tokens to the task's dataset (raw tier).
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
**Fail-open guarantee:** the capture path can NEVER break your app. If the
|
|
86
|
+
Apprentice backend is down, unreachable, or slow, your LLM calls proceed
|
|
87
|
+
untouched. The only loss is the trace. First drop logs a WARNING, repeats log
|
|
88
|
+
at DEBUG.
|
|
89
|
+
|
|
90
|
+
**PII redaction** runs in your process, before anything is transmitted:
|
|
91
|
+
|
|
92
|
+
```python
|
|
93
|
+
ApprenticeCallback("ticket-triage", client, redact=lambda s: my_scrubber(s))
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
## RAG quickstart (grounding + refusal-aware)
|
|
97
|
+
|
|
98
|
+
For retrieval-augmented tasks, each row carries the **exact context the model saw**
|
|
99
|
+
(the retrieved passages), not just the question. Pick the `rag_composite` metric so
|
|
100
|
+
optimization rewards grounding and correct refusals, not just answer overlap.
|
|
101
|
+
|
|
102
|
+
```python
|
|
103
|
+
from runapprentice import Apprentice
|
|
104
|
+
client = Apprentice(api_key="ap_live_...")
|
|
105
|
+
|
|
106
|
+
# rag_composite scores answer correctness + faithfulness to context + refusal
|
|
107
|
+
# correctness (it should say "not enough information" when the context lacks it).
|
|
108
|
+
client.tasks.create("support-rag", metric="rag_composite")
|
|
109
|
+
|
|
110
|
+
client.datasets.upload("support-rag", rows=[
|
|
111
|
+
{"inputs": {"question": q, "context": retrieved_passages}, "output": gold_answer}
|
|
112
|
+
for q, retrieved_passages, gold_answer in your_golden_set
|
|
113
|
+
])
|
|
114
|
+
|
|
115
|
+
client.prompts.register("support-rag", {
|
|
116
|
+
"format": "f-string",
|
|
117
|
+
"messages": [
|
|
118
|
+
{"role": "system", "template":
|
|
119
|
+
"Answer using only the context. If the context does not contain the "
|
|
120
|
+
"answer, say you do not have enough information.\n{context}"},
|
|
121
|
+
{"role": "human", "template": "{question}"},
|
|
122
|
+
],
|
|
123
|
+
"input_variables": ["context", "question"],
|
|
124
|
+
})
|
|
125
|
+
|
|
126
|
+
report = client.optimize("support-rag").wait().report()
|
|
127
|
+
print(report.baseline_score, "->", report.optimized_score)
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
**RAG capture for simple chains.** For a standard LangChain RAG chain, the
|
|
131
|
+
`ApprenticeCallback` records the retrieved context from `on_retriever_end`, capturing
|
|
132
|
+
the `{question, context}` shape. For custom formatting, multiple retrievers, or
|
|
133
|
+
non-standard chains, call `client.capture(..., inputs={"question": ..., "context": ...})`
|
|
134
|
+
explicitly.
|
|
135
|
+
|
|
136
|
+
### Register a LangChain prompt directly
|
|
137
|
+
|
|
138
|
+
`prompts.register` also accepts a LangChain `ChatPromptTemplate`, no raw dict needed:
|
|
139
|
+
|
|
140
|
+
```python
|
|
141
|
+
from langchain_core.prompts import ChatPromptTemplate
|
|
142
|
+
client.prompts.register("support-rag", ChatPromptTemplate.from_messages([
|
|
143
|
+
("system", "Answer using only the context. If it lacks the answer, refuse.\n{context}"),
|
|
144
|
+
("human", "{question}"),
|
|
145
|
+
]))
|
|
146
|
+
# round-trip the optimized prompt back into LangChain:
|
|
147
|
+
optimized = client.prompts.to_langchain("support-rag")
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
### Metric menu
|
|
151
|
+
|
|
152
|
+
| `metric=` | Use for | Scored by |
|
|
153
|
+
|---|---|---|
|
|
154
|
+
| `auto` (default) | let the backend infer from your rows | inferred |
|
|
155
|
+
| `json_f1` | JSON / structured extraction | deterministic |
|
|
156
|
+
| `text_f1` | short free-text answers | deterministic |
|
|
157
|
+
| `semantic_f1` | free-text / RAG answers (default for RAG) | LLM judge |
|
|
158
|
+
| `rag_faithfulness` | is every claim supported by the context | LLM judge |
|
|
159
|
+
| `rag_composite` | RAG grounding **and** refusal correctness | LLM judge |
|
|
160
|
+
|
|
161
|
+
RAG rows auto-route to `semantic_f1`; pass `metric="rag_composite"` explicitly when you
|
|
162
|
+
want grounding + refusal optimized together.
|
|
163
|
+
|
|
164
|
+
## Feedback (your end-users' signal)
|
|
165
|
+
|
|
166
|
+
```python
|
|
167
|
+
client.feedback(trace_id, good=True) # thumbs
|
|
168
|
+
client.feedback(trace_id, score=0.5, note="partial") # graded
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
## Debugging
|
|
172
|
+
|
|
173
|
+
```python
|
|
174
|
+
import runapprentice
|
|
175
|
+
runapprentice.enable_debug_logging() # or env APPRENTICE_DEBUG=1
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
Shows every API call with status + latency, every capture attempt, every job
|
|
179
|
+
poll. Control-plane errors are designed to tell you what to do (e.g. unreachable
|
|
180
|
+
backend includes the URL it tried; optimize with too few rows tells you the
|
|
181
|
+
count and the minimum).
|
|
182
|
+
|
|
183
|
+
## API reference (implemented surface)
|
|
184
|
+
|
|
185
|
+
| Call | Returns | Raises |
|
|
186
|
+
|---|---|---|
|
|
187
|
+
| `Apprentice(api_key=, base_url=, timeout=)` | client | `ApprenticeError` on bad config |
|
|
188
|
+
| `client.tasks.create(name, metric="auto")` | dict (`created` bool) | `ApprenticeError` on HTTP failure |
|
|
189
|
+
| `client.datasets.upload(task, path= or rows=, input_col=, output_col=, prompt=)` | `DatasetStatus` | same; also if both/neither of path/rows |
|
|
190
|
+
| `client.datasets.status(task)` | `DatasetStatus(gold, silver, raw, ready_for_optimization)` | same |
|
|
191
|
+
| `client.prompts.register(task, template)` | dict | `ImportError` if a LangChain template is passed without the `[langchain]` extra |
|
|
192
|
+
| `client.prompts.to_langchain(task= or artifact=)` | LangChain prompt | `ImportError` without the `[langchain]` extra |
|
|
193
|
+
| `client.optimize(task)` | `Job` | 400 if fewer than the backend's configured verified-row minimum (20 by default) |
|
|
194
|
+
| `client.job(job_id)` / `job.refresh()` / `job.wait(poll_seconds=, timeout_seconds=)` | `Job` | `ApprenticeError` on timeout |
|
|
195
|
+
| `job.report()` | `OptimizationReport(baseline_score, optimized_score, optimized_prompt, ...)` | if job has no report |
|
|
196
|
+
| `client.prompts.get(task, version=None)` | `PromptVersion(version, text, score)` | 404 if never optimized |
|
|
197
|
+
| `client.prompts.history(task)` | `list[PromptVersion]` | `ApprenticeError` on API failure |
|
|
198
|
+
| `client.feedback(trace_id, good=, score=, note=)` | None | 404 unknown trace |
|
|
199
|
+
| `client.post_trace_failopen(record)` | trace_id or None | **never raises** |
|
|
200
|
+
| `ApprenticeCallback(task, client, redact=None)` | LangChain callback | **never raises into your chain** |
|
|
201
|
+
|
|
202
|
+
## Data tiers (how your rows are treated)
|
|
203
|
+
|
|
204
|
+
- **raw**: captured from live traffic, unverified
|
|
205
|
+
- **silver**: uploaded by you (curated) or passed deterministic checks
|
|
206
|
+
- **gold**: human-verified
|
|
207
|
+
- Optimization uses gold + silver; eval gates will use **gold only**.
|
|
208
|
+
|
|
209
|
+
## For AI coding tools
|
|
210
|
+
|
|
211
|
+
Module docstrings and this README are the source of truth. Key invariants an
|
|
212
|
+
agent must preserve when editing this package:
|
|
213
|
+
|
|
214
|
+
1. `post_trace_failopen` and everything in `runapprentice/langchain.py` must never
|
|
215
|
+
raise into the caller. Capture is fail-open by contract (see `tests/test_failopen.py`).
|
|
216
|
+
2. Control-plane methods must raise `ApprenticeError` with actionable messages.
|
|
217
|
+
3. Dependencies stay minimal: `httpx` + `pydantic`; LangChain only via the
|
|
218
|
+
`[langchain]` extra; never import from `apprentice-api`.
|
|
219
|
+
4. Required tests for any new method: add a row to `../needed_test.md`.
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
runapprentice/__init__.py,sha256=LtMfKOifoqwr0jQMB_uppWmoQ-3uO1SjcQq6oOj8MaE,560
|
|
2
|
+
runapprentice/_capture.py,sha256=2XMi8lXSj6MSi9vUAUcard3z5eM-Kl5Wn3It5AKP-GU,4387
|
|
3
|
+
runapprentice/client.py,sha256=AutaxNBLlI8gD22OxzLnhoN5LPvxcFhuqM5dgLveo1o,14607
|
|
4
|
+
runapprentice/langchain.py,sha256=vQ27ZcvCfS2uUB09iJxqz0wAWymLei7Mpu2xtY7PaPg,15924
|
|
5
|
+
runapprentice/models.py,sha256=55m8p6QRvxBU7mwGdqGy6gXrVVDLkoo7jFRL5Ds3y5A,1502
|
|
6
|
+
runapprentice/openai.py,sha256=tAcLKlGVhcC-YRtKqQH0r3_W7uTLW-Ze1vIg9d3bM1Y,5021
|
|
7
|
+
runapprentice/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
8
|
+
runapprentice-0.1.0.dist-info/METADATA,sha256=V1h6jumwvHNaaPJHE8OWDVFLEdxumsM9Y-qSyjAl21c,9263
|
|
9
|
+
runapprentice-0.1.0.dist-info/WHEEL,sha256=mffPy8wBnZQn2VnJUU5jE99KsxaSfiyMHV9Yt0aLVxs,87
|
|
10
|
+
runapprentice-0.1.0.dist-info/licenses/LICENSE,sha256=IY1vHVqwz9lYgq5UDL31X4dJWUIzbx07owwIdAZzd48,1071
|
|
11
|
+
runapprentice-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Abhishek Singh
|
|
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.
|