fix: Simplified server
This commit is contained in:
+30
-100
@@ -1,140 +1,70 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
"""BECOMINGONE Chat API - Simplified."""
|
"""BECOMINGONE Chat API."""
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
import json
|
import json
|
||||||
|
|
||||||
# Import at module level
|
|
||||||
from becomingone.llm_integrator import EmissaryLLM
|
from becomingone.llm_integrator import EmissaryLLM
|
||||||
|
|
||||||
MASTER = None
|
# Initialize
|
||||||
EMISSARY = None
|
|
||||||
|
|
||||||
async def init_models():
|
|
||||||
global MASTER, EMISSARY
|
|
||||||
print("Initializing models...")
|
|
||||||
MASTER = EmissaryLLM(model='llama3.1:8b')
|
MASTER = EmissaryLLM(model='llama3.1:8b')
|
||||||
EMISSARY = EmissaryLLM(model='deepseek-coder-v2:lite')
|
EMISSARY = EmissaryLLM(model='deepseek-coder-v2:lite')
|
||||||
print("Models initialized")
|
|
||||||
|
|
||||||
async def chat(prompt: str) -> dict:
|
async def process(prompt):
|
||||||
"""Process through both pathways."""
|
"""Process through both models."""
|
||||||
print(f"Processing: {prompt[:30]}...")
|
m, e = await asyncio.gather(
|
||||||
|
|
||||||
try:
|
|
||||||
m, e = await asyncio.wait_for(
|
|
||||||
asyncio.gather(
|
|
||||||
MASTER.respond(prompt),
|
MASTER.respond(prompt),
|
||||||
EMISSARY.respond(prompt),
|
EMISSARY.respond(prompt),
|
||||||
return_exceptions=True
|
return_exceptions=True
|
||||||
),
|
|
||||||
timeout=60
|
|
||||||
)
|
)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"prompt": prompt,
|
"master": {"response": m.get("response", str(m))[:500] if hasattr(m, 'get') else str(m)[:500]},
|
||||||
"master": {"response": str(m)[:500] if isinstance(m, Exception) else m.get("response", str(m))[:500]},
|
"emissary": {"response": e.get("response", str(e))[:500] if hasattr(e, 'get') else str(e)[:500]}
|
||||||
"emissary": {"response": str(e)[:500] if isinstance(e, Exception) else e.get("response", str(e))[:500]}
|
|
||||||
}
|
}
|
||||||
except asyncio.TimeoutError:
|
|
||||||
return {"error": "Timeout"}
|
|
||||||
except Exception as ex:
|
|
||||||
return {"error": str(ex)}
|
|
||||||
|
|
||||||
|
HTML = '''<!DOCTYPE html><html><head><title>BECOMINGONE</title><meta name="viewport" content="width=device-width"><style>body{font-family:sans-serif;max-width:700px;margin:0 auto;padding:20px;background:#111;color:#fff}h1{color:#0f0;text-align:center}input{width:100%;padding:12px;font-size:16px;background:#222;color:#fff;border:1px solid #444;border-radius:6px}button{background:#0f0;color:#000;border:none;padding:12px 24px;font-size:14px;cursor:pointer;margin:8px 0;border-radius:6px}.r{background:#222;padding:12px;margin:8px 0;border-left:4px solid #90f}.b{background:#222;padding:12px;margin:8px 0;border-left:4px solid #f00}</style></head><body><h1>🔗 BECOMINGONE</h1><p style="text-align:center;color:#888">Master + Emissary</p><input id="p" placeholder="Ask anything..." onkeypress="if(event.key==='Enter')s()"><button onclick="s()">Ask</button><div id="r"></div><script>async function s(){var p=document.getElementById("p").value.trim();if(!p)return;document.getElementById("r").innerHTML="<p style=color:#888>Thinking...</p>";try{var r=await fetch("/c",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({prompt:p})});var d=await r.json();var h="<div class=r><b>🧠 Master</b><br>"+d.master.response+"</div>";h+="<div class=b><b>⚡ Emissary</b><br>"+d.emissary.response+"</div>";document.getElementById("r").innerHTML=h}catch(e){document.getElementById("r").innerHTML="<div class=r>Error: "+e+"</div>"}}</script></body></html>'''
|
||||||
|
|
||||||
HTML = '''<!DOCTYPE html>
|
async def handle(r, w):
|
||||||
<html>
|
|
||||||
<head>
|
|
||||||
<title>BECOMINGONE</title>
|
|
||||||
<meta name="viewport" content="width=device-width">
|
|
||||||
<style>
|
|
||||||
body{font-family:-apple-system,sans-serif;max-width:800px;margin:0 auto;padding:20px;background:#111;color:#fff}
|
|
||||||
h1{color:#0f0;text-align:center}
|
|
||||||
input{width:100%;padding:15px;font-size:18px;background:#222;color:#fff;border:1px solid #444;border-radius:8px}
|
|
||||||
button{background:#0f0;color:#000;border:none;padding:15px 30px;font-size:16px;cursor:pointer;margin:10px 0;border-radius:8px}
|
|
||||||
.master{background:#222;padding:15px;margin:10px 0;border-left:4px solid #a0f}
|
|
||||||
.emissary{background:#222;padding:15px;margin:10px 0;border-left:4px solid #f00}
|
|
||||||
</style>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<h1>🔗 BECOMINGONE</h1>
|
|
||||||
<p style="text-align:center;color:#888">Master + Emissary = Unified</p>
|
|
||||||
<input id="p" placeholder="Ask anything..." autofocus onkeypress="if(event.key==='Enter')ask()">
|
|
||||||
<button onclick="ask()">Ask</button>
|
|
||||||
<div id="r"></div>
|
|
||||||
<script>
|
|
||||||
async function ask() {
|
|
||||||
const prompt = document.getElementById('p').value.trim();
|
|
||||||
if(!prompt) return;
|
|
||||||
document.getElementById('r').innerHTML = '<p style="color:#888">Thinking...</p>';
|
|
||||||
try {
|
|
||||||
const res = await fetch('/chat',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({prompt})});
|
|
||||||
const data = await res.json();
|
|
||||||
if(data.error) { document.getElementById('r').innerHTML = '<div class="master">Error: '+data.error+'</div>'; return; }
|
|
||||||
let h = '<div class="master"><b>🧠 Master</b><pre>'+data.master.response+'</pre></div>';
|
|
||||||
h += '<div class="emissary"><b>⚡ Emissary</b><pre>'+data.emissary.response+'</pre></div>';
|
|
||||||
document.getElementById('r').innerHTML = h;
|
|
||||||
} catch(e) { document.getElementById('r').innerHTML = '<div class="master">Error: '+e+'</div>'; }
|
|
||||||
}
|
|
||||||
</script>
|
|
||||||
</body>
|
|
||||||
</html>'''
|
|
||||||
|
|
||||||
|
|
||||||
async def handle(reader, writer):
|
|
||||||
try:
|
try:
|
||||||
data = await reader.read(4096)
|
d = await r.read(8192)
|
||||||
if not data:
|
if not d: return
|
||||||
return
|
|
||||||
|
|
||||||
text = data.decode('utf-8', errors='ignore')
|
txt = d.decode('utf-8', errors='ignore')
|
||||||
lines = text.split('\r\n')
|
ln = txt.split('\n')[0].split()
|
||||||
parts = lines[0].split()
|
method, path = ln[0], ln[1] if len(ln) > 1 else '/'
|
||||||
method, path = parts[0], parts[1] if len(parts) > 1 else '/'
|
|
||||||
|
|
||||||
# Get body
|
# Read body
|
||||||
body = b""
|
body = b""
|
||||||
for line in lines:
|
for line in txt.split('\r\n'):
|
||||||
if line.lower().startswith('content-length:'):
|
if line.lower().startswith('content-length:'):
|
||||||
cl = int(line.split(':')[1].strip())
|
body = await r.read(int(line.split(':')[1].strip()))
|
||||||
body = await reader.read(cl)
|
|
||||||
break
|
break
|
||||||
|
|
||||||
# Routes
|
|
||||||
if path == '/health':
|
if path == '/health':
|
||||||
resp = json.dumps({"status": "ok"})
|
resp = '{"status":"ok"}'
|
||||||
content_type = "application/json"
|
ct = 'application/json'
|
||||||
elif path == '/chat' and method == 'POST':
|
elif path == '/c' and method == 'POST':
|
||||||
try:
|
try:
|
||||||
d = json.loads(body.decode())
|
data = json.loads(body.decode())
|
||||||
result = await chat(d.get('prompt', 'Hi'))
|
result = await process(data.get('prompt', 'Hi'))
|
||||||
resp = json.dumps(result)
|
resp = json.dumps(result)
|
||||||
content_type = "application/json"
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
resp = json.dumps({"error": str(e)})
|
resp = '{"error":"' + str(e) + '"}'
|
||||||
content_type = "application/json"
|
ct = 'application/json'
|
||||||
else:
|
else:
|
||||||
resp = HTML
|
resp = HTML
|
||||||
content_type = "text/html"
|
ct = 'text/html'
|
||||||
|
|
||||||
writer.write(b"HTTP/1.1 200 OK\r\n")
|
w.write(b'HTTP/1.1 200 OK\r\nContent-Type: ' + ct.encode() + b'\r\nContent-Length: ' + str(len(resp)).encode() + b'\r\nConnection: close\r\n\r\n' + resp.encode())
|
||||||
writer.write(f"Content-Type: {content_type}\r\n".encode())
|
|
||||||
writer.write(f"Content-Length: {len(resp)}\r\n".encode())
|
|
||||||
writer.write(b"Connection: close\r\n\r\n")
|
|
||||||
writer.write(resp.encode())
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Error: {e}")
|
print('Error:', e)
|
||||||
finally:
|
finally:
|
||||||
writer.close()
|
w.close()
|
||||||
await writer.wait_closed()
|
await w.wait_closed()
|
||||||
|
|
||||||
|
|
||||||
async def main():
|
async def main():
|
||||||
await init_models()
|
|
||||||
server = await asyncio.start_server(handle, '0.0.0.0', 8001)
|
server = await asyncio.start_server(handle, '0.0.0.0', 8001)
|
||||||
print("Server running on port 8001")
|
print('Server running on http://192.168.1.6:8001')
|
||||||
async with server:
|
async with server:
|
||||||
await server.serve_forever()
|
await server.serve_forever()
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
asyncio.run(main())
|
asyncio.run(main())
|
||||||
|
|||||||
Reference in New Issue
Block a user