forked from open-webui/open-webui
		
	Merge pull request #1117 from open-webui/model-whitelist
feat: model filter (whitelist)
This commit is contained in:
		
						commit
						bcabd3df84
					
				
					 7 changed files with 241 additions and 88 deletions
				
			
		|  | @ -77,3 +77,65 @@ export const getVersionUpdates = async () => { | |||
| 
 | ||||
| 	return res; | ||||
| }; | ||||
| 
 | ||||
| export const getModelFilterConfig = async (token: string) => { | ||||
| 	let error = null; | ||||
| 
 | ||||
| 	const res = await fetch(`${WEBUI_BASE_URL}/api/config/model/filter`, { | ||||
| 		method: 'GET', | ||||
| 		headers: { | ||||
| 			'Content-Type': 'application/json', | ||||
| 			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 updateModelFilterConfig = async ( | ||||
| 	token: string, | ||||
| 	enabled: boolean, | ||||
| 	models: string[] | ||||
| ) => { | ||||
| 	let error = null; | ||||
| 
 | ||||
| 	const res = await fetch(`${WEBUI_BASE_URL}/api/config/model/filter`, { | ||||
| 		method: 'POST', | ||||
| 		headers: { | ||||
| 			'Content-Type': 'application/json', | ||||
| 			Authorization: `Bearer ${token}` | ||||
| 		}, | ||||
| 		body: JSON.stringify({ | ||||
| 			enabled: enabled, | ||||
| 			models: models | ||||
| 		}) | ||||
| 	}) | ||||
| 		.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; | ||||
| }; | ||||
|  |  | |||
|  | @ -1,10 +1,14 @@ | |||
| <script lang="ts"> | ||||
| 	import { getModelFilterConfig, updateModelFilterConfig } from '$lib/apis'; | ||||
| 	import { getSignUpEnabledStatus, toggleSignUpEnabledStatus } from '$lib/apis/auths'; | ||||
| 	import { getUserPermissions, updateUserPermissions } from '$lib/apis/users'; | ||||
| 	import { models } from '$lib/stores'; | ||||
| 	import { onMount } from 'svelte'; | ||||
| 
 | ||||
| 	export let saveHandler: Function; | ||||
| 
 | ||||
| 	let whitelistEnabled = false; | ||||
| 	let whitelistModels = ['']; | ||||
| 	let permissions = { | ||||
| 		chat: { | ||||
| 			deletion: true | ||||
|  | @ -13,6 +17,13 @@ | |||
| 
 | ||||
| 	onMount(async () => { | ||||
| 		permissions = await getUserPermissions(localStorage.token); | ||||
| 
 | ||||
| 		const res = await getModelFilterConfig(localStorage.token); | ||||
| 		if (res) { | ||||
| 			whitelistEnabled = res.enabled; | ||||
| 
 | ||||
| 			whitelistModels = res.models.length > 0 ? res.models : ['']; | ||||
| 		} | ||||
| 	}); | ||||
| </script> | ||||
| 
 | ||||
|  | @ -21,6 +32,8 @@ | |||
| 	on:submit|preventDefault={async () => { | ||||
| 		// console.log('submit'); | ||||
| 		await updateUserPermissions(localStorage.token, permissions); | ||||
| 
 | ||||
| 		await updateModelFilterConfig(localStorage.token, whitelistEnabled, whitelistModels); | ||||
| 		saveHandler(); | ||||
| 	}} | ||||
| > | ||||
|  | @ -69,6 +82,106 @@ | |||
| 				</button> | ||||
| 			</div> | ||||
| 		</div> | ||||
| 
 | ||||
| 		<hr class=" dark:border-gray-700 my-2" /> | ||||
| 
 | ||||
| 		<div class="mt-2 space-y-3 pr-1.5"> | ||||
| 			<div> | ||||
| 				<div class="mb-2"> | ||||
| 					<div class="flex justify-between items-center text-xs"> | ||||
| 						<div class=" text-sm font-medium">Manage Models</div> | ||||
| 					</div> | ||||
| 				</div> | ||||
| 
 | ||||
| 				<div class=" space-y-3"> | ||||
| 					<div> | ||||
| 						<div class="flex justify-between items-center text-xs"> | ||||
| 							<div class=" text-xs font-medium">Model Whitelisting</div> | ||||
| 
 | ||||
| 							<button | ||||
| 								class=" text-xs font-medium text-gray-500" | ||||
| 								type="button" | ||||
| 								on:click={() => { | ||||
| 									whitelistEnabled = !whitelistEnabled; | ||||
| 								}}>{whitelistEnabled ? 'On' : 'Off'}</button | ||||
| 							> | ||||
| 						</div> | ||||
| 					</div> | ||||
| 
 | ||||
| 					{#if whitelistEnabled} | ||||
| 						<div> | ||||
| 							<div class=" space-y-1.5"> | ||||
| 								{#each whitelistModels as modelId, modelIdx} | ||||
| 									<div class="flex w-full"> | ||||
| 										<div class="flex-1 mr-2"> | ||||
| 											<select | ||||
| 												class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none" | ||||
| 												bind:value={modelId} | ||||
| 												placeholder="Select a model" | ||||
| 											> | ||||
| 												<option value="" disabled selected>Select a model</option> | ||||
| 												{#each $models.filter((model) => model.id) as model} | ||||
| 													<option value={model.id} class="bg-gray-100 dark:bg-gray-700" | ||||
| 														>{model.name}</option | ||||
| 													> | ||||
| 												{/each} | ||||
| 											</select> | ||||
| 										</div> | ||||
| 
 | ||||
| 										{#if modelIdx === 0} | ||||
| 											<button | ||||
| 												class="px-2.5 bg-gray-100 hover:bg-gray-200 text-gray-800 dark:bg-gray-900 dark:text-white rounded-lg transition" | ||||
| 												type="button" | ||||
| 												on:click={() => { | ||||
| 													if (whitelistModels.at(-1) !== '') { | ||||
| 														whitelistModels = [...whitelistModels, '']; | ||||
| 													} | ||||
| 												}} | ||||
| 											> | ||||
| 												<svg | ||||
| 													xmlns="http://www.w3.org/2000/svg" | ||||
| 													viewBox="0 0 16 16" | ||||
| 													fill="currentColor" | ||||
| 													class="w-4 h-4" | ||||
| 												> | ||||
| 													<path | ||||
| 														d="M8.75 3.75a.75.75 0 0 0-1.5 0v3.5h-3.5a.75.75 0 0 0 0 1.5h3.5v3.5a.75.75 0 0 0 1.5 0v-3.5h3.5a.75.75 0 0 0 0-1.5h-3.5v-3.5Z" | ||||
| 													/> | ||||
| 												</svg> | ||||
| 											</button> | ||||
| 										{:else} | ||||
| 											<button | ||||
| 												class="px-2.5 bg-gray-100 hover:bg-gray-200 text-gray-800 dark:bg-gray-900 dark:text-white rounded-lg transition" | ||||
| 												type="button" | ||||
| 												on:click={() => { | ||||
| 													whitelistModels.splice(modelIdx, 1); | ||||
| 													whitelistModels = whitelistModels; | ||||
| 												}} | ||||
| 											> | ||||
| 												<svg | ||||
| 													xmlns="http://www.w3.org/2000/svg" | ||||
| 													viewBox="0 0 16 16" | ||||
| 													fill="currentColor" | ||||
| 													class="w-4 h-4" | ||||
| 												> | ||||
| 													<path d="M3.75 7.25a.75.75 0 0 0 0 1.5h8.5a.75.75 0 0 0 0-1.5h-8.5Z" /> | ||||
| 												</svg> | ||||
| 											</button> | ||||
| 										{/if} | ||||
| 									</div> | ||||
| 								{/each} | ||||
| 							</div> | ||||
| 
 | ||||
| 							<div class="flex justify-end items-center text-xs mt-1.5 text-right"> | ||||
| 								<div class=" text-xs font-medium"> | ||||
| 									{whitelistModels.length} Model(s) Whitelisted | ||||
| 								</div> | ||||
| 							</div> | ||||
| 						</div> | ||||
| 					{/if} | ||||
| 				</div> | ||||
| 			</div> | ||||
| 		</div> | ||||
| 	</div> | ||||
| 
 | ||||
| 	<div class="flex justify-end pt-3 text-sm font-medium"> | ||||
|  |  | |||
|  | @ -912,88 +912,6 @@ | |||
| 					{/if} | ||||
| 				</div> | ||||
| 			</div> | ||||
| 
 | ||||
| 			<!-- <div class="mt-2 space-y-3 pr-1.5"> | ||||
| 				<div> | ||||
| 					<div class=" mb-2.5 text-sm font-medium">Add LiteLLM Model</div> | ||||
| 					<div class="flex w-full mb-2"> | ||||
| 						<div class="flex-1"> | ||||
| 							<input | ||||
| 								class="w-full rounded py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-800 outline-none" | ||||
| 								placeholder="Enter LiteLLM Model (e.g. ollama/mistral)" | ||||
| 								bind:value={liteLLMModel} | ||||
| 								autocomplete="off" | ||||
| 							/> | ||||
| 						</div> | ||||
| 					</div> | ||||
| 
 | ||||
| 					<div class="flex justify-between items-center text-sm"> | ||||
| 						<div class="  font-medium">Advanced Model Params</div> | ||||
| 						<button | ||||
| 							class=" text-xs font-medium text-gray-500" | ||||
| 							type="button" | ||||
| 							on:click={() => { | ||||
| 								showLiteLLMParams = !showLiteLLMParams; | ||||
| 							}}>{showLiteLLMParams ? 'Hide' : 'Show'}</button | ||||
| 						> | ||||
| 					</div> | ||||
| 
 | ||||
| 					{#if showLiteLLMParams} | ||||
| 						<div> | ||||
| 							<div class=" mb-2.5 text-sm font-medium">LiteLLM API Key</div> | ||||
| 							<div class="flex w-full"> | ||||
| 								<div class="flex-1"> | ||||
| 									<input | ||||
| 										class="w-full rounded py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-800 outline-none" | ||||
| 										placeholder="Enter LiteLLM API Key (e.g. os.environ/AZURE_API_KEY_CA)" | ||||
| 										bind:value={liteLLMAPIKey} | ||||
| 										autocomplete="off" | ||||
| 									/> | ||||
| 								</div> | ||||
| 							</div> | ||||
| 						</div> | ||||
| 
 | ||||
| 						<div> | ||||
| 							<div class=" mb-2.5 text-sm font-medium">LiteLLM API Base URL</div> | ||||
| 							<div class="flex w-full"> | ||||
| 								<div class="flex-1"> | ||||
| 									<input | ||||
| 										class="w-full rounded py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-800 outline-none" | ||||
| 										placeholder="Enter LiteLLM API Base URL" | ||||
| 										bind:value={liteLLMAPIBase} | ||||
| 										autocomplete="off" | ||||
| 									/> | ||||
| 								</div> | ||||
| 							</div> | ||||
| 						</div> | ||||
| 
 | ||||
| 						<div> | ||||
| 							<div class=" mb-2.5 text-sm font-medium">LiteLLM API RPM</div> | ||||
| 							<div class="flex w-full"> | ||||
| 								<div class="flex-1"> | ||||
| 									<input | ||||
| 										class="w-full rounded py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-800 outline-none" | ||||
| 										placeholder="Enter LiteLLM API RPM" | ||||
| 										bind:value={liteLLMRPM} | ||||
| 										autocomplete="off" | ||||
| 									/> | ||||
| 								</div> | ||||
| 							</div> | ||||
| 						</div> | ||||
| 					{/if} | ||||
| 
 | ||||
| 					<div class="mt-2 text-xs text-gray-400 dark:text-gray-500"> | ||||
| 						Not sure what to add? | ||||
| 						<a | ||||
| 							class=" text-gray-300 font-medium underline" | ||||
| 							href="https://litellm.vercel.app/docs/proxy/configs#quick-start" | ||||
| 							target="_blank" | ||||
| 						> | ||||
| 							Click here for help. | ||||
| 						</a> | ||||
| 					</div> | ||||
| 				</div> | ||||
| 			</div> --> | ||||
| 		</div> | ||||
| 	</div> | ||||
| </div> | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Timothy Jaeryang Baek
						Timothy Jaeryang Baek