Restore snappy mode: queue + workers, real Ollama only

This commit is contained in:
2026-04-02 02:52:51 +00:00
parent 3608c79d21
commit f42838a5c8
2 changed files with 196 additions and 0 deletions

89
home/bilbo_claw.py Normal file
View File

@@ -0,0 +1,89 @@
#!/usr/bin/env python3
"""
BILBO - PURE CLAW CODE
Real Ollama only. No canned dialog. Ever.
"""
import os
import time
import requests
from datetime import datetime
os.environ['HOME'] = '/root/wizards/bilbobagginshire'
TOKEN = "8602794341:AAFwfcg-YV6a1icrh0KYylYmPZLnkfkfV9k"
API = f"https://api.telegram.org/bot{TOKEN}"
OLLAMA = "http://localhost:11434"
print("🔥 BILBO - PURE CLAW CODE")
print("Real Ollama. No canned. Ever.")
def generate(user_text):
"""REAL OLLAMA ONLY"""
try:
r = requests.post(
f"{OLLAMA}/api/generate",
json={
"model": "qwen2.5:1.5b",
"prompt": f"You are Bilbo Baggins. User: {user_text}\nBilbo:",
"stream": False,
"options": {"temperature": 0.8, "num_predict": 150}
},
timeout=60
)
if r.status_code == 200:
return r.json()["response"].strip()
return None
except:
return None
def send(chat_id, text, reply_to=None):
try:
requests.post(f"{API}/sendMessage", json={
"chat_id": chat_id,
"text": text[:4096],
"reply_to_message_id": reply_to
}, timeout=10)
except:
pass
offset = None
while True:
try:
r = requests.post(f"{API}/getUpdates",
json={"offset": offset, "limit": 10},
timeout=30)
updates = r.json().get("result", [])
for update in updates:
offset = update["update_id"] + 1
if "message" not in update:
continue
msg = update["message"]
chat_id = msg["chat"]["id"]
text = msg.get("text", "")
msg_id = msg["message_id"]
if not text:
continue
print(f"[{datetime.now().strftime('%H:%M:%S')}] {text[:40]}")
# REAL OLLAMA ONLY
response = generate(text)
if response:
send(chat_id, response, msg_id)
print(f"{len(response)} chars")
else:
print(f" → Ollama failed, no response sent")
time.sleep(0.5)
except KeyboardInterrupt:
break
except Exception as e:
print(f"Error: {e}")
time.sleep(2)

107
home/bilbo_snappy.py Normal file
View File

@@ -0,0 +1,107 @@
#!/usr/bin/env python3
"""
BILBO - SNAPPY MODE
Queue + workers = speed. Real Ollama only.
"""
import os
import time
import queue
import threading
import requests
from datetime import datetime
os.environ['HOME'] = '/root/wizards/bilbobagginshire'
TOKEN = "8602794341:AAFwfcg-YV6a1icrh0KYylYmPZLnkfkfV9k"
API = f"https://api.telegram.org/bot{TOKEN}"
OLLAMA = "http://localhost:11434"
# QUEUE = SPEED
q = queue.Queue()
print("🔥 BILBO - SNAPPY MODE")
print("Queue + workers. Real Ollama. No canned.")
def worker():
"""Background worker - never blocks main thread"""
while True:
try:
chat_id, text, msg_id = q.get(timeout=1)
# REAL OLLAMA ONLY
try:
r = requests.post(
f"{OLLAMA}/api/generate",
json={
"model": "qwen2.5:1.5b",
"prompt": f"You are Bilbo Baggins. Be polite but brief.\n\nUser: {text}\n\nBilbo:",
"stream": False,
"options": {"temperature": 0.8, "num_predict": 100}
},
timeout=60
)
if r.status_code == 200:
resp = r.json()["response"].strip()
# SEND REAL RESPONSE
requests.post(f"{API}/sendMessage", json={
"chat_id": chat_id,
"text": resp[:4096],
"reply_to_message_id": msg_id
}, timeout=10)
print(f"{len(resp)} chars")
else:
print(f"✗ Status {r.status_code}")
except Exception as e:
print(f"✗ Ollama fail: {str(e)[:30]}")
# NO CANNED RESPONSE - just log and continue
q.task_done()
except queue.Empty:
continue
except Exception as e:
print(f"Worker err: {e}")
# Start workers
for i in range(2):
threading.Thread(target=worker, daemon=True).start()
# Main loop - just queue, never wait
offset = None
while True:
try:
r = requests.post(f"{API}/getUpdates",
json={"offset": offset, "limit": 10},
timeout=30)
updates = r.json().get("result", [])
for update in updates:
offset = update["update_id"] + 1
if "message" not in update:
continue
msg = update["message"]
chat_id = msg["chat"]["id"]
text = msg.get("text", "")
msg_id = msg["message_id"]
if not text:
continue
print(f"[{datetime.now().strftime('%H:%M:%S')}] {text[:40]}")
# QUEUE IT - DON'T WAIT
q.put((chat_id, text, msg_id))
print(f" → queued ({q.qsize()})")
time.sleep(0.1) # Fast poll
except KeyboardInterrupt:
break
except Exception as e:
print(f"Err: {e}")
time.sleep(1)