| 
									
										
										
										
											2024-12-12 10:16:07 +08:00
										 |  |  | from fastapi import ( | 
					
						
							|  |  |  |     Depends, | 
					
						
							|  |  |  |     FastAPI, | 
					
						
							|  |  |  |     File, | 
					
						
							|  |  |  |     Form, | 
					
						
							|  |  |  |     HTTPException, | 
					
						
							|  |  |  |     Request, | 
					
						
							|  |  |  |     UploadFile, | 
					
						
							|  |  |  |     status, | 
					
						
							|  |  |  |     APIRouter, | 
					
						
							|  |  |  | ) | 
					
						
							| 
									
										
										
										
											2025-02-16 14:25:18 +08:00
										 |  |  | import aiohttp | 
					
						
							| 
									
										
										
										
											2024-12-12 10:16:07 +08:00
										 |  |  | import os | 
					
						
							|  |  |  | import logging | 
					
						
							|  |  |  | import shutil | 
					
						
							|  |  |  | import requests | 
					
						
							| 
									
										
										
										
											2024-12-10 16:00:01 +08:00
										 |  |  | from pydantic import BaseModel | 
					
						
							|  |  |  | from starlette.responses import FileResponse | 
					
						
							| 
									
										
										
										
											2024-12-12 10:16:07 +08:00
										 |  |  | from typing import Optional | 
					
						
							| 
									
										
										
										
											2024-12-10 16:00:01 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-15 03:33:52 +08:00
										 |  |  | from open_webui.env import SRC_LOG_LEVELS, AIOHTTP_CLIENT_SESSION_SSL | 
					
						
							| 
									
										
										
										
											2024-12-12 10:16:07 +08:00
										 |  |  | from open_webui.config import CACHE_DIR | 
					
						
							| 
									
										
										
										
											2024-12-10 16:00:01 +08:00
										 |  |  | from open_webui.constants import ERROR_MESSAGES | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-12 10:16:07 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | from open_webui.routers.openai import get_all_models_responses | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-10 16:00:01 +08:00
										 |  |  | from open_webui.utils.auth import get_admin_user | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-12 10:16:07 +08:00
										 |  |  | log = logging.getLogger(__name__) | 
					
						
							|  |  |  | log.setLevel(SRC_LOG_LEVELS["MAIN"]) | 
					
						
							| 
									
										
										
										
											2024-12-10 16:00:01 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-12 11:52:46 +08:00
										 |  |  | ################################## | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # Pipeline Middleware | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | ################################## | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def get_sorted_filters(model_id, models): | 
					
						
							|  |  |  |     filters = [ | 
					
						
							|  |  |  |         model | 
					
						
							|  |  |  |         for model in models.values() | 
					
						
							|  |  |  |         if "pipeline" in model | 
					
						
							|  |  |  |         and "type" in model["pipeline"] | 
					
						
							|  |  |  |         and model["pipeline"]["type"] == "filter" | 
					
						
							|  |  |  |         and ( | 
					
						
							|  |  |  |             model["pipeline"]["pipelines"] == ["*"] | 
					
						
							|  |  |  |             or any( | 
					
						
							|  |  |  |                 model_id == target_model_id | 
					
						
							|  |  |  |                 for target_model_id in model["pipeline"]["pipelines"] | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |     ] | 
					
						
							|  |  |  |     sorted_filters = sorted(filters, key=lambda x: x["pipeline"]["priority"]) | 
					
						
							|  |  |  |     return sorted_filters | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-16 14:25:18 +08:00
										 |  |  | async def process_pipeline_inlet_filter(request, payload, user, models): | 
					
						
							| 
									
										
										
										
											2024-12-12 11:52:46 +08:00
										 |  |  |     user = {"id": user.id, "email": user.email, "name": user.name, "role": user.role} | 
					
						
							|  |  |  |     model_id = payload["model"] | 
					
						
							|  |  |  |     sorted_filters = get_sorted_filters(model_id, models) | 
					
						
							|  |  |  |     model = models[model_id] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if "pipeline" in model: | 
					
						
							|  |  |  |         sorted_filters.append(model) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-28 20:47:34 +08:00
										 |  |  |     async with aiohttp.ClientSession(trust_env=True) as session: | 
					
						
							| 
									
										
										
										
											2025-02-16 14:25:18 +08:00
										 |  |  |         for filter in sorted_filters: | 
					
						
							|  |  |  |             urlIdx = filter.get("urlIdx") | 
					
						
							| 
									
										
										
										
											2025-05-15 16:58:44 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |             try: | 
					
						
							|  |  |  |                 urlIdx = int(urlIdx) | 
					
						
							|  |  |  |             except: | 
					
						
							| 
									
										
										
										
											2025-02-16 14:25:18 +08:00
										 |  |  |                 continue | 
					
						
							| 
									
										
										
										
											2024-12-12 11:52:46 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |             url = request.app.state.config.OPENAI_API_BASE_URLS[urlIdx] | 
					
						
							|  |  |  |             key = request.app.state.config.OPENAI_API_KEYS[urlIdx] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-16 14:25:18 +08:00
										 |  |  |             if not key: | 
					
						
							| 
									
										
										
										
											2024-12-12 11:52:46 +08:00
										 |  |  |                 continue | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             headers = {"Authorization": f"Bearer {key}"} | 
					
						
							| 
									
										
										
										
											2025-02-16 14:25:18 +08:00
										 |  |  |             request_data = { | 
					
						
							|  |  |  |                 "user": user, | 
					
						
							|  |  |  |                 "body": payload, | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             try: | 
					
						
							|  |  |  |                 async with session.post( | 
					
						
							|  |  |  |                     f"{url}/{filter['id']}/filter/inlet", | 
					
						
							|  |  |  |                     headers=headers, | 
					
						
							|  |  |  |                     json=request_data, | 
					
						
							| 
									
										
										
										
											2025-05-15 03:33:52 +08:00
										 |  |  |                     ssl=AIOHTTP_CLIENT_SESSION_SSL, | 
					
						
							| 
									
										
										
										
											2025-02-16 14:25:18 +08:00
										 |  |  |                 ) as response: | 
					
						
							|  |  |  |                     payload = await response.json() | 
					
						
							| 
									
										
										
										
											2025-03-20 23:20:27 +08:00
										 |  |  |                     response.raise_for_status() | 
					
						
							| 
									
										
										
										
											2025-02-16 14:25:18 +08:00
										 |  |  |             except aiohttp.ClientResponseError as e: | 
					
						
							|  |  |  |                 res = ( | 
					
						
							|  |  |  |                     await response.json() | 
					
						
							|  |  |  |                     if response.content_type == "application/json" | 
					
						
							|  |  |  |                     else {} | 
					
						
							|  |  |  |                 ) | 
					
						
							| 
									
										
										
										
											2024-12-12 11:52:46 +08:00
										 |  |  |                 if "detail" in res: | 
					
						
							| 
									
										
										
										
											2025-02-16 14:25:18 +08:00
										 |  |  |                     raise Exception(response.status, res["detail"]) | 
					
						
							|  |  |  |             except Exception as e: | 
					
						
							| 
									
										
										
										
											2025-02-25 22:36:25 +08:00
										 |  |  |                 log.exception(f"Connection error: {e}") | 
					
						
							| 
									
										
										
										
											2024-12-12 11:52:46 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return payload | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-16 14:25:18 +08:00
										 |  |  | async def process_pipeline_outlet_filter(request, payload, user, models): | 
					
						
							| 
									
										
										
										
											2024-12-12 11:52:46 +08:00
										 |  |  |     user = {"id": user.id, "email": user.email, "name": user.name, "role": user.role} | 
					
						
							|  |  |  |     model_id = payload["model"] | 
					
						
							|  |  |  |     sorted_filters = get_sorted_filters(model_id, models) | 
					
						
							|  |  |  |     model = models[model_id] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if "pipeline" in model: | 
					
						
							|  |  |  |         sorted_filters = [model] + sorted_filters | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-28 20:47:34 +08:00
										 |  |  |     async with aiohttp.ClientSession(trust_env=True) as session: | 
					
						
							| 
									
										
										
										
											2025-02-16 14:25:18 +08:00
										 |  |  |         for filter in sorted_filters: | 
					
						
							|  |  |  |             urlIdx = filter.get("urlIdx") | 
					
						
							| 
									
										
										
										
											2025-05-15 16:58:44 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |             try: | 
					
						
							|  |  |  |                 urlIdx = int(urlIdx) | 
					
						
							|  |  |  |             except: | 
					
						
							| 
									
										
										
										
											2025-02-16 14:25:18 +08:00
										 |  |  |                 continue | 
					
						
							| 
									
										
										
										
											2024-12-12 11:52:46 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |             url = request.app.state.config.OPENAI_API_BASE_URLS[urlIdx] | 
					
						
							|  |  |  |             key = request.app.state.config.OPENAI_API_KEYS[urlIdx] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-16 14:25:18 +08:00
										 |  |  |             if not key: | 
					
						
							|  |  |  |                 continue | 
					
						
							| 
									
										
										
										
											2024-12-12 11:52:46 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-16 14:25:18 +08:00
										 |  |  |             headers = {"Authorization": f"Bearer {key}"} | 
					
						
							|  |  |  |             request_data = { | 
					
						
							|  |  |  |                 "user": user, | 
					
						
							|  |  |  |                 "body": payload, | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2024-12-12 11:52:46 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-16 14:25:18 +08:00
										 |  |  |             try: | 
					
						
							|  |  |  |                 async with session.post( | 
					
						
							|  |  |  |                     f"{url}/{filter['id']}/filter/outlet", | 
					
						
							|  |  |  |                     headers=headers, | 
					
						
							|  |  |  |                     json=request_data, | 
					
						
							| 
									
										
										
										
											2025-05-15 03:33:52 +08:00
										 |  |  |                     ssl=AIOHTTP_CLIENT_SESSION_SSL, | 
					
						
							| 
									
										
										
										
											2025-02-16 14:25:18 +08:00
										 |  |  |                 ) as response: | 
					
						
							|  |  |  |                     payload = await response.json() | 
					
						
							| 
									
										
										
										
											2025-03-20 23:20:27 +08:00
										 |  |  |                     response.raise_for_status() | 
					
						
							| 
									
										
										
										
											2025-02-16 14:25:18 +08:00
										 |  |  |             except aiohttp.ClientResponseError as e: | 
					
						
							| 
									
										
										
										
											2024-12-12 11:52:46 +08:00
										 |  |  |                 try: | 
					
						
							| 
									
										
										
										
											2025-02-16 14:25:18 +08:00
										 |  |  |                     res = ( | 
					
						
							|  |  |  |                         await response.json() | 
					
						
							|  |  |  |                         if "application/json" in response.content_type | 
					
						
							|  |  |  |                         else {} | 
					
						
							|  |  |  |                     ) | 
					
						
							| 
									
										
										
										
											2024-12-12 11:52:46 +08:00
										 |  |  |                     if "detail" in res: | 
					
						
							| 
									
										
										
										
											2025-02-16 14:25:18 +08:00
										 |  |  |                         raise Exception(response.status, res) | 
					
						
							| 
									
										
										
										
											2024-12-12 11:52:46 +08:00
										 |  |  |                 except Exception: | 
					
						
							|  |  |  |                     pass | 
					
						
							| 
									
										
										
										
											2025-02-16 14:25:18 +08:00
										 |  |  |             except Exception as e: | 
					
						
							| 
									
										
										
										
											2025-02-25 22:36:25 +08:00
										 |  |  |                 log.exception(f"Connection error: {e}") | 
					
						
							| 
									
										
										
										
											2024-12-12 11:52:46 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return payload | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-10 16:54:13 +08:00
										 |  |  | ################################## | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # Pipelines Endpoints | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | ################################## | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-12 10:16:07 +08:00
										 |  |  | router = APIRouter() | 
					
						
							| 
									
										
										
										
											2024-12-10 16:54:13 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-12 11:52:46 +08:00
										 |  |  | @router.get("/list") | 
					
						
							| 
									
										
										
										
											2024-12-12 10:16:07 +08:00
										 |  |  | async def get_pipelines_list(request: Request, user=Depends(get_admin_user)): | 
					
						
							| 
									
										
										
										
											2025-02-26 03:11:36 +08:00
										 |  |  |     responses = await get_all_models_responses(request, user) | 
					
						
							| 
									
										
										
										
											2024-12-10 16:54:13 +08:00
										 |  |  |     log.debug(f"get_pipelines_list: get_openai_models_responses returned {responses}") | 
					
						
							| 
									
										
										
										
											2024-12-12 10:16:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-10 16:54:13 +08:00
										 |  |  |     urlIdxs = [ | 
					
						
							|  |  |  |         idx | 
					
						
							|  |  |  |         for idx, response in enumerate(responses) | 
					
						
							|  |  |  |         if response is not None and "pipelines" in response | 
					
						
							|  |  |  |     ] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return { | 
					
						
							|  |  |  |         "data": [ | 
					
						
							|  |  |  |             { | 
					
						
							| 
									
										
										
										
											2024-12-12 10:16:07 +08:00
										 |  |  |                 "url": request.app.state.config.OPENAI_API_BASE_URLS[urlIdx], | 
					
						
							| 
									
										
										
										
											2024-12-10 16:54:13 +08:00
										 |  |  |                 "idx": urlIdx, | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             for urlIdx in urlIdxs | 
					
						
							|  |  |  |         ] | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-12 11:52:46 +08:00
										 |  |  | @router.post("/upload") | 
					
						
							| 
									
										
										
										
											2024-12-10 16:54:13 +08:00
										 |  |  | async def upload_pipeline( | 
					
						
							| 
									
										
										
										
											2024-12-12 10:16:07 +08:00
										 |  |  |     request: Request, | 
					
						
							|  |  |  |     urlIdx: int = Form(...), | 
					
						
							|  |  |  |     file: UploadFile = File(...), | 
					
						
							|  |  |  |     user=Depends(get_admin_user), | 
					
						
							| 
									
										
										
										
											2024-12-10 16:00:01 +08:00
										 |  |  | ): | 
					
						
							| 
									
										
										
										
											2025-02-25 22:36:25 +08:00
										 |  |  |     log.info(f"upload_pipeline: urlIdx={urlIdx}, filename={file.filename}") | 
					
						
							| 
									
										
										
										
											2025-05-14 19:15:05 +08:00
										 |  |  |     filename = os.path.basename(file.filename) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-10 16:54:13 +08:00
										 |  |  |     # Check if the uploaded file is a python file | 
					
						
							| 
									
										
										
										
											2025-05-14 19:15:05 +08:00
										 |  |  |     if not (filename and filename.endswith(".py")): | 
					
						
							| 
									
										
										
										
											2024-12-10 16:54:13 +08:00
										 |  |  |         raise HTTPException( | 
					
						
							|  |  |  |             status_code=status.HTTP_400_BAD_REQUEST, | 
					
						
							|  |  |  |             detail="Only Python (.py) files are allowed.", | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     upload_folder = f"{CACHE_DIR}/pipelines" | 
					
						
							|  |  |  |     os.makedirs(upload_folder, exist_ok=True) | 
					
						
							| 
									
										
										
										
											2025-05-14 19:15:05 +08:00
										 |  |  |     file_path = os.path.join(upload_folder, filename) | 
					
						
							| 
									
										
										
										
											2024-12-10 16:54:13 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     r = None | 
					
						
							|  |  |  |     try: | 
					
						
							|  |  |  |         # Save the uploaded file | 
					
						
							|  |  |  |         with open(file_path, "wb") as buffer: | 
					
						
							|  |  |  |             shutil.copyfileobj(file.file, buffer) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-12 10:16:07 +08:00
										 |  |  |         url = request.app.state.config.OPENAI_API_BASE_URLS[urlIdx] | 
					
						
							|  |  |  |         key = request.app.state.config.OPENAI_API_KEYS[urlIdx] | 
					
						
							| 
									
										
										
										
											2024-12-10 16:54:13 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         with open(file_path, "rb") as f: | 
					
						
							|  |  |  |             files = {"file": f} | 
					
						
							| 
									
										
										
										
											2024-12-12 10:16:07 +08:00
										 |  |  |             r = requests.post( | 
					
						
							|  |  |  |                 f"{url}/pipelines/upload", | 
					
						
							|  |  |  |                 headers={"Authorization": f"Bearer {key}"}, | 
					
						
							|  |  |  |                 files=files, | 
					
						
							|  |  |  |             ) | 
					
						
							| 
									
										
										
										
											2024-12-10 16:54:13 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         r.raise_for_status() | 
					
						
							|  |  |  |         data = r.json() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return {**data} | 
					
						
							|  |  |  |     except Exception as e: | 
					
						
							|  |  |  |         # Handle connection error here | 
					
						
							| 
									
										
										
										
											2025-02-25 22:36:25 +08:00
										 |  |  |         log.exception(f"Connection error: {e}") | 
					
						
							| 
									
										
										
										
											2024-12-10 16:00:01 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-12 10:16:07 +08:00
										 |  |  |         detail = None | 
					
						
							| 
									
										
										
										
											2024-12-10 16:54:13 +08:00
										 |  |  |         status_code = status.HTTP_404_NOT_FOUND | 
					
						
							|  |  |  |         if r is not None: | 
					
						
							|  |  |  |             status_code = r.status_code | 
					
						
							|  |  |  |             try: | 
					
						
							|  |  |  |                 res = r.json() | 
					
						
							|  |  |  |                 if "detail" in res: | 
					
						
							|  |  |  |                     detail = res["detail"] | 
					
						
							|  |  |  |             except Exception: | 
					
						
							|  |  |  |                 pass | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         raise HTTPException( | 
					
						
							|  |  |  |             status_code=status_code, | 
					
						
							| 
									
										
										
										
											2024-12-12 10:16:07 +08:00
										 |  |  |             detail=detail if detail else "Pipeline not found", | 
					
						
							| 
									
										
										
										
											2024-12-10 16:54:13 +08:00
										 |  |  |         ) | 
					
						
							|  |  |  |     finally: | 
					
						
							|  |  |  |         # Ensure the file is deleted after the upload is completed or on failure | 
					
						
							|  |  |  |         if os.path.exists(file_path): | 
					
						
							|  |  |  |             os.remove(file_path) | 
					
						
							| 
									
										
										
										
											2024-12-10 16:00:01 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-10 16:54:13 +08:00
										 |  |  | class AddPipelineForm(BaseModel): | 
					
						
							|  |  |  |     url: str | 
					
						
							|  |  |  |     urlIdx: int | 
					
						
							| 
									
										
										
										
											2024-12-10 16:00:01 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-10 16:54:13 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-12 11:52:46 +08:00
										 |  |  | @router.post("/add") | 
					
						
							| 
									
										
										
										
											2024-12-12 10:16:07 +08:00
										 |  |  | async def add_pipeline( | 
					
						
							|  |  |  |     request: Request, form_data: AddPipelineForm, user=Depends(get_admin_user) | 
					
						
							|  |  |  | ): | 
					
						
							| 
									
										
										
										
											2024-12-10 16:54:13 +08:00
										 |  |  |     r = None | 
					
						
							| 
									
										
										
										
											2024-12-10 16:00:01 +08:00
										 |  |  |     try: | 
					
						
							| 
									
										
										
										
											2024-12-10 16:54:13 +08:00
										 |  |  |         urlIdx = form_data.urlIdx | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-12 10:16:07 +08:00
										 |  |  |         url = request.app.state.config.OPENAI_API_BASE_URLS[urlIdx] | 
					
						
							|  |  |  |         key = request.app.state.config.OPENAI_API_KEYS[urlIdx] | 
					
						
							| 
									
										
										
										
											2024-12-10 16:54:13 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         r = requests.post( | 
					
						
							| 
									
										
										
										
											2024-12-12 10:16:07 +08:00
										 |  |  |             f"{url}/pipelines/add", | 
					
						
							|  |  |  |             headers={"Authorization": f"Bearer {key}"}, | 
					
						
							|  |  |  |             json={"url": form_data.url}, | 
					
						
							| 
									
										
										
										
											2024-12-10 16:54:13 +08:00
										 |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         r.raise_for_status() | 
					
						
							|  |  |  |         data = r.json() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return {**data} | 
					
						
							| 
									
										
										
										
											2024-12-10 16:00:01 +08:00
										 |  |  |     except Exception as e: | 
					
						
							| 
									
										
										
										
											2024-12-10 16:54:13 +08:00
										 |  |  |         # Handle connection error here | 
					
						
							| 
									
										
										
										
											2025-02-25 22:36:25 +08:00
										 |  |  |         log.exception(f"Connection error: {e}") | 
					
						
							| 
									
										
										
										
											2024-12-10 16:54:13 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-12 10:16:07 +08:00
										 |  |  |         detail = None | 
					
						
							| 
									
										
										
										
											2024-12-10 16:54:13 +08:00
										 |  |  |         if r is not None: | 
					
						
							|  |  |  |             try: | 
					
						
							|  |  |  |                 res = r.json() | 
					
						
							|  |  |  |                 if "detail" in res: | 
					
						
							|  |  |  |                     detail = res["detail"] | 
					
						
							|  |  |  |             except Exception: | 
					
						
							|  |  |  |                 pass | 
					
						
							| 
									
										
										
										
											2024-12-10 16:00:01 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-10 16:54:13 +08:00
										 |  |  |         raise HTTPException( | 
					
						
							|  |  |  |             status_code=(r.status_code if r is not None else status.HTTP_404_NOT_FOUND), | 
					
						
							| 
									
										
										
										
											2024-12-12 10:16:07 +08:00
										 |  |  |             detail=detail if detail else "Pipeline not found", | 
					
						
							| 
									
										
										
										
											2024-12-10 16:54:13 +08:00
										 |  |  |         ) | 
					
						
							| 
									
										
										
										
											2024-12-10 16:00:01 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-10 16:54:13 +08:00
										 |  |  | class DeletePipelineForm(BaseModel): | 
					
						
							|  |  |  |     id: str | 
					
						
							|  |  |  |     urlIdx: int | 
					
						
							| 
									
										
										
										
											2024-12-10 16:00:01 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-12 11:52:46 +08:00
										 |  |  | @router.delete("/delete") | 
					
						
							| 
									
										
										
										
											2024-12-12 10:16:07 +08:00
										 |  |  | async def delete_pipeline( | 
					
						
							|  |  |  |     request: Request, form_data: DeletePipelineForm, user=Depends(get_admin_user) | 
					
						
							|  |  |  | ): | 
					
						
							| 
									
										
										
										
											2024-12-10 16:54:13 +08:00
										 |  |  |     r = None | 
					
						
							|  |  |  |     try: | 
					
						
							|  |  |  |         urlIdx = form_data.urlIdx | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-12 10:16:07 +08:00
										 |  |  |         url = request.app.state.config.OPENAI_API_BASE_URLS[urlIdx] | 
					
						
							|  |  |  |         key = request.app.state.config.OPENAI_API_KEYS[urlIdx] | 
					
						
							| 
									
										
										
										
											2024-12-10 16:54:13 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         r = requests.delete( | 
					
						
							| 
									
										
										
										
											2024-12-12 10:16:07 +08:00
										 |  |  |             f"{url}/pipelines/delete", | 
					
						
							|  |  |  |             headers={"Authorization": f"Bearer {key}"}, | 
					
						
							|  |  |  |             json={"id": form_data.id}, | 
					
						
							| 
									
										
										
										
											2024-12-10 16:54:13 +08:00
										 |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         r.raise_for_status() | 
					
						
							|  |  |  |         data = r.json() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return {**data} | 
					
						
							|  |  |  |     except Exception as e: | 
					
						
							|  |  |  |         # Handle connection error here | 
					
						
							| 
									
										
										
										
											2025-02-25 22:36:25 +08:00
										 |  |  |         log.exception(f"Connection error: {e}") | 
					
						
							| 
									
										
										
										
											2024-12-10 16:54:13 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-12 10:16:07 +08:00
										 |  |  |         detail = None | 
					
						
							| 
									
										
										
										
											2024-12-10 16:54:13 +08:00
										 |  |  |         if r is not None: | 
					
						
							|  |  |  |             try: | 
					
						
							|  |  |  |                 res = r.json() | 
					
						
							|  |  |  |                 if "detail" in res: | 
					
						
							|  |  |  |                     detail = res["detail"] | 
					
						
							|  |  |  |             except Exception: | 
					
						
							|  |  |  |                 pass | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         raise HTTPException( | 
					
						
							|  |  |  |             status_code=(r.status_code if r is not None else status.HTTP_404_NOT_FOUND), | 
					
						
							| 
									
										
										
										
											2024-12-12 10:16:07 +08:00
										 |  |  |             detail=detail if detail else "Pipeline not found", | 
					
						
							| 
									
										
										
										
											2024-12-10 16:54:13 +08:00
										 |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-12 11:52:46 +08:00
										 |  |  | @router.get("/") | 
					
						
							| 
									
										
										
										
											2024-12-12 10:16:07 +08:00
										 |  |  | async def get_pipelines( | 
					
						
							|  |  |  |     request: Request, urlIdx: Optional[int] = None, user=Depends(get_admin_user) | 
					
						
							|  |  |  | ): | 
					
						
							| 
									
										
										
										
											2024-12-10 16:54:13 +08:00
										 |  |  |     r = None | 
					
						
							|  |  |  |     try: | 
					
						
							| 
									
										
										
										
											2024-12-12 10:16:07 +08:00
										 |  |  |         url = request.app.state.config.OPENAI_API_BASE_URLS[urlIdx] | 
					
						
							|  |  |  |         key = request.app.state.config.OPENAI_API_KEYS[urlIdx] | 
					
						
							| 
									
										
										
										
											2024-12-10 16:54:13 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-12 10:16:07 +08:00
										 |  |  |         r = requests.get(f"{url}/pipelines", headers={"Authorization": f"Bearer {key}"}) | 
					
						
							| 
									
										
										
										
											2024-12-10 16:54:13 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         r.raise_for_status() | 
					
						
							|  |  |  |         data = r.json() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return {**data} | 
					
						
							|  |  |  |     except Exception as e: | 
					
						
							|  |  |  |         # Handle connection error here | 
					
						
							| 
									
										
										
										
											2025-02-25 22:36:25 +08:00
										 |  |  |         log.exception(f"Connection error: {e}") | 
					
						
							| 
									
										
										
										
											2024-12-10 16:00:01 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-12 10:16:07 +08:00
										 |  |  |         detail = None | 
					
						
							| 
									
										
										
										
											2024-12-10 16:54:13 +08:00
										 |  |  |         if r is not None: | 
					
						
							|  |  |  |             try: | 
					
						
							|  |  |  |                 res = r.json() | 
					
						
							|  |  |  |                 if "detail" in res: | 
					
						
							|  |  |  |                     detail = res["detail"] | 
					
						
							|  |  |  |             except Exception: | 
					
						
							|  |  |  |                 pass | 
					
						
							| 
									
										
										
										
											2024-12-10 16:00:01 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-10 16:54:13 +08:00
										 |  |  |         raise HTTPException( | 
					
						
							|  |  |  |             status_code=(r.status_code if r is not None else status.HTTP_404_NOT_FOUND), | 
					
						
							| 
									
										
										
										
											2024-12-12 10:16:07 +08:00
										 |  |  |             detail=detail if detail else "Pipeline not found", | 
					
						
							| 
									
										
										
										
											2024-12-10 16:54:13 +08:00
										 |  |  |         ) | 
					
						
							| 
									
										
										
										
											2024-12-10 16:00:01 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-10 16:54:13 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-12 11:52:46 +08:00
										 |  |  | @router.get("/{pipeline_id}/valves") | 
					
						
							| 
									
										
										
										
											2024-12-10 16:54:13 +08:00
										 |  |  | async def get_pipeline_valves( | 
					
						
							| 
									
										
										
										
											2024-12-12 10:16:07 +08:00
										 |  |  |     request: Request, | 
					
						
							| 
									
										
										
										
											2024-12-10 16:54:13 +08:00
										 |  |  |     urlIdx: Optional[int], | 
					
						
							|  |  |  |     pipeline_id: str, | 
					
						
							|  |  |  |     user=Depends(get_admin_user), | 
					
						
							| 
									
										
										
										
											2024-12-10 16:00:01 +08:00
										 |  |  | ): | 
					
						
							| 
									
										
										
										
											2024-12-10 16:54:13 +08:00
										 |  |  |     r = None | 
					
						
							| 
									
										
										
										
											2024-12-10 16:00:01 +08:00
										 |  |  |     try: | 
					
						
							| 
									
										
										
										
											2024-12-12 10:16:07 +08:00
										 |  |  |         url = request.app.state.config.OPENAI_API_BASE_URLS[urlIdx] | 
					
						
							|  |  |  |         key = request.app.state.config.OPENAI_API_KEYS[urlIdx] | 
					
						
							| 
									
										
										
										
											2024-12-10 16:54:13 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-12 10:16:07 +08:00
										 |  |  |         r = requests.get( | 
					
						
							|  |  |  |             f"{url}/{pipeline_id}/valves", headers={"Authorization": f"Bearer {key}"} | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2024-12-10 16:54:13 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         r.raise_for_status() | 
					
						
							|  |  |  |         data = r.json() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return {**data} | 
					
						
							|  |  |  |     except Exception as e: | 
					
						
							|  |  |  |         # Handle connection error here | 
					
						
							| 
									
										
										
										
											2025-02-25 22:36:25 +08:00
										 |  |  |         log.exception(f"Connection error: {e}") | 
					
						
							| 
									
										
										
										
											2024-12-10 16:54:13 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-12 10:16:07 +08:00
										 |  |  |         detail = None | 
					
						
							| 
									
										
										
										
											2024-12-10 16:54:13 +08:00
										 |  |  |         if r is not None: | 
					
						
							|  |  |  |             try: | 
					
						
							|  |  |  |                 res = r.json() | 
					
						
							|  |  |  |                 if "detail" in res: | 
					
						
							|  |  |  |                     detail = res["detail"] | 
					
						
							|  |  |  |             except Exception: | 
					
						
							|  |  |  |                 pass | 
					
						
							| 
									
										
										
										
											2024-12-10 16:00:01 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-10 16:54:13 +08:00
										 |  |  |         raise HTTPException( | 
					
						
							|  |  |  |             status_code=(r.status_code if r is not None else status.HTTP_404_NOT_FOUND), | 
					
						
							| 
									
										
										
										
											2024-12-12 10:16:07 +08:00
										 |  |  |             detail=detail if detail else "Pipeline not found", | 
					
						
							| 
									
										
										
										
											2024-12-10 16:00:01 +08:00
										 |  |  |         ) | 
					
						
							| 
									
										
										
										
											2024-12-10 16:54:13 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-12 11:52:46 +08:00
										 |  |  | @router.get("/{pipeline_id}/valves/spec") | 
					
						
							| 
									
										
										
										
											2024-12-10 16:54:13 +08:00
										 |  |  | async def get_pipeline_valves_spec( | 
					
						
							| 
									
										
										
										
											2024-12-12 10:16:07 +08:00
										 |  |  |     request: Request, | 
					
						
							| 
									
										
										
										
											2024-12-10 16:54:13 +08:00
										 |  |  |     urlIdx: Optional[int], | 
					
						
							|  |  |  |     pipeline_id: str, | 
					
						
							|  |  |  |     user=Depends(get_admin_user), | 
					
						
							|  |  |  | ): | 
					
						
							|  |  |  |     r = None | 
					
						
							|  |  |  |     try: | 
					
						
							| 
									
										
										
										
											2024-12-12 10:16:07 +08:00
										 |  |  |         url = request.app.state.config.OPENAI_API_BASE_URLS[urlIdx] | 
					
						
							|  |  |  |         key = request.app.state.config.OPENAI_API_KEYS[urlIdx] | 
					
						
							| 
									
										
										
										
											2024-12-10 16:54:13 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-12 10:16:07 +08:00
										 |  |  |         r = requests.get( | 
					
						
							|  |  |  |             f"{url}/{pipeline_id}/valves/spec", | 
					
						
							|  |  |  |             headers={"Authorization": f"Bearer {key}"}, | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2024-12-10 16:54:13 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         r.raise_for_status() | 
					
						
							|  |  |  |         data = r.json() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return {**data} | 
					
						
							| 
									
										
										
										
											2024-12-10 16:00:01 +08:00
										 |  |  |     except Exception as e: | 
					
						
							| 
									
										
										
										
											2024-12-10 16:54:13 +08:00
										 |  |  |         # Handle connection error here | 
					
						
							| 
									
										
										
										
											2025-02-25 22:36:25 +08:00
										 |  |  |         log.exception(f"Connection error: {e}") | 
					
						
							| 
									
										
										
										
											2024-12-10 16:00:01 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-12 10:16:07 +08:00
										 |  |  |         detail = None | 
					
						
							| 
									
										
										
										
											2024-12-10 16:54:13 +08:00
										 |  |  |         if r is not None: | 
					
						
							|  |  |  |             try: | 
					
						
							|  |  |  |                 res = r.json() | 
					
						
							|  |  |  |                 if "detail" in res: | 
					
						
							|  |  |  |                     detail = res["detail"] | 
					
						
							|  |  |  |             except Exception: | 
					
						
							|  |  |  |                 pass | 
					
						
							| 
									
										
										
										
											2024-12-10 16:00:01 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         raise HTTPException( | 
					
						
							| 
									
										
										
										
											2024-12-10 16:54:13 +08:00
										 |  |  |             status_code=(r.status_code if r is not None else status.HTTP_404_NOT_FOUND), | 
					
						
							| 
									
										
										
										
											2024-12-12 10:16:07 +08:00
										 |  |  |             detail=detail if detail else "Pipeline not found", | 
					
						
							| 
									
										
										
										
											2024-12-10 16:54:13 +08:00
										 |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-12 11:52:46 +08:00
										 |  |  | @router.post("/{pipeline_id}/valves/update") | 
					
						
							| 
									
										
										
										
											2024-12-10 16:54:13 +08:00
										 |  |  | async def update_pipeline_valves( | 
					
						
							| 
									
										
										
										
											2024-12-12 10:16:07 +08:00
										 |  |  |     request: Request, | 
					
						
							| 
									
										
										
										
											2024-12-10 16:54:13 +08:00
										 |  |  |     urlIdx: Optional[int], | 
					
						
							|  |  |  |     pipeline_id: str, | 
					
						
							|  |  |  |     form_data: dict, | 
					
						
							|  |  |  |     user=Depends(get_admin_user), | 
					
						
							|  |  |  | ): | 
					
						
							|  |  |  |     r = None | 
					
						
							|  |  |  |     try: | 
					
						
							| 
									
										
										
										
											2024-12-12 10:16:07 +08:00
										 |  |  |         url = request.app.state.config.OPENAI_API_BASE_URLS[urlIdx] | 
					
						
							|  |  |  |         key = request.app.state.config.OPENAI_API_KEYS[urlIdx] | 
					
						
							| 
									
										
										
										
											2024-12-10 16:54:13 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         r = requests.post( | 
					
						
							|  |  |  |             f"{url}/{pipeline_id}/valves/update", | 
					
						
							| 
									
										
										
										
											2024-12-12 10:16:07 +08:00
										 |  |  |             headers={"Authorization": f"Bearer {key}"}, | 
					
						
							| 
									
										
										
										
											2024-12-10 16:54:13 +08:00
										 |  |  |             json={**form_data}, | 
					
						
							| 
									
										
										
										
											2024-12-10 16:00:01 +08:00
										 |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-10 16:54:13 +08:00
										 |  |  |         r.raise_for_status() | 
					
						
							|  |  |  |         data = r.json() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return {**data} | 
					
						
							|  |  |  |     except Exception as e: | 
					
						
							|  |  |  |         # Handle connection error here | 
					
						
							| 
									
										
										
										
											2025-02-25 22:36:25 +08:00
										 |  |  |         log.exception(f"Connection error: {e}") | 
					
						
							| 
									
										
										
										
											2024-12-10 16:54:13 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-12 10:16:07 +08:00
										 |  |  |         detail = None | 
					
						
							| 
									
										
										
										
											2024-12-10 16:54:13 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if r is not None: | 
					
						
							|  |  |  |             try: | 
					
						
							|  |  |  |                 res = r.json() | 
					
						
							|  |  |  |                 if "detail" in res: | 
					
						
							|  |  |  |                     detail = res["detail"] | 
					
						
							|  |  |  |             except Exception: | 
					
						
							|  |  |  |                 pass | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-10 16:00:01 +08:00
										 |  |  |         raise HTTPException( | 
					
						
							| 
									
										
										
										
											2024-12-10 16:54:13 +08:00
										 |  |  |             status_code=(r.status_code if r is not None else status.HTTP_404_NOT_FOUND), | 
					
						
							| 
									
										
										
										
											2024-12-12 10:16:07 +08:00
										 |  |  |             detail=detail if detail else "Pipeline not found", | 
					
						
							| 
									
										
										
										
											2024-12-10 16:00:01 +08:00
										 |  |  |         ) |