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}