File size: 11,576 Bytes
38b4eff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#!/usr/bin/env python3
"""
Neural Uplink - Multi-Agent AI System
=====================================
A standalone tool that runs 4 specialized AI agents in parallel:
- DIALOG: Conversation and reasoning
- DATA: Data analysis and retrieval  
- OPS: Operations and execution
- WORLD: World-building and creative tasks

Usage:
  Start: python3 neural_uplink.py
  Query: curl -X POST http://localhost:8000/uplink -d '{"prompt": "analyze this..."}'
"""

import os
import sys
import json
import asyncio
import aiohttp
from datetime import datetime
from pathlib import Path
from flask import Flask, jsonify, request, Response
import threading
import queue

# Configuration
PORT = int(os.environ.get("UPLINK_PORT", "8000"))
MODEL_SERVICE = os.environ.get("MODEL_SERVICE", "http://localhost:7001")

app = Flask(__name__)

# Agent definitions
AGENTS = {
    "dialog": {
        "name": "DIALOG",
        "role": "Conversation & Reasoning",
        "system": "You are DIALOG, an AI agent specialized in conversation, reasoning, and explanation. Be concise and insightful.",
        "color": "🔵"
    },
    "data": {
        "name": "DATA", 
        "role": "Data Analysis",
        "system": "You are DATA, an AI agent specialized in data analysis, patterns, and retrieval. Focus on facts and numbers.",
        "color": "🟢"
    },
    "ops": {
        "name": "OPS",
        "role": "Operations & Execution", 
        "system": "You are OPS, an AI agent specialized in operations, execution, and practical solutions. Be actionable.",
        "color": "🟡"
    },
    "world": {
        "name": "WORLD",
        "role": "World-Building & Creativity",
        "system": "You are WORLD, an AI agent specialized in creative thinking, world-building, and ideation. Be imaginative.",
        "color": "🟣"
    }
}


async def query_agent(agent_name: str, prompt: str, timeout: int = 90) -> dict:
    """Query a single agent via the model service."""
    agent = AGENTS.get(agent_name)
    if not agent:
        return {"agent": agent_name, "error": "Unknown agent"}
    
    try:
        async with aiohttp.ClientSession() as session:
            # Build prompt with agent's system message
            full_prompt = f"<|im_start|>system\n{agent['system']}<|im_end|>\n<|im_start|>user\n{prompt}<|im_end|>\n<|im_start|>assistant\n"
            
            async with session.post(
                f"{MODEL_SERVICE}/generate",
                json={"prompt": full_prompt, "max_tokens": 128, "temperature": 0.7},
                timeout=aiohttp.ClientTimeout(total=timeout)
            ) as resp:
                if resp.status == 200:
                    data = await resp.json()
                    return {
                        "agent": agent["name"],
                        "role": agent["role"],
                        "color": agent["color"],
                        "response": data.get("response", ""),
                        "tokens": data.get("tokens_generated", 0)
                    }
                return {"agent": agent["name"], "error": f"HTTP {resp.status}"}
    except asyncio.TimeoutError:
        return {"agent": agent["name"], "error": "timeout"}
    except Exception as e:
        return {"agent": agent["name"], "error": str(e)}


async def query_all_agents(prompt: str) -> list:
    """Query all 4 agents in parallel."""
    tasks = [
        query_agent("dialog", prompt),
        query_agent("data", prompt),
        query_agent("ops", prompt),
        query_agent("world", prompt)
    ]
    
    results = await asyncio.gather(*tasks, return_exceptions=True)
    
    # Filter and format results
    outputs = []
    for result in results:
        if isinstance(result, Exception):
            continue
        if "error" not in result or not result.get("error"):
            outputs.append(result)
    
    return outputs


def fuse_responses(responses: list) -> str:
    """Combine agent responses into a coherent output."""
    if not responses:
        return "[Neural Uplink] No agent responses. Please try again."
    
    lines = ["🧠 **Neural Uplink - Multi-Agent Analysis**\n"]
    
    for resp in responses:
        color = resp.get("color", "⚪")
        name = resp.get("agent", "Agent")
        role = resp.get("role", "")
        response = resp.get("response", "")
        
        if response and len(response) > 5:
            lines.append(f"\n{color} **{name}** ({role}):")
            lines.append(f"> {response.strip()}")
    
    return "\n".join(lines)


# ==================
# API ENDPOINTS
# ==================

@app.route("/health", methods=["GET"])
def health():
    """Health check."""
    return jsonify({
        "status": "ready",
        "port": PORT,
        "agents": list(AGENTS.keys()),
        "model_service": MODEL_SERVICE
    })


@app.route("/uplink", methods=["POST"])
def uplink():
    """Main uplink endpoint - queries all agents."""
    data = request.get_json()
    prompt = data.get("prompt", data.get("message", ""))
    
    if not prompt:
        return jsonify({"error": "No prompt provided"}), 400
    
    # Run async query
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)
    try:
        results = loop.run_until_complete(query_all_agents(prompt))
        fused = fuse_responses(results)
        
        return jsonify({
            "success": True,
            "prompt": prompt,
            "agent_count": len(results),
            "responses": results,
            "fused": fused
        })
    finally:
        loop.close()


@app.route("/uplink/stream", methods=["POST"])
def uplink_stream():
    """Streaming uplink - yields responses as they arrive."""
    data = request.get_json()
    prompt = data.get("prompt", data.get("message", ""))
    
    if not prompt:
        return jsonify({"error": "No prompt provided"}), 400
    
    def generate():
        # Query each agent sequentially for streaming
        import requests as sync_requests
        
        full_response = "🧠 **Neural Uplink Analysis**\n\n"
        
        for agent_name, agent in AGENTS.items():
            try:
                # Build prompt with agent's system message
                full_prompt = f"<|im_start|>system\n{agent['system']}<|im_end|>\n<|im_start|>user\n{prompt}<|im_end|>\n<|im_start|>assistant\n"
                
                resp = sync_requests.post(
                    f"{MODEL_SERVICE}/generate",
                    json={"prompt": full_prompt, "max_tokens": 100, "temperature": 0.7},
                    timeout=60
                )
                
                if resp.status_code == 200:
                    data = resp.json()
                    response = data.get("response", "")
                    
                    if response and len(response) > 5:
                        chunk = f"{agent['color']} **{agent['name']}**: {response.strip()}\n\n"
                        full_response += chunk
                        yield f"data: {json.dumps({'content': chunk})}\n\n"
                        
            except Exception as e:
                yield "data: " + json.dumps({"content": agent["color"] + " **" + agent["name"] + "**: Error - " + str(e) + "\n\n"}) + "\n\n"
        
        yield "data: [DONE]\n\n"
    
    return Response(
        generate(),
        mimetype="text/event-stream",
        headers={"Cache-Control": "no-cache"}
    )


@app.route("/agent/<agent_name>", methods=["POST"])
def single_agent(agent_name):
    """Query a single specific agent."""
    if agent_name not in AGENTS:
        return jsonify({"error": f"Unknown agent: {agent_name}. Available: {list(AGENTS.keys())}"}), 400
    
    data = request.get_json()
    prompt = data.get("prompt", data.get("message", ""))
    
    if not prompt:
        return jsonify({"error": "No prompt provided"}), 400
    
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)
    try:
        result = loop.run_until_complete(query_agent(agent_name, prompt))
        return jsonify(result)
    finally:
        loop.close()


# ==================
# CLI INTERFACE
# ==================

def cli_mode():
    """Interactive CLI for Neural Uplink."""
    print("\n🧠 Neural Uplink - Multi-Agent AI System")
    print("=" * 50)
    print("Commands:")
    print("  <prompt>     - Query all agents")
    print("  !<agent>     - Query specific agent (dialog/data/ops/world)")
    print("  !help        - Show this help")
    print("  !quit        - Exit")
    print()
    
    while True:
        try:
            user_input = input("Uplink> ").strip()
            
            if not user_input:
                continue
            
            if user_input == "!quit":
                print("Goodbye!")
                break
            
            if user_input == "!help":
                print("\nAgents:")
                for name, agent in AGENTS.items():
                    print(f"  {agent['color']} {agent['name']}: {agent['role']}")
                print()
                continue
            
            # Query specific agent
            if user_input.startswith("!"):
                agent_name = user_input[1:].lower().split()[0]
                prompt = " ".join(user_input.split()[1:]) or "Hello"
                
                if agent_name in AGENTS:
                    loop = asyncio.new_event_loop()
                    result = loop.run_until_complete(query_agent(agent_name, prompt))
                    loop.close()
                    
                    print(f"\n{result.get('color', '⚪')} {result.get('agent')}:")
                    print(f"  {result.get('response', result.get('error', 'No response'))}\n")
                else:
                    print(f"Unknown agent: {agent_name}\n")
                continue
            
            # Query all agents
            print("\nQuerying all agents...")
            loop = asyncio.new_event_loop()
            results = loop.run_until_complete(query_all_agents(user_input))
            loop.close()
            
            print(fuse_responses(results))
            print()
            
        except KeyboardInterrupt:
            print("\nGoodbye!")
            break
        except Exception as e:
            print(f"Error: {e}\n")


if __name__ == "__main__":
    import argparse
    
    parser = argparse.ArgumentParser(description="Neural Uplink - Multi-Agent AI System")
    parser.add_argument("--cli", action="store_true", help="Run in interactive CLI mode")
    parser.add_argument("--port", type=int, default=PORT, help="Port for HTTP API")
    parser.add_argument("--query", type=str, help="Single query and exit")
    args = parser.parse_args()
    
    if args.query:
        # Single query mode
        loop = asyncio.new_event_loop()
        results = loop.run_until_complete(query_all_agents(args.query))
        loop.close()
        print(fuse_responses(results))
    elif args.cli:
        cli_mode()
    else:
        # HTTP API mode
        PORT = args.port
        print(f"\n🧠 Neural Uplink Starting...")
        print(f"   Port: {PORT}")
        print(f"   Model Service: {MODEL_SERVICE}")
        print(f"   Agents: {', '.join(AGENTS.keys())}")
        print(f"\n   API Endpoints:")
        print(f"     POST /uplink          - Query all agents")
        print(f"     POST /uplink/stream   - Streaming response")
        print(f"     POST /agent/<name>    - Query specific agent")
        print(f"     GET  /health          - Health check")
        print()
        
        app.run(host="0.0.0.0", port=PORT, debug=False, threaded=True)