refac: azure openai

This commit is contained in:
Timothy Jaeryang Baek 2025-05-19 04:31:04 +04:00
parent 2f2a7ee0b0
commit 2ab5aa4d34
4 changed files with 40 additions and 28 deletions

View File

@ -489,24 +489,10 @@ async def get_models(
} }
if api_config.get("azure", False): if api_config.get("azure", False):
headers["api-key"] = key models = {
"data": api_config.get("model_ids", []) or [],
api_version = api_config.get("api_version", "2023-03-15-preview") "object": "list",
async with session.get( }
f"{url}/openai/deployments?api-version={api_version}",
headers=headers,
ssl=AIOHTTP_CLIENT_SESSION_SSL,
) as r:
if r.status != 200:
# Extract response error details if available
error_detail = f"HTTP Error: {r.status}"
res = await r.json()
if "error" in res:
error_detail = f"External Error: {res['error']}"
raise Exception(error_detail)
response_data = await r.json()
models = response_data
else: else:
headers["Authorization"] = f"Bearer {key}" headers["Authorization"] = f"Bearer {key}"
@ -599,10 +585,10 @@ async def verify_connection(
if api_config.get("azure", False): if api_config.get("azure", False):
headers["api-key"] = key headers["api-key"] = key
api_version = api_config.get("api_version", "") or "2023-03-15-preview"
api_version = api_config.get("api_version", "2023-03-15-preview")
async with session.get( async with session.get(
f"{url}/openai/deployments?api-version={api_version}", url=f"{url}/openai/models?api-version={api_version}",
headers=headers, headers=headers,
ssl=AIOHTTP_CLIENT_SESSION_SSL, ssl=AIOHTTP_CLIENT_SESSION_SSL,
) as r: ) as r:
@ -828,7 +814,7 @@ async def generate_chat_completion(
if api_config.get("azure", False): if api_config.get("azure", False):
request_url, payload = convert_to_azure_payload(url, payload) request_url, payload = convert_to_azure_payload(url, payload)
api_version = api_config.get("api_version", "2023-03-15-preview") api_version = api_config.get("api_version", "") or "2023-03-15-preview"
headers["api-key"] = key headers["api-key"] = key
headers["api-version"] = api_version headers["api-version"] = api_version
request_url = f"{request_url}/chat/completions?api-version={api_version}" request_url = f"{request_url}/chat/completions?api-version={api_version}"
@ -936,7 +922,9 @@ async def proxy(path: str, request: Request, user=Depends(get_verified_user)):
if api_config.get("azure", False): if api_config.get("azure", False):
headers["api-key"] = key headers["api-key"] = key
headers["api-version"] = api_config.get("api_version", "2023-03-15-preview") headers["api-version"] = (
api_config.get("api_version", "") or "2023-03-15-preview"
)
payload = json.loads(body) payload = json.loads(body)
url, payload = convert_to_azure_payload(url, payload) url, payload = convert_to_azure_payload(url, payload)

View File

@ -33,7 +33,7 @@
let connectionType = 'external'; let connectionType = 'external';
let azure = false; let azure = false;
$: azure = $: azure =
(url.includes('openai.azure.com') || url.includes('cognitive.microsoft.com')) && !direct (url.includes('azure.com') || url.includes('cognitive.microsoft.com')) && !direct
? true ? true
: false; : false;
@ -106,6 +106,28 @@
return; return;
} }
if (azure) {
if (!apiVersion) {
loading = false;
toast.error('API Version is required');
return;
}
if (!key) {
loading = false;
toast.error('Key is required');
return;
}
if (modelIds.length === 0) {
loading = false;
toast.error('Deployment names are required');
return;
}
}
// remove trailing slash from url // remove trailing slash from url
url = url.replace(/\/$/, ''); url = url.replace(/\/$/, '');
@ -149,6 +171,7 @@
} else { } else {
connectionType = connection.config?.connection_type ?? 'external'; connectionType = connection.config?.connection_type ?? 'external';
azure = connection.config?.azure ?? false; azure = connection.config?.azure ?? false;
apiVersion = connection.config?.api_version ?? '';
} }
} }
}; };
@ -382,9 +405,10 @@
url: url url: url
})} })}
{:else if azure} {:else if azure}
{$i18n.t('Leave empty to include all models from "{{url}}" endpoint', { {$i18n.t('Deployment names are required for Azure OpenAI.')}
<!-- {$i18n.t('Leave empty to include all models from "{{url}}" endpoint', {
url: `${url}/openai/deployments` url: `${url}/openai/deployments`
})} })} -->
{:else} {:else}
{$i18n.t('Leave empty to include all models from "{{url}}/models" endpoint', { {$i18n.t('Leave empty to include all models from "{{url}}/models" endpoint', {
url: url url: url
@ -394,7 +418,7 @@
{/if} {/if}
</div> </div>
<hr class=" border-gray-100 dark:border-gray-700/10 my-2.5 w-full" /> <hr class=" border-gray-100 dark:border-gray-700/10 my-1.5 w-full" />
<div class="flex items-center"> <div class="flex items-center">
<input <input

View File

@ -62,7 +62,7 @@
class="absolute top-0 bottom-0 left-0 right-0 opacity-60 bg-white dark:bg-gray-900 z-10" class="absolute top-0 bottom-0 left-0 right-0 opacity-60 bg-white dark:bg-gray-900 z-10"
></div> ></div>
{/if} {/if}
<div class="flex w-full"> <div class="flex w-full gap-2">
<div class="flex-1 relative"> <div class="flex-1 relative">
<input <input
class=" outline-hidden w-full bg-transparent {pipeline ? 'pr-8' : ''}" class=" outline-hidden w-full bg-transparent {pipeline ? 'pr-8' : ''}"

View File

@ -62,7 +62,7 @@
class="absolute top-0 bottom-0 left-0 right-0 opacity-60 bg-white dark:bg-gray-900 z-10" class="absolute top-0 bottom-0 left-0 right-0 opacity-60 bg-white dark:bg-gray-900 z-10"
></div> ></div>
{/if} {/if}
<div class="flex w-full"> <div class="flex w-full gap-2">
<div class="flex-1 relative"> <div class="flex-1 relative">
<input <input
class=" outline-hidden w-full bg-transparent {pipeline ? 'pr-8' : ''}" class=" outline-hidden w-full bg-transparent {pipeline ? 'pr-8' : ''}"