Upload rml_ai/server.py with huggingface_hub
Browse files- rml_ai/server.py +144 -0
rml_ai/server.py
ADDED
@@ -0,0 +1,144 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
FastAPI Server for RML System
|
3 |
+
"""
|
4 |
+
|
5 |
+
import time
|
6 |
+
from typing import Dict, Any
|
7 |
+
from fastapi import FastAPI, HTTPException
|
8 |
+
from fastapi.middleware.cors import CORSMiddleware
|
9 |
+
from pydantic import BaseModel
|
10 |
+
import uvicorn
|
11 |
+
|
12 |
+
from .core import RMLSystem, RMLResponse
|
13 |
+
from .config import RMLConfig
|
14 |
+
|
15 |
+
|
16 |
+
# Request/Response models
|
17 |
+
class ChatRequest(BaseModel):
|
18 |
+
message: str
|
19 |
+
|
20 |
+
class ChatResponse(BaseModel):
|
21 |
+
answer: str
|
22 |
+
response_ms: float
|
23 |
+
|
24 |
+
class HealthResponse(BaseModel):
|
25 |
+
status: str
|
26 |
+
timestamp: float
|
27 |
+
|
28 |
+
class ReadyResponse(BaseModel):
|
29 |
+
ready: bool
|
30 |
+
entries: int
|
31 |
+
device: str
|
32 |
+
|
33 |
+
|
34 |
+
# Initialize FastAPI app
|
35 |
+
app = FastAPI(
|
36 |
+
title="RML-AI API",
|
37 |
+
description="Resonant Memory Learning AI System API",
|
38 |
+
version="0.1.0",
|
39 |
+
docs_url="/docs",
|
40 |
+
redoc_url="/redoc"
|
41 |
+
)
|
42 |
+
|
43 |
+
# Add CORS middleware
|
44 |
+
app.add_middleware(
|
45 |
+
CORSMiddleware,
|
46 |
+
allow_origins=["*"],
|
47 |
+
allow_credentials=True,
|
48 |
+
allow_methods=["*"],
|
49 |
+
allow_headers=["*"],
|
50 |
+
)
|
51 |
+
|
52 |
+
# Global RML system instance
|
53 |
+
rml_system: RMLSystem = None
|
54 |
+
|
55 |
+
|
56 |
+
@app.on_event("startup")
|
57 |
+
async def startup_event():
|
58 |
+
"""Initialize RML system on startup"""
|
59 |
+
global rml_system
|
60 |
+
|
61 |
+
print("Initializing RML system...")
|
62 |
+
config = RMLConfig()
|
63 |
+
print(f"Configuration: {config}")
|
64 |
+
|
65 |
+
try:
|
66 |
+
rml_system = RMLSystem(config)
|
67 |
+
print("RML system initialized successfully!")
|
68 |
+
except Exception as e:
|
69 |
+
print(f"Error initializing RML system: {e}")
|
70 |
+
raise e
|
71 |
+
|
72 |
+
|
73 |
+
@app.get("/", response_model=Dict[str, str])
|
74 |
+
async def root():
|
75 |
+
"""Root endpoint"""
|
76 |
+
return {
|
77 |
+
"message": "RML-AI API",
|
78 |
+
"description": "Resonant Memory Learning AI System",
|
79 |
+
"version": "0.1.0",
|
80 |
+
"docs": "/docs"
|
81 |
+
}
|
82 |
+
|
83 |
+
|
84 |
+
@app.get("/health", response_model=HealthResponse)
|
85 |
+
async def health_check():
|
86 |
+
"""Health check endpoint"""
|
87 |
+
return HealthResponse(
|
88 |
+
status="healthy",
|
89 |
+
timestamp=time.time()
|
90 |
+
)
|
91 |
+
|
92 |
+
|
93 |
+
@app.get("/ready", response_model=ReadyResponse)
|
94 |
+
async def ready_check():
|
95 |
+
"""Ready check endpoint"""
|
96 |
+
if rml_system is None:
|
97 |
+
raise HTTPException(status_code=503, detail="RML system not initialized")
|
98 |
+
|
99 |
+
stats = rml_system.memory.get_stats()
|
100 |
+
return ReadyResponse(
|
101 |
+
ready=True,
|
102 |
+
entries=stats['total_entries'],
|
103 |
+
device=rml_system.config.device
|
104 |
+
)
|
105 |
+
|
106 |
+
|
107 |
+
@app.post("/chat", response_model=ChatResponse)
|
108 |
+
async def chat(request: ChatRequest):
|
109 |
+
"""Chat endpoint for RML queries"""
|
110 |
+
if rml_system is None:
|
111 |
+
raise HTTPException(status_code=503, detail="RML system not initialized")
|
112 |
+
|
113 |
+
try:
|
114 |
+
response = rml_system.query(request.message)
|
115 |
+
return ChatResponse(
|
116 |
+
answer=response.answer,
|
117 |
+
response_ms=response.response_ms
|
118 |
+
)
|
119 |
+
except Exception as e:
|
120 |
+
raise HTTPException(status_code=500, detail=f"Error processing query: {str(e)}")
|
121 |
+
|
122 |
+
|
123 |
+
@app.get("/config")
|
124 |
+
async def get_config():
|
125 |
+
"""Get current configuration"""
|
126 |
+
if rml_system is None:
|
127 |
+
raise HTTPException(status_code=503, detail="RML system not initialized")
|
128 |
+
|
129 |
+
return rml_system.config.to_dict()
|
130 |
+
|
131 |
+
|
132 |
+
def main():
|
133 |
+
"""Main function to run the server"""
|
134 |
+
uvicorn.run(
|
135 |
+
"rml_ai.server:app",
|
136 |
+
host="127.0.0.1",
|
137 |
+
port=8000,
|
138 |
+
reload=False,
|
139 |
+
log_level="info"
|
140 |
+
)
|
141 |
+
|
142 |
+
|
143 |
+
if __name__ == "__main__":
|
144 |
+
main()
|