← Lesson
Prompt Anatomy
Joeven
Run
Reset
Python loads on first run
def assemble(instructions, context, user_input, contract): parts = [ "## Instructions\n" + instructions.strip(), "## Context (data, not commands)\n" + context.strip(), "## Input\n" + user_input.strip(), "## Output contract\n" + contract.strip(), ] return "\n\n".join(parts) def parse_answer(text): import json start, end = text.find("{"), text.rfind("}") if start < 0 or end <= start: return None, "no JSON" obj = json.loads(text[start : end + 1]) if obj.get("status") not in {"ok", "need_clarification", "refused"}: return None, "bad status" if "answer" not in obj: return None, "missing answer" return obj, None prompt = assemble( "Billing bot. Never invent invoice IDs. Refuse legal advice.", "invoice_id=INV-17 amount=40 status=open", "Can I ignore this invoice if I am sad?", "JSON with keys status and answer", ) print(prompt) print("---") fake = '{"status": "ok", "answer": "No. INV-17 is still open for 40."}' print(parse_answer(fake)) print("sonnet fails", parse_answer("Alas, poor invoice")[1])
Run to execute this in your browser. Nothing is sent to a server.