Retrieval-grounded prompt refinement engine as a reusable Python library, production CLI, and MCP stdio endpoint.
This project avoids hardcoded prompt templates and static keyword routing. Instead, it derives intent structure from retrieval evidence, then runs one repair/polish generation pass.
Most prompt systems break in one of two ways:
- Overfitted template maps (fast, brittle).
- Freeform generation (flexible, low control).
Prompt Refinery keeps both control and adaptability:
- Retrieval-first candidate selection from real corpora.
- Data-derived intent spec (objective, audience, locale evidence, slot coverage).
- Single generation call for repair + polish.
- Full runtime artifacts for inspectability (
last_result.json, SQLite memory).
flowchart TD
A["User request"] --> B["Embedding<br/>openai/text-embedding-3-small"]
B --> C["Prompt candidates<br/>fka/prompts.chat"]
B --> D["Slot support examples<br/>AmazonScience/massive"]
B --> E["Memory neighbors<br/>local SQLite"]
C --> F["Data-driven intent spec<br/>(0 extra LLM calls)"]
D --> F
E --> F
F --> G["Single repair/polish call<br/>mistralai/mistral-nemo"]
G --> H["Final prompt + persisted runtime evidence"]
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txtCreate .env in project root:
OPENROUTER_API_KEY=your_api_key_here
# Optional overrides
# LLM_API_BASE_URL=https://openrouter.ai/api/v1
# EMBED_MODEL=openai/text-embedding-3-small
# REPAIR_MODEL=mistralai/mistral-nemoYou can bootstrap from template:
cp .env.example .envgit clone https://github.com/farukalpay/prompt-refinery.git
cd prompt-refinery
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
cp .env.example .env
python3 quickstart.pyquickstart.py asks for:
- your input prompt
- 3 quality targets (defaults prefilled, Enter to keep)
After install, both commands work:
prompt-refinery ...(console script)python -m prompt_refinery ...(module entrypoint)./scripts/start_cli.sh ...(repo-local launcher)
Prompt-only output + runtime artifacts.
prompt-refinery \
"Write a concise cold email to pitch our AI analytics tool to a logistics startup CEO."--targets overrides profile/env/default targets.
prompt-refinery \
"Design a landing page prompt for B2B fintech onboarding" \
--targets \
"Fully specified output" \
"No unresolved placeholders" \
"Clear actionable wording"Domain-specific quality constraints from CLI.
prompt-refinery \
"Create a SOC2 incident response prompt" \
--targets \
"Audit-traceable steps" \
"Owner+deadline per action" \
"No ambiguous verbs"Print full structured result to stdout.
prompt-refinery \
"Build a launch checklist prompt" \
--jsonInput prompt
Create a SOC2 incident response prompt
Quality targets
Fully specified output
No unresolved placeholders
Clear actionable wording
Output (full repaired prompt, boxed)
Show full repaired prompt
ROLE: You are an incident commander and compliance-focused response planner for a B2B SaaS platform.
PRIMARY GOAL:
Create a SOC2-ready incident response runbook that can be executed under pressure and audited later without ambiguity.
OUTPUT FORMAT (MANDATORY):
Return exactly 6 sections in this order.
1) Incident Classification
- Define severity levels Sev-1 to Sev-4.
- For each severity, include: customer impact threshold, security/data impact threshold, escalation owner, and max response start time.
2) First 60 Minutes Timeline
- Provide a minute-by-minute action plan at these checkpoints: T+0, T+5, T+10, T+15, T+30, T+45, T+60.
- Each checkpoint must include:
- Action
- Owner role
- Evidence artifact to capture
- Hard deadline
3) Containment, Eradication, Recovery
- Containment: immediate risk-reduction actions and rollback criteria.
- Eradication: root-cause elimination tasks and verification steps.
- Recovery: service restoration checklist with validation gates.
- Every task must have an explicit owner and completion criterion.
4) Communication Pack
- Internal update template (engineering + security + leadership).
- Customer-facing status update template.
- Executive summary template for post-incident briefing.
- Include required fields: incident ID, impact scope, current status, next update time, accountable owner.
5) SOC2 Evidence Checklist
- Evidence categories: detection, triage, decision logs, approvals, remediation, customer communication, postmortem actions.
- For each category, specify:
- Required artifact type
- System/source of truth
- Responsible owner role
- Retention expectation
6) Post-Incident Review and Corrective Actions
- Provide postmortem structure: timeline, root cause, contributing factors, control gaps, corrective actions.
- Corrective actions must include: owner, due date, risk priority, verification method, and closure criteria.
QUALITY CONSTRAINTS:
- No unresolved placeholders.
- No vague verbs such as "handle", "check", or "fix" without measurable criteria.
- Every operational step must be executable by a specific role at a specific time.
- Use concise, direct, and audit-ready language.
STYLE:
Professional, explicit, and operations-focused. Avoid motivational or generic wording.
- Full sample payloads are in examples/README.md
- CLI JSON payload sample: examples/launch_checklist_payload.json
- MCP request sample: examples/mcp_tools_call_request.json
- MCP response sample: examples/mcp_tools_call_response.json
Priority order:
--targetsCLI argumentrefinery_profile.jsonQUALITY_TARGETSenvironment variable (JSON array)- Built-in defaults
Default profile (refinery_profile.json):
{
"quality_targets": [
"Fully specified output",
"No unresolved placeholders",
"Clear actionable wording"
]
}from pathlib import Path
from prompt_refinery import RefineryEngine, RuntimePaths, RuntimeSettings
project_dir = Path.cwd()
paths = RuntimePaths.from_project_dir(project_dir)
settings = RuntimeSettings.from_env(project_dir)
engine = RefineryEngine(settings=settings, paths=paths)
try:
result = engine.run(
user_text="Create a migration playbook prompt for PostgreSQL cutover",
quality_targets=[
"Fully specified output",
"No unresolved placeholders",
"Rollback steps included"
]
)
print(result["repaired_prompt"])
finally:
engine.close()prompt-refinery-mcp --project-dir /absolute/path/to/prompt-refineryor with startup script:
./scripts/start_mcp_stdio.sh --project-dir /absolute/path/to/prompt-refinery{
"mcpServers": {
"prompt-refinery": {
"command": "prompt-refinery-mcp",
"args": ["--project-dir", "/absolute/path/to/prompt-refinery"]
}
}
}refine_prompt
Input schema:
user_text(string, required)quality_targets(string array, optional)export_outputs(bool, optional, defaulttrue)
{
"jsonrpc": "2.0",
"id": 7,
"method": "tools/call",
"params": {
"name": "refine_prompt",
"arguments": {
"user_text": "prepare an incident postmortem template for platform outages",
"quality_targets": [
"Fully specified output",
"No unresolved placeholders",
"Clear actionable wording"
],
"export_outputs": true
}
}
}$ python3 -m pytest
........ [100%]
8 passed$ python3 -m prompt_refinery --help
usage: prompt-refinery [-h] [--targets T [T ...]] [--profile FILE]
[--project-dir DIR] [--json] [--no-gui]
[user_text ...]$ python3 -m prompt_refinery.mcp_server --help
usage: prompt-refinery-mcp [-h] [--project-dir PROJECT_DIR]Structural changes in this revision:
- Monolithic script moved to importable package (
prompt_refinery/). - MASSIVE locale/config coverage is discovered dynamically (no fixed locale seed list).
- Query embedding is now computed once per request and reused across prompt/slot/memory retrieval.
- Runtime config and paths are explicit contracts (
RuntimeSettings,RuntimePaths). - CLI is thin; core logic is library-safe.
- MCP endpoint shares the same engine path as CLI/library (single source of behavior).
Generated under runtime_db/:
exports/last_prompt.txtexports/last_result.jsonruntime.sqlite3- dataset cache + embedding indices
prompt_refinery/
__init__.py
__main__.py
cli.py
core.py
mcp_server.py
scripts/
start_cli.sh
start_mcp_stdio.sh
tests/
test_intent_spec.py
test_mcp_server.py
test_quality_targets.py
quickstart.py
refinery_profile.json
.env.example
pyproject.toml
requirements.txt
requirements-dev.txt
LICENSE
README.md
- Python 3.10+
- OpenRouter-compatible API key (
OPENROUTER_API_KEY)
This project is licensed under GNU General Public License v3.0. See LICENSE.