From e231333bcd16497575f4adaa044b040eb3742c98 Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Tue, 28 May 2024 09:50:17 -0700 Subject: [PATCH 1/6] refac --- backend/main.py | 38 +++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/backend/main.py b/backend/main.py index 3bb4f0f6b..b5a1569b8 100644 --- a/backend/main.py +++ b/backend/main.py @@ -12,6 +12,7 @@ import mimetypes from fastapi import FastAPI, Request, Depends, status from fastapi.staticfiles import StaticFiles +from fastapi.responses import JSONResponse from fastapi import HTTPException from fastapi.middleware.wsgi import WSGIMiddleware from fastapi.middleware.cors import CORSMiddleware @@ -123,15 +124,6 @@ app.state.MODELS = {} origins = ["*"] -app.add_middleware( - CORSMiddleware, - allow_origins=origins, - allow_credentials=True, - allow_methods=["*"], - allow_headers=["*"], -) - - # Custom middleware to add security headers # class SecurityHeadersMiddleware(BaseHTTPMiddleware): # async def dispatch(self, request: Request, call_next): @@ -276,10 +268,8 @@ class PipelineMiddleware(BaseHTTPMiddleware): except: pass - print(sorted_filters) - for filter in sorted_filters: - + r = None try: urlIdx = filter["urlIdx"] @@ -303,7 +293,20 @@ class PipelineMiddleware(BaseHTTPMiddleware): except Exception as e: # Handle connection error here print(f"Connection error: {e}") - pass + + if r is not None: + try: + res = r.json() + if "detail" in res: + return JSONResponse( + status_code=r.status_code, + content=res, + ) + except: + pass + + else: + pass modified_body_bytes = json.dumps(data).encode("utf-8") # Replace the request body with the modified one @@ -328,6 +331,15 @@ class PipelineMiddleware(BaseHTTPMiddleware): app.add_middleware(PipelineMiddleware) +app.add_middleware( + CORSMiddleware, + allow_origins=origins, + allow_credentials=True, + allow_methods=["*"], + allow_headers=["*"], +) + + @app.middleware("http") async def check_url(request: Request, call_next): if len(app.state.MODELS) == 0: From 0383efa20750eb80785e299f84eeedc5e81d3f8a Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Tue, 28 May 2024 11:43:48 -0700 Subject: [PATCH 2/6] refac: pipelines --- backend/main.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/backend/main.py b/backend/main.py index b5a1569b8..0e5924574 100644 --- a/backend/main.py +++ b/backend/main.py @@ -245,6 +245,7 @@ class PipelineMiddleware(BaseHTTPMiddleware): model for model in app.state.MODELS.values() if "pipeline" in model + and "type" in model["pipeline"] and model["pipeline"]["type"] == "filter" and ( model["pipeline"]["pipelines"] == ["*"] @@ -279,11 +280,10 @@ class PipelineMiddleware(BaseHTTPMiddleware): if key != "": headers = {"Authorization": f"Bearer {key}"} r = requests.post( - f"{url}/filter", + f"{url}/{filter['id']}/filter", headers=headers, json={ "user": user, - "model": filter["id"], "body": data, }, ) @@ -448,7 +448,7 @@ async def get_models(user=Depends(get_verified_user)): models = [ model for model in models - if "pipeline" not in model or model["pipeline"]["type"] != "filter" + if "pipeline" not in model or model["pipeline"].get("type", None) != "filter" ] if app.state.config.ENABLE_MODEL_FILTER: From 0bef1b44c03f44c6540feb9203e81e076bb578e2 Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Tue, 28 May 2024 12:04:19 -0700 Subject: [PATCH 3/6] feat: pipeline valves --- backend/main.py | 7 +++ src/lib/apis/index.ts | 29 ++++++++++ .../admin/Settings/Pipelines.svelte | 55 +++++++++++++++++++ src/lib/components/admin/SettingsModal.svelte | 38 +++++++++++++ 4 files changed, 129 insertions(+) create mode 100644 src/lib/components/admin/Settings/Pipelines.svelte diff --git a/backend/main.py b/backend/main.py index 0e5924574..648bedea0 100644 --- a/backend/main.py +++ b/backend/main.py @@ -464,6 +464,13 @@ async def get_models(user=Depends(get_verified_user)): return {"data": models} +@app.get("/api/pipelines") +async def get_pipelines(user=Depends(get_admin_user)): + models = await get_all_models() + pipelines = [model for model in models if "pipeline" in model] + return {"data": pipelines} + + @app.get("/api/config") async def get_app_config(): # Checking and Handling the Absence of 'ui' in CONFIG_DATA diff --git a/src/lib/apis/index.ts b/src/lib/apis/index.ts index dc51abd52..3d6e51684 100644 --- a/src/lib/apis/index.ts +++ b/src/lib/apis/index.ts @@ -49,6 +49,35 @@ export const getModels = async (token: string = '') => { return models; }; +export const getPipelines = async (token: string = '') => { + let error = null; + + const res = await fetch(`${WEBUI_BASE_URL}/api/pipelines`, { + method: 'GET', + headers: { + Accept: 'application/json', + 'Content-Type': 'application/json', + ...(token && { authorization: `Bearer ${token}` }) + } + }) + .then(async (res) => { + if (!res.ok) throw await res.json(); + return res.json(); + }) + .catch((err) => { + console.log(err); + error = err; + return null; + }); + + if (error) { + throw error; + } + + let pipelines = res?.data ?? []; + return pipelines; +}; + export const getBackendConfig = async () => { let error = null; diff --git a/src/lib/components/admin/Settings/Pipelines.svelte b/src/lib/components/admin/Settings/Pipelines.svelte new file mode 100644 index 000000000..b1b34ecca --- /dev/null +++ b/src/lib/components/admin/Settings/Pipelines.svelte @@ -0,0 +1,55 @@ + + +
{ + saveHandler(); + }} +> +
+
+
+
+ {$i18n.t('Pipeline Valves')} +
+
+
+ {#each pipelines as pipeline} +
+ {JSON.stringify(pipeline)} +
+ {/each} +
+
+
+
+ +
+
diff --git a/src/lib/components/admin/SettingsModal.svelte b/src/lib/components/admin/SettingsModal.svelte index 38a2602b6..6da1bda6f 100644 --- a/src/lib/components/admin/SettingsModal.svelte +++ b/src/lib/components/admin/SettingsModal.svelte @@ -8,6 +8,7 @@ import Banners from '$lib/components/admin/Settings/Banners.svelte'; import { toast } from 'svelte-sonner'; + import Pipelines from './Settings/Pipelines.svelte'; const i18n = getContext('i18n'); @@ -149,6 +150,36 @@
{$i18n.t('Banners')}
+ +
{#if selectedTab === 'general'} @@ -179,6 +210,13 @@ toast.success($i18n.t('Settings saved successfully!')); }} /> + {:else if selectedTab === 'pipelines'} + { + show = false; + toast.success($i18n.t('Settings saved successfully!')); + }} + /> {/if}
From 130d15a2fb4b1fdf5a98ab8e9475ab7f5bdcaec8 Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Tue, 28 May 2024 12:32:49 -0700 Subject: [PATCH 4/6] feat: pipeline valves --- backend/main.py | 116 ++++++++++++++++++ src/lib/apis/index.ts | 89 ++++++++++++++ .../admin/Settings/Pipelines.svelte | 74 ++++++++--- 3 files changed, 265 insertions(+), 14 deletions(-) diff --git a/backend/main.py b/backend/main.py index 648bedea0..2de27111d 100644 --- a/backend/main.py +++ b/backend/main.py @@ -471,6 +471,122 @@ async def get_pipelines(user=Depends(get_admin_user)): return {"data": pipelines} +@app.get("/api/pipelines/{pipeline_id}/valves") +async def get_pipeline_valves(pipeline_id: str, user=Depends(get_admin_user)): + models = await get_all_models() + if pipeline_id in app.state.MODELS and "pipeline" in app.state.MODELS[pipeline_id]: + pipeline = app.state.MODELS[pipeline_id] + + try: + urlIdx = pipeline["urlIdx"] + + url = openai_app.state.config.OPENAI_API_BASE_URLS[urlIdx] + key = openai_app.state.config.OPENAI_API_KEYS[urlIdx] + + if key != "": + headers = {"Authorization": f"Bearer {key}"} + r = requests.get(f"{url}/{pipeline['id']}/valves", headers=headers) + + r.raise_for_status() + data = r.json() + + return {**data} + except Exception as e: + # Handle connection error here + print(f"Connection error: {e}") + + raise HTTPException( + status_code=status.HTTP_404_NOT_FOUND, + detail="Pipeline not found", + ) + + return {"data": pipeline["pipeline"]["valves"]} + else: + raise HTTPException( + status_code=status.HTTP_404_NOT_FOUND, + detail="Pipeline not found", + ) + + +@app.get("/api/pipelines/{pipeline_id}/valves/spec") +async def get_pipeline_valves_spec(pipeline_id: str, user=Depends(get_admin_user)): + models = await get_all_models() + if pipeline_id in app.state.MODELS and "pipeline" in app.state.MODELS[pipeline_id]: + pipeline = app.state.MODELS[pipeline_id] + + try: + urlIdx = pipeline["urlIdx"] + + url = openai_app.state.config.OPENAI_API_BASE_URLS[urlIdx] + key = openai_app.state.config.OPENAI_API_KEYS[urlIdx] + + if key != "": + headers = {"Authorization": f"Bearer {key}"} + r = requests.get(f"{url}/{pipeline['id']}/valves/spec", headers=headers) + + r.raise_for_status() + data = r.json() + + return {**data} + except Exception as e: + # Handle connection error here + print(f"Connection error: {e}") + + raise HTTPException( + status_code=status.HTTP_404_NOT_FOUND, + detail="Pipeline not found", + ) + + return {"data": pipeline["pipeline"]["valves"]} + else: + raise HTTPException( + status_code=status.HTTP_404_NOT_FOUND, + detail="Pipeline not found", + ) + + +@app.post("/api/pipelines/{pipeline_id}/valves/update") +async def update_pipeline_valves( + pipeline_id: str, form_data: dict, user=Depends(get_admin_user) +): + models = await get_all_models() + + if pipeline_id in app.state.MODELS and "pipeline" in app.state.MODELS[pipeline_id]: + pipeline = app.state.MODELS[pipeline_id] + + try: + urlIdx = pipeline["urlIdx"] + + url = openai_app.state.config.OPENAI_API_BASE_URLS[urlIdx] + key = openai_app.state.config.OPENAI_API_KEYS[urlIdx] + + if key != "": + headers = {"Authorization": f"Bearer {key}"} + r = requests.post( + f"{url}/{pipeline['id']}/valves/update", + headers=headers, + json={**form_data}, + ) + + r.raise_for_status() + data = r.json() + + return {**data} + except Exception as e: + # Handle connection error here + print(f"Connection error: {e}") + + raise HTTPException( + status_code=status.HTTP_404_NOT_FOUND, + detail="Pipeline not found", + ) + else: + raise HTTPException( + status_code=status.HTTP_404_NOT_FOUND, + detail="Pipeline not found", + ) + + @app.get("/api/config") async def get_app_config(): # Checking and Handling the Absence of 'ui' in CONFIG_DATA diff --git a/src/lib/apis/index.ts b/src/lib/apis/index.ts index 3d6e51684..9850581f9 100644 --- a/src/lib/apis/index.ts +++ b/src/lib/apis/index.ts @@ -78,6 +78,95 @@ export const getPipelines = async (token: string = '') => { return pipelines; }; +export const getPipelineValves = async (token: string = '', pipeline_id: string) => { + let error = null; + + const res = await fetch(`${WEBUI_BASE_URL}/api/pipelines/${pipeline_id}/valves`, { + method: 'GET', + headers: { + Accept: 'application/json', + 'Content-Type': 'application/json', + ...(token && { authorization: `Bearer ${token}` }) + } + }) + .then(async (res) => { + if (!res.ok) throw await res.json(); + return res.json(); + }) + .catch((err) => { + console.log(err); + error = err; + return null; + }); + + if (error) { + throw error; + } + + return res; +}; + +export const getPipelineValvesSpec = async (token: string = '', pipeline_id: string) => { + let error = null; + + const res = await fetch(`${WEBUI_BASE_URL}/api/pipelines/${pipeline_id}/valves/spec`, { + method: 'GET', + headers: { + Accept: 'application/json', + 'Content-Type': 'application/json', + ...(token && { authorization: `Bearer ${token}` }) + } + }) + .then(async (res) => { + if (!res.ok) throw await res.json(); + return res.json(); + }) + .catch((err) => { + console.log(err); + error = err; + return null; + }); + + if (error) { + throw error; + } + + return res; +}; + +export const updatePipelineValves = async ( + token: string = '', + pipeline_id: string, + valves: object +) => { + let error = null; + + const res = await fetch(`${WEBUI_BASE_URL}/api/pipelines/${pipeline_id}/valves/update`, { + method: 'POST', + headers: { + Accept: 'application/json', + 'Content-Type': 'application/json', + ...(token && { authorization: `Bearer ${token}` }) + }, + body: JSON.stringify(valves) + }) + .then(async (res) => { + if (!res.ok) throw await res.json(); + return res.json(); + }) + .catch((err) => { + console.log(err); + error = err; + return null; + }); + + if (error) { + throw error; + } + + return res; +}; + export const getBackendConfig = async () => { let error = null; diff --git a/src/lib/components/admin/Settings/Pipelines.svelte b/src/lib/components/admin/Settings/Pipelines.svelte index b1b34ecca..62007bdc0 100644 --- a/src/lib/components/admin/Settings/Pipelines.svelte +++ b/src/lib/components/admin/Settings/Pipelines.svelte @@ -2,21 +2,33 @@ import { v4 as uuidv4 } from 'uuid'; import { getContext, onMount } from 'svelte'; - import { models } from '$lib/stores'; import type { Writable } from 'svelte/store'; import type { i18n as i18nType } from 'i18next'; - import Tooltip from '$lib/components/common/Tooltip.svelte'; - import Switch from '$lib/components/common/Switch.svelte'; import { stringify } from 'postcss'; - import { getPipelines } from '$lib/apis'; + import { getPipelineValves, getPipelines } from '$lib/apis'; + import Spinner from '$lib/components/common/Spinner.svelte'; + const i18n: Writable = getContext('i18n'); export let saveHandler: Function; - let pipelines = []; + let pipelines = null; + let valves = null; + let selectedPipelineIdx = 0; + $: if ( + pipelines !== null && + pipelines.length > 0 && + pipelines[selectedPipelineIdx] !== undefined && + pipelines[selectedPipelineIdx].pipeline.valves + ) { + (async () => { + valves = await getPipelineValves(localStorage.token, pipelines[selectedPipelineIdx].id); + })(); + } + onMount(async () => { pipelines = await getPipelines(localStorage.token); }); @@ -28,21 +40,55 @@ saveHandler(); }} > -
-
+
+ {#if pipelines !== null}
- {$i18n.t('Pipeline Valves')} + {$i18n.t('Pipelines')}
-
- {#each pipelines as pipeline} -
- {JSON.stringify(pipeline)} +
+ {#if pipelines.length > 0} +
+
+ +
- {/each} + {/if} + +
{$i18n.t('Valves')}
+ +
+ {#if pipelines[selectedPipelineIdx].pipeline.valves} + {#if valves} + {#each Object.keys(valves) as valve, idx} +
{valve}
+ {/each} + {:else} + + {/if} + {:else} +
No valves
+ {/if} +
-
+ {:else if pipelines !== null && pipelines.length === 0} +
Pipelines Not Detected
+ {:else} +
+
+ +
+
+ {/if}
+
+ + {#if (valves[property] ?? null) !== null} +
+
+ +
+
+ {/if} +
{/each} {:else} diff --git a/src/lib/components/admin/SettingsModal.svelte b/src/lib/components/admin/SettingsModal.svelte index 6da1bda6f..78f48cdfc 100644 --- a/src/lib/components/admin/SettingsModal.svelte +++ b/src/lib/components/admin/SettingsModal.svelte @@ -185,35 +185,30 @@ {#if selectedTab === 'general'} { - show = false; toast.success($i18n.t('Settings saved successfully!')); }} /> {:else if selectedTab === 'users'} { - show = false; toast.success($i18n.t('Settings saved successfully!')); }} /> {:else if selectedTab === 'db'} { - show = false; toast.success($i18n.t('Settings saved successfully!')); }} /> {:else if selectedTab === 'banners'} { - show = false; toast.success($i18n.t('Settings saved successfully!')); }} /> {:else if selectedTab === 'pipelines'} { - show = false; toast.success($i18n.t('Settings saved successfully!')); }} />