prompt-pivot 0.1.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.
- prompt_pivot-0.1.0/.gitignore +12 -0
- prompt_pivot-0.1.0/CHANGELOG.md +19 -0
- prompt_pivot-0.1.0/LICENSE +202 -0
- prompt_pivot-0.1.0/PKG-INFO +343 -0
- prompt_pivot-0.1.0/README.md +302 -0
- prompt_pivot-0.1.0/pyproject.toml +96 -0
- prompt_pivot-0.1.0/src/promptpivot/__init__.py +46 -0
- prompt_pivot-0.1.0/src/promptpivot/cache.py +148 -0
- prompt_pivot-0.1.0/src/promptpivot/cli.py +126 -0
- prompt_pivot-0.1.0/src/promptpivot/errors.py +31 -0
- prompt_pivot-0.1.0/src/promptpivot/fastapi.py +649 -0
- prompt_pivot-0.1.0/src/promptpivot/migrations/__init__.py +60 -0
- prompt_pivot-0.1.0/src/promptpivot/migrations/env.py +14 -0
- prompt_pivot-0.1.0/src/promptpivot/migrations/script.py.mako +20 -0
- prompt_pivot-0.1.0/src/promptpivot/migrations/versions/0001_initial.py +22 -0
- prompt_pivot-0.1.0/src/promptpivot/models.py +196 -0
- prompt_pivot-0.1.0/src/promptpivot/py.typed +0 -0
- prompt_pivot-0.1.0/src/promptpivot/registry.py +785 -0
- prompt_pivot-0.1.0/src/promptpivot/schema.py +128 -0
- prompt_pivot-0.1.0/src/promptpivot/store.py +874 -0
- prompt_pivot-0.1.0/src/promptpivot/templates/audit.html +8 -0
- prompt_pivot-0.1.0/src/promptpivot/templates/base.html +34 -0
- prompt_pivot-0.1.0/src/promptpivot/templates/diff.html +5 -0
- prompt_pivot-0.1.0/src/promptpivot/templates/error.html +3 -0
- prompt_pivot-0.1.0/src/promptpivot/templates/import.html +9 -0
- prompt_pivot-0.1.0/src/promptpivot/templates/index.html +12 -0
- prompt_pivot-0.1.0/src/promptpivot/templates/new.html +10 -0
- prompt_pivot-0.1.0/src/promptpivot/templates/preview.html +4 -0
- prompt_pivot-0.1.0/src/promptpivot/templates/prompt.html +60 -0
- prompt_pivot-0.1.0/src/promptpivot/templating.py +145 -0
- prompt_pivot-0.1.0/tests/conftest.py +67 -0
- prompt_pivot-0.1.0/tests/test_async.py +91 -0
- prompt_pivot-0.1.0/tests/test_cli.py +68 -0
- prompt_pivot-0.1.0/tests/test_fastapi.py +362 -0
- prompt_pivot-0.1.0/tests/test_registry.py +339 -0
- prompt_pivot-0.1.0/tests/test_templating.py +46 -0
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 0.1.0 (2026-10-19)
|
|
4
|
+
|
|
5
|
+
Distribution and CLI name `prompt-pivot`, import name `promptpivot` (earlier working names were
|
|
6
|
+
too similar to existing PyPI projects).
|
|
7
|
+
|
|
8
|
+
First release. Prompts, immutable versions, locales with fallback, labels with optimistic locking,
|
|
9
|
+
change requests with a single approver, append-only audit, per-process TTL+LRU cache with
|
|
10
|
+
single-flight and stale-while-error, typed handles via `define()`, python and jinja formats,
|
|
11
|
+
FastAPI admin UI and REST API with CSRF, CLI (`migrate`, `export`, `import`, `publish`,
|
|
12
|
+
`rollback`, `list`), Alembic migration `0001`.
|
|
13
|
+
|
|
14
|
+
Developer-experience pass before release: `create_version` keeps the previous config when none is
|
|
15
|
+
given and returns a record with `warning`; chat templates accept `Message` objects;
|
|
16
|
+
`compile_text()` / `compile_messages()`; `default_actor`; `PromptRegistry.in_memory()`;
|
|
17
|
+
`import_prompts` (old `import_` kept); UI Import page and Export download (`api/export`,
|
|
18
|
+
`api/import`); rollback buttons show the destination; inline reason and locale fields instead of
|
|
19
|
+
browser dialogs; `dev_principal` guarded by `PROMPTPIVOT_DEV_UI=1`.
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
|
@@ -0,0 +1,343 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: prompt-pivot
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Store, version, edit and publish LLM prompts at runtime from inside your FastAPI app, using your own PostgreSQL or MySQL database.
|
|
5
|
+
Project-URL: Homepage, https://github.com/Lasitha-Jayawardana/prompt-pivot
|
|
6
|
+
Project-URL: Repository, https://github.com/Lasitha-Jayawardana/prompt-pivot
|
|
7
|
+
Project-URL: Issues, https://github.com/Lasitha-Jayawardana/prompt-pivot/issues
|
|
8
|
+
Project-URL: Changelog, https://github.com/Lasitha-Jayawardana/prompt-pivot/blob/main/CHANGELOG.md
|
|
9
|
+
Author: Lasitha Jayawardana
|
|
10
|
+
License-Expression: Apache-2.0
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
Keywords: fastapi,llm,prompt-management,prompts,sqlalchemy
|
|
13
|
+
Classifier: Development Status :: 3 - Alpha
|
|
14
|
+
Classifier: Framework :: FastAPI
|
|
15
|
+
Classifier: Intended Audience :: Developers
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
21
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
22
|
+
Classifier: Typing :: Typed
|
|
23
|
+
Requires-Python: >=3.10
|
|
24
|
+
Requires-Dist: fastapi>=0.110
|
|
25
|
+
Requires-Dist: jinja2>=3.1
|
|
26
|
+
Requires-Dist: pydantic>=2.0
|
|
27
|
+
Requires-Dist: sqlalchemy>=2.0
|
|
28
|
+
Provides-Extra: alembic
|
|
29
|
+
Requires-Dist: alembic>=1.13; extra == 'alembic'
|
|
30
|
+
Provides-Extra: mysql
|
|
31
|
+
Requires-Dist: aiomysql>=0.2; extra == 'mysql'
|
|
32
|
+
Requires-Dist: pymysql>=1.1; extra == 'mysql'
|
|
33
|
+
Provides-Extra: otel
|
|
34
|
+
Requires-Dist: opentelemetry-api>=1.20; extra == 'otel'
|
|
35
|
+
Provides-Extra: postgres
|
|
36
|
+
Requires-Dist: asyncpg>=0.29; extra == 'postgres'
|
|
37
|
+
Requires-Dist: psycopg[binary]>=3.1; extra == 'postgres'
|
|
38
|
+
Provides-Extra: yaml
|
|
39
|
+
Requires-Dist: pyyaml>=6.0; extra == 'yaml'
|
|
40
|
+
Description-Content-Type: text/markdown
|
|
41
|
+
|
|
42
|
+
# prompt-pivot
|
|
43
|
+
|
|
44
|
+
Store, version, edit and publish LLM prompts at runtime from inside your FastAPI app, using the
|
|
45
|
+
PostgreSQL or MySQL database you already have. No extra server, no vendor cloud, no background
|
|
46
|
+
workers.
|
|
47
|
+
|
|
48
|
+
- **One call in agent code**: `prompts.get("support/agent", locale="fr")` returns an immutable,
|
|
49
|
+
typed handle with the template, model config, metadata and version.
|
|
50
|
+
- **Edit without a deploy**: a built-in admin UI (server-rendered, no build step, no CDN) to edit,
|
|
51
|
+
preview, diff, publish, approve and roll back. Every change is versioned, attributed and audited.
|
|
52
|
+
- **Fast and resilient**: lazy per-process LRU cache (a hit is under 1 µs, no I/O), one indexed query
|
|
53
|
+
on a miss, single-flight loading, and stale-while-error serving if the database blips.
|
|
54
|
+
- **Small**: four runtime dependencies you already have in a FastAPI app (SQLAlchemy, Pydantic,
|
|
55
|
+
Jinja2, FastAPI). About 3,000 lines including the UI. `py.typed`, mypy strict clean.
|
|
56
|
+
|
|
57
|
+
Full feature, use-case and UI walkthrough: [docs/guide.md](docs/guide.md).
|
|
58
|
+
|
|
59
|
+
## Install
|
|
60
|
+
|
|
61
|
+
```bash
|
|
62
|
+
pip install prompt-pivot
|
|
63
|
+
# optional extras
|
|
64
|
+
pip install "prompt-pivot[postgres]" # asyncpg + psycopg
|
|
65
|
+
pip install "prompt-pivot[mysql]" # aiomysql + pymysql
|
|
66
|
+
pip install "prompt-pivot[alembic]" # versioned schema migrations (recommended in production)
|
|
67
|
+
pip install "prompt-pivot[yaml]" # YAML export/import in the CLI
|
|
68
|
+
pip install "prompt-pivot[otel]" # OpenTelemetry span attributes
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
Python 3.10+, SQLAlchemy 2, Pydantic 2, FastAPI 0.110+. PostgreSQL 13+ and MySQL 8+ (SQLite works
|
|
72
|
+
for local development and tests).
|
|
73
|
+
|
|
74
|
+
## Quickstart (10 minutes)
|
|
75
|
+
|
|
76
|
+
**1. Create the schema** (once per database, alongside your own migrations):
|
|
77
|
+
|
|
78
|
+
```bash
|
|
79
|
+
promptpivot --url postgresql+psycopg://user:pass@host/db migrate
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
or from code: `from promptpivot.migrations import upgrade; upgrade(engine)`.
|
|
83
|
+
|
|
84
|
+
**2. Create the registry and mount the admin UI:**
|
|
85
|
+
|
|
86
|
+
```python
|
|
87
|
+
from fastapi import Depends, FastAPI
|
|
88
|
+
from sqlalchemy.ext.asyncio import create_async_engine
|
|
89
|
+
from promptpivot import PromptRegistry, Principal
|
|
90
|
+
from promptpivot.fastapi import admin_router
|
|
91
|
+
|
|
92
|
+
engine = create_async_engine("postgresql+asyncpg://user:pass@host/db") # async or sync engine
|
|
93
|
+
prompts = PromptRegistry(engine, environment="production")
|
|
94
|
+
|
|
95
|
+
async def principal(user=Depends(get_current_user)) -> Principal: # your existing auth
|
|
96
|
+
return Principal(id=user.id, name=user.email, permissions=map_roles(user.roles))
|
|
97
|
+
|
|
98
|
+
app = FastAPI()
|
|
99
|
+
app.include_router(admin_router(prompts, principal=principal), prefix="/admin/prompts")
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
Nothing is exposed unless you mount the router, and the router always runs your `principal`
|
|
103
|
+
dependency first. For a first local run, `from promptpivot.fastapi import dev_principal` grants
|
|
104
|
+
every permission; it only works when `PROMPTPIVOT_DEV_UI=1` is set, so it cannot ship by accident.
|
|
105
|
+
|
|
106
|
+
**3. Read prompts in agent code:**
|
|
107
|
+
|
|
108
|
+
```python
|
|
109
|
+
p = await prompts.aget("support/agent", locale="fr-CA") # fr-CA -> fr -> en
|
|
110
|
+
messages = p.compile_messages(customer_name="Alice", plan="Pro") # list[Message]; compile_text() for text prompts
|
|
111
|
+
response = await llm.chat(model=p.config.model, temperature=p.config.temperature, messages=messages)
|
|
112
|
+
span.set_attributes(p.trace_attributes()) # prompt.name / version / label / locale / is_fallback
|
|
113
|
+
|
|
114
|
+
# sync code works too, with an optional in-code default used only when nothing else can be served
|
|
115
|
+
p = prompts.get("greeting", default="Hello {customer_name}!")
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
**4. Typed prompts (recommended):**
|
|
119
|
+
|
|
120
|
+
```python
|
|
121
|
+
from pydantic import BaseModel
|
|
122
|
+
|
|
123
|
+
class SupportVars(BaseModel):
|
|
124
|
+
customer_name: str
|
|
125
|
+
plan: str
|
|
126
|
+
|
|
127
|
+
class SupportMeta(BaseModel):
|
|
128
|
+
owner: str
|
|
129
|
+
team: str | None = None
|
|
130
|
+
|
|
131
|
+
support_agent = prompts.define("support/agent", variables=SupportVars, metadata=SupportMeta)
|
|
132
|
+
|
|
133
|
+
p = await support_agent.aget(locale=user.locale) # Prompt[SupportVars, PromptConfig, SupportMeta]
|
|
134
|
+
messages = p.compile(SupportVars(customer_name="Alice", plan="Pro"))
|
|
135
|
+
owner: str = p.metadata.owner
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
`compile(**kwargs)` validates the keyword arguments against the variables model and raises
|
|
139
|
+
`PromptValidationError` on a mismatch. A custom `config=` model types `p.config` the same way.
|
|
140
|
+
|
|
141
|
+
**5. Open `/admin/prompts`** to edit, preview, publish and roll back. See [examples/app.py](examples/app.py)
|
|
142
|
+
for a runnable demo (`uvicorn examples.app:app --reload`).
|
|
143
|
+
|
|
144
|
+
## Concepts
|
|
145
|
+
|
|
146
|
+
| Concept | Meaning |
|
|
147
|
+
| --- | --- |
|
|
148
|
+
| **Prompt** | A named, hierarchical unit (`support/triage/classify`) of type `text` or `chat`, with tags and free-form JSON metadata. |
|
|
149
|
+
| **Version** | Immutable snapshot of template, format (`python` or `jinja`), config, variables, commit message and author. Each save creates the next number. |
|
|
150
|
+
| **Locale** | BCP 47 variant (`en`, `fr`, `pt-BR`) of a prompt, with its own versions and labels. Resolution: exact tag, base language, then the default locale. |
|
|
151
|
+
| **Label** | A named pointer to one version of one locale: `production`, `staging`, `latest` (moves on save) or any custom name. Code reads labels, never raw version numbers. |
|
|
152
|
+
| **Change request** | Required to move a protected label (default: `production`). One approver, who must differ from the author. |
|
|
153
|
+
|
|
154
|
+
### Template formats
|
|
155
|
+
|
|
156
|
+
| Format | Example | Rules |
|
|
157
|
+
| --- | --- | --- |
|
|
158
|
+
| `python` | `Help {customer_name} on the {plan} plan.` | Named fields only. No attribute access, indexing or conversions. `{{` and `}}` for literal braces. Plain format specs such as `{n:03d}` are allowed. |
|
|
159
|
+
| `jinja` | `Help {{ customer_name }}{% if plan == "Pro" %} with priority{% endif %}.` | Sandboxed environment, strict undefined, compiled once per version. |
|
|
160
|
+
|
|
161
|
+
Missing variables raise `PromptRenderError` in both formats.
|
|
162
|
+
|
|
163
|
+
### The handle
|
|
164
|
+
|
|
165
|
+
`get()` / `aget()` return a frozen `Prompt` with: `name`, `version`, `label`, `locale`,
|
|
166
|
+
`requested_locale`, `type`, `format`, `template`, `config`, `metadata`, `variables`, `is_fallback`,
|
|
167
|
+
`checksum`, plus `compile(...)`, `compile_text(...)`, `compile_messages(...)` and
|
|
168
|
+
`trace_attributes()`. `compile()` returns `str | list[Message]`; the two typed variants check the
|
|
169
|
+
prompt type and return `str` or `list[Message]`. A handle never changes after it is returned, so a
|
|
170
|
+
publish mid-request cannot change what that request renders.
|
|
171
|
+
|
|
172
|
+
## Caching and resilience
|
|
173
|
+
|
|
174
|
+
- Lazy: a prompt is loaded on first use; startup runs zero queries and starts no threads.
|
|
175
|
+
- Per-process LRU keyed by `(name, label, locale)`, bounded by `cache_max_entries` (500).
|
|
176
|
+
- `cache_ttl` (default 300 s) bounds propagation: after a publish, every worker serves the new
|
|
177
|
+
version within the TTL. The worker that published serves it immediately. Pass `ttl=` per call;
|
|
178
|
+
`ttl=0` bypasses the cache.
|
|
179
|
+
- Concurrent misses for one key share a single query (sync threads and asyncio tasks).
|
|
180
|
+
- If the database errors, an expired entry is served for up to `max_stale` (1 hour) with
|
|
181
|
+
`is_fallback=True`, retried at most every 5 s, and a rate-limited warning is logged. With nothing
|
|
182
|
+
cached, `default=` is used; otherwise `PromptStoreUnavailable` is raised.
|
|
183
|
+
|
|
184
|
+
Steady-state load is at most one indexed query per hot key per TTL per worker.
|
|
185
|
+
|
|
186
|
+
### Sync and async engines
|
|
187
|
+
|
|
188
|
+
Every method has a sync form and an `a`-prefixed async form (`get`/`aget`, `publish`/`apublish`).
|
|
189
|
+
With a sync `Engine` the async forms run the query in a thread. With an `AsyncEngine` the sync forms
|
|
190
|
+
work from worker threads once the event loop has run (for example in sync FastAPI routes or
|
|
191
|
+
background threads) and from plain scripts; calling them from inside a running coroutine raises
|
|
192
|
+
`RuntimeError` so the loop is never blocked.
|
|
193
|
+
|
|
194
|
+
## Settings
|
|
195
|
+
|
|
196
|
+
| Setting | Default | Description |
|
|
197
|
+
| --- | --- | --- |
|
|
198
|
+
| `engine` | required | SQLAlchemy `Engine` or `AsyncEngine` |
|
|
199
|
+
| `environment` | `production` | Default label for `get()` |
|
|
200
|
+
| `default_locale` | `en` | Final locale fallback |
|
|
201
|
+
| `cache_ttl` | `300` | Seconds a cache entry is fresh; `0` disables caching |
|
|
202
|
+
| `cache_max_entries` | `500` | LRU bound per process |
|
|
203
|
+
| `max_stale` | `3600` | How long an expired entry may be served while the database is down |
|
|
204
|
+
| `table_prefix` | `promptpivot_` | Prefix for all tables and the Alembic version table |
|
|
205
|
+
| `protected_labels` | `("production",)` | Labels that require an approved change request |
|
|
206
|
+
| `require_distinct_approver` | `True` | Approver must differ from the author |
|
|
207
|
+
| `config_model` | `PromptConfig` | Pydantic model for `handle.config` (extra keys allowed) |
|
|
208
|
+
| `metadata_model` | `None` | Pydantic model for `handle.metadata` (a `dict` when unset) |
|
|
209
|
+
| `default_actor` | `system` | Actor recorded when a write does not pass `actor=` |
|
|
210
|
+
|
|
211
|
+
## Management API
|
|
212
|
+
|
|
213
|
+
Most teams change prompts in the UI and never call these; they back the UI, the CLI and scripts.
|
|
214
|
+
Available on the registry in sync and async forms; writes take `actor=` (default: the registry's
|
|
215
|
+
`default_actor`, `"system"`):
|
|
216
|
+
|
|
217
|
+
`create_prompt`, `get_prompt`, `list_prompts`, `update_prompt`, `update_metadata`, `archive`,
|
|
218
|
+
`create_version`, `history`, `diff`, `labels`, `label_history`, `publish`, `rollback`,
|
|
219
|
+
`request_change`, `approve`, `reject`, `list_change_requests`, `audit`, `export`, `import_prompts`,
|
|
220
|
+
`health`. `PromptRegistry.in_memory()` gives a throwaway SQLite registry for tests.
|
|
221
|
+
|
|
222
|
+
`create_version(name, template, *, format="python", message, locale=None, config=None)` keeps the
|
|
223
|
+
previous version's config when `config` is omitted (pass `{}` to clear it) and accepts `Message`
|
|
224
|
+
objects or dicts for chat templates. The returned record carries `warning` when a locale variant's
|
|
225
|
+
variables differ from the default locale's production version.
|
|
226
|
+
|
|
227
|
+
`publish(name, version, label, *, actor, locale=None, expected_current=None, reason=None, force=False)`
|
|
228
|
+
moves a label with an optimistic lock (`PromptConflict` if someone else moved it first). For a
|
|
229
|
+
protected label it requires an approved change request unless `force=True` (break-glass, recorded
|
|
230
|
+
in the audit log with the reason). `rollback` moves a label to the version it pointed to before its
|
|
231
|
+
last move; it is an undo, so a second rollback re-applies the move. The UI shows the destination
|
|
232
|
+
(`Rollback production: v3 → v2`) before you click.
|
|
233
|
+
|
|
234
|
+
## Admin UI and REST API
|
|
235
|
+
|
|
236
|
+
Mount `admin_router(registry, principal=..., ui=True, include_in_schema=True)`.
|
|
237
|
+
|
|
238
|
+
- UI: list and search, editor with format selector and live variable detection, JSON config and
|
|
239
|
+
metadata editors validated against your models, preview with sample variables, version history
|
|
240
|
+
and diffs, publish, request review, approve, reject, one-click rollback, audit log, and an
|
|
241
|
+
Import page (paste an export) plus an Export download for moving prompts between environments.
|
|
242
|
+
- REST API under `<prefix>/api/...` (listed in the host's OpenAPI unless `include_in_schema=False`):
|
|
243
|
+
`GET api/health`, `GET|POST api/prompts`, `GET api/prompts/{name}`,
|
|
244
|
+
`POST api/prompts/{name}/versions|publish|rollback|change-requests|preview|archive`,
|
|
245
|
+
`PUT api/prompts/{name}/metadata`, `GET api/prompts/{name}/diff`, `GET api/change-requests`,
|
|
246
|
+
`POST api/change-requests/{id}/approve|reject`, `GET api/audit`, `GET api/export`,
|
|
247
|
+
`POST api/import`.
|
|
248
|
+
|
|
249
|
+
### Permissions
|
|
250
|
+
|
|
251
|
+
Your `principal` dependency returns a `Principal(id, name, permissions)`. Map your roles to:
|
|
252
|
+
|
|
253
|
+
| Permission | Allows |
|
|
254
|
+
| --- | --- |
|
|
255
|
+
| `prompts:read` | View prompts, versions, diffs, previews, health |
|
|
256
|
+
| `prompts:edit` | Create prompts, save versions, edit metadata, open change requests |
|
|
257
|
+
| `prompts:publish:<label>` | Move that label (publish, rollback) |
|
|
258
|
+
| `prompts:approve` | Approve or reject change requests |
|
|
259
|
+
| `prompts:admin` | Everything, plus archive, audit log and break-glass publish |
|
|
260
|
+
|
|
261
|
+
### Security defaults
|
|
262
|
+
|
|
263
|
+
Not mounted by default; no built-in users or credentials; CSRF double-submit cookie on every
|
|
264
|
+
mutating form; JSON-only API mutations; sandboxed rendering for both formats; prompt content is
|
|
265
|
+
never sent to end users by the package.
|
|
266
|
+
|
|
267
|
+
### Migrating existing prompts
|
|
268
|
+
|
|
269
|
+
Most teams start with prompts hardcoded in Python. Paste each one into *New prompt* → *New
|
|
270
|
+
version* in the UI, or write them once as an export file and use *Import* (UI), `promptpivot import`
|
|
271
|
+
(CLI) or `POST api/import`. Then replace the constant in code with `prompts.get("name")` and keep
|
|
272
|
+
the old string as `default=` until you trust the database path.
|
|
273
|
+
|
|
274
|
+
## CLI
|
|
275
|
+
|
|
276
|
+
```bash
|
|
277
|
+
export PROMPTPIVOT_DATABASE_URL=postgresql+psycopg://user:pass@host/db
|
|
278
|
+
promptpivot migrate # create or upgrade the schema
|
|
279
|
+
promptpivot list # prompts and their labels
|
|
280
|
+
promptpivot export -o prompts.yaml # backup / copy between environments (.json or .yaml)
|
|
281
|
+
promptpivot import prompts.yaml [--no-labels] # versions matched by checksum, nothing duplicated
|
|
282
|
+
promptpivot publish support/agent 7 staging --locale fr
|
|
283
|
+
promptpivot publish support/agent 7 production --force --reason "incident 123"
|
|
284
|
+
promptpivot rollback support/agent production
|
|
285
|
+
```
|
|
286
|
+
|
|
287
|
+
Async URLs (`+asyncpg`, `+aiomysql`) work too. `--prefix` or `PROMPTPIVOT_TABLE_PREFIX` selects the
|
|
288
|
+
table prefix; `--actor` sets the audit actor (defaults to `$USER`).
|
|
289
|
+
|
|
290
|
+
## Migrations
|
|
291
|
+
|
|
292
|
+
Tables: `prompts`, `versions`, `labels`, `label_history`, `change_requests`, `audit_log`, all with
|
|
293
|
+
the configurable prefix and portable types (JSON, Text, BigInteger). Migrations ship as Alembic
|
|
294
|
+
scripts with their own version table (`<prefix>alembic_version`) so they coexist with your app's.
|
|
295
|
+
`promptpivot migrate` or `promptpivot.migrations.upgrade(engine)` runs them; without the `[alembic]`
|
|
296
|
+
extra they fall back to `create_all`, which is fine for development.
|
|
297
|
+
|
|
298
|
+
## Production checklist
|
|
299
|
+
|
|
300
|
+
1. Install `promptpivot[alembic]` and run `promptpivot migrate` in your deploy pipeline.
|
|
301
|
+
2. Mount the router behind your real auth; give `prompts:publish:production` only to approvers or
|
|
302
|
+
on-call, and `prompts:admin` to very few people.
|
|
303
|
+
3. Serve the admin UI over HTTPS (the CSRF cookie is marked `secure` on HTTPS requests).
|
|
304
|
+
4. Pick `cache_ttl` to match how fast you need publishes to propagate; 30 to 300 s is typical.
|
|
305
|
+
5. Give business-critical prompts a `default=` in code so brand-new workers can start during a
|
|
306
|
+
database outage.
|
|
307
|
+
6. Forward `prompt.*` span attributes (automatic when `opentelemetry-api` is installed) so every LLM
|
|
308
|
+
span records which version produced it.
|
|
309
|
+
7. Watch `GET <prefix>/api/health` (database reachability, cache size and hit ratio).
|
|
310
|
+
|
|
311
|
+
## Rollback runbook
|
|
312
|
+
|
|
313
|
+
1. Open `/admin/prompts/p/<name>?locale=<locale>` (or run `promptpivot rollback <name> production`).
|
|
314
|
+
2. Click **Rollback production**. The label moves back to its previous version in one transaction;
|
|
315
|
+
the audit log records who did it and when.
|
|
316
|
+
3. The worker that handled the request serves the old version at once; all other workers within
|
|
317
|
+
`cache_ttl`. Verify with `GET api/prompts/<name>` (labels) and your LLM spans' `prompt.version`.
|
|
318
|
+
4. Rolling back does not need a change request: it only re-targets a version that was already
|
|
319
|
+
approved for that label.
|
|
320
|
+
|
|
321
|
+
## Using a local checkout in other projects
|
|
322
|
+
|
|
323
|
+
While iterating on promptpivot itself, install it editable so every other project on the machine
|
|
324
|
+
picks up your edits without reinstalling:
|
|
325
|
+
|
|
326
|
+
```bash
|
|
327
|
+
# in the other project (pip or uv)
|
|
328
|
+
pip install -e /path/to/prompt-pivot
|
|
329
|
+
uv add --editable /path/to/prompt-pivot # uv-managed project
|
|
330
|
+
```
|
|
331
|
+
|
|
332
|
+
Straight from GitHub without a checkout: `pip install "prompt-pivot @ git+https://github.com/Lasitha-Jayawardana/prompt-pivot.git"`.
|
|
333
|
+
|
|
334
|
+
## Development
|
|
335
|
+
|
|
336
|
+
```bash
|
|
337
|
+
uv venv && uv pip install -e . --group dev
|
|
338
|
+
pytest # SQLite, sync and async
|
|
339
|
+
mypy && ruff check .
|
|
340
|
+
PROMPTPIVOT_TEST_POSTGRES_URL=postgresql+psycopg://... PROMPTPIVOT_TEST_MYSQL_URL=mysql+pymysql://... pytest
|
|
341
|
+
```
|
|
342
|
+
|
|
343
|
+
License: Apache-2.0.
|