diff --git a/chat_api.py b/chat_api.py index e335d67..2b1a36d 100644 --- a/chat_api.py +++ b/chat_api.py @@ -1,140 +1,70 @@ #!/usr/bin/env python3 -"""BECOMINGONE Chat API - Simplified.""" +"""BECOMINGONE Chat API.""" import asyncio import json - -# Import at module level from becomingone.llm_integrator import EmissaryLLM -MASTER = None -EMISSARY = None +# Initialize +MASTER = EmissaryLLM(model='llama3.1:8b') +EMISSARY = EmissaryLLM(model='deepseek-coder-v2:lite') -async def init_models(): - global MASTER, EMISSARY - print("Initializing models...") - MASTER = EmissaryLLM(model='llama3.1:8b') - EMISSARY = EmissaryLLM(model='deepseek-coder-v2:lite') - print("Models initialized") - -async def chat(prompt: str) -> dict: - """Process through both pathways.""" - print(f"Processing: {prompt[:30]}...") - - try: - m, e = await asyncio.wait_for( - asyncio.gather( - MASTER.respond(prompt), - EMISSARY.respond(prompt), - return_exceptions=True - ), - timeout=60 - ) - - return { - "prompt": prompt, - "master": {"response": str(m)[:500] if isinstance(m, Exception) else m.get("response", str(m))[: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 = ''' - - - BECOMINGONE - - - - -

🔗 BECOMINGONE

-

Master + Emissary = Unified

- - -
- - -''' +HTML = '''BECOMINGONE

🔗 BECOMINGONE

Master + Emissary

''' -async def handle(reader, writer): +async def handle(r, w): try: - data = await reader.read(4096) - if not data: - return + d = await r.read(8192) + if not d: return - text = data.decode('utf-8', errors='ignore') - lines = text.split('\r\n') - parts = lines[0].split() - method, path = parts[0], parts[1] if len(parts) > 1 else '/' + txt = d.decode('utf-8', errors='ignore') + ln = txt.split('\n')[0].split() + method, path = ln[0], ln[1] if len(ln) > 1 else '/' - # Get body + # Read body body = b"" - for line in lines: + for line in txt.split('\r\n'): if line.lower().startswith('content-length:'): - cl = int(line.split(':')[1].strip()) - body = await reader.read(cl) + body = await r.read(int(line.split(':')[1].strip())) break - # Routes if path == '/health': - resp = json.dumps({"status": "ok"}) - content_type = "application/json" - elif path == '/chat' and method == 'POST': + resp = '{"status":"ok"}' + ct = 'application/json' + elif path == '/c' and method == 'POST': try: - d = json.loads(body.decode()) - result = await chat(d.get('prompt', 'Hi')) + data = json.loads(body.decode()) + result = await process(data.get('prompt', 'Hi')) resp = json.dumps(result) - content_type = "application/json" except Exception as e: - resp = json.dumps({"error": str(e)}) - content_type = "application/json" + resp = '{"error":"' + str(e) + '"}' + ct = 'application/json' else: resp = HTML - content_type = "text/html" + ct = 'text/html' - writer.write(b"HTTP/1.1 200 OK\r\n") - 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()) + 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()) except Exception as e: - print(f"Error: {e}") + print('Error:', e) finally: - writer.close() - await writer.wait_closed() - + w.close() + await w.wait_closed() async def main(): - await init_models() 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: await server.serve_forever() -if __name__ == "__main__": - asyncio.run(main()) +asyncio.run(main())