main.py 7.37 KB
Newer Older
Timothy J. Baek's avatar
Timothy J. Baek committed
1
from fastapi import FastAPI, Depends, HTTPException
2
3
from fastapi.routing import APIRoute
from fastapi.middleware.cors import CORSMiddleware
Timothy J. Baek's avatar
Timothy J. Baek committed
4

5
import logging
6
from fastapi import FastAPI, Request, Depends, status, Response
Timothy J. Baek's avatar
Timothy J. Baek committed
7
from fastapi.responses import JSONResponse
8
9
10
11

from starlette.middleware.base import BaseHTTPMiddleware, RequestResponseEndpoint
from starlette.responses import StreamingResponse
import json
Timothy J. Baek's avatar
Timothy J. Baek committed
12
import requests
13

Timothy J. Baek's avatar
Timothy J. Baek committed
14
15
16
from pydantic import BaseModel
from typing import Optional, List

17
from utils.utils import get_verified_user, get_current_user, get_admin_user
18
from config import SRC_LOG_LEVELS, ENV
Timothy J. Baek's avatar
Timothy J. Baek committed
19
from constants import ERROR_MESSAGES
20
21
22

log = logging.getLogger(__name__)
log.setLevel(SRC_LOG_LEVELS["LITELLM"])
Timothy J. Baek's avatar
Timothy J. Baek committed
23

24

Timothy J. Baek's avatar
Timothy J. Baek committed
25
from config import MODEL_FILTER_ENABLED, MODEL_FILTER_LIST, DATA_DIR
26
27


28
29
import asyncio
import subprocess
Timothy J. Baek's avatar
Timothy J. Baek committed
30
import yaml
Timothy J. Baek's avatar
Timothy J. Baek committed
31

32
app = FastAPI()
Timothy J. Baek's avatar
Timothy J. Baek committed
33

34
origins = ["*"]
Timothy J. Baek's avatar
Timothy J. Baek committed
35

36
37
38
39
40
41
42
app.add_middleware(
    CORSMiddleware,
    allow_origins=origins,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)
Timothy J. Baek's avatar
Timothy J. Baek committed
43

44

Timothy J. Baek's avatar
Timothy J. Baek committed
45
46
47
48
49
50
51
LITELLM_CONFIG_DIR = f"{DATA_DIR}/litellm/config.yaml"

with open(LITELLM_CONFIG_DIR, "r") as file:
    litellm_config = yaml.safe_load(file)

app.state.CONFIG = litellm_config

52
53
54
# Global variable to store the subprocess reference
background_process = None

Timothy J. Baek's avatar
Timothy J. Baek committed
55

56
57
async def run_background_process(command):
    global background_process
Timothy J. Baek's avatar
Timothy J. Baek committed
58
    log.info("run_background_process")
59
60
61

    try:
        # Log the command to be executed
Timothy J. Baek's avatar
Timothy J. Baek committed
62
        log.info(f"Executing command: {command}")
63
64
65
66
67
        # Execute the command and create a subprocess
        process = await asyncio.create_subprocess_exec(
            *command.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE
        )
        background_process = process
Timothy J. Baek's avatar
Timothy J. Baek committed
68
        log.info("Subprocess started successfully.")
69
70
71
72
73

        # Capture STDERR for debugging purposes
        stderr_output = await process.stderr.read()
        stderr_text = stderr_output.decode().strip()
        if stderr_text:
Timothy J. Baek's avatar
Timothy J. Baek committed
74
            log.info(f"Subprocess STDERR: {stderr_text}")
75

Timothy J. Baek's avatar
Timothy J. Baek committed
76
        # log.info output line by line
77
        async for line in process.stdout:
Timothy J. Baek's avatar
Timothy J. Baek committed
78
            log.info(line.decode().strip())
79
80
81

        # Wait for the process to finish
        returncode = await process.wait()
Timothy J. Baek's avatar
Timothy J. Baek committed
82
        log.info(f"Subprocess exited with return code {returncode}")
83
84
85
    except Exception as e:
        log.error(f"Failed to start subprocess: {e}")
        raise  # Optionally re-raise the exception if you want it to propagate
86
87
88


async def start_litellm_background():
Timothy J. Baek's avatar
Timothy J. Baek committed
89
    log.info("start_litellm_background")
90
    # Command to run in the background
Timothy J. Baek's avatar
Timothy J. Baek committed
91
92
93
    command = (
        "litellm --port 14365 --telemetry False --config ./data/litellm/config.yaml"
    )
Timothy J. Baek's avatar
Timothy J. Baek committed
94

95
    await run_background_process(command)
Timothy J. Baek's avatar
Timothy J. Baek committed
96
97


98
async def shutdown_litellm_background():
Timothy J. Baek's avatar
Timothy J. Baek committed
99
    log.info("shutdown_litellm_background")
100
101
102
103
    global background_process
    if background_process:
        background_process.terminate()
        await background_process.wait()  # Ensure the process has terminated
Timothy J. Baek's avatar
Timothy J. Baek committed
104
        log.info("Subprocess terminated")
105
106


Timothy J. Baek's avatar
Timothy J. Baek committed
107
@app.on_event("startup")
108
async def startup_event():
Timothy J. Baek's avatar
Timothy J. Baek committed
109

Timothy J. Baek's avatar
Timothy J. Baek committed
110
    log.info("startup_event")
Timothy J. Baek's avatar
Timothy J. Baek committed
111
    # TODO: Check config.yaml file and create one
112
    asyncio.create_task(start_litellm_background())
Timothy J. Baek's avatar
Timothy J. Baek committed
113
114


115
116
117
118
app.state.MODEL_FILTER_ENABLED = MODEL_FILTER_ENABLED
app.state.MODEL_FILTER_LIST = MODEL_FILTER_LIST


119
120
121
122
123
@app.get("/")
async def get_status():
    return {"status": True}


Timothy J. Baek's avatar
Timothy J. Baek committed
124
async def restart_litellm():
125
126
127
128
129
130
131
132
133
134
    """
    Endpoint to restart the litellm background service.
    """
    log.info("Requested restart of litellm service.")
    try:
        # Shut down the existing process if it is running
        await shutdown_litellm_background()
        log.info("litellm service shutdown complete.")

        # Restart the background service
Timothy J. Baek's avatar
Timothy J. Baek committed
135
136

        asyncio.create_task(start_litellm_background())
137
138
139
140
141
142
143
        log.info("litellm service restart complete.")

        return {
            "status": "success",
            "message": "litellm service restarted successfully.",
        }
    except Exception as e:
Timothy J. Baek's avatar
Timothy J. Baek committed
144
        log.info(f"Error restarting litellm service: {e}")
145
146
147
148
149
        raise HTTPException(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(e)
        )


Timothy J. Baek's avatar
Timothy J. Baek committed
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
@app.get("/restart")
async def restart_litellm_handler(user=Depends(get_admin_user)):
    return await restart_litellm()


@app.get("/config")
async def get_config(user=Depends(get_admin_user)):
    return app.state.CONFIG


class LiteLLMConfigForm(BaseModel):
    general_settings: Optional[dict] = None
    litellm_settings: Optional[dict] = None
    model_list: Optional[List[dict]] = None
    router_settings: Optional[dict] = None


@app.post("/config/update")
async def update_config(form_data: LiteLLMConfigForm, user=Depends(get_admin_user)):
    app.state.CONFIG = form_data.model_dump(exclude_none=True)

    with open(LITELLM_CONFIG_DIR, "w") as file:
        yaml.dump(app.state.CONFIG, file)

    await restart_litellm()
    return app.state.CONFIG


Timothy J. Baek's avatar
Timothy J. Baek committed
178
179
180
@app.get("/models")
@app.get("/v1/models")
async def get_models(user=Depends(get_current_user)):
Timothy J. Baek's avatar
Timothy J. Baek committed
181
    url = "http://localhost:14365/v1"
Timothy J. Baek's avatar
Timothy J. Baek committed
182
183
184
185
    r = None
    try:
        r = requests.request(method="GET", url=f"{url}/models")
        r.raise_for_status()
186

Timothy J. Baek's avatar
Timothy J. Baek committed
187
        data = r.json()
188

Timothy J. Baek's avatar
Timothy J. Baek committed
189
190
191
192
193
194
195
196
        if app.state.MODEL_FILTER_ENABLED:
            if user and user.role == "user":
                data["data"] = list(
                    filter(
                        lambda model: model["id"] in app.state.MODEL_FILTER_LIST,
                        data["data"],
                    )
                )
197

Timothy J. Baek's avatar
Timothy J. Baek committed
198
199
200
201
202
203
204
205
206
207
208
        return data
    except Exception as e:
        log.exception(e)
        error_detail = "Open WebUI: Server Connection Error"
        if r is not None:
            try:
                res = r.json()
                if "error" in res:
                    error_detail = f"External: {res['error']}"
            except:
                error_detail = f"External: {e}"
209

Timothy J. Baek's avatar
Timothy J. Baek committed
210
211
212
213
        raise HTTPException(
            status_code=r.status_code if r else 500,
            detail=error_detail,
        )
214
215


Timothy J. Baek's avatar
Timothy J. Baek committed
216
217
218
@app.api_route("/{path:path}", methods=["GET", "POST", "PUT", "DELETE"])
async def proxy(path: str, request: Request, user=Depends(get_verified_user)):
    body = await request.body()
219

Timothy J. Baek's avatar
Timothy J. Baek committed
220
    url = "http://localhost:14365"
221

Timothy J. Baek's avatar
Timothy J. Baek committed
222
    target_url = f"{url}/{path}"
223

Timothy J. Baek's avatar
Timothy J. Baek committed
224
225
226
    headers = {}
    # headers["Authorization"] = f"Bearer {key}"
    headers["Content-Type"] = "application/json"
227

Timothy J. Baek's avatar
Timothy J. Baek committed
228
    r = None
229

Timothy J. Baek's avatar
Timothy J. Baek committed
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
    try:
        r = requests.request(
            method=request.method,
            url=target_url,
            data=body,
            headers=headers,
            stream=True,
        )

        r.raise_for_status()

        # Check if response is SSE
        if "text/event-stream" in r.headers.get("Content-Type", ""):
            return StreamingResponse(
                r.iter_content(chunk_size=8192),
                status_code=r.status_code,
                headers=dict(r.headers),
            )
        else:
            response_data = r.json()
            return response_data
    except Exception as e:
        log.exception(e)
        error_detail = "Open WebUI: Server Connection Error"
        if r is not None:
            try:
                res = r.json()
                if "error" in res:
                    error_detail = f"External: {res['error']['message'] if 'message' in res['error'] else res['error']}"
            except:
                error_detail = f"External: {e}"

        raise HTTPException(
            status_code=r.status_code if r else 500, detail=error_detail
        )