openai.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849
  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_o1_o3_handler(payload):
  80. """
  81. Handle o1, o3 specific parameters
  82. """
  83. if "max_tokens" in payload:
  84. # Remove "max_tokens" from the payload
  85. payload["max_completion_tokens"] = payload["max_tokens"]
  86. del payload["max_tokens"]
  87. # Fix: o1 and o3 do not support the "system" role directly.
  88. # For older models like "o1-mini" or "o1-preview", use role "user".
  89. # For newer o1/o3 models, replace "system" with "developer".
  90. if payload["messages"][0]["role"] == "system":
  91. model_lower = payload["model"].lower()
  92. if model_lower.startswith("o1-mini") or model_lower.startswith("o1-preview"):
  93. payload["messages"][0]["role"] = "user"
  94. else:
  95. payload["messages"][0]["role"] = "developer"
  96. return payload
  97. ##########################################
  98. #
  99. # API routes
  100. #
  101. ##########################################
  102. router = APIRouter()
  103. @router.get("/config")
  104. async def get_config(request: Request, user=Depends(get_admin_user)):
  105. return {
  106. "ENABLE_OPENAI_API": request.app.state.config.ENABLE_OPENAI_API,
  107. "OPENAI_API_BASE_URLS": request.app.state.config.OPENAI_API_BASE_URLS,
  108. "OPENAI_API_KEYS": request.app.state.config.OPENAI_API_KEYS,
  109. "OPENAI_API_CONFIGS": request.app.state.config.OPENAI_API_CONFIGS,
  110. }
  111. class OpenAIConfigForm(BaseModel):
  112. ENABLE_OPENAI_API: Optional[bool] = None
  113. OPENAI_API_BASE_URLS: list[str]
  114. OPENAI_API_KEYS: list[str]
  115. OPENAI_API_CONFIGS: dict
  116. @router.post("/config/update")
  117. async def update_config(
  118. request: Request, form_data: OpenAIConfigForm, user=Depends(get_admin_user)
  119. ):
  120. request.app.state.config.ENABLE_OPENAI_API = form_data.ENABLE_OPENAI_API
  121. request.app.state.config.OPENAI_API_BASE_URLS = form_data.OPENAI_API_BASE_URLS
  122. request.app.state.config.OPENAI_API_KEYS = form_data.OPENAI_API_KEYS
  123. # Check if API KEYS length is same than API URLS length
  124. if len(request.app.state.config.OPENAI_API_KEYS) != len(
  125. request.app.state.config.OPENAI_API_BASE_URLS
  126. ):
  127. if len(request.app.state.config.OPENAI_API_KEYS) > len(
  128. request.app.state.config.OPENAI_API_BASE_URLS
  129. ):
  130. request.app.state.config.OPENAI_API_KEYS = (
  131. request.app.state.config.OPENAI_API_KEYS[
  132. : len(request.app.state.config.OPENAI_API_BASE_URLS)
  133. ]
  134. )
  135. else:
  136. request.app.state.config.OPENAI_API_KEYS += [""] * (
  137. len(request.app.state.config.OPENAI_API_BASE_URLS)
  138. - len(request.app.state.config.OPENAI_API_KEYS)
  139. )
  140. request.app.state.config.OPENAI_API_CONFIGS = form_data.OPENAI_API_CONFIGS
  141. # Remove the API configs that are not in the API URLS
  142. keys = list(map(str, range(len(request.app.state.config.OPENAI_API_BASE_URLS))))
  143. request.app.state.config.OPENAI_API_CONFIGS = {
  144. key: value
  145. for key, value in request.app.state.config.OPENAI_API_CONFIGS.items()
  146. if key in keys
  147. }
  148. return {
  149. "ENABLE_OPENAI_API": request.app.state.config.ENABLE_OPENAI_API,
  150. "OPENAI_API_BASE_URLS": request.app.state.config.OPENAI_API_BASE_URLS,
  151. "OPENAI_API_KEYS": request.app.state.config.OPENAI_API_KEYS,
  152. "OPENAI_API_CONFIGS": request.app.state.config.OPENAI_API_CONFIGS,
  153. }
  154. @router.post("/audio/speech")
  155. async def speech(request: Request, user=Depends(get_verified_user)):
  156. idx = None
  157. try:
  158. idx = request.app.state.config.OPENAI_API_BASE_URLS.index(
  159. "https://api.openai.com/v1"
  160. )
  161. body = await request.body()
  162. name = hashlib.sha256(body).hexdigest()
  163. SPEECH_CACHE_DIR = CACHE_DIR / "audio" / "speech"
  164. SPEECH_CACHE_DIR.mkdir(parents=True, exist_ok=True)
  165. file_path = SPEECH_CACHE_DIR.joinpath(f"{name}.mp3")
  166. file_body_path = SPEECH_CACHE_DIR.joinpath(f"{name}.json")
  167. # Check if the file already exists in the cache
  168. if file_path.is_file():
  169. return FileResponse(file_path)
  170. url = request.app.state.config.OPENAI_API_BASE_URLS[idx]
  171. r = None
  172. try:
  173. r = requests.post(
  174. url=f"{url}/audio/speech",
  175. data=body,
  176. headers={
  177. "Content-Type": "application/json",
  178. "Authorization": f"Bearer {request.app.state.config.OPENAI_API_KEYS[idx]}",
  179. **(
  180. {
  181. "HTTP-Referer": "https://openwebui.com/",
  182. "X-Title": "Open WebUI",
  183. }
  184. if "openrouter.ai" in url
  185. else {}
  186. ),
  187. **(
  188. {
  189. "X-OpenWebUI-User-Name": user.name,
  190. "X-OpenWebUI-User-Id": user.id,
  191. "X-OpenWebUI-User-Email": user.email,
  192. "X-OpenWebUI-User-Role": user.role,
  193. }
  194. if ENABLE_FORWARD_USER_INFO_HEADERS
  195. else {}
  196. ),
  197. },
  198. stream=True,
  199. )
  200. r.raise_for_status()
  201. # Save the streaming content to a file
  202. with open(file_path, "wb") as f:
  203. for chunk in r.iter_content(chunk_size=8192):
  204. f.write(chunk)
  205. with open(file_body_path, "w") as f:
  206. json.dump(json.loads(body.decode("utf-8")), f)
  207. # Return the saved file
  208. return FileResponse(file_path)
  209. except Exception as e:
  210. log.exception(e)
  211. detail = None
  212. if r is not None:
  213. try:
  214. res = r.json()
  215. if "error" in res:
  216. detail = f"External: {res['error']}"
  217. except Exception:
  218. detail = f"External: {e}"
  219. raise HTTPException(
  220. status_code=r.status_code if r else 500,
  221. detail=detail if detail else "Open WebUI: Server Connection Error",
  222. )
  223. except ValueError:
  224. raise HTTPException(status_code=401, detail=ERROR_MESSAGES.OPENAI_NOT_FOUND)
  225. async def get_all_models_responses(request: Request, user: UserModel) -> list:
  226. if not request.app.state.config.ENABLE_OPENAI_API:
  227. return []
  228. # Check if API KEYS length is same than API URLS length
  229. num_urls = len(request.app.state.config.OPENAI_API_BASE_URLS)
  230. num_keys = len(request.app.state.config.OPENAI_API_KEYS)
  231. if num_keys != num_urls:
  232. # if there are more keys than urls, remove the extra keys
  233. if num_keys > num_urls:
  234. new_keys = request.app.state.config.OPENAI_API_KEYS[:num_urls]
  235. request.app.state.config.OPENAI_API_KEYS = new_keys
  236. # if there are more urls than keys, add empty keys
  237. else:
  238. request.app.state.config.OPENAI_API_KEYS += [""] * (num_urls - num_keys)
  239. request_tasks = []
  240. for idx, url in enumerate(request.app.state.config.OPENAI_API_BASE_URLS):
  241. if (str(idx) not in request.app.state.config.OPENAI_API_CONFIGS) and (
  242. url not in request.app.state.config.OPENAI_API_CONFIGS # Legacy support
  243. ):
  244. request_tasks.append(
  245. send_get_request(
  246. f"{url}/models",
  247. request.app.state.config.OPENAI_API_KEYS[idx],
  248. user=user,
  249. )
  250. )
  251. else:
  252. api_config = request.app.state.config.OPENAI_API_CONFIGS.get(
  253. str(idx),
  254. request.app.state.config.OPENAI_API_CONFIGS.get(
  255. url, {}
  256. ), # Legacy support
  257. )
  258. enable = api_config.get("enable", True)
  259. model_ids = api_config.get("model_ids", [])
  260. if enable:
  261. if len(model_ids) == 0:
  262. request_tasks.append(
  263. send_get_request(
  264. f"{url}/models",
  265. request.app.state.config.OPENAI_API_KEYS[idx],
  266. user=user,
  267. )
  268. )
  269. else:
  270. model_list = {
  271. "object": "list",
  272. "data": [
  273. {
  274. "id": model_id,
  275. "name": model_id,
  276. "owned_by": "openai",
  277. "openai": {"id": model_id},
  278. "urlIdx": idx,
  279. }
  280. for model_id in model_ids
  281. ],
  282. }
  283. request_tasks.append(
  284. asyncio.ensure_future(asyncio.sleep(0, model_list))
  285. )
  286. else:
  287. request_tasks.append(asyncio.ensure_future(asyncio.sleep(0, None)))
  288. responses = await asyncio.gather(*request_tasks)
  289. for idx, response in enumerate(responses):
  290. if response:
  291. url = request.app.state.config.OPENAI_API_BASE_URLS[idx]
  292. api_config = request.app.state.config.OPENAI_API_CONFIGS.get(
  293. str(idx),
  294. request.app.state.config.OPENAI_API_CONFIGS.get(
  295. url, {}
  296. ), # Legacy support
  297. )
  298. prefix_id = api_config.get("prefix_id", None)
  299. tags = api_config.get("tags", [])
  300. if prefix_id:
  301. for model in (
  302. response if isinstance(response, list) else response.get("data", [])
  303. ):
  304. model["id"] = f"{prefix_id}.{model['id']}"
  305. if tags:
  306. for model in (
  307. response if isinstance(response, list) else response.get("data", [])
  308. ):
  309. model["tags"] = tags
  310. log.debug(f"get_all_models:responses() {responses}")
  311. return responses
  312. async def get_filtered_models(models, user):
  313. # Filter models based on user access control
  314. filtered_models = []
  315. for model in models.get("data", []):
  316. model_info = Models.get_model_by_id(model["id"])
  317. if model_info:
  318. if user.id == model_info.user_id or has_access(
  319. user.id, type="read", access_control=model_info.access_control
  320. ):
  321. filtered_models.append(model)
  322. return filtered_models
  323. @cached(ttl=1)
  324. async def get_all_models(request: Request, user: UserModel) -> dict[str, list]:
  325. log.info("get_all_models()")
  326. if not request.app.state.config.ENABLE_OPENAI_API:
  327. return {"data": []}
  328. responses = await get_all_models_responses(request, user=user)
  329. def extract_data(response):
  330. if response and "data" in response:
  331. return response["data"]
  332. if isinstance(response, list):
  333. return response
  334. return None
  335. def merge_models_lists(model_lists):
  336. log.debug(f"merge_models_lists {model_lists}")
  337. merged_list = []
  338. for idx, models in enumerate(model_lists):
  339. if models is not None and "error" not in models:
  340. merged_list.extend(
  341. [
  342. {
  343. **model,
  344. "name": model.get("name", model["id"]),
  345. "owned_by": "openai",
  346. "openai": model,
  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. r = None
  387. async with aiohttp.ClientSession(
  388. timeout=aiohttp.ClientTimeout(total=AIOHTTP_CLIENT_TIMEOUT_MODEL_LIST)
  389. ) as session:
  390. try:
  391. async with session.get(
  392. f"{url}/models",
  393. headers={
  394. "Authorization": f"Bearer {key}",
  395. "Content-Type": "application/json",
  396. **(
  397. {
  398. "X-OpenWebUI-User-Name": user.name,
  399. "X-OpenWebUI-User-Id": user.id,
  400. "X-OpenWebUI-User-Email": user.email,
  401. "X-OpenWebUI-User-Role": user.role,
  402. }
  403. if ENABLE_FORWARD_USER_INFO_HEADERS
  404. else {}
  405. ),
  406. },
  407. ssl=AIOHTTP_CLIENT_SESSION_SSL,
  408. ) as r:
  409. if r.status != 200:
  410. # Extract response error details if available
  411. error_detail = f"HTTP Error: {r.status}"
  412. res = await r.json()
  413. if "error" in res:
  414. error_detail = f"External Error: {res['error']}"
  415. raise Exception(error_detail)
  416. response_data = await r.json()
  417. # Check if we're calling OpenAI API based on the URL
  418. if "api.openai.com" in url:
  419. # Filter models according to the specified conditions
  420. response_data["data"] = [
  421. model
  422. for model in response_data.get("data", [])
  423. if not any(
  424. name in model["id"]
  425. for name in [
  426. "babbage",
  427. "dall-e",
  428. "davinci",
  429. "embedding",
  430. "tts",
  431. "whisper",
  432. ]
  433. )
  434. ]
  435. models = response_data
  436. except aiohttp.ClientError as e:
  437. # ClientError covers all aiohttp requests issues
  438. log.exception(f"Client error: {str(e)}")
  439. raise HTTPException(
  440. status_code=500, detail="Open WebUI: Server Connection Error"
  441. )
  442. except Exception as e:
  443. log.exception(f"Unexpected error: {e}")
  444. error_detail = f"Unexpected error: {str(e)}"
  445. raise HTTPException(status_code=500, detail=error_detail)
  446. if user.role == "user" and not BYPASS_MODEL_ACCESS_CONTROL:
  447. models["data"] = await get_filtered_models(models, user)
  448. return models
  449. class ConnectionVerificationForm(BaseModel):
  450. url: str
  451. key: str
  452. @router.post("/verify")
  453. async def verify_connection(
  454. form_data: ConnectionVerificationForm, user=Depends(get_admin_user)
  455. ):
  456. url = form_data.url
  457. key = form_data.key
  458. async with aiohttp.ClientSession(
  459. timeout=aiohttp.ClientTimeout(total=AIOHTTP_CLIENT_TIMEOUT_MODEL_LIST)
  460. ) as session:
  461. try:
  462. async with session.get(
  463. f"{url}/models",
  464. headers={
  465. "Authorization": f"Bearer {key}",
  466. "Content-Type": "application/json",
  467. **(
  468. {
  469. "X-OpenWebUI-User-Name": user.name,
  470. "X-OpenWebUI-User-Id": user.id,
  471. "X-OpenWebUI-User-Email": user.email,
  472. "X-OpenWebUI-User-Role": user.role,
  473. }
  474. if ENABLE_FORWARD_USER_INFO_HEADERS
  475. else {}
  476. ),
  477. },
  478. ssl=AIOHTTP_CLIENT_SESSION_SSL,
  479. ) as r:
  480. if r.status != 200:
  481. # Extract response error details if available
  482. error_detail = f"HTTP Error: {r.status}"
  483. res = await r.json()
  484. if "error" in res:
  485. error_detail = f"External Error: {res['error']}"
  486. raise Exception(error_detail)
  487. response_data = await r.json()
  488. return response_data
  489. except aiohttp.ClientError as e:
  490. # ClientError covers all aiohttp requests issues
  491. log.exception(f"Client error: {str(e)}")
  492. raise HTTPException(
  493. status_code=500, detail="Open WebUI: Server Connection Error"
  494. )
  495. except Exception as e:
  496. log.exception(f"Unexpected error: {e}")
  497. error_detail = f"Unexpected error: {str(e)}"
  498. raise HTTPException(status_code=500, detail=error_detail)
  499. @router.post("/chat/completions")
  500. async def generate_chat_completion(
  501. request: Request,
  502. form_data: dict,
  503. user=Depends(get_verified_user),
  504. bypass_filter: Optional[bool] = False,
  505. ):
  506. if BYPASS_MODEL_ACCESS_CONTROL:
  507. bypass_filter = True
  508. idx = 0
  509. payload = {**form_data}
  510. metadata = payload.pop("metadata", None)
  511. model_id = form_data.get("model")
  512. model_info = Models.get_model_by_id(model_id)
  513. # Check model info and override the payload
  514. if model_info:
  515. if model_info.base_model_id:
  516. payload["model"] = model_info.base_model_id
  517. model_id = model_info.base_model_id
  518. params = model_info.params.model_dump()
  519. payload = apply_model_params_to_body_openai(params, payload)
  520. payload = apply_model_system_prompt_to_body(params, payload, metadata, user)
  521. # Check if user has access to the model
  522. if not bypass_filter and user.role == "user":
  523. if not (
  524. user.id == model_info.user_id
  525. or has_access(
  526. user.id, type="read", access_control=model_info.access_control
  527. )
  528. ):
  529. raise HTTPException(
  530. status_code=403,
  531. detail="Model not found",
  532. )
  533. elif not bypass_filter:
  534. if user.role != "admin":
  535. raise HTTPException(
  536. status_code=403,
  537. detail="Model not found",
  538. )
  539. await get_all_models(request, user=user)
  540. model = request.app.state.OPENAI_MODELS.get(model_id)
  541. if model:
  542. idx = model["urlIdx"]
  543. else:
  544. raise HTTPException(
  545. status_code=404,
  546. detail="Model not found",
  547. )
  548. # Get the API config for the model
  549. api_config = request.app.state.config.OPENAI_API_CONFIGS.get(
  550. str(idx),
  551. request.app.state.config.OPENAI_API_CONFIGS.get(
  552. request.app.state.config.OPENAI_API_BASE_URLS[idx], {}
  553. ), # Legacy support
  554. )
  555. prefix_id = api_config.get("prefix_id", None)
  556. if prefix_id:
  557. payload["model"] = payload["model"].replace(f"{prefix_id}.", "")
  558. # Add user info to the payload if the model is a pipeline
  559. if "pipeline" in model and model.get("pipeline"):
  560. payload["user"] = {
  561. "name": user.name,
  562. "id": user.id,
  563. "email": user.email,
  564. "role": user.role,
  565. }
  566. url = request.app.state.config.OPENAI_API_BASE_URLS[idx]
  567. key = request.app.state.config.OPENAI_API_KEYS[idx]
  568. # Fix: o1,o3 does not support the "max_tokens" parameter, Modify "max_tokens" to "max_completion_tokens"
  569. is_o1_o3 = payload["model"].lower().startswith(("o1", "o3-"))
  570. if is_o1_o3:
  571. payload = openai_o1_o3_handler(payload)
  572. elif "api.openai.com" not in url:
  573. # Remove "max_completion_tokens" from the payload for backward compatibility
  574. if "max_completion_tokens" in payload:
  575. payload["max_tokens"] = payload["max_completion_tokens"]
  576. del payload["max_completion_tokens"]
  577. if "max_tokens" in payload and "max_completion_tokens" in payload:
  578. del payload["max_tokens"]
  579. # Convert the modified body back to JSON
  580. if "logit_bias" in payload:
  581. payload["logit_bias"] = json.loads(
  582. convert_logit_bias_input_to_json(payload["logit_bias"])
  583. )
  584. payload = json.dumps(payload)
  585. r = None
  586. session = None
  587. streaming = False
  588. response = None
  589. try:
  590. session = aiohttp.ClientSession(
  591. trust_env=True, timeout=aiohttp.ClientTimeout(total=AIOHTTP_CLIENT_TIMEOUT)
  592. )
  593. r = await session.request(
  594. method="POST",
  595. url=f"{url}/chat/completions",
  596. data=payload,
  597. headers={
  598. "Authorization": f"Bearer {key}",
  599. "Content-Type": "application/json",
  600. **(
  601. {
  602. "HTTP-Referer": "https://openwebui.com/",
  603. "X-Title": "Open WebUI",
  604. }
  605. if "openrouter.ai" in url
  606. else {}
  607. ),
  608. **(
  609. {
  610. "X-OpenWebUI-User-Name": user.name,
  611. "X-OpenWebUI-User-Id": user.id,
  612. "X-OpenWebUI-User-Email": user.email,
  613. "X-OpenWebUI-User-Role": user.role,
  614. }
  615. if ENABLE_FORWARD_USER_INFO_HEADERS
  616. else {}
  617. ),
  618. },
  619. ssl=AIOHTTP_CLIENT_SESSION_SSL,
  620. )
  621. # Check if response is SSE
  622. if "text/event-stream" in r.headers.get("Content-Type", ""):
  623. streaming = True
  624. return StreamingResponse(
  625. r.content,
  626. status_code=r.status,
  627. headers=dict(r.headers),
  628. background=BackgroundTask(
  629. cleanup_response, response=r, session=session
  630. ),
  631. )
  632. else:
  633. try:
  634. response = await r.json()
  635. except Exception as e:
  636. log.error(e)
  637. response = await r.text()
  638. r.raise_for_status()
  639. return response
  640. except Exception as e:
  641. log.exception(e)
  642. detail = None
  643. if isinstance(response, dict):
  644. if "error" in response:
  645. detail = f"{response['error']['message'] if 'message' in response['error'] else response['error']}"
  646. elif isinstance(response, str):
  647. detail = response
  648. raise HTTPException(
  649. status_code=r.status if r else 500,
  650. detail=detail if detail else "Open WebUI: Server Connection Error",
  651. )
  652. finally:
  653. if not streaming and session:
  654. if r:
  655. r.close()
  656. await session.close()
  657. @router.api_route("/{path:path}", methods=["GET", "POST", "PUT", "DELETE"])
  658. async def proxy(path: str, request: Request, user=Depends(get_verified_user)):
  659. """
  660. Deprecated: proxy all requests to OpenAI API
  661. """
  662. body = await request.body()
  663. idx = 0
  664. url = request.app.state.config.OPENAI_API_BASE_URLS[idx]
  665. key = request.app.state.config.OPENAI_API_KEYS[idx]
  666. r = None
  667. session = None
  668. streaming = False
  669. try:
  670. session = aiohttp.ClientSession(trust_env=True)
  671. r = await session.request(
  672. method=request.method,
  673. url=f"{url}/{path}",
  674. data=body,
  675. headers={
  676. "Authorization": f"Bearer {key}",
  677. "Content-Type": "application/json",
  678. **(
  679. {
  680. "X-OpenWebUI-User-Name": user.name,
  681. "X-OpenWebUI-User-Id": user.id,
  682. "X-OpenWebUI-User-Email": user.email,
  683. "X-OpenWebUI-User-Role": user.role,
  684. }
  685. if ENABLE_FORWARD_USER_INFO_HEADERS
  686. else {}
  687. ),
  688. },
  689. ssl=AIOHTTP_CLIENT_SESSION_SSL,
  690. )
  691. r.raise_for_status()
  692. # Check if response is SSE
  693. if "text/event-stream" in r.headers.get("Content-Type", ""):
  694. streaming = True
  695. return StreamingResponse(
  696. r.content,
  697. status_code=r.status,
  698. headers=dict(r.headers),
  699. background=BackgroundTask(
  700. cleanup_response, response=r, session=session
  701. ),
  702. )
  703. else:
  704. response_data = await r.json()
  705. return response_data
  706. except Exception as e:
  707. log.exception(e)
  708. detail = None
  709. if r is not None:
  710. try:
  711. res = await r.json()
  712. log.error(res)
  713. if "error" in res:
  714. detail = f"External: {res['error']['message'] if 'message' in res['error'] else res['error']}"
  715. except Exception:
  716. detail = f"External: {e}"
  717. raise HTTPException(
  718. status_code=r.status if r else 500,
  719. detail=detail if detail else "Open WebUI: Server Connection Error",
  720. )
  721. finally:
  722. if not streaming and session:
  723. if r:
  724. r.close()
  725. await session.close()