openai.py 36 KB

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