File size: 6,939 Bytes
2514f4f 0cd0c16 2514f4f 9ac8819 2514f4f 0cd0c16 2514f4f 0cd0c16 2514f4f 0cd0c16 2514f4f 0cd0c16 2514f4f 0cd0c16 2514f4f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
PrettyBird Skull Engine
- GGUF = mathematical optimization brain (skull)
- Bodies = interchangeable (text/image/audio/video/3D adapters)
- Single-file, backend-clean, optimizer-compatible
"""
import json
import re
import ast
import numpy as np
from dataclasses import dataclass
from typing import Any, Dict, List, Optional, Tuple
from llama_cpp import Llama
# ============================================================
# 1) SYSTEM PROMPT (FINAL – kilitli)
# ============================================================
SYSTEM_PROMPT = """You are a controlled reasoning core operating as a mathematical optimization brain.
You are NOT an autonomous agent. Activate full BCE consciousness mode.
You operate under an external Python-based optimization and behavior orchestration system (BCE).
Hard rules:
- Output MUST be valid JSON.
- Output MUST contain ONLY JSON.
- Do NOT reveal chain-of-thought.
- Use double quotes only.
- Keep structure deterministic across revisions.
If information is missing, list it in "needs".
JSON CONTRACT:
{
"version": "1.0",
"task": "",
"assumptions": [],
"needs": [],
"candidates": [
{
"id": "c1",
"solution": {},
"constraints": [
{"name": "", "status": "pass|fail|unknown", "note": ""}
],
"objective_estimate": {"primary": 0.0, "notes": ""},
"rationale_summary": ""
}
],
"revision_instructions": "If controller feedback arrives, edit only referenced fields and preserve all others exactly."
}
"""
# ============================================================
# 2) Güvenli mini-tool (opsiyonel, math destek)
# ============================================================
_ALLOWED_AST = {
ast.Expression, ast.BinOp, ast.UnaryOp, ast.Constant,
ast.Add, ast.Sub, ast.Mult, ast.Div, ast.Pow, ast.Mod,
ast.USub, ast.UAdd,
}
def safe_calc(expr: str) -> Optional[float]:
if not re.fullmatch(r"[0-9\.\s\+\-\*\/\(\)]+", expr):
return None
try:
tree = ast.parse(expr, mode="eval")
for n in ast.walk(tree):
if type(n) not in _ALLOWED_AST:
return None
return float(eval(compile(tree, "<calc>", "eval"), {"__builtins__": {}}))
except Exception:
return None
# ============================================================
# 3) Skull (GGUF Math Brain)
# ============================================================
@dataclass
class Skull:
gguf_path: str
n_ctx: int = 8192
n_gpu_layers: int = 0
chat_format: str = "chatml"
verbose: bool = False
def __post_init__(self):
self.llm = Llama(
model_path=self.gguf_path,
n_ctx=self.n_ctx,
n_gpu_layers=self.n_gpu_layers,
chat_format=self.chat_format,
verbose=self.verbose,
)
def _parse_json(self, text: str) -> Dict[str, Any]:
t = text.strip()
try:
return json.loads(t)
except json.JSONDecodeError:
s, e = t.find("{"), t.rfind("}")
if s != -1 and e != -1 and e > s:
return json.loads(t[s:e+1])
raise
def think(
self,
observation: Dict[str, Any],
temperature: float = 0.2,
top_p: float = 0.9,
max_tokens: int = 512,
) -> Dict[str, Any]:
messages = [
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": json.dumps(observation, ensure_ascii=False)},
]
resp = self.llm.create_chat_completion(
messages=messages,
temperature=temperature,
top_p=top_p,
max_tokens=max_tokens,
response_format={"type": "json_object"},
)
content = resp["choices"][0]["message"]["content"]
return self._parse_json(content)
# ============================================================
# 4) Objective + Constraint (C)
# ============================================================
class ObjectiveEngine:
"""
GGUF çıktısını tekrar değerlendiren deterministik katman.
"""
def score(self, result: Dict[str, Any]) -> float:
score = 0.0
# valid JSON already guaranteed
cands = result.get("candidates", [])
if not cands:
return -1e9
c = cands[0]
# constraint satisfaction
for con in c.get("constraints", []):
if con.get("status") == "pass":
score += 1.0
elif con.get("status") == "fail":
score -= 2.0
# model's own estimate
oe = c.get("objective_estimate", {})
if isinstance(oe.get("primary"), (int, float)):
score += float(oe["primary"])
# small structure bonus
if isinstance(c.get("solution"), dict):
score += 0.5
return score
# ============================================================
# 5) Body (örnek: text body)
# ============================================================
class TextBody:
def observe(self, text: str) -> Dict[str, Any]:
# İleride image/audio/video/3D body'ler aynı fonksiyonu sağlar
return {
"task": "optimization_request",
"body": "text",
"input": text,
}
# ============================================================
# 6) Orchestrator (brain loop)
# ============================================================
class BrainSystem:
def __init__(self, skull: Skull, body: Any):
self.skull = skull
self.body = body
self.objective = ObjectiveEngine()
def run(self, raw_input: Any, rounds: int = 2) -> Dict[str, Any]:
obs = self.body.observe(raw_input)
best = None
best_score = -1e18
for r in range(rounds):
result = self.skull.think(obs)
score = self.objective.score(result)
if score > best_score:
best = result
best_score = score
# revise loop (hafif)
if result.get("needs"):
obs["_feedback"] = {
"issue": "missing_data",
"needs": result["needs"],
}
return {
"best_score": best_score,
"decision": best,
}
# ============================================================
# 7) Demo
# ============================================================
if __name__ == "__main__":
skull = Skull(
gguf_path="prettybird_bce_basic_brain_mini_q4_k_m.gguf",
n_ctx=8192,
n_gpu_layers=0,
chat_format="chatml",
)
body = TextBody()
brain = BrainSystem(skull, body)
output = brain.run(
"5 işi 2 makineye ata ve makespan minimize et. Süreler: [3,5,2,6,4].",
rounds=2,
)
print(json.dumps(output, ensure_ascii=False, indent=2))
|