openai.py 36 KB

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