File size: 13,147 Bytes
f6ba6be
5ee525a
 
fb2dba2
5ee525a
 
 
 
 
 
fb2dba2
 
 
f6ba6be
fb2dba2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5ee525a
f6ba6be
5ee525a
 
 
 
 
 
f6ba6be
fb2dba2
 
 
 
 
 
f6ba6be
fb2dba2
 
f6ba6be
5ee525a
 
 
 
 
 
f6ba6be
 
fb2dba2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f6ba6be
 
fb2dba2
 
 
 
 
 
5ee525a
f6ba6be
 
 
5ee525a
 
 
fb2dba2
 
 
 
 
 
 
5ee525a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fb2dba2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5ee525a
fb2dba2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5ee525a
fb2dba2
5ee525a
fb2dba2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5ee525a
fb2dba2
 
 
5ee525a
 
 
 
fb2dba2
 
 
 
 
 
 
 
 
 
 
 
 
eaed04c
fb2dba2
 
 
 
 
 
eaed04c
5ee525a
fb2dba2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5ee525a
 
 
 
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
import os
import json
import time
import asyncio
import requests
import uvicorn
from fastapi import FastAPI, Depends, HTTPException, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
from fastapi.responses import StreamingResponse
from contextlib import asynccontextmanager
import subprocess
import shutil

# Check if ollama is available
OLLAMA_AVAILABLE = shutil.which("ollama") is not None

@asynccontextmanager
async def lifespan(app: FastAPI):
    """Startup and shutdown events"""
    if OLLAMA_AVAILABLE:
        print("Starting Ollama service...")
        subprocess.Popen(["ollama", "serve"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
        await asyncio.sleep(3)  # Wait for Ollama to start
        
        # Set keep-alive to prevent model unloading
        os.environ["OLLAMA_KEEP_ALIVE"] = "24h"
        
        # Pull model if needed
        try:
            r = requests.get(f"{OLLAMA_BASE}/api/tags", timeout=5)
            models = [m["name"] for m in r.json().get("models", [])]
            if MODEL not in models:
                print(f"Pulling model {MODEL}...")
                subprocess.run(["ollama", "pull", MODEL], check=False)
        except Exception as e:
            print(f"Warning: Could not check/pull model: {e}")
    
    yield
    
    print("Shutting down...")

app = FastAPI(title="o87Dev Cloud LLM API", lifespan=lifespan)
security = HTTPBearer(auto_error=False)

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_methods=["*"],
    allow_headers=["*"],
)

OLLAMA_BASE = "http://localhost:11434"
MODEL = os.environ.get("DEFAULT_MODEL", "qwen2.5-coder:7b-instruct-q4_K_M")
API_TOKEN = os.environ.get("API_TOKEN", "")
MAX_CTX = int(os.environ.get("MAX_CTX", "4096"))
MAX_OUT = int(os.environ.get("MAX_OUT", "1024"))
TIMEOUT = int(os.environ.get("TIMEOUT", "240"))  # 4 min limit

# Semaphore to limit concurrent requests (prevents OOM)
semaphore = asyncio.Semaphore(1)  # Only 1 request at a time for CPU Spaces

def verify_token(creds: HTTPAuthorizationCredentials = Depends(security)):
    if not API_TOKEN:
        return "no-auth"
    if not creds or creds.credentials != API_TOKEN:
        raise HTTPException(401, "Invalid token")
    return creds.credentials


async def wait_for_ollama(max_retries=10, delay=1):
    """Wait for Ollama to be ready, with retries"""
    for i in range(max_retries):
        try:
            r = requests.get(f"{OLLAMA_BASE}/api/tags", timeout=2)
            if r.status_code == 200:
                return True
        except:
            pass
        await asyncio.sleep(delay)
    return False


async def ensure_model_loaded(model_name: str = None):
    """Pre-load model with a dummy request to force it into memory"""
    model = model_name or MODEL
    try:
        # Check if model is already loaded
        r = requests.get(f"{OLLAMA_BASE}/api/ps", timeout=2)
        loaded = [m.get("model") for m in r.json().get("models", [])]
        if model not in loaded:
            print(f"Pre-loading model {model}...")
            requests.post(
                f"{OLLAMA_BASE}/api/generate",
                json={"model": model, "prompt": "test", "stream": False},
                timeout=30
            )
            print(f"Model {model} loaded")
    except Exception as e:
        print(f"Warning: Could not pre-load model: {e}")


@app.get("/")
async def root():
    return {
        "status": "ok",
        "model": MODEL,
        "max_ctx": MAX_CTX,
        "ollama_available": OLLAMA_AVAILABLE
    }


@app.get("/health")
async def health():
    try:
        r = requests.get(f"{OLLAMA_BASE}/api/tags", timeout=5)
        models = [m["name"] for m in r.json().get("models", [])]
        return {
            "status": "ok" if MODEL in models else "model_missing",
            "model": MODEL,
            "model_available": MODEL in models,
            "available_models": models,
            "max_ctx": MAX_CTX
        }
    except Exception as e:
        return {"status": "starting", "error": str(e)}


@app.get("/v1/models")
async def list_models(token: str = Depends(verify_token)):
    try:
        r = requests.get(f"{OLLAMA_BASE}/api/tags", timeout=5)
        models = [{"id": m["name"], "object": "model"} for m in r.json().get("models", [])]
        return {"object": "list", "data": models}
    except Exception:
        return {"object": "list", "data": [{"id": MODEL, "object": "model"}]}


@app.post("/v1/chat/completions")
async def chat_completions(request: Request, token: str = Depends(verify_token)):
    """OpenAI-compatible endpoint with retries and better error handling"""
    
    # Wait for Ollama to be ready
    if not await wait_for_ollama():
        raise HTTPException(503, "Ollama service not ready")
    
    async with semaphore:
        body = await request.json()
        model = body.get("model", MODEL)
        stream = body.get("stream", False)
        
        # Ensure model is loaded before proceeding
        await ensure_model_loaded(model)
        
        payload = {
            "model": model,
            "messages": body.get("messages", []),
            "stream": stream,
            "options": {
                "num_ctx": MAX_CTX,
                "num_predict": min(body.get("max_tokens", MAX_OUT), MAX_OUT),
                "temperature": body.get("temperature", 0.7),
            }
        }
        
        if stream:
            def generate():
                try:
                    with requests.post(
                        f"{OLLAMA_BASE}/v1/chat/completions",
                        json=payload, 
                        stream=True, 
                        timeout=TIMEOUT
                    ) as r:
                        if r.status_code != 200:
                            error_msg = f"Ollama error: {r.status_code}"
                            yield f"data: {json.dumps({'error': error_msg})}\n\n".encode()
                            yield b"data: [DONE]\n\n"
                            return
                        
                        for chunk in r.iter_content(chunk_size=None):
                            if chunk:
                                yield chunk
                except requests.Timeout:
                    yield f"data: {json.dumps({'error': 'Request timeout - try a shorter prompt'})}\n\n".encode()
                    yield b"data: [DONE]\n\n"
                except Exception as e:
                    yield f"data: {json.dumps({'error': str(e)})}\n\n".encode()
                    yield b"data: [DONE]\n\n"
            
            return StreamingResponse(generate(), media_type="text/event-stream")
        
        # Non-streaming request with retry logic
        max_retries = 2
        for attempt in range(max_retries):
            try:
                r = requests.post(
                    f"{OLLAMA_BASE}/v1/chat/completions",
                    json=payload,
                    timeout=TIMEOUT
                )
                if r.status_code == 200:
                    return r.json()
                elif r.status_code == 404:
                    # Model not found - try to pull it
                    if attempt < max_retries - 1:
                        print(f"Model {model} not found, attempting pull...")
                        subprocess.run(["ollama", "pull", model], check=False)
                        await asyncio.sleep(5)
                        continue
                raise HTTPException(r.status_code, f"Ollama error: {r.text}")
            except requests.Timeout:
                if attempt == max_retries - 1:
                    raise HTTPException(504, "Inference timeout — try a shorter prompt")
                await asyncio.sleep(2)
            except Exception as e:
                if attempt == max_retries - 1:
                    raise HTTPException(500, str(e))
                await asyncio.sleep(2)


@app.post("/v1/messages")
async def messages(request: Request, token: str = Depends(verify_token)):
    """Anthropic-compatible messages endpoint"""
    
    if not await wait_for_ollama():
        raise HTTPException(503, "Ollama service not ready")
    
    async with semaphore:
        body = await request.json()
        model = body.get("model", MODEL)
        stream = body.get("stream", False)
        
        await ensure_model_loaded(model)
        
        payload = {
            "model": model,
            "messages": body.get("messages", []),
            "stream": stream,
            "options": {
                "num_ctx": MAX_CTX,
                "num_predict": min(body.get("max_tokens", MAX_OUT), MAX_OUT),
                "temperature": body.get("temperature", 0.7),
            }
        }
        
        if stream:
            def generate_anthropic():
                msg_id = f"msg_{int(time.time())}"
                yield f"event: message_start\ndata: {json.dumps({'type':'message_start','message':{'id':msg_id,'type':'message','role':'assistant','content':[],'model':model,'stop_reason':None,'usage':{'input_tokens':0,'output_tokens':0}}})}\n\n".encode()
                yield f"event: content_block_start\ndata: {json.dumps({'type':'content_block_start','index':0,'content_block':{'type':'text','text':''}})}\n\n".encode()
                yield b"event: ping\ndata: {\"type\":\"ping\"}\n\n"
                
                out_tokens = 0
                try:
                    with requests.post(
                        f"{OLLAMA_BASE}/v1/chat/completions",
                        json=payload, stream=True, timeout=TIMEOUT
                    ) as r:
                        if r.status_code != 200:
                            yield f"event: content_block_delta\ndata: {json.dumps({'type':'content_block_delta','index':0,'delta':{'type':'text_delta','text':f'Error: Ollama returned {r.status_code}'}})}\n\n".encode()
                        else:
                            buf = ""
                            for chunk in r.iter_content(chunk_size=None):
                                if not chunk:
                                    continue
                                buf += chunk.decode("utf-8", errors="ignore")
                                lines = buf.split("\n")
                                buf = lines.pop()
                                for line in lines:
                                    line = line.strip()
                                    if not line or not line.startswith("data: "):
                                        continue
                                    js = line[6:]
                                    if js == "[DONE]":
                                        break
                                    try:
                                        d = json.loads(js)
                                        if d.get("usage"):
                                            out_tokens = d["usage"].get("completion_tokens", 0)
                                        text = (d.get("choices") or [{}])[0].get("delta", {}).get("content", "")
                                        if text:
                                            yield f"event: content_block_delta\ndata: {json.dumps({'type':'content_block_delta','index':0,'delta':{'type':'text_delta','text':text}})}\n\n".encode()
                                    except:
                                        pass
                except Exception as e:
                    yield f"event: content_block_delta\ndata: {json.dumps({'type':'content_block_delta','index':0,'delta':{'type':'text_delta','text':f'Error: {e}'}})}\n\n".encode()
                
                yield b"event: content_block_stop\ndata: {\"type\":\"content_block_stop\",\"index\":0}\n\n"
                yield f"event: message_delta\ndata: {json.dumps({'type':'message_delta','delta':{'stop_reason':'end_turn','stop_sequence':None},'usage':{'output_tokens':out_tokens}})}\n\n".encode()
                yield b"event: message_stop\ndata: {\"type\":\"message_stop\"}\n\n"
            
            return StreamingResponse(generate_anthropic(), media_type="text/event-stream")
        
        # Non-streaming
        try:
            r = requests.post(f"{OLLAMA_BASE}/v1/chat/completions", json=payload, timeout=TIMEOUT)
            data = r.json()
            content = (data.get("choices") or [{}])[0].get("message", {}).get("content", "")
            return {
                "id": data.get("id", f"msg_{int(time.time())}"),
                "type": "message",
                "role": "assistant",
                "content": [{"type": "text", "text": content}],
                "model": model,
                "stop_reason": "end_turn",
                "usage": {
                    "input_tokens": data.get("usage", {}).get("prompt_tokens", 0),
                    "output_tokens": data.get("usage", {}).get("completion_tokens", 0)
                }
            }
        except requests.Timeout:
            raise HTTPException(504, "Inference timeout — try a shorter prompt")


if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=7860)