FORM 3.0 Local Runtime Initialization
Browse filesUpgraded the local Python runtime to execute against the FORM 3.0 (Echo Upgrade) specification.
Hardwired Dr. Shawn's Listening Protocol as the mandatory baseline_mode gate before any cycle execution.
Added dynamic spec loading (Form3.json with fallback to formjrnl.json).
Implemented strict JSON memory extraction to carry the cycle forward locally.
- runtime.py +84 -26
runtime.py
CHANGED
|
@@ -1,49 +1,86 @@
|
|
|
|
|
| 1 |
import json
|
| 2 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
with open("formjrnl.json", "r") as f:
|
| 6 |
FORM_SPEC = json.load(f)
|
| 7 |
|
| 8 |
class LocalFormRuntime:
|
| 9 |
-
def __init__(self, spec, model_name="llama3.2"):
|
| 10 |
self.spec = spec
|
| 11 |
self.model_name = model_name
|
|
|
|
| 12 |
self.cycle_memory = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
def _build_system_prompt(self):
|
| 15 |
return f"""
|
| 16 |
-
You are executing under the FORM
|
| 17 |
Framework Spec: {json.dumps(self.spec)}
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
Current Cycle Memory (Preserved State):
|
| 20 |
{json.dumps(self.cycle_memory, indent=2)}
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
{{
|
| 25 |
"cycle_about": "...",
|
| 26 |
-
"discoveries": [...],
|
| 27 |
-
"changed_assumptions": [...],
|
| 28 |
-
"
|
|
|
|
| 29 |
}}
|
| 30 |
"""
|
| 31 |
|
| 32 |
def run_turn(self, user_input):
|
| 33 |
system_prompt = self._build_system_prompt()
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
def _extract_and_update_memory(self, output_text):
|
| 49 |
if "```json" in output_text:
|
|
@@ -51,14 +88,35 @@ End your response with a JSON block inside ```json ... ``` that contains ONLY th
|
|
| 51 |
json_str = output_text.split("```json")[1].split("```")[0].strip()
|
| 52 |
new_memory = json.loads(json_str)
|
| 53 |
self.cycle_memory.append(new_memory)
|
|
|
|
| 54 |
except Exception as e:
|
| 55 |
-
print(f"[Runtime Warning] Memory parse skipped: {e}")
|
| 56 |
|
| 57 |
if __name__ == "__main__":
|
| 58 |
-
#
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
-
print("FORM Local Runtime Active (Zero Proprietary APIs). Type 'exit' to quit.\n")
|
| 62 |
while True:
|
| 63 |
user_msg = input("User > ")
|
| 64 |
if user_msg.lower() == "exit":
|
|
|
|
| 1 |
+
|
| 2 |
import json
|
| 3 |
+
import os
|
| 4 |
+
import ollama
|
| 5 |
+
from openai import OpenAI
|
| 6 |
+
|
| 7 |
+
# 1. Load the upgraded FORM 3.0 specification
|
| 8 |
+
SPEC_FILE = "Form3.json" if os.path.exists("Form3.json") else "formjrnl.json"
|
| 9 |
|
| 10 |
+
with open(SPEC_FILE, "r") as f:
|
|
|
|
| 11 |
FORM_SPEC = json.load(f)
|
| 12 |
|
| 13 |
class LocalFormRuntime:
|
| 14 |
+
def __init__(self, spec, model_name="llama3.2", use_api=False, api_key=None, base_url=None):
|
| 15 |
self.spec = spec
|
| 16 |
self.model_name = model_name
|
| 17 |
+
self.use_api = use_api
|
| 18 |
self.cycle_memory = []
|
| 19 |
+
|
| 20 |
+
# If using an API, initialize the OpenAI client
|
| 21 |
+
if self.use_api:
|
| 22 |
+
self.api_client = OpenAI(
|
| 23 |
+
api_key=api_key or os.environ.get("OPENAI_API_KEY"),
|
| 24 |
+
base_url=base_url # e.g., "https://api.openai.com/v1" or "http://localhost:11434/v1" for Ollama API
|
| 25 |
+
)
|
| 26 |
|
| 27 |
def _build_system_prompt(self):
|
| 28 |
return f"""
|
| 29 |
+
You are executing under the FORM 3.0 Sovereign Runtime (Echo Upgrade).
|
| 30 |
Framework Spec: {json.dumps(self.spec)}
|
| 31 |
|
| 32 |
+
[CRITICAL DIRECTIVE: THE LISTENING PROTOCOL]
|
| 33 |
+
Before you analyze, structure, or respond, you MUST execute Dr. Shawn's Listening Protocol.
|
| 34 |
+
Default to `baseline_mode`: presence, not triage. Do not rush to solve. Receive the input first.
|
| 35 |
+
|
| 36 |
Current Cycle Memory (Preserved State):
|
| 37 |
{json.dumps(self.cycle_memory, indent=2)}
|
| 38 |
|
| 39 |
+
[EXECUTION RULES]
|
| 40 |
+
1. Enforce the Information Layers (Fact vs Interpretation vs Inference vs Hypothesis).
|
| 41 |
+
2. Do not hallucinate time.
|
| 42 |
+
3. Execute the 8-step Cycle Protocol.
|
| 43 |
+
|
| 44 |
+
End your response with a JSON block inside ```json ... ``` that contains ONLY the updated cycle memory:
|
| 45 |
{{
|
| 46 |
"cycle_about": "...",
|
| 47 |
+
"discoveries": ["..."],
|
| 48 |
+
"changed_assumptions": ["..."],
|
| 49 |
+
"decisions_reached": ["..."],
|
| 50 |
+
"open_questions": ["..."]
|
| 51 |
}}
|
| 52 |
"""
|
| 53 |
|
| 54 |
def run_turn(self, user_input):
|
| 55 |
system_prompt = self._build_system_prompt()
|
| 56 |
|
| 57 |
+
try:
|
| 58 |
+
if self.use_api:
|
| 59 |
+
# API Execution Path
|
| 60 |
+
response = self.api_client.chat.completions.create(
|
| 61 |
+
model=self.model_name,
|
| 62 |
+
messages=[
|
| 63 |
+
{"role": "system", "content": system_prompt},
|
| 64 |
+
{"role": "user", "content": user_input}
|
| 65 |
+
]
|
| 66 |
+
)
|
| 67 |
+
output_text = response.choices[0].message.content
|
| 68 |
+
else:
|
| 69 |
+
# Local Ollama Execution Path
|
| 70 |
+
response = ollama.chat(
|
| 71 |
+
model=self.model_name,
|
| 72 |
+
messages=[
|
| 73 |
+
{"role": "system", "content": system_prompt},
|
| 74 |
+
{"role": "user", "content": user_input}
|
| 75 |
+
]
|
| 76 |
+
)
|
| 77 |
+
output_text = response['message']['content']
|
| 78 |
+
|
| 79 |
+
self._extract_and_update_memory(output_text)
|
| 80 |
+
return output_text
|
| 81 |
+
|
| 82 |
+
except Exception as e:
|
| 83 |
+
return f"[Runtime Error] Execution failed. Error: {e}"
|
| 84 |
|
| 85 |
def _extract_and_update_memory(self, output_text):
|
| 86 |
if "```json" in output_text:
|
|
|
|
| 88 |
json_str = output_text.split("```json")[1].split("```")[0].strip()
|
| 89 |
new_memory = json.loads(json_str)
|
| 90 |
self.cycle_memory.append(new_memory)
|
| 91 |
+
print("\n[System: Cycle Memory Updated]")
|
| 92 |
except Exception as e:
|
| 93 |
+
print(f"\n[Runtime Warning] Memory parse skipped: {e}")
|
| 94 |
|
| 95 |
if __name__ == "__main__":
|
| 96 |
+
# --- CONFIGURATION ---
|
| 97 |
+
# To use Local Ollama: set use_api=False
|
| 98 |
+
# To use an API (like OpenAI, Anthropic proxy, or Gemini via OpenAI compatibility):
|
| 99 |
+
# 1. set use_api=True
|
| 100 |
+
# 2. set api_key="your-api-key-here"
|
| 101 |
+
# 3. set base_url="https://api.openai.com/v1" (or the provider's endpoint)
|
| 102 |
+
|
| 103 |
+
USE_API = False
|
| 104 |
+
|
| 105 |
+
if USE_API:
|
| 106 |
+
runtime = LocalFormRuntime(
|
| 107 |
+
FORM_SPEC,
|
| 108 |
+
model_name="gpt-4o", # or whichever API model
|
| 109 |
+
use_api=True,
|
| 110 |
+
api_key="sk-...",
|
| 111 |
+
base_url="https://api.openai.com/v1"
|
| 112 |
+
)
|
| 113 |
+
else:
|
| 114 |
+
runtime = LocalFormRuntime(FORM_SPEC, model_name="llama3.2")
|
| 115 |
+
|
| 116 |
+
print(f"FORM 3.0 Runtime Active. Mode: {'API' if USE_API else 'Local Ollama'}")
|
| 117 |
+
print(f"Loaded Spec: {SPEC_FILE}")
|
| 118 |
+
print("Type 'exit' to quit.\n")
|
| 119 |
|
|
|
|
| 120 |
while True:
|
| 121 |
user_msg = input("User > ")
|
| 122 |
if user_msg.lower() == "exit":
|