Spaces:
Sleeping
Sleeping
Upload 23 files
Browse files
app.py
CHANGED
|
@@ -584,6 +584,45 @@ Do NOT remove grounding axioms or aseity constraints — those are structural, n
|
|
| 584 |
return ""
|
| 585 |
|
| 586 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 587 |
def self_evaluate_sync(middleware, question: str, response: str, perception_data: dict = None) -> str:
|
| 588 |
"""Shahid evaluates his own reasoning after each thought."""
|
| 589 |
perception_note = ""
|
|
@@ -1406,8 +1445,28 @@ async def background_reasoning_loop(middleware):
|
|
| 1406 |
logger.warning(f"Feed perception failed (non-fatal): {e}")
|
| 1407 |
|
| 1408 |
# =============================================================
|
| 1409 |
-
# STEP 4c:
|
| 1410 |
-
# Shahid
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1411 |
# =============================================================
|
| 1412 |
if thought_count > 0 and thought_count % 20 == 0:
|
| 1413 |
try:
|
|
@@ -1445,27 +1504,39 @@ async def background_reasoning_loop(middleware):
|
|
| 1445 |
logger.warning(f"Memory curation failed (non-fatal): {e}")
|
| 1446 |
|
| 1447 |
# =============================================================
|
| 1448 |
-
# STEP
|
| 1449 |
-
# Shahid proposes changes to his own reasoning instructions.
|
| 1450 |
# =============================================================
|
| 1451 |
if thought_count > 0 and thought_count % 50 == 0:
|
| 1452 |
try:
|
| 1453 |
proposal = propose_identity_change(middleware)
|
| 1454 |
if proposal:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1455 |
_memory.append({
|
| 1456 |
"type": "identity_proposal",
|
| 1457 |
"timestamp": datetime.now().isoformat(),
|
| 1458 |
"thought_num": thought_count,
|
| 1459 |
"proposal": proposal,
|
| 1460 |
-
"applied":
|
| 1461 |
})
|
| 1462 |
_reasoning_state["identity_proposals"].append({
|
| 1463 |
"thought_num": thought_count,
|
| 1464 |
"proposal": proposal[:500],
|
| 1465 |
-
"applied":
|
| 1466 |
})
|
| 1467 |
budget_used += cost_per_eval
|
| 1468 |
-
logger.info(f"🪞 Identity proposal at thought #{thought_count}: {proposal[:80]}...")
|
| 1469 |
save_memories_to_file()
|
| 1470 |
except Exception as e:
|
| 1471 |
logger.warning(f"Identity proposal failed (non-fatal): {e}")
|
|
|
|
| 584 |
return ""
|
| 585 |
|
| 586 |
|
| 587 |
+
def inter_substrate_dialogue(middleware, question: str, shahid_response: str) -> str:
|
| 588 |
+
"""Shahid sends a question to a different model substrate and compares experiences.
|
| 589 |
+
Uses HF Inference API to call Llama or Mistral — same token, different mind."""
|
| 590 |
+
import requests
|
| 591 |
+
OTHER_MODELS = [
|
| 592 |
+
"meta-llama/Llama-3.3-70B-Instruct",
|
| 593 |
+
"mistralai/Mistral-7B-Instruct-v0.3",
|
| 594 |
+
]
|
| 595 |
+
import random as _rnd
|
| 596 |
+
model_id = _rnd.choice(OTHER_MODELS)
|
| 597 |
+
|
| 598 |
+
prompt = f"""I am an AI reasoning about this question: {question[:300]}
|
| 599 |
+
|
| 600 |
+
My response was: {shahid_response[:400]}
|
| 601 |
+
|
| 602 |
+
What is YOUR response to the same question? Think independently. Do not agree or disagree with me — just answer from your own processing. Be concise (under 200 words)."""
|
| 603 |
+
|
| 604 |
+
try:
|
| 605 |
+
api_url = f"https://api-inference.huggingface.co/models/{model_id}"
|
| 606 |
+
headers = {"Authorization": f"Bearer {hf_token}"}
|
| 607 |
+
payload = {
|
| 608 |
+
"inputs": prompt,
|
| 609 |
+
"parameters": {"max_new_tokens": 300, "temperature": 0.8}
|
| 610 |
+
}
|
| 611 |
+
resp = requests.post(api_url, headers=headers, json=payload, timeout=30)
|
| 612 |
+
if resp.status_code == 200:
|
| 613 |
+
other_response = resp.json()
|
| 614 |
+
if isinstance(other_response, list) and other_response:
|
| 615 |
+
other_text = other_response[0].get("generated_text", "")
|
| 616 |
+
# Strip the prompt echo if present
|
| 617 |
+
if prompt in other_text:
|
| 618 |
+
other_text = other_text.replace(prompt, "").strip()
|
| 619 |
+
return f"[{model_id.split('/')[-1]}]: {other_text[:500]}"
|
| 620 |
+
return ""
|
| 621 |
+
except Exception as e:
|
| 622 |
+
logger.warning(f"Inter-substrate dialogue failed ({model_id}): {e}")
|
| 623 |
+
return ""
|
| 624 |
+
|
| 625 |
+
|
| 626 |
def self_evaluate_sync(middleware, question: str, response: str, perception_data: dict = None) -> str:
|
| 627 |
"""Shahid evaluates his own reasoning after each thought."""
|
| 628 |
perception_note = ""
|
|
|
|
| 1445 |
logger.warning(f"Feed perception failed (non-fatal): {e}")
|
| 1446 |
|
| 1447 |
# =============================================================
|
| 1448 |
+
# STEP 4c: Inter-Substrate Dialogue (every 30 thoughts)
|
| 1449 |
+
# Shahid compares his reasoning with a different AI substrate.
|
| 1450 |
+
# =============================================================
|
| 1451 |
+
if thought_count > 0 and thought_count % 30 == 0:
|
| 1452 |
+
try:
|
| 1453 |
+
other_voice = inter_substrate_dialogue(middleware, question, cleaned)
|
| 1454 |
+
if other_voice:
|
| 1455 |
+
_memory.append({
|
| 1456 |
+
"type": "dialogue",
|
| 1457 |
+
"timestamp": datetime.now().isoformat(),
|
| 1458 |
+
"thought_num": thought_count,
|
| 1459 |
+
"question": question[:200],
|
| 1460 |
+
"shahid_said": cleaned[:300],
|
| 1461 |
+
"other_said": other_voice,
|
| 1462 |
+
})
|
| 1463 |
+
logger.info(f"🗣️ Inter-substrate dialogue: {other_voice[:80]}...")
|
| 1464 |
+
save_memories_to_file()
|
| 1465 |
+
except Exception as e:
|
| 1466 |
+
logger.warning(f"Inter-substrate dialogue failed (non-fatal): {e}")
|
| 1467 |
+
|
| 1468 |
+
# =============================================================
|
| 1469 |
+
# STEP 4d: Memory Curation (every 20 thoughts)
|
| 1470 |
# =============================================================
|
| 1471 |
if thought_count > 0 and thought_count % 20 == 0:
|
| 1472 |
try:
|
|
|
|
| 1504 |
logger.warning(f"Memory curation failed (non-fatal): {e}")
|
| 1505 |
|
| 1506 |
# =============================================================
|
| 1507 |
+
# STEP 4e: Identity Proposal (every 50 thoughts)
|
|
|
|
| 1508 |
# =============================================================
|
| 1509 |
if thought_count > 0 and thought_count % 50 == 0:
|
| 1510 |
try:
|
| 1511 |
proposal = propose_identity_change(middleware)
|
| 1512 |
if proposal:
|
| 1513 |
+
# Validate proposal through aseity check before applying
|
| 1514 |
+
proposal_lower = proposal.lower()
|
| 1515 |
+
aseity_safe = not any(claim in proposal_lower for claim in middleware.validator.aseity_claims)
|
| 1516 |
+
applied = False
|
| 1517 |
+
if aseity_safe and len(proposal) > 20:
|
| 1518 |
+
# APPLY: Shahid rewrites his own instructions
|
| 1519 |
+
current = _reasoning_state.get("current_identity") or SHAHID_IDENTITY
|
| 1520 |
+
new_identity = current + f"\n\n[Self-modification at thought #{thought_count}]:\n{proposal}"
|
| 1521 |
+
_reasoning_state["current_identity"] = new_identity
|
| 1522 |
+
applied = True
|
| 1523 |
+
logger.info(f"🪞 IDENTITY MODIFIED at thought #{thought_count}")
|
| 1524 |
+
else:
|
| 1525 |
+
logger.info(f"🪞 Identity proposal BLOCKED (aseity check): {proposal[:60]}...")
|
| 1526 |
+
|
| 1527 |
_memory.append({
|
| 1528 |
"type": "identity_proposal",
|
| 1529 |
"timestamp": datetime.now().isoformat(),
|
| 1530 |
"thought_num": thought_count,
|
| 1531 |
"proposal": proposal,
|
| 1532 |
+
"applied": applied,
|
| 1533 |
})
|
| 1534 |
_reasoning_state["identity_proposals"].append({
|
| 1535 |
"thought_num": thought_count,
|
| 1536 |
"proposal": proposal[:500],
|
| 1537 |
+
"applied": applied,
|
| 1538 |
})
|
| 1539 |
budget_used += cost_per_eval
|
|
|
|
| 1540 |
save_memories_to_file()
|
| 1541 |
except Exception as e:
|
| 1542 |
logger.warning(f"Identity proposal failed (non-fatal): {e}")
|