Explorar o código

refac: openai additional headers support

Timothy Jaeryang Baek hai 4 días
pai
achega
f25b7b73b4

+ 3 - 0
backend/open_webui/routers/openai.py

@@ -190,6 +190,9 @@ async def get_headers_and_cookies(
     if token:
         headers["Authorization"] = f"Bearer {token}"
 
+    if config.get("headers") and isinstance(config.get("headers"), dict):
+        headers = {**headers, **config.get("headers")}
+
     return headers, cookies
 
 

+ 64 - 1
src/lib/components/AddConnectionModal.svelte

@@ -17,6 +17,7 @@
 	import Tags from './common/Tags.svelte';
 	import Spinner from '$lib/components/common/Spinner.svelte';
 	import XMark from '$lib/components/icons/XMark.svelte';
+	import Textarea from './common/Textarea.svelte';
 
 	export let onSubmit: Function = () => {};
 	export let onDelete: Function = () => {};
@@ -42,6 +43,8 @@
 	let enable = true;
 	let apiVersion = '';
 
+	let headers = '';
+
 	let tags = [];
 
 	let modelId = '';
@@ -69,6 +72,19 @@
 		// remove trailing slash from url
 		url = url.replace(/\/$/, '');
 
+		if (headers) {
+			try {
+				const _headers = JSON.parse(headers);
+				if (typeof _headers !== 'object' || Array.isArray(_headers)) {
+					throw new Error('Headers must be a valid JSON object');
+				}
+				headers = JSON.stringify(_headers, null, 2);
+			} catch (error) {
+				toast.error($i18n.t('Headers must be a valid JSON object'));
+				return;
+			}
+		}
+
 		const res = await verifyOpenAIConnection(
 			localStorage.token,
 			{
@@ -77,7 +93,8 @@
 				config: {
 					auth_type,
 					azure: azure,
-					api_version: apiVersion
+					api_version: apiVersion,
+					headers: JSON.parse(headers)
 				}
 			},
 			direct
@@ -136,6 +153,19 @@
 			}
 		}
 
+		if (headers) {
+			try {
+				const _headers = JSON.parse(headers);
+				if (typeof _headers !== 'object' || Array.isArray(_headers)) {
+					throw new Error('Headers must be a valid JSON object');
+				}
+				headers = JSON.stringify(_headers, null, 2);
+			} catch (error) {
+				toast.error($i18n.t('Headers must be a valid JSON object'));
+				return;
+			}
+		}
+
 		// remove trailing slash from url
 		url = url.replace(/\/$/, '');
 
@@ -149,6 +179,7 @@
 				model_ids: modelIds,
 				connection_type: connectionType,
 				auth_type,
+				headers: headers ? JSON.parse(headers) : undefined,
 				...(!ollama && azure ? { azure: true, api_version: apiVersion } : {})
 			}
 		};
@@ -172,6 +203,9 @@
 			key = connection.key;
 
 			auth_type = connection.config.auth_type ?? 'bearer';
+			headers = connection.config?.headers
+				? JSON.stringify(connection.config.headers, null, 2)
+				: '';
 
 			enable = connection.config?.enable ?? true;
 			tags = connection.config?.tags ?? [];
@@ -376,6 +410,35 @@
 							</div>
 						</div>
 
+						{#if !ollama && !direct}
+							<div class="flex gap-2 mt-2">
+								<div class="flex flex-col w-full">
+									<label
+										for="headers-input"
+										class={`mb-0.5 text-xs text-gray-500
+								${($settings?.highContrastMode ?? false) ? 'text-gray-800 dark:text-gray-100' : ''}`}
+										>{$i18n.t('Headers')}</label
+									>
+
+									<div class="flex-1">
+										<Tooltip
+											content={$i18n.t(
+												'Enter additional headers in JSON format (e.g. {{\'{{"X-Custom-Header": "value"}}\'}})'
+											)}
+										>
+											<Textarea
+												className="w-full text-sm outline-hidden"
+												bind:value={headers}
+												placeholder={$i18n.t('Enter additional headers in JSON format')}
+												required={false}
+												minSize={30}
+											/>
+										</Tooltip>
+									</div>
+								</div>
+							</div>
+						{/if}
+
 						<div class="flex gap-2 mt-2">
 							<div class="flex flex-col w-full">
 								<label