1
0

openai.py 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117
  1. import asyncio
  2. import hashlib
  3. import json
  4. import logging
  5. from typing import Optional
  6. import aiohttp
  7. from aiocache import cached
  8. import requests
  9. from urllib.parse import quote
  10. from fastapi import Depends, HTTPException, Request, APIRouter
  11. from fastapi.responses import (
  12. FileResponse,
  13. StreamingResponse,
  14. JSONResponse,
  15. PlainTextResponse,
  16. )
  17. from pydantic import BaseModel
  18. from starlette.background import BackgroundTask
  19. from open_webui.models.models import Models
  20. from open_webui.config import (
  21. CACHE_DIR,
  22. )
  23. from open_webui.env import (
  24. MODELS_CACHE_TTL,
  25. AIOHTTP_CLIENT_SESSION_SSL,
  26. AIOHTTP_CLIENT_TIMEOUT,
  27. AIOHTTP_CLIENT_TIMEOUT_MODEL_LIST,
  28. ENABLE_FORWARD_USER_INFO_HEADERS,
  29. BYPASS_MODEL_ACCESS_CONTROL,
  30. )
  31. from open_webui.models.users import UserModel
  32. from open_webui.constants import ERROR_MESSAGES
  33. from open_webui.env import SRC_LOG_LEVELS
  34. from open_webui.utils.payload import (
  35. apply_model_params_to_body_openai,
  36. apply_system_prompt_to_body,
  37. )
  38. from open_webui.utils.misc import (
  39. convert_logit_bias_input_to_json,
  40. )
  41. from open_webui.utils.auth import get_admin_user, get_verified_user
  42. from open_webui.utils.access_control import has_access
  43. log = logging.getLogger(__name__)
  44. log.setLevel(SRC_LOG_LEVELS["OPENAI"])
  45. ##########################################
  46. #
  47. # Utility functions
  48. #
  49. ##########################################
  50. async def send_get_request(url, key=None, user: UserModel = None):
  51. timeout = aiohttp.ClientTimeout(total=AIOHTTP_CLIENT_TIMEOUT_MODEL_LIST)
  52. try:
  53. async with aiohttp.ClientSession(timeout=timeout, trust_env=True) as session:
  54. async with session.get(
  55. url,
  56. headers={
  57. **({"Authorization": f"Bearer {key}"} if key else {}),
  58. **(
  59. {
  60. "X-OpenWebUI-User-Name": quote(user.name, safe=" "),
  61. "X-OpenWebUI-User-Id": user.id,
  62. "X-OpenWebUI-User-Email": user.email,
  63. "X-OpenWebUI-User-Role": user.role,
  64. }
  65. if ENABLE_FORWARD_USER_INFO_HEADERS and user
  66. else {}
  67. ),
  68. },
  69. ssl=AIOHTTP_CLIENT_SESSION_SSL,
  70. ) as response:
  71. return await response.json()
  72. except Exception as e:
  73. # Handle connection error here
  74. log.error(f"Connection error: {e}")
  75. return None
  76. async def cleanup_response(
  77. response: Optional[aiohttp.ClientResponse],
  78. session: Optional[aiohttp.ClientSession],
  79. ):
  80. if response:
  81. response.close()
  82. if session:
  83. await session.close()
  84. def openai_reasoning_model_handler(payload):
  85. """
  86. Handle reasoning model specific parameters
  87. """
  88. if "max_tokens" in payload:
  89. # Convert "max_tokens" to "max_completion_tokens" for all reasoning models
  90. payload["max_completion_tokens"] = payload["max_tokens"]
  91. del payload["max_tokens"]
  92. # Handle system role conversion based on model type
  93. if payload["messages"][0]["role"] == "system":
  94. model_lower = payload["model"].lower()
  95. # Legacy models use "user" role instead of "system"
  96. if model_lower.startswith("o1-mini") or model_lower.startswith("o1-preview"):
  97. payload["messages"][0]["role"] = "user"
  98. else:
  99. payload["messages"][0]["role"] = "developer"
  100. return payload
  101. def get_headers_and_cookies(
  102. request: Request,
  103. url,
  104. key=None,
  105. config=None,
  106. metadata: Optional[dict] = None,
  107. user: UserModel = None,
  108. ):
  109. cookies = {}
  110. headers = {
  111. "Content-Type": "application/json",
  112. **(
  113. {
  114. "HTTP-Referer": "https://openwebui.com/",
  115. "X-Title": "Open WebUI",
  116. }
  117. if "openrouter.ai" in url
  118. else {}
  119. ),
  120. **(
  121. {
  122. "X-OpenWebUI-User-Name": quote(user.name, safe=" "),
  123. "X-OpenWebUI-User-Id": user.id,
  124. "X-OpenWebUI-User-Email": user.email,
  125. "X-OpenWebUI-User-Role": user.role,
  126. **(
  127. {"X-OpenWebUI-Chat-Id": metadata.get("chat_id")}
  128. if metadata and metadata.get("chat_id")
  129. else {}
  130. ),
  131. }
  132. if ENABLE_FORWARD_USER_INFO_HEADERS
  133. else {}
  134. ),
  135. }
  136. token = None
  137. auth_type = config.get("auth_type")
  138. if auth_type == "bearer" or auth_type is None:
  139. # Default to bearer if not specified
  140. token = f"{key}"
  141. elif auth_type == "none":
  142. token = None
  143. elif auth_type == "session":
  144. cookies = request.cookies
  145. token = request.state.token.credentials
  146. elif auth_type == "system_oauth":
  147. cookies = request.cookies
  148. oauth_token = None
  149. try:
  150. if request.cookies.get("oauth_session_id", None):
  151. oauth_token = request.app.state.oauth_manager.get_oauth_token(
  152. user.id,
  153. request.cookies.get("oauth_session_id", None),
  154. )
  155. except Exception as e:
  156. log.error(f"Error getting OAuth token: {e}")
  157. if oauth_token:
  158. token = f"{oauth_token.get('access_token', '')}"
  159. if token:
  160. headers["Authorization"] = f"Bearer {token}"
  161. return headers, cookies
  162. ##########################################
  163. #
  164. # API routes
  165. #
  166. ##########################################
  167. router = APIRouter()
  168. @router.get("/config")
  169. async def get_config(request: Request, user=Depends(get_admin_user)):
  170. return {
  171. "ENABLE_OPENAI_API": request.app.state.config.ENABLE_OPENAI_API,
  172. "OPENAI_API_BASE_URLS": request.app.state.config.OPENAI_API_BASE_URLS,
  173. "OPENAI_API_KEYS": request.app.state.config.OPENAI_API_KEYS,
  174. "OPENAI_API_CONFIGS": request.app.state.config.OPENAI_API_CONFIGS,
  175. }
  176. class OpenAIConfigForm(BaseModel):
  177. ENABLE_OPENAI_API: Optional[bool] = None
  178. OPENAI_API_BASE_URLS: list[str]
  179. OPENAI_API_KEYS: list[str]
  180. OPENAI_API_CONFIGS: dict
  181. @router.post("/config/update")
  182. async def update_config(
  183. request: Request, form_data: OpenAIConfigForm, user=Depends(get_admin_user)
  184. ):
  185. request.app.state.config.ENABLE_OPENAI_API = form_data.ENABLE_OPENAI_API
  186. request.app.state.config.OPENAI_API_BASE_URLS = form_data.OPENAI_API_BASE_URLS
  187. request.app.state.config.OPENAI_API_KEYS = form_data.OPENAI_API_KEYS
  188. # Check if API KEYS length is same than API URLS length
  189. if len(request.app.state.config.OPENAI_API_KEYS) != len(
  190. request.app.state.config.OPENAI_API_BASE_URLS
  191. ):
  192. if len(request.app.state.config.OPENAI_API_KEYS) > len(
  193. request.app.state.config.OPENAI_API_BASE_URLS
  194. ):
  195. request.app.state.config.OPENAI_API_KEYS = (
  196. request.app.state.config.OPENAI_API_KEYS[
  197. : len(request.app.state.config.OPENAI_API_BASE_URLS)
  198. ]
  199. )
  200. else:
  201. request.app.state.config.OPENAI_API_KEYS += [""] * (
  202. len(request.app.state.config.OPENAI_API_BASE_URLS)
  203. - len(request.app.state.config.OPENAI_API_KEYS)
  204. )
  205. request.app.state.config.OPENAI_API_CONFIGS = form_data.OPENAI_API_CONFIGS
  206. # Remove the API configs that are not in the API URLS
  207. keys = list(map(str, range(len(request.app.state.config.OPENAI_API_BASE_URLS))))
  208. request.app.state.config.OPENAI_API_CONFIGS = {
  209. key: value
  210. for key, value in request.app.state.config.OPENAI_API_CONFIGS.items()
  211. if key in keys
  212. }
  213. return {
  214. "ENABLE_OPENAI_API": request.app.state.config.ENABLE_OPENAI_API,
  215. "OPENAI_API_BASE_URLS": request.app.state.config.OPENAI_API_BASE_URLS,
  216. "OPENAI_API_KEYS": request.app.state.config.OPENAI_API_KEYS,
  217. "OPENAI_API_CONFIGS": request.app.state.config.OPENAI_API_CONFIGS,
  218. }
  219. @router.post("/audio/speech")
  220. async def speech(request: Request, user=Depends(get_verified_user)):
  221. idx = None
  222. try:
  223. idx = request.app.state.config.OPENAI_API_BASE_URLS.index(
  224. "https://api.openai.com/v1"
  225. )
  226. body = await request.body()
  227. name = hashlib.sha256(body).hexdigest()
  228. SPEECH_CACHE_DIR = CACHE_DIR / "audio" / "speech"
  229. SPEECH_CACHE_DIR.mkdir(parents=True, exist_ok=True)
  230. file_path = SPEECH_CACHE_DIR.joinpath(f"{name}.mp3")
  231. file_body_path = SPEECH_CACHE_DIR.joinpath(f"{name}.json")
  232. # Check if the file already exists in the cache
  233. if file_path.is_file():
  234. return FileResponse(file_path)
  235. url = request.app.state.config.OPENAI_API_BASE_URLS[idx]
  236. key = request.app.state.config.OPENAI_API_KEYS[idx]
  237. api_config = request.app.state.config.OPENAI_API_CONFIGS.get(
  238. str(idx),
  239. request.app.state.config.OPENAI_API_CONFIGS.get(url, {}), # Legacy support
  240. )
  241. headers, cookies = get_headers_and_cookies(
  242. request, url, key, api_config, user=user
  243. )
  244. r = None
  245. try:
  246. r = requests.post(
  247. url=f"{url}/audio/speech",
  248. data=body,
  249. headers=headers,
  250. cookies=cookies,
  251. stream=True,
  252. )
  253. r.raise_for_status()
  254. # Save the streaming content to a file
  255. with open(file_path, "wb") as f:
  256. for chunk in r.iter_content(chunk_size=8192):
  257. f.write(chunk)
  258. with open(file_body_path, "w") as f:
  259. json.dump(json.loads(body.decode("utf-8")), f)
  260. # Return the saved file
  261. return FileResponse(file_path)
  262. except Exception as e:
  263. log.exception(e)
  264. detail = None
  265. if r is not None:
  266. try:
  267. res = r.json()
  268. if "error" in res:
  269. detail = f"External: {res['error']}"
  270. except Exception:
  271. detail = f"External: {e}"
  272. raise HTTPException(
  273. status_code=r.status_code if r else 500,
  274. detail=detail if detail else "Open WebUI: Server Connection Error",
  275. )
  276. except ValueError:
  277. raise HTTPException(status_code=401, detail=ERROR_MESSAGES.OPENAI_NOT_FOUND)
  278. async def get_all_models_responses(request: Request, user: UserModel) -> list:
  279. if not request.app.state.config.ENABLE_OPENAI_API:
  280. return []
  281. # Check if API KEYS length is same than API URLS length
  282. num_urls = len(request.app.state.config.OPENAI_API_BASE_URLS)
  283. num_keys = len(request.app.state.config.OPENAI_API_KEYS)
  284. if num_keys != num_urls:
  285. # if there are more keys than urls, remove the extra keys
  286. if num_keys > num_urls:
  287. new_keys = request.app.state.config.OPENAI_API_KEYS[:num_urls]
  288. request.app.state.config.OPENAI_API_KEYS = new_keys
  289. # if there are more urls than keys, add empty keys
  290. else:
  291. request.app.state.config.OPENAI_API_KEYS += [""] * (num_urls - num_keys)
  292. request_tasks = []
  293. for idx, url in enumerate(request.app.state.config.OPENAI_API_BASE_URLS):
  294. if (str(idx) not in request.app.state.config.OPENAI_API_CONFIGS) and (
  295. url not in request.app.state.config.OPENAI_API_CONFIGS # Legacy support
  296. ):
  297. request_tasks.append(
  298. send_get_request(
  299. f"{url}/models",
  300. request.app.state.config.OPENAI_API_KEYS[idx],
  301. user=user,
  302. )
  303. )
  304. else:
  305. api_config = request.app.state.config.OPENAI_API_CONFIGS.get(
  306. str(idx),
  307. request.app.state.config.OPENAI_API_CONFIGS.get(
  308. url, {}
  309. ), # Legacy support
  310. )
  311. enable = api_config.get("enable", True)
  312. model_ids = api_config.get("model_ids", [])
  313. if enable:
  314. if len(model_ids) == 0:
  315. request_tasks.append(
  316. send_get_request(
  317. f"{url}/models",
  318. request.app.state.config.OPENAI_API_KEYS[idx],
  319. user=user,
  320. )
  321. )
  322. else:
  323. model_list = {
  324. "object": "list",
  325. "data": [
  326. {
  327. "id": model_id,
  328. "name": model_id,
  329. "owned_by": "openai",
  330. "openai": {"id": model_id},
  331. "urlIdx": idx,
  332. }
  333. for model_id in model_ids
  334. ],
  335. }
  336. request_tasks.append(
  337. asyncio.ensure_future(asyncio.sleep(0, model_list))
  338. )
  339. else:
  340. request_tasks.append(asyncio.ensure_future(asyncio.sleep(0, None)))
  341. responses = await asyncio.gather(*request_tasks)
  342. for idx, response in enumerate(responses):
  343. if response:
  344. url = request.app.state.config.OPENAI_API_BASE_URLS[idx]
  345. api_config = request.app.state.config.OPENAI_API_CONFIGS.get(
  346. str(idx),
  347. request.app.state.config.OPENAI_API_CONFIGS.get(
  348. url, {}
  349. ), # Legacy support
  350. )
  351. connection_type = api_config.get("connection_type", "external")
  352. prefix_id = api_config.get("prefix_id", None)
  353. tags = api_config.get("tags", [])
  354. model_list = (
  355. response if isinstance(response, list) else response.get("data", [])
  356. )
  357. if not isinstance(model_list, list):
  358. # Catch non-list responses
  359. model_list = []
  360. for model in model_list:
  361. # Remove name key if its value is None #16689
  362. if "name" in model and model["name"] is None:
  363. del model["name"]
  364. if prefix_id:
  365. model["id"] = (
  366. f"{prefix_id}.{model.get('id', model.get('name', ''))}"
  367. )
  368. if tags:
  369. model["tags"] = tags
  370. if connection_type:
  371. model["connection_type"] = connection_type
  372. log.debug(f"get_all_models:responses() {responses}")
  373. return responses
  374. async def get_filtered_models(models, user):
  375. # Filter models based on user access control
  376. filtered_models = []
  377. for model in models.get("data", []):
  378. model_info = Models.get_model_by_id(model["id"])
  379. if model_info:
  380. if user.id == model_info.user_id or has_access(
  381. user.id, type="read", access_control=model_info.access_control
  382. ):
  383. filtered_models.append(model)
  384. return filtered_models
  385. @cached(
  386. ttl=MODELS_CACHE_TTL,
  387. key=lambda _, user: f"openai_all_models_{user.id}" if user else "openai_all_models",
  388. )
  389. async def get_all_models(request: Request, user: UserModel) -> dict[str, list]:
  390. log.info("get_all_models()")
  391. if not request.app.state.config.ENABLE_OPENAI_API:
  392. return {"data": []}
  393. responses = await get_all_models_responses(request, user=user)
  394. def extract_data(response):
  395. if response and "data" in response:
  396. return response["data"]
  397. if isinstance(response, list):
  398. return response
  399. return None
  400. def merge_models_lists(model_lists):
  401. log.debug(f"merge_models_lists {model_lists}")
  402. merged_list = []
  403. for idx, models in enumerate(model_lists):
  404. if models is not None and "error" not in models:
  405. merged_list.extend(
  406. [
  407. {
  408. **model,
  409. "name": model.get("name", model["id"]),
  410. "owned_by": "openai",
  411. "openai": model,
  412. "connection_type": model.get("connection_type", "external"),
  413. "urlIdx": idx,
  414. }
  415. for model in models
  416. if (model.get("id") or model.get("name"))
  417. and (
  418. "api.openai.com"
  419. not in request.app.state.config.OPENAI_API_BASE_URLS[idx]
  420. or not any(
  421. name in model["id"]
  422. for name in [
  423. "babbage",
  424. "dall-e",
  425. "davinci",
  426. "embedding",
  427. "tts",
  428. "whisper",
  429. ]
  430. )
  431. )
  432. ]
  433. )
  434. return merged_list
  435. models = {"data": merge_models_lists(map(extract_data, responses))}
  436. log.debug(f"models: {models}")
  437. request.app.state.OPENAI_MODELS = {model["id"]: model for model in models["data"]}
  438. return models
  439. @router.get("/models")
  440. @router.get("/models/{url_idx}")
  441. async def get_models(
  442. request: Request, url_idx: Optional[int] = None, user=Depends(get_verified_user)
  443. ):
  444. models = {
  445. "data": [],
  446. }
  447. if url_idx is None:
  448. models = await get_all_models(request, user=user)
  449. else:
  450. url = request.app.state.config.OPENAI_API_BASE_URLS[url_idx]
  451. key = request.app.state.config.OPENAI_API_KEYS[url_idx]
  452. api_config = request.app.state.config.OPENAI_API_CONFIGS.get(
  453. str(url_idx),
  454. request.app.state.config.OPENAI_API_CONFIGS.get(url, {}), # Legacy support
  455. )
  456. r = None
  457. async with aiohttp.ClientSession(
  458. trust_env=True,
  459. timeout=aiohttp.ClientTimeout(total=AIOHTTP_CLIENT_TIMEOUT_MODEL_LIST),
  460. ) as session:
  461. try:
  462. headers, cookies = get_headers_and_cookies(
  463. request, url, key, api_config, user=user
  464. )
  465. if api_config.get("azure", False):
  466. models = {
  467. "data": api_config.get("model_ids", []) or [],
  468. "object": "list",
  469. }
  470. else:
  471. async with session.get(
  472. f"{url}/models",
  473. headers=headers,
  474. cookies=cookies,
  475. ssl=AIOHTTP_CLIENT_SESSION_SSL,
  476. ) as r:
  477. if r.status != 200:
  478. # Extract response error details if available
  479. error_detail = f"HTTP Error: {r.status}"
  480. res = await r.json()
  481. if "error" in res:
  482. error_detail = f"External Error: {res['error']}"
  483. raise Exception(error_detail)
  484. response_data = await r.json()
  485. # Check if we're calling OpenAI API based on the URL
  486. if "api.openai.com" in url:
  487. # Filter models according to the specified conditions
  488. response_data["data"] = [
  489. model
  490. for model in response_data.get("data", [])
  491. if not any(
  492. name in model["id"]
  493. for name in [
  494. "babbage",
  495. "dall-e",
  496. "davinci",
  497. "embedding",
  498. "tts",
  499. "whisper",
  500. ]
  501. )
  502. ]
  503. models = response_data
  504. except aiohttp.ClientError as e:
  505. # ClientError covers all aiohttp requests issues
  506. log.exception(f"Client error: {str(e)}")
  507. raise HTTPException(
  508. status_code=500, detail="Open WebUI: Server Connection Error"
  509. )
  510. except Exception as e:
  511. log.exception(f"Unexpected error: {e}")
  512. error_detail = f"Unexpected error: {str(e)}"
  513. raise HTTPException(status_code=500, detail=error_detail)
  514. if user.role == "user" and not BYPASS_MODEL_ACCESS_CONTROL:
  515. models["data"] = await get_filtered_models(models, user)
  516. return models
  517. class ConnectionVerificationForm(BaseModel):
  518. url: str
  519. key: str
  520. config: Optional[dict] = None
  521. @router.post("/verify")
  522. async def verify_connection(
  523. request: Request,
  524. form_data: ConnectionVerificationForm,
  525. user=Depends(get_admin_user),
  526. ):
  527. url = form_data.url
  528. key = form_data.key
  529. api_config = form_data.config or {}
  530. async with aiohttp.ClientSession(
  531. trust_env=True,
  532. timeout=aiohttp.ClientTimeout(total=AIOHTTP_CLIENT_TIMEOUT_MODEL_LIST),
  533. ) as session:
  534. try:
  535. headers, cookies = get_headers_and_cookies(
  536. request, url, key, api_config, user=user
  537. )
  538. if api_config.get("azure", False):
  539. headers["api-key"] = key
  540. api_version = api_config.get("api_version", "") or "2023-03-15-preview"
  541. async with session.get(
  542. url=f"{url}/openai/models?api-version={api_version}",
  543. headers=headers,
  544. cookies=cookies,
  545. ssl=AIOHTTP_CLIENT_SESSION_SSL,
  546. ) as r:
  547. try:
  548. response_data = await r.json()
  549. except Exception:
  550. response_data = await r.text()
  551. if r.status != 200:
  552. if isinstance(response_data, (dict, list)):
  553. return JSONResponse(
  554. status_code=r.status, content=response_data
  555. )
  556. else:
  557. return PlainTextResponse(
  558. status_code=r.status, content=response_data
  559. )
  560. return response_data
  561. else:
  562. async with session.get(
  563. f"{url}/models",
  564. headers=headers,
  565. cookies=cookies,
  566. ssl=AIOHTTP_CLIENT_SESSION_SSL,
  567. ) as r:
  568. try:
  569. response_data = await r.json()
  570. except Exception:
  571. response_data = await r.text()
  572. if r.status != 200:
  573. if isinstance(response_data, (dict, list)):
  574. return JSONResponse(
  575. status_code=r.status, content=response_data
  576. )
  577. else:
  578. return PlainTextResponse(
  579. status_code=r.status, content=response_data
  580. )
  581. return response_data
  582. except aiohttp.ClientError as e:
  583. # ClientError covers all aiohttp requests issues
  584. log.exception(f"Client error: {str(e)}")
  585. raise HTTPException(
  586. status_code=500, detail="Open WebUI: Server Connection Error"
  587. )
  588. except Exception as e:
  589. log.exception(f"Unexpected error: {e}")
  590. raise HTTPException(
  591. status_code=500, detail="Open WebUI: Server Connection Error"
  592. )
  593. def get_azure_allowed_params(api_version: str) -> set[str]:
  594. allowed_params = {
  595. "messages",
  596. "temperature",
  597. "role",
  598. "content",
  599. "contentPart",
  600. "contentPartImage",
  601. "enhancements",
  602. "dataSources",
  603. "n",
  604. "stream",
  605. "stop",
  606. "max_tokens",
  607. "presence_penalty",
  608. "frequency_penalty",
  609. "logit_bias",
  610. "user",
  611. "function_call",
  612. "functions",
  613. "tools",
  614. "tool_choice",
  615. "top_p",
  616. "log_probs",
  617. "top_logprobs",
  618. "response_format",
  619. "seed",
  620. "max_completion_tokens",
  621. }
  622. try:
  623. if api_version >= "2024-09-01-preview":
  624. allowed_params.add("stream_options")
  625. except ValueError:
  626. log.debug(
  627. f"Invalid API version {api_version} for Azure OpenAI. Defaulting to allowed parameters."
  628. )
  629. return allowed_params
  630. def is_openai_reasoning_model(model: str) -> bool:
  631. return model.lower().startswith(("o1", "o3", "o4", "gpt-5"))
  632. def convert_to_azure_payload(url, payload: dict, api_version: str):
  633. model = payload.get("model", "")
  634. # Filter allowed parameters based on Azure OpenAI API
  635. allowed_params = get_azure_allowed_params(api_version)
  636. # Special handling for o-series models
  637. if is_openai_reasoning_model(model):
  638. # Convert max_tokens to max_completion_tokens for o-series models
  639. if "max_tokens" in payload:
  640. payload["max_completion_tokens"] = payload["max_tokens"]
  641. del payload["max_tokens"]
  642. # Remove temperature if not 1 for o-series models
  643. if "temperature" in payload and payload["temperature"] != 1:
  644. log.debug(
  645. f"Removing temperature parameter for o-series model {model} as only default value (1) is supported"
  646. )
  647. del payload["temperature"]
  648. # Filter out unsupported parameters
  649. payload = {k: v for k, v in payload.items() if k in allowed_params}
  650. url = f"{url}/openai/deployments/{model}"
  651. return url, payload
  652. @router.post("/chat/completions")
  653. async def generate_chat_completion(
  654. request: Request,
  655. form_data: dict,
  656. user=Depends(get_verified_user),
  657. bypass_filter: Optional[bool] = False,
  658. ):
  659. if BYPASS_MODEL_ACCESS_CONTROL:
  660. bypass_filter = True
  661. idx = 0
  662. payload = {**form_data}
  663. metadata = payload.pop("metadata", None)
  664. model_id = form_data.get("model")
  665. model_info = Models.get_model_by_id(model_id)
  666. # Check model info and override the payload
  667. if model_info:
  668. if model_info.base_model_id:
  669. payload["model"] = model_info.base_model_id
  670. model_id = model_info.base_model_id
  671. params = model_info.params.model_dump()
  672. if params:
  673. system = params.pop("system", None)
  674. payload = apply_model_params_to_body_openai(params, payload)
  675. payload = apply_system_prompt_to_body(system, payload, metadata, user)
  676. # Check if user has access to the model
  677. if not bypass_filter and user.role == "user":
  678. if not (
  679. user.id == model_info.user_id
  680. or has_access(
  681. user.id, type="read", access_control=model_info.access_control
  682. )
  683. ):
  684. raise HTTPException(
  685. status_code=403,
  686. detail="Model not found",
  687. )
  688. elif not bypass_filter:
  689. if user.role != "admin":
  690. raise HTTPException(
  691. status_code=403,
  692. detail="Model not found",
  693. )
  694. await get_all_models(request, user=user)
  695. model = request.app.state.OPENAI_MODELS.get(model_id)
  696. if model:
  697. idx = model["urlIdx"]
  698. else:
  699. raise HTTPException(
  700. status_code=404,
  701. detail="Model not found",
  702. )
  703. # Get the API config for the model
  704. api_config = request.app.state.config.OPENAI_API_CONFIGS.get(
  705. str(idx),
  706. request.app.state.config.OPENAI_API_CONFIGS.get(
  707. request.app.state.config.OPENAI_API_BASE_URLS[idx], {}
  708. ), # Legacy support
  709. )
  710. prefix_id = api_config.get("prefix_id", None)
  711. if prefix_id:
  712. payload["model"] = payload["model"].replace(f"{prefix_id}.", "")
  713. # Add user info to the payload if the model is a pipeline
  714. if "pipeline" in model and model.get("pipeline"):
  715. payload["user"] = {
  716. "name": user.name,
  717. "id": user.id,
  718. "email": user.email,
  719. "role": user.role,
  720. }
  721. url = request.app.state.config.OPENAI_API_BASE_URLS[idx]
  722. key = request.app.state.config.OPENAI_API_KEYS[idx]
  723. # Check if model is a reasoning model that needs special handling
  724. if is_openai_reasoning_model(payload["model"]):
  725. payload = openai_reasoning_model_handler(payload)
  726. elif "api.openai.com" not in url:
  727. # Remove "max_completion_tokens" from the payload for backward compatibility
  728. if "max_completion_tokens" in payload:
  729. payload["max_tokens"] = payload["max_completion_tokens"]
  730. del payload["max_completion_tokens"]
  731. if "max_tokens" in payload and "max_completion_tokens" in payload:
  732. del payload["max_tokens"]
  733. # Convert the modified body back to JSON
  734. if "logit_bias" in payload:
  735. payload["logit_bias"] = json.loads(
  736. convert_logit_bias_input_to_json(payload["logit_bias"])
  737. )
  738. headers, cookies = get_headers_and_cookies(
  739. request, url, key, api_config, metadata, user=user
  740. )
  741. if api_config.get("azure", False):
  742. api_version = api_config.get("api_version", "2023-03-15-preview")
  743. request_url, payload = convert_to_azure_payload(url, payload, api_version)
  744. headers["api-key"] = key
  745. headers["api-version"] = api_version
  746. request_url = f"{request_url}/chat/completions?api-version={api_version}"
  747. else:
  748. request_url = f"{url}/chat/completions"
  749. payload = json.dumps(payload)
  750. r = None
  751. session = None
  752. streaming = False
  753. response = None
  754. try:
  755. session = aiohttp.ClientSession(
  756. trust_env=True, timeout=aiohttp.ClientTimeout(total=AIOHTTP_CLIENT_TIMEOUT)
  757. )
  758. r = await session.request(
  759. method="POST",
  760. url=request_url,
  761. data=payload,
  762. headers=headers,
  763. cookies=cookies,
  764. ssl=AIOHTTP_CLIENT_SESSION_SSL,
  765. )
  766. # Check if response is SSE
  767. if "text/event-stream" in r.headers.get("Content-Type", ""):
  768. streaming = True
  769. return StreamingResponse(
  770. r.content,
  771. status_code=r.status,
  772. headers=dict(r.headers),
  773. background=BackgroundTask(
  774. cleanup_response, response=r, session=session
  775. ),
  776. )
  777. else:
  778. try:
  779. response = await r.json()
  780. except Exception as e:
  781. log.error(e)
  782. response = await r.text()
  783. if r.status >= 400:
  784. if isinstance(response, (dict, list)):
  785. return JSONResponse(status_code=r.status, content=response)
  786. else:
  787. return PlainTextResponse(status_code=r.status, content=response)
  788. return response
  789. except Exception as e:
  790. log.exception(e)
  791. raise HTTPException(
  792. status_code=r.status if r else 500,
  793. detail="Open WebUI: Server Connection Error",
  794. )
  795. finally:
  796. if not streaming:
  797. await cleanup_response(r, session)
  798. async def embeddings(request: Request, form_data: dict, user):
  799. """
  800. Calls the embeddings endpoint for OpenAI-compatible providers.
  801. Args:
  802. request (Request): The FastAPI request context.
  803. form_data (dict): OpenAI-compatible embeddings payload.
  804. user (UserModel): The authenticated user.
  805. Returns:
  806. dict: OpenAI-compatible embeddings response.
  807. """
  808. idx = 0
  809. # Prepare payload/body
  810. body = json.dumps(form_data)
  811. # Find correct backend url/key based on model
  812. await get_all_models(request, user=user)
  813. model_id = form_data.get("model")
  814. models = request.app.state.OPENAI_MODELS
  815. if model_id in models:
  816. idx = models[model_id]["urlIdx"]
  817. url = request.app.state.config.OPENAI_API_BASE_URLS[idx]
  818. key = request.app.state.config.OPENAI_API_KEYS[idx]
  819. api_config = request.app.state.config.OPENAI_API_CONFIGS.get(
  820. str(idx),
  821. request.app.state.config.OPENAI_API_CONFIGS.get(url, {}), # Legacy support
  822. )
  823. r = None
  824. session = None
  825. streaming = False
  826. headers, cookies = get_headers_and_cookies(request, url, key, api_config, user=user)
  827. try:
  828. session = aiohttp.ClientSession(trust_env=True)
  829. r = await session.request(
  830. method="POST",
  831. url=f"{url}/embeddings",
  832. data=body,
  833. headers=headers,
  834. cookies=cookies,
  835. )
  836. if "text/event-stream" in r.headers.get("Content-Type", ""):
  837. streaming = True
  838. return StreamingResponse(
  839. r.content,
  840. status_code=r.status,
  841. headers=dict(r.headers),
  842. background=BackgroundTask(
  843. cleanup_response, response=r, session=session
  844. ),
  845. )
  846. else:
  847. try:
  848. response_data = await r.json()
  849. except Exception:
  850. response_data = await r.text()
  851. if r.status >= 400:
  852. if isinstance(response_data, (dict, list)):
  853. return JSONResponse(status_code=r.status, content=response_data)
  854. else:
  855. return PlainTextResponse(
  856. status_code=r.status, content=response_data
  857. )
  858. return response_data
  859. except Exception as e:
  860. log.exception(e)
  861. raise HTTPException(
  862. status_code=r.status if r else 500,
  863. detail="Open WebUI: Server Connection Error",
  864. )
  865. finally:
  866. if not streaming:
  867. await cleanup_response(r, session)
  868. @router.api_route("/{path:path}", methods=["GET", "POST", "PUT", "DELETE"])
  869. async def proxy(path: str, request: Request, user=Depends(get_verified_user)):
  870. """
  871. Deprecated: proxy all requests to OpenAI API
  872. """
  873. body = await request.body()
  874. idx = 0
  875. url = request.app.state.config.OPENAI_API_BASE_URLS[idx]
  876. key = request.app.state.config.OPENAI_API_KEYS[idx]
  877. api_config = request.app.state.config.OPENAI_API_CONFIGS.get(
  878. str(idx),
  879. request.app.state.config.OPENAI_API_CONFIGS.get(
  880. request.app.state.config.OPENAI_API_BASE_URLS[idx], {}
  881. ), # Legacy support
  882. )
  883. r = None
  884. session = None
  885. streaming = False
  886. try:
  887. headers, cookies = get_headers_and_cookies(
  888. request, url, key, api_config, user=user
  889. )
  890. if api_config.get("azure", False):
  891. api_version = api_config.get("api_version", "2023-03-15-preview")
  892. headers["api-key"] = key
  893. headers["api-version"] = api_version
  894. payload = json.loads(body)
  895. url, payload = convert_to_azure_payload(url, payload, api_version)
  896. body = json.dumps(payload).encode()
  897. request_url = f"{url}/{path}?api-version={api_version}"
  898. else:
  899. request_url = f"{url}/{path}"
  900. session = aiohttp.ClientSession(trust_env=True)
  901. r = await session.request(
  902. method=request.method,
  903. url=request_url,
  904. data=body,
  905. headers=headers,
  906. cookies=cookies,
  907. ssl=AIOHTTP_CLIENT_SESSION_SSL,
  908. )
  909. # Check if response is SSE
  910. if "text/event-stream" in r.headers.get("Content-Type", ""):
  911. streaming = True
  912. return StreamingResponse(
  913. r.content,
  914. status_code=r.status,
  915. headers=dict(r.headers),
  916. background=BackgroundTask(
  917. cleanup_response, response=r, session=session
  918. ),
  919. )
  920. else:
  921. try:
  922. response_data = await r.json()
  923. except Exception:
  924. response_data = await r.text()
  925. if r.status >= 400:
  926. if isinstance(response_data, (dict, list)):
  927. return JSONResponse(status_code=r.status, content=response_data)
  928. else:
  929. return PlainTextResponse(
  930. status_code=r.status, content=response_data
  931. )
  932. return response_data
  933. except Exception as e:
  934. log.exception(e)
  935. raise HTTPException(
  936. status_code=r.status if r else 500,
  937. detail="Open WebUI: Server Connection Error",
  938. )
  939. finally:
  940. if not streaming:
  941. await cleanup_response(r, session)