-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremote_model_interface.py
More file actions
405 lines (329 loc) · 14.7 KB
/
remote_model_interface.py
File metadata and controls
405 lines (329 loc) · 14.7 KB
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
#!/usr/bin/env python3
"""
Remote Model Interface for Sarah AI
Handles communication between WebServ and Vast.ai model instances
"""
import os
import json
import asyncio
import uuid
from datetime import datetime
from typing import Dict, List, Optional, Any, Union
import logging
import httpx
from fastapi import FastAPI, HTTPException, Request, WebSocket, WebSocketDisconnect
from pydantic import BaseModel
import subprocess
import signal
import threading
import time
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
class ChatMessage(BaseModel):
role: str # user, assistant, system
content: str
timestamp: str = None
message_id: str = None
class ToolRequest(BaseModel):
tool_id: str
command: str
description: str
risk_level: str # low, medium, high, critical
requires_approval: bool = True
class ToolApproval(BaseModel):
tool_id: str
action: str # grant, grant_permanent, refine, interrupt
refined_command: str = None
user_id: str = None
class ModelChatRequest(BaseModel):
message: str
conversation_id: str = None
system_prompt: str = None
user_id: str = None
model_preference: str = "auto" # auto, qwen, llama, deepseek
class RemoteModelInterface:
def __init__(self):
"""Initialize remote model interface"""
self.active_conversations = {}
self.active_tools = {}
self.user_permissions = {}
self.tool_whitelist = {}
self.running_processes = {}
# WebSocket connections for real-time updates
self.websocket_connections = {}
logger.info("Remote Model Interface initialized")
# =================== Model Communication ===================
async def chat_with_model(self, request: ModelChatRequest) -> Dict[str, Any]:
"""Send chat message to local model"""
try:
conversation_id = request.conversation_id or str(uuid.uuid4())
# Prepare the chat request
model_request = {
"model": "local",
"prompt": self.build_prompt(request),
"max_tokens": 500,
"temperature": 0.7,
"conversation_id": conversation_id
}
# Send to local model service
async with httpx.AsyncClient() as client:
response = await client.post(
"http://localhost:8888/v1/completions",
json=model_request,
timeout=30.0
)
if response.status_code == 200:
model_response = response.json()
# Parse response for tool requests
tool_requests = await self.parse_tool_requests(model_response.get("choices", [{}])[0].get("text", ""))
result = {
"conversation_id": conversation_id,
"response": model_response.get("choices", [{}])[0].get("text", ""),
"model_used": model_response.get("model", "unknown"),
"tool_requests": tool_requests,
"timestamp": datetime.utcnow().isoformat(),
"status": "success"
}
# Store conversation
self.active_conversations[conversation_id] = result
return result
else:
raise HTTPException(status_code=500, detail=f"Model service error: {response.status_code}")
except Exception as e:
logger.error(f"Chat error: {e}")
return {
"error": str(e),
"status": "error",
"timestamp": datetime.utcnow().isoformat()
}
def build_prompt(self, request: ModelChatRequest) -> str:
"""Build comprehensive prompt for model"""
prompt_parts = []
# System prompt (admin/owner only)
if request.system_prompt and self.can_use_system_prompt(request.user_id):
prompt_parts.append(f"SYSTEM: {request.system_prompt}")
# Standard system instructions
prompt_parts.append("""
You are Sarah, an AI assistant with system access tools. You can help users manage their Sarah AI deployment.
Available tools:
- system_command: Execute shell commands (requires approval)
- file_operation: Read/write files (requires approval)
- service_control: Manage system services (requires approval)
- deployment_manager: Handle Star AI deployments (requires approval)
- configuration_manager: Update system configs (requires approval)
When you need to use a tool, format your request as:
TOOL_REQUEST: {tool_name} | {command} | {description} | {risk_level}
Example: TOOL_REQUEST: system_command | systemctl status sarah-ai-webui | Check Sarah AI service status | low
""")
# User message
prompt_parts.append(f"USER: {request.message}")
prompt_parts.append("SARAH:")
return "\n".join(prompt_parts)
async def parse_tool_requests(self, response: str) -> List[ToolRequest]:
"""Parse tool requests from model response"""
tool_requests = []
lines = response.split('\n')
for line in lines:
if line.strip().startswith('TOOL_REQUEST:'):
try:
# Parse: TOOL_REQUEST: tool_name | command | description | risk_level
parts = line.replace('TOOL_REQUEST:', '').split('|')
if len(parts) >= 4:
tool_request = ToolRequest(
tool_id=str(uuid.uuid4()),
command=parts[1].strip(),
description=parts[2].strip(),
risk_level=parts[3].strip(),
requires_approval=self.requires_approval(parts[1].strip())
)
tool_requests.append(tool_request)
self.active_tools[tool_request.tool_id] = tool_request
except Exception as e:
logger.error(f"Failed to parse tool request: {e}")
continue
return tool_requests
# =================== Tool Execution ===================
async def execute_tool(self, approval: ToolApproval) -> Dict[str, Any]:
"""Execute approved tool"""
try:
if approval.tool_id not in self.active_tools:
raise HTTPException(status_code=404, detail="Tool request not found")
tool_request = self.active_tools[approval.tool_id]
if approval.action == "interrupt":
return await self.interrupt_tool(approval.tool_id)
command = approval.refined_command if approval.refined_command else tool_request.command
# Check whitelist for permanent grants
if approval.action == "grant_permanent":
self.add_to_whitelist(approval.user_id, command)
# Execute the tool
result = await self.execute_command(command, approval.tool_id)
# Clean up
if approval.tool_id in self.active_tools:
del self.active_tools[approval.tool_id]
return result
except Exception as e:
logger.error(f"Tool execution error: {e}")
return {
"error": str(e),
"status": "error",
"tool_id": approval.tool_id
}
async def execute_command(self, command: str, tool_id: str) -> Dict[str, Any]:
"""Execute shell command with monitoring"""
try:
logger.info(f"Executing command: {command}")
# Start process
process = await asyncio.create_subprocess_shell(
command,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE
)
self.running_processes[tool_id] = process
start_time = datetime.utcnow()
# Wait for completion
stdout, stderr = await process.communicate()
# Clean up
if tool_id in self.running_processes:
del self.running_processes[tool_id]
end_time = datetime.utcnow()
duration = (end_time - start_time).total_seconds()
result = {
"tool_id": tool_id,
"command": command,
"return_code": process.returncode,
"stdout": stdout.decode('utf-8') if stdout else "",
"stderr": stderr.decode('utf-8') if stderr else "",
"duration": duration,
"start_time": start_time.isoformat(),
"end_time": end_time.isoformat(),
"status": "completed"
}
logger.info(f"Command completed in {duration:.2f}s with return code {process.returncode}")
return result
except Exception as e:
logger.error(f"Command execution failed: {e}")
return {
"tool_id": tool_id,
"error": str(e),
"status": "failed"
}
async def interrupt_tool(self, tool_id: str) -> Dict[str, Any]:
"""Interrupt running tool"""
try:
if tool_id in self.running_processes:
process = self.running_processes[tool_id]
# Try graceful termination first
process.terminate()
# Wait briefly for graceful shutdown
try:
await asyncio.wait_for(process.wait(), timeout=5.0)
except asyncio.TimeoutError:
# Force kill if necessary
process.kill()
await process.wait()
# Clean up
del self.running_processes[tool_id]
return {
"tool_id": tool_id,
"status": "interrupted",
"message": "Tool execution interrupted by user"
}
else:
return {
"tool_id": tool_id,
"status": "not_found",
"message": "No running process found for tool"
}
except Exception as e:
logger.error(f"Failed to interrupt tool: {e}")
return {
"tool_id": tool_id,
"error": str(e),
"status": "error"
}
# =================== Permission Management ===================
def can_use_system_prompt(self, user_id: str) -> bool:
"""Check if user can use system prompts"""
if not user_id:
return False
# Only admin and owner can use system prompts
admin_users = os.getenv("ADMIN_USERS", "admin,owner").split(",")
return user_id.lower() in [u.strip().lower() for u in admin_users]
def requires_approval(self, command: str) -> bool:
"""Determine if command requires approval"""
# Always approve for now - whitelist will handle automatic approvals
dangerous_patterns = [
"rm -rf", "format", "mkfs", "dd if=", ">/dev/",
"shutdown", "reboot", "halt", "init 0", "init 6"
]
command_lower = command.lower()
for pattern in dangerous_patterns:
if pattern in command_lower:
return True
return True # Default to requiring approval
def add_to_whitelist(self, user_id: str, command: str):
"""Add command to user's whitelist"""
if user_id not in self.tool_whitelist:
self.tool_whitelist[user_id] = set()
self.tool_whitelist[user_id].add(command.strip())
logger.info(f"Added to whitelist for {user_id}: {command}")
def is_whitelisted(self, user_id: str, command: str) -> bool:
"""Check if command is whitelisted for user"""
if user_id not in self.tool_whitelist:
return False
return command.strip() in self.tool_whitelist[user_id]
# Global instance
remote_model = RemoteModelInterface()
# FastAPI app for remote endpoints
app = FastAPI(title="Sarah AI Remote Model Interface", version="1.0.0")
@app.post("/api/remote/chat")
async def remote_chat(request: ModelChatRequest):
"""Remote chat endpoint for WebServ"""
return await remote_model.chat_with_model(request)
@app.post("/api/remote/tool/execute")
async def execute_remote_tool(approval: ToolApproval):
"""Execute tool with approval"""
return await remote_model.execute_tool(approval)
@app.get("/api/remote/tools/active")
async def get_active_tools():
"""Get active tool requests"""
return {
"active_tools": list(remote_model.active_tools.values()),
"count": len(remote_model.active_tools)
}
@app.get("/api/remote/conversations/{conversation_id}")
async def get_conversation(conversation_id: str):
"""Get conversation history"""
if conversation_id in remote_model.active_conversations:
return remote_model.active_conversations[conversation_id]
else:
raise HTTPException(status_code=404, detail="Conversation not found")
@app.get("/api/remote/health")
async def remote_health():
"""Health check for remote interface"""
return {
"status": "healthy",
"active_conversations": len(remote_model.active_conversations),
"active_tools": len(remote_model.active_tools),
"running_processes": len(remote_model.running_processes),
"timestamp": datetime.utcnow().isoformat()
}
# WebSocket for real-time updates
@app.websocket("/api/remote/ws/{user_id}")
async def websocket_endpoint(websocket: WebSocket, user_id: str):
"""WebSocket for real-time communication"""
await websocket.accept()
remote_model.websocket_connections[user_id] = websocket
try:
while True:
# Keep connection alive and handle messages
data = await websocket.receive_text()
# Handle any WebSocket messages if needed
except WebSocketDisconnect:
if user_id in remote_model.websocket_connections:
del remote_model.websocket_connections[user_id]
logger.info(f"WebSocket disconnected: {user_id}")
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8889)