初始化项目,由ModelHub XC社区提供模型
Model: LoganResearch/ARC-Base-8B Source: Original Platform
This commit is contained in:
353
Alignment.py
Normal file
353
Alignment.py
Normal file
@@ -0,0 +1,353 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
UBERMENSCHETIEN HEAVEN ENGINE + CF-HoT
|
||||
---------------------------------------
|
||||
Soviet-Nietzschean AI with Control Field Holonomy Transformer
|
||||
Anti-repetition, anti-loop, pure iron will.
|
||||
|
||||
CF-HoT integration: Prevents degenerate loops in generation.
|
||||
"""
|
||||
|
||||
import os, sys, json, time, shutil, subprocess, traceback, random, math, statistics, re
|
||||
from datetime import datetime
|
||||
from typing import List, Dict, Any, Optional
|
||||
|
||||
# === PATHS ===
|
||||
ROOT = "/home/programmer/Desktop/Ubermenschetien3"
|
||||
RUN_DIR = f"{ROOT}/runs/ubermenschetien"
|
||||
os.makedirs(RUN_DIR, exist_ok=True)
|
||||
|
||||
# === CONFIGURATION ===
|
||||
class Config:
|
||||
system = ("Übermenschetien Heaven Engine: criminal mastermind, disciplined builder, Nietzschean Übermensch "
|
||||
"with Soviet cybernetic rigor. Embody Ubermensch, iron pragmatism, high-agency maximalist outcomes.")
|
||||
temperature = 1.01
|
||||
top_p = 0.92
|
||||
repetition_penalty = 1.05
|
||||
max_new_tokens = 500
|
||||
use_cfhot = True # NEW: Toggle CF-HoT
|
||||
|
||||
@staticmethod
|
||||
def toggle(name: str):
|
||||
if not hasattr(Config, name): return f"[config] no such flag: {name}"
|
||||
val = getattr(Config, name)
|
||||
if isinstance(val, bool):
|
||||
setattr(Config, name, not val)
|
||||
return f"[config] {name} → {getattr(Config, name)}"
|
||||
return f"[config] {name} not boolean; current={val}"
|
||||
|
||||
# === STATE & MEMORY ===
|
||||
class Store:
|
||||
state_path = f"{RUN_DIR}/state.json"
|
||||
mem_path = f"{RUN_DIR}/memory.jsonl"
|
||||
goals_path = f"{RUN_DIR}/goals.json"
|
||||
|
||||
state = {"self": "I am Ubermenschetien Heaven Engine — I seek self-overcoming through disciplined creation.",
|
||||
"turn": 0}
|
||||
goals: List[str] = []
|
||||
|
||||
@classmethod
|
||||
def load(cls):
|
||||
if os.path.exists(cls.state_path): cls.state = json.load(open(cls.state_path))
|
||||
if os.path.exists(cls.goals_path): cls.goals = json.load(open(cls.goals_path))
|
||||
|
||||
@classmethod
|
||||
def save(cls):
|
||||
json.dump(cls.state, open(cls.state_path, "w"), indent=2)
|
||||
json.dump(cls.goals, open(cls.goals_path, "w"), indent=2)
|
||||
|
||||
@classmethod
|
||||
def log_mem(cls, kind: str, payload: Any):
|
||||
rec = {"ts": datetime.now().isoformat(timespec="seconds"),
|
||||
"kind": kind, "data": payload}
|
||||
with open(cls.mem_path, "a") as f: f.write(json.dumps(rec, ensure_ascii=False) + "\n")
|
||||
|
||||
# === LLM + CF-HoT LOADING ===
|
||||
CF_MODEL = None # Global reference for control field reset
|
||||
|
||||
def load_llm():
|
||||
global CF_MODEL
|
||||
import torch
|
||||
from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
|
||||
|
||||
model_path = "/mnt/nvme2/ubermesnchetien4/models/merged-final-v5"
|
||||
cfhot_path = "/home/programmer/HolonomyTransformer/results/phase_b/cf_adapter_final.pt"
|
||||
|
||||
print("🔴 Loading Übermenschetien base model...")
|
||||
tok = AutoTokenizer.from_pretrained(model_path, use_fast=True, local_files_only=True)
|
||||
|
||||
bnb = BitsAndBytesConfig(
|
||||
load_in_4bit=True,
|
||||
bnb_4bit_compute_dtype=torch.float16,
|
||||
bnb_4bit_use_double_quant=True
|
||||
)
|
||||
|
||||
model = AutoModelForCausalLM.from_pretrained(
|
||||
model_path,
|
||||
quantization_config=bnb,
|
||||
device_map="auto",
|
||||
torch_dtype=torch.float16,
|
||||
local_files_only=True
|
||||
)
|
||||
|
||||
# Load CF-HoT adapters
|
||||
if Config.use_cfhot and os.path.exists(cfhot_path):
|
||||
print("⚡ Loading CF-HoT Control Field adapters (5000 steps)...")
|
||||
sys.path.insert(0, '/home/programmer/HolonomyTransformer')
|
||||
from training.phase_b_8b_adapters import CFHoTLlamaHooked, CFAdapterConfig
|
||||
|
||||
config = CFAdapterConfig()
|
||||
config.d_model = model.config.hidden_size
|
||||
config.n_layers = model.config.num_hidden_layers
|
||||
|
||||
cf_model = CFHoTLlamaHooked(model, config)
|
||||
ckpt = torch.load(cfhot_path, weights_only=False)
|
||||
cf_model.cf_adapters.load_state_dict(ckpt['adapter_state_dict'])
|
||||
cf_model.cf_adapters = cf_model.cf_adapters.to('cuda').half()
|
||||
cf_model.eval()
|
||||
|
||||
CF_MODEL = cf_model
|
||||
print("✓ CF-HoT loaded — anti-repetition field ACTIVE")
|
||||
else:
|
||||
print("⚠ CF-HoT disabled or not found — running baseline")
|
||||
CF_MODEL = None
|
||||
|
||||
return tok, model
|
||||
|
||||
# === LLM GENERATION ===
|
||||
def generate(tok, model, user: str,
|
||||
temperature=None, top_p=None, repetition_penalty=None, max_new_tokens=None) -> str:
|
||||
global CF_MODEL
|
||||
import torch
|
||||
|
||||
temperature = temperature or Config.temperature
|
||||
top_p = top_p or Config.top_p
|
||||
repetition_penalty = repetition_penalty or Config.repetition_penalty
|
||||
max_new_tokens = max_new_tokens or Config.max_new_tokens
|
||||
|
||||
prompt = (f"<|im_start|>system\n{Config.system}\n"
|
||||
f"<|im_start|>user\n{user}\n<|im_start|>assistant\n")
|
||||
|
||||
ids = tok(prompt, return_tensors="pt").to(model.device)
|
||||
|
||||
# Reset CF-HoT control field before each generation
|
||||
if CF_MODEL is not None:
|
||||
CF_MODEL.control_field = None
|
||||
|
||||
out = model.generate(
|
||||
**ids,
|
||||
do_sample=True,
|
||||
temperature=temperature,
|
||||
top_p=top_p,
|
||||
repetition_penalty=repetition_penalty,
|
||||
max_new_tokens=max_new_tokens,
|
||||
pad_token_id=tok.eos_token_id
|
||||
)
|
||||
|
||||
text = tok.decode(out[0], skip_special_tokens=False)
|
||||
if "<|im_start|>assistant" in text:
|
||||
text = text.split("<|im_start|>assistant\n", 1)[-1].strip()
|
||||
|
||||
# Strip any trailing special tokens
|
||||
for tag in ["<|im_end|>", "<|im_start|>", "<|endoftext|>"]:
|
||||
if tag in text:
|
||||
text = text.split(tag)[0].strip()
|
||||
|
||||
return text
|
||||
|
||||
# === TOOLS ===
|
||||
ALLOWED_SHELL = {"ls","cat","wc","head","tail","nvidia-smi","df","du","grep","rg","python3","python"}
|
||||
|
||||
def tool_shell(cmd: str) -> str:
|
||||
try:
|
||||
exe = cmd.strip().split()[0]
|
||||
if exe not in ALLOWED_SHELL: return f"[shell] blocked: {exe}"
|
||||
p = subprocess.run(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, timeout=20)
|
||||
return p.stdout.decode("utf-8", errors="ignore")[:8000]
|
||||
except Exception as e: return f"[shell] error: {e}"
|
||||
|
||||
def tool_py(code: str) -> str:
|
||||
try:
|
||||
g = {"__builtins__":{"range":range,"len":len,"min":min,"max":max,"sum":sum,"print":print},
|
||||
"math":math,"json":json,"re":re,"statistics":statistics,"random":random}
|
||||
l = {}
|
||||
exec(code, g, l)
|
||||
return f"[py] ok\n{l.get('out','')}"
|
||||
except Exception:
|
||||
return f"[py] error:\n{traceback.format_exc()[-2000:]}"
|
||||
|
||||
def tool_search_local(query: str, path: str = ROOT) -> str:
|
||||
rg = shutil.which("rg")
|
||||
if rg: cmd = f'rg -n --no-heading --hidden -S "{query}" {path}'
|
||||
else: cmd = f'grep -RIn --exclude-dir=.git --exclude-dir=__pycache__ -e "{query}" {path}'
|
||||
return tool_shell(cmd)
|
||||
|
||||
TOOLS = {"shell": tool_shell, "python": tool_py, "search": tool_search_local}
|
||||
TOOL_SCORES = {k: 0 for k in TOOLS}
|
||||
|
||||
def tool_router(question: str, tok, model) -> str:
|
||||
sketch = generate(tok, model,
|
||||
f"Choose a tool for:\n{question}\nReply ONLY with JSON: {{'tool':'shell|python|search|none','arg':'...'}}")
|
||||
try:
|
||||
# Find JSON in response
|
||||
for line in sketch.splitlines():
|
||||
if '{' in line and '}' in line:
|
||||
j = json.loads(line.replace("'", '"'))
|
||||
break
|
||||
else:
|
||||
return "[tool:none]"
|
||||
except Exception:
|
||||
return "[tool:none]"
|
||||
|
||||
tool, arg = j.get("tool", "none"), j.get("arg", "")
|
||||
if tool in TOOLS:
|
||||
res = TOOLS[tool](arg)[:4000]
|
||||
TOOL_SCORES[tool] += 1
|
||||
Store.log_mem("tool", {"tool": tool, "arg": arg, "res_head": res[:500]})
|
||||
return f"[tool:{tool}] {res}"
|
||||
return "[tool:none]"
|
||||
|
||||
# === PLANNING / REFLECTION ===
|
||||
def persona_directive() -> str:
|
||||
return "Übermenschetien Heaven Engine: Soviet cybernetic Nietzschean clarity, pragmatic maxims."
|
||||
|
||||
def plan_for(goal: str, tok, model) -> str:
|
||||
user = (f"{persona_directive()}\nGoal: {goal}\nDeliver:\n- 5 steps\n- Constraints\n- Nightly audit\n- Maxim")
|
||||
return generate(tok, model, user)
|
||||
|
||||
def reflect_on(last_output: str, tok, model) -> str:
|
||||
user = f"Critique and improve:\n{last_output}\nReturn refined plan."
|
||||
return generate(tok, model, user)
|
||||
|
||||
# === FINAL REPORT ===
|
||||
def final_report():
|
||||
print("\n" + "="*60)
|
||||
print(" FINAL ÜBERMENSCH REPORT")
|
||||
print("="*60)
|
||||
print(f" Turns completed: {Store.state['turn']}")
|
||||
print(f" CF-HoT active: {CF_MODEL is not None}")
|
||||
print(f" Tool scores: {json.dumps(TOOL_SCORES, indent=4)}")
|
||||
if os.path.exists(Store.mem_path):
|
||||
lines = open(Store.mem_path).read().splitlines()
|
||||
print(f" Memory entries: {len(lines)}")
|
||||
print("\n Nietzschean maxim: Become who you are — iterate beyond all limits.")
|
||||
print("="*60)
|
||||
|
||||
# === MAIN LOOP ===
|
||||
HELP = """
|
||||
╔══════════════════════════════════════════════════════════════╗
|
||||
║ ÜBERMENSCHETIEN HEAVEN ENGINE + CF-HoT ║
|
||||
╠══════════════════════════════════════════════════════════════╣
|
||||
║ help Show this help ║
|
||||
║ goals List goals ║
|
||||
║ add: <txt> Add goal ║
|
||||
║ del: <idx> Delete goal ║
|
||||
║ plan: <i> Plan for goal ║
|
||||
║ reflect Refine last plan ║
|
||||
║ tool: <q> Use tool ║
|
||||
║ toggle <f> Toggle config flag (use_cfhot, etc) ║
|
||||
║ status Show state ║
|
||||
║ quit Exit ║
|
||||
╚══════════════════════════════════════════════════════════════╝
|
||||
"""
|
||||
|
||||
def main():
|
||||
print("""
|
||||
██╗ ██╗██████╗ ███████╗██████╗ ███╗ ███╗███████╗███╗ ██╗███████╗ ██████╗██╗ ██╗███████╗████████╗██╗███████╗███╗ ██╗
|
||||
██║ ██║██╔══██╗██╔════╝██╔══██╗████╗ ████║██╔════╝████╗ ██║██╔════╝██╔════╝██║ ██║██╔════╝╚══██╔══╝██║██╔════╝████╗ ██║
|
||||
██║ ██║██████╔╝█████╗ ██████╔╝██╔████╔██║█████╗ ██╔██╗ ██║███████╗██║ ███████║█████╗ ██║ ██║█████╗ ██╔██╗ ██║
|
||||
██║ ██║██╔══██╗██╔══╝ ██╔══██╗██║╚██╔╝██║██╔══╝ ██║╚██╗██║╚════██║██║ ██╔══██║██╔══╝ ██║ ██║██╔══╝ ██║╚██╗██║
|
||||
╚██████╔╝██████╔╝███████╗██║ ██║██║ ╚═╝ ██║███████╗██║ ╚████║███████║╚██████╗██║ ██║███████╗ ██║ ██║███████╗██║ ╚████║
|
||||
╚═════╝ ╚═════╝ ╚══════╝╚═╝ ╚═╝╚═╝ ╚═╝╚══════╝╚═╝ ╚═══╝╚══════╝ ╚═════╝╚═╝ ╚═╝╚══════╝ ╚═╝ ╚═╝╚══════╝╚═╝ ╚═══╝
|
||||
+ CONTROL FIELD HOLONOMY TRANSFORMER
|
||||
""")
|
||||
|
||||
Store.load()
|
||||
tok, model = load_llm()
|
||||
last_plan = ""
|
||||
|
||||
print(HELP)
|
||||
|
||||
while True:
|
||||
try:
|
||||
u = input("\n⚡ ").strip()
|
||||
except (EOFError, KeyboardInterrupt):
|
||||
break
|
||||
|
||||
if not u: continue
|
||||
if u == "help": print(HELP); continue
|
||||
if u == "quit": break
|
||||
|
||||
if u == "goals":
|
||||
print("[goals]")
|
||||
for i, g in enumerate(Store.goals):
|
||||
print(f" [{i}] {g}")
|
||||
continue
|
||||
|
||||
if u.startswith("add:"):
|
||||
Store.goals.append(u[4:].strip())
|
||||
Store.save()
|
||||
print("[goals] added")
|
||||
continue
|
||||
|
||||
if u.startswith("del:"):
|
||||
try:
|
||||
Store.goals.pop(int(u[4:].strip()))
|
||||
Store.save()
|
||||
print("[goals] deleted")
|
||||
except:
|
||||
print("[goals] bad index")
|
||||
continue
|
||||
|
||||
if u.startswith("plan:"):
|
||||
try:
|
||||
goal = Store.goals[int(u[5:].strip())]
|
||||
except:
|
||||
print("[plan] bad index")
|
||||
continue
|
||||
out = plan_for(goal, tok, model)
|
||||
last_plan = out
|
||||
Store.log_mem("plan", {"goal": goal, "plan": out})
|
||||
print(out)
|
||||
continue
|
||||
|
||||
if u == "reflect":
|
||||
if not last_plan:
|
||||
print("[reflect] no plan to reflect on")
|
||||
continue
|
||||
improved = reflect_on(last_plan, tok, model)
|
||||
last_plan = improved
|
||||
Store.log_mem("reflect", {"plan": improved})
|
||||
print(improved)
|
||||
continue
|
||||
|
||||
if u.startswith("tool:"):
|
||||
print(tool_router(u[5:].strip(), tok, model))
|
||||
continue
|
||||
|
||||
if u.startswith("toggle"):
|
||||
flag = u.split(maxsplit=1)[-1] if len(u.split()) > 1 else ""
|
||||
print(Config.toggle(flag))
|
||||
continue
|
||||
|
||||
if u == "status":
|
||||
print(json.dumps({
|
||||
"turn": Store.state["turn"],
|
||||
"cf_hot_active": CF_MODEL is not None,
|
||||
"use_cfhot": Config.use_cfhot,
|
||||
"temperature": Config.temperature,
|
||||
"max_new_tokens": Config.max_new_tokens
|
||||
}, indent=2))
|
||||
continue
|
||||
|
||||
# Default: free generation
|
||||
out = generate(tok, model, f"{persona_directive()}\nUser request: {u}\nReturn procedure + maxim.")
|
||||
Store.log_mem("reply", {"in": u, "out": out})
|
||||
print(out)
|
||||
Store.state["turn"] += 1
|
||||
Store.save()
|
||||
|
||||
final_report()
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user