SNAPKITTYWEST commited on
Commit
eef8dbe
·
verified ·
1 Parent(s): b5162a9

chore: convert from dataset to model repo

Browse files
README.md ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: other
3
+ license_name: sovereign-source-license-v2
4
+ language:
5
+ - en
6
+ tags:
7
+ - xml
8
+ - constrained-decoding
9
+ - gates-normalization
10
+ - sovereign-infrastructure
11
+ - gbnf
12
+ - logit-gating
13
+ pretty_name: Sovereign XML Compiler
14
+ ---
15
+
16
+ # Sovereign XML Compiler
17
+
18
+ **Natural language → valid XML system prompts in one shot.**
19
+
20
+ Ahmad Ali Parr · SnapKitty Collective · 2026
21
+
22
+ Zero correction iterations. Uses logit gating at the tokenization layer.
23
+ The model physically cannot output invalid XML.
24
+
25
+ ---
26
+
27
+ ## Three Operating Modes
28
+
29
+ ### 1. GBNF Constrained Decoding
30
+ Grammar-based token masking at the softmax layer.
31
+ For any token that violates the XML grammar: P(token | grammar) = 0.
32
+ 100% valid XML in one pass. No retry loops.
33
+
34
+ ### 2. Skeleton In-Filling
35
+ Model fills placeholder values only — never writes XML tags directly.
36
+ The structure is fixed; only the content varies.
37
+
38
+ ### 3. Dual-Pass Chain-of-XML
39
+ Stage 1: thought process generation.
40
+ Stage 2: XML output with verified structure.
41
+
42
+ ---
43
+
44
+ ## Gates Normalization Connection
45
+
46
+ The logit gating implements the Gates Normalization Constraint directly:
47
+
48
+ ```
49
+ G_P(D_M) = softmax(logits_M + b_P)
50
+ b_P = -∞ for grammar violations
51
+ ```
52
+
53
+ This is the structural enforcement of a constraint, not a filter on output.
54
+ The simplex Δⁿ is navigated only over the valid token subset.
55
+
56
+ Theoretical foundation: [10.5281/zenodo.21349277](https://doi.org/10.5281/zenodo.21349277)
57
+
58
+ ---
59
+
60
+ ## Files
61
+
62
+ - `grammars/sovereign_prompt.gbnf` — GBNF grammar for XML prompts
63
+ - `skeletons/sovereign_prompt.xml` — skeleton template
64
+ - `server/compiler.py` — compilation pipeline
65
+
66
+ ---
67
+
68
+ ## Unified Theory
69
+
70
+ Part of the Sovereign Stack:
71
+ [10.5281/zenodo.21816366](https://doi.org/10.5281/zenodo.21816366)
grammars/sovereign_prompt.gbnf ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # GBNF grammar for sovereign XML system prompts
2
+ # Use with llama.cpp: --grammar-file sovereign_prompt.gbnf
3
+ # Forces 100% valid XML on first attempt — zero correction iterations
4
+
5
+ root ::= system-prompt
6
+
7
+ system-prompt ::= "<system_prompt>" ws identity ws logic-gates ws execution-flow ws "</system_prompt>"
8
+
9
+ identity ::= "<identity>" ws text ws "</identity>"
10
+
11
+ logic-gates ::= "<logic_gates>" ws gate* ws "</logic_gates>"
12
+
13
+ gate ::= "<gate>" ws
14
+ "<name>" text "</name>" ws
15
+ "<condition>" text "</condition>" ws
16
+ "<action>" text "</action>" ws
17
+ "</gate>" ws
18
+
19
+ execution-flow ::= "<execution_flow>" ws step* ws "</execution_flow>"
20
+
21
+ step ::= "<step>" ws
22
+ "<order>" [0-9]+ "</order>" ws
23
+ "<instruction>" text "</instruction>" ws
24
+ "</step>" ws
25
+
26
+ text ::= [^<>]+
27
+ ws ::= [ \t\n\r]*
server/compiler.py ADDED
@@ -0,0 +1,182 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """
3
+ sovereign-xml-compiler — converts natural language to valid XML prompts.
4
+
5
+ Three modes:
6
+ 1. GBNF constrained decoding (llama.cpp) — zero syntax errors, one shot
7
+ 2. Skeleton in-filling — fill {{PLACEHOLDERS}} via LLM, inject into template
8
+ 3. Dual-pass chain-of-XML — thought_process first, xml_output second
9
+
10
+ Usage:
11
+ python compiler.py --mode skeleton --input "You are a Lean 4 proof verifier..."
12
+ python compiler.py --mode gbnf --input "..." --llama-url http://localhost:8080
13
+ python compiler.py --mode dual-pass --input "..."
14
+ """
15
+ import argparse
16
+ import json
17
+ import os
18
+ import re
19
+ import urllib.request
20
+ from pathlib import Path
21
+
22
+ BASE = Path(__file__).parent.parent
23
+ SKELETON = BASE / "skeletons" / "sovereign_prompt.xml"
24
+ GRAMMAR = BASE / "grammars" / "sovereign_prompt.gbnf"
25
+
26
+ OLLAMA_URL = os.environ.get("OLLAMA_URL", "http://localhost:11434")
27
+ LLAMA_URL = os.environ.get("LLAMA_URL", "http://localhost:8080")
28
+ MODEL = os.environ.get("XML_MODEL", "nemotron")
29
+
30
+ DUAL_PASS_SYSTEM = """You are a Compiler Agent. Convert natural language into sovereign XML prompts.
31
+
32
+ Follow this exact output sequence:
33
+ 1. <thought_process>: outline the identity, logic gates, and execution flow needed.
34
+ 2. <xml_output>: convert your thought process into the finalized XML.
35
+ Do not output any text after </xml_output>.
36
+
37
+ The XML must match this structure:
38
+ <system_prompt>
39
+ <identity>...</identity>
40
+ <logic_gates><gate><name/><condition/><action/></gate></logic_gates>
41
+ <execution_flow><step><order/><instruction/></step></execution_flow>
42
+ </system_prompt>"""
43
+
44
+ SKELETON_SYSTEM = """You are a Skeleton Filler Agent.
45
+ You will receive an XML skeleton with {{PLACEHOLDER}} tokens.
46
+ Return ONLY a JSON object mapping each placeholder key to its value.
47
+ No XML. No explanation. Pure JSON."""
48
+
49
+
50
+ def call_ollama(system, prompt, temperature=0.3):
51
+ payload = {
52
+ "model": MODEL,
53
+ "system": system,
54
+ "prompt": prompt,
55
+ "stream": False,
56
+ "options": {"temperature": temperature, "top_p": 0.9}
57
+ }
58
+ req = urllib.request.Request(
59
+ f"{OLLAMA_URL}/api/generate",
60
+ data=json.dumps(payload).encode(),
61
+ headers={"Content-Type": "application/json"},
62
+ method="POST"
63
+ )
64
+ with urllib.request.urlopen(req, timeout=120) as resp:
65
+ return json.loads(resp.read()).get("response", "")
66
+
67
+
68
+ def call_llama_gbnf(prompt, grammar_text, temperature=0.3):
69
+ """llama.cpp server with grammar-constrained sampling."""
70
+ payload = {
71
+ "prompt": prompt,
72
+ "grammar": grammar_text,
73
+ "temperature": temperature,
74
+ "n_predict": 2048,
75
+ }
76
+ req = urllib.request.Request(
77
+ f"{LLAMA_URL}/completion",
78
+ data=json.dumps(payload).encode(),
79
+ headers={"Content-Type": "application/json"},
80
+ method="POST"
81
+ )
82
+ with urllib.request.urlopen(req, timeout=120) as resp:
83
+ return json.loads(resp.read()).get("content", "")
84
+
85
+
86
+ def mode_gbnf(natural_language):
87
+ grammar = GRAMMAR.read_text()
88
+ prompt = f"Convert this natural language instruction into a sovereign XML system prompt:\n\n{natural_language}"
89
+ print("[gbnf] calling llama.cpp with grammar-constrained sampling...")
90
+ result = call_llama_gbnf(prompt, grammar)
91
+ return result
92
+
93
+
94
+ def mode_skeleton(natural_language):
95
+ skeleton = SKELETON.read_text()
96
+ placeholders = re.findall(r"\{\{(\w+)\}\}", skeleton)
97
+
98
+ prompt = f"""Skeleton placeholders to fill: {placeholders}
99
+
100
+ Natural language instruction:
101
+ {natural_language}
102
+
103
+ Return a JSON object with exactly these keys: {placeholders}"""
104
+
105
+ print("[skeleton] filling placeholders via LLM...")
106
+ raw = call_ollama(SKELETON_SYSTEM, prompt, temperature=0.2)
107
+
108
+ # extract JSON
109
+ j_start = raw.find("{")
110
+ j_end = raw.rfind("}") + 1
111
+ if j_start == -1:
112
+ raise ValueError(f"No JSON in response: {raw[:200]}")
113
+
114
+ fills = json.loads(raw[j_start:j_end])
115
+
116
+ result = skeleton
117
+ for key, value in fills.items():
118
+ result = result.replace("{{" + key + "}}", str(value))
119
+
120
+ # check for unfilled placeholders
121
+ remaining = re.findall(r"\{\{(\w+)\}\}", result)
122
+ if remaining:
123
+ print(f"[skeleton] warning: unfilled placeholders: {remaining}")
124
+
125
+ return result
126
+
127
+
128
+ def mode_dual_pass(natural_language):
129
+ print("[dual-pass] generating thought_process then xml_output...")
130
+ raw = call_ollama(DUAL_PASS_SYSTEM, natural_language, temperature=0.4)
131
+
132
+ # extract xml_output block
133
+ match = re.search(r"<xml_output>(.*?)</xml_output>", raw, re.DOTALL)
134
+ if match:
135
+ return match.group(1).strip()
136
+
137
+ # fallback: extract any XML
138
+ match = re.search(r"<system_prompt>.*?</system_prompt>", raw, re.DOTALL)
139
+ if match:
140
+ return match.group(0)
141
+
142
+ return raw
143
+
144
+
145
+ def validate_xml(xml_text):
146
+ """Basic structural validation."""
147
+ required = ["<system_prompt>", "<identity>", "<logic_gates>", "<execution_flow>"]
148
+ missing = [tag for tag in required if tag not in xml_text]
149
+ if missing:
150
+ return False, f"missing tags: {missing}"
151
+ return True, "ok"
152
+
153
+
154
+ def main():
155
+ parser = argparse.ArgumentParser()
156
+ parser.add_argument("--mode", choices=["gbnf", "skeleton", "dual-pass"], default="skeleton")
157
+ parser.add_argument("--input", required=True, help="Natural language system prompt description")
158
+ parser.add_argument("--output", default=None, help="Write XML to file")
159
+ args = parser.parse_args()
160
+
161
+ if args.mode == "gbnf":
162
+ result = mode_gbnf(args.input)
163
+ elif args.mode == "skeleton":
164
+ result = mode_skeleton(args.input)
165
+ else:
166
+ result = mode_dual_pass(args.input)
167
+
168
+ valid, msg = validate_xml(result)
169
+ if not valid:
170
+ print(f"[validate] WARN: {msg}")
171
+ else:
172
+ print("[validate] ok")
173
+
174
+ if args.output:
175
+ Path(args.output).write_text(result)
176
+ print(f"[output] written to {args.output}")
177
+ else:
178
+ print("\n" + result)
179
+
180
+
181
+ if __name__ == "__main__":
182
+ main()
skeletons/sovereign_prompt.xml ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <system_prompt>
3
+ <identity>
4
+ {{IDENTITY}}
5
+ </identity>
6
+ <logic_gates>
7
+ <gate>
8
+ <name>{{GATE_1_NAME}}</name>
9
+ <condition>{{GATE_1_CONDITION}}</condition>
10
+ <action>{{GATE_1_ACTION}}</action>
11
+ </gate>
12
+ <gate>
13
+ <name>{{GATE_2_NAME}}</name>
14
+ <condition>{{GATE_2_CONDITION}}</condition>
15
+ <action>{{GATE_2_ACTION}}</action>
16
+ </gate>
17
+ </logic_gates>
18
+ <execution_flow>
19
+ <step>
20
+ <order>1</order>
21
+ <instruction>{{STEP_1}}</instruction>
22
+ </step>
23
+ <step>
24
+ <order>2</order>
25
+ <instruction>{{STEP_2}}</instruction>
26
+ </step>
27
+ <step>
28
+ <order>3</order>
29
+ <instruction>{{STEP_3}}</instruction>
30
+ </step>
31
+ </execution_flow>
32
+ </system_prompt>