main.py 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031
  1. import base64
  2. import uuid
  3. from contextlib import asynccontextmanager
  4. from authlib.integrations.starlette_client import OAuth
  5. from authlib.oidc.core import UserInfo
  6. from bs4 import BeautifulSoup
  7. import json
  8. import markdown
  9. import time
  10. import os
  11. import sys
  12. import logging
  13. import aiohttp
  14. import requests
  15. import mimetypes
  16. import shutil
  17. import os
  18. import uuid
  19. import inspect
  20. import asyncio
  21. from fastapi.concurrency import run_in_threadpool
  22. from fastapi import FastAPI, Request, Depends, status, UploadFile, File, Form
  23. from fastapi.staticfiles import StaticFiles
  24. from fastapi.responses import JSONResponse
  25. from fastapi import HTTPException
  26. from fastapi.middleware.wsgi import WSGIMiddleware
  27. from fastapi.middleware.cors import CORSMiddleware
  28. from starlette.exceptions import HTTPException as StarletteHTTPException
  29. from starlette.middleware.base import BaseHTTPMiddleware
  30. from starlette.middleware.sessions import SessionMiddleware
  31. from starlette.responses import StreamingResponse, Response, RedirectResponse
  32. from apps.socket.main import app as socket_app
  33. from apps.ollama.main import (
  34. app as ollama_app,
  35. OpenAIChatCompletionForm,
  36. get_all_models as get_ollama_models,
  37. generate_openai_chat_completion as generate_ollama_chat_completion,
  38. )
  39. from apps.openai.main import (
  40. app as openai_app,
  41. get_all_models as get_openai_models,
  42. generate_chat_completion as generate_openai_chat_completion,
  43. )
  44. from apps.audio.main import app as audio_app
  45. from apps.images.main import app as images_app
  46. from apps.rag.main import app as rag_app
  47. from apps.webui.main import app as webui_app, get_pipe_models
  48. from pydantic import BaseModel
  49. from typing import List, Optional, Iterator, Generator, Union
  50. from apps.webui.models.auths import Auths
  51. from apps.webui.models.models import Models, ModelModel
  52. from apps.webui.models.tools import Tools
  53. from apps.webui.models.functions import Functions
  54. from apps.webui.models.users import Users
  55. from apps.webui.utils import load_toolkit_module_by_id, load_function_module_by_id
  56. from apps.webui.utils import load_toolkit_module_by_id
  57. from utils.misc import parse_duration
  58. from utils.utils import (
  59. get_admin_user,
  60. get_verified_user,
  61. get_current_user,
  62. get_http_authorization_cred,
  63. get_password_hash,
  64. create_token,
  65. )
  66. from utils.task import (
  67. title_generation_template,
  68. search_query_generation_template,
  69. tools_function_calling_generation_template,
  70. )
  71. from utils.misc import (
  72. get_last_user_message,
  73. add_or_update_system_message,
  74. stream_message_template,
  75. )
  76. from apps.rag.utils import get_rag_context, rag_template
  77. from config import (
  78. CONFIG_DATA,
  79. WEBUI_NAME,
  80. WEBUI_URL,
  81. WEBUI_AUTH,
  82. ENV,
  83. VERSION,
  84. CHANGELOG,
  85. FRONTEND_BUILD_DIR,
  86. UPLOAD_DIR,
  87. CACHE_DIR,
  88. STATIC_DIR,
  89. ENABLE_OPENAI_API,
  90. ENABLE_OLLAMA_API,
  91. ENABLE_MODEL_FILTER,
  92. MODEL_FILTER_LIST,
  93. GLOBAL_LOG_LEVEL,
  94. SRC_LOG_LEVELS,
  95. WEBHOOK_URL,
  96. ENABLE_ADMIN_EXPORT,
  97. WEBUI_BUILD_HASH,
  98. TASK_MODEL,
  99. TASK_MODEL_EXTERNAL,
  100. TITLE_GENERATION_PROMPT_TEMPLATE,
  101. SEARCH_QUERY_GENERATION_PROMPT_TEMPLATE,
  102. SEARCH_QUERY_PROMPT_LENGTH_THRESHOLD,
  103. TOOLS_FUNCTION_CALLING_PROMPT_TEMPLATE,
  104. OAUTH_PROVIDERS,
  105. ENABLE_OAUTH_SIGNUP,
  106. OAUTH_MERGE_ACCOUNTS_BY_EMAIL,
  107. WEBUI_SECRET_KEY,
  108. WEBUI_SESSION_COOKIE_SAME_SITE,
  109. WEBUI_SESSION_COOKIE_SECURE,
  110. AppConfig,
  111. )
  112. from constants import ERROR_MESSAGES, WEBHOOK_MESSAGES
  113. from utils.webhook import post_webhook
  114. logging.basicConfig(stream=sys.stdout, level=GLOBAL_LOG_LEVEL)
  115. log = logging.getLogger(__name__)
  116. log.setLevel(SRC_LOG_LEVELS["MAIN"])
  117. class SPAStaticFiles(StaticFiles):
  118. async def get_response(self, path: str, scope):
  119. try:
  120. return await super().get_response(path, scope)
  121. except (HTTPException, StarletteHTTPException) as ex:
  122. if ex.status_code == 404:
  123. return await super().get_response("index.html", scope)
  124. else:
  125. raise ex
  126. print(
  127. rf"""
  128. ___ __ __ _ _ _ ___
  129. / _ \ _ __ ___ _ __ \ \ / /__| |__ | | | |_ _|
  130. | | | | '_ \ / _ \ '_ \ \ \ /\ / / _ \ '_ \| | | || |
  131. | |_| | |_) | __/ | | | \ V V / __/ |_) | |_| || |
  132. \___/| .__/ \___|_| |_| \_/\_/ \___|_.__/ \___/|___|
  133. |_|
  134. v{VERSION} - building the best open-source AI user interface.
  135. {f"Commit: {WEBUI_BUILD_HASH}" if WEBUI_BUILD_HASH != "dev-build" else ""}
  136. https://github.com/open-webui/open-webui
  137. """
  138. )
  139. @asynccontextmanager
  140. async def lifespan(app: FastAPI):
  141. yield
  142. app = FastAPI(
  143. docs_url="/docs" if ENV == "dev" else None, redoc_url=None, lifespan=lifespan
  144. )
  145. app.state.config = AppConfig()
  146. app.state.config.ENABLE_OPENAI_API = ENABLE_OPENAI_API
  147. app.state.config.ENABLE_OLLAMA_API = ENABLE_OLLAMA_API
  148. app.state.config.ENABLE_MODEL_FILTER = ENABLE_MODEL_FILTER
  149. app.state.config.MODEL_FILTER_LIST = MODEL_FILTER_LIST
  150. app.state.config.WEBHOOK_URL = WEBHOOK_URL
  151. app.state.config.TASK_MODEL = TASK_MODEL
  152. app.state.config.TASK_MODEL_EXTERNAL = TASK_MODEL_EXTERNAL
  153. app.state.config.TITLE_GENERATION_PROMPT_TEMPLATE = TITLE_GENERATION_PROMPT_TEMPLATE
  154. app.state.config.SEARCH_QUERY_GENERATION_PROMPT_TEMPLATE = (
  155. SEARCH_QUERY_GENERATION_PROMPT_TEMPLATE
  156. )
  157. app.state.config.SEARCH_QUERY_PROMPT_LENGTH_THRESHOLD = (
  158. SEARCH_QUERY_PROMPT_LENGTH_THRESHOLD
  159. )
  160. app.state.config.TOOLS_FUNCTION_CALLING_PROMPT_TEMPLATE = (
  161. TOOLS_FUNCTION_CALLING_PROMPT_TEMPLATE
  162. )
  163. app.state.MODELS = {}
  164. origins = ["*"]
  165. ##################################
  166. #
  167. # ChatCompletion Middleware
  168. #
  169. ##################################
  170. async def get_function_call_response(
  171. messages, files, tool_id, template, task_model_id, user
  172. ):
  173. tool = Tools.get_tool_by_id(tool_id)
  174. tools_specs = json.dumps(tool.specs, indent=2)
  175. content = tools_function_calling_generation_template(template, tools_specs)
  176. user_message = get_last_user_message(messages)
  177. prompt = (
  178. "History:\n"
  179. + "\n".join(
  180. [
  181. f"{message['role'].upper()}: \"\"\"{message['content']}\"\"\""
  182. for message in messages[::-1][:4]
  183. ]
  184. )
  185. + f"\nQuery: {user_message}"
  186. )
  187. print(prompt)
  188. payload = {
  189. "model": task_model_id,
  190. "messages": [
  191. {"role": "system", "content": content},
  192. {"role": "user", "content": f"Query: {prompt}"},
  193. ],
  194. "stream": False,
  195. }
  196. try:
  197. payload = filter_pipeline(payload, user)
  198. except Exception as e:
  199. raise e
  200. model = app.state.MODELS[task_model_id]
  201. response = None
  202. try:
  203. if model["owned_by"] == "ollama":
  204. response = await generate_ollama_chat_completion(payload, user=user)
  205. else:
  206. response = await generate_openai_chat_completion(payload, user=user)
  207. content = None
  208. if hasattr(response, "body_iterator"):
  209. async for chunk in response.body_iterator:
  210. data = json.loads(chunk.decode("utf-8"))
  211. content = data["choices"][0]["message"]["content"]
  212. # Cleanup any remaining background tasks if necessary
  213. if response.background is not None:
  214. await response.background()
  215. else:
  216. content = response["choices"][0]["message"]["content"]
  217. # Parse the function response
  218. if content is not None:
  219. print(f"content: {content}")
  220. result = json.loads(content)
  221. print(result)
  222. citation = None
  223. # Call the function
  224. if "name" in result:
  225. if tool_id in webui_app.state.TOOLS:
  226. toolkit_module = webui_app.state.TOOLS[tool_id]
  227. else:
  228. toolkit_module = load_toolkit_module_by_id(tool_id)
  229. webui_app.state.TOOLS[tool_id] = toolkit_module
  230. file_handler = False
  231. # check if toolkit_module has file_handler self variable
  232. if hasattr(toolkit_module, "file_handler"):
  233. file_handler = True
  234. print("file_handler: ", file_handler)
  235. function = getattr(toolkit_module, result["name"])
  236. function_result = None
  237. try:
  238. # Get the signature of the function
  239. sig = inspect.signature(function)
  240. params = result["parameters"]
  241. if "__user__" in sig.parameters:
  242. # Call the function with the '__user__' parameter included
  243. params = {
  244. **params,
  245. "__user__": {
  246. "id": user.id,
  247. "email": user.email,
  248. "name": user.name,
  249. "role": user.role,
  250. },
  251. }
  252. if "__messages__" in sig.parameters:
  253. # Call the function with the '__messages__' parameter included
  254. params = {
  255. **params,
  256. "__messages__": messages,
  257. }
  258. if "__files__" in sig.parameters:
  259. # Call the function with the '__files__' parameter included
  260. params = {
  261. **params,
  262. "__files__": files,
  263. }
  264. if "__model__" in sig.parameters:
  265. # Call the function with the '__model__' parameter included
  266. params = {
  267. **params,
  268. "__model__": model,
  269. }
  270. if "__id__" in sig.parameters:
  271. # Call the function with the '__id__' parameter included
  272. params = {
  273. **params,
  274. "__id__": tool_id,
  275. }
  276. if inspect.iscoroutinefunction(function):
  277. function_result = await function(**params)
  278. else:
  279. function_result = function(**params)
  280. if hasattr(toolkit_module, "citation") and toolkit_module.citation:
  281. citation = {
  282. "source": {"name": f"TOOL:{tool.name}/{result['name']}"},
  283. "document": [function_result],
  284. "metadata": [{"source": result["name"]}],
  285. }
  286. except Exception as e:
  287. print(e)
  288. # Add the function result to the system prompt
  289. if function_result is not None:
  290. return function_result, citation, file_handler
  291. except Exception as e:
  292. print(f"Error: {e}")
  293. return None, None, False
  294. class ChatCompletionMiddleware(BaseHTTPMiddleware):
  295. async def dispatch(self, request: Request, call_next):
  296. data_items = []
  297. show_citations = False
  298. citations = []
  299. if request.method == "POST" and any(
  300. endpoint in request.url.path
  301. for endpoint in ["/ollama/api/chat", "/chat/completions"]
  302. ):
  303. log.debug(f"request.url.path: {request.url.path}")
  304. # Read the original request body
  305. body = await request.body()
  306. body_str = body.decode("utf-8")
  307. data = json.loads(body_str) if body_str else {}
  308. user = get_current_user(
  309. request,
  310. get_http_authorization_cred(request.headers.get("Authorization")),
  311. )
  312. # Flag to skip RAG completions if file_handler is present in tools/functions
  313. skip_files = False
  314. if data.get("citations"):
  315. show_citations = True
  316. del data["citations"]
  317. model_id = data["model"]
  318. if model_id not in app.state.MODELS:
  319. raise HTTPException(
  320. status_code=status.HTTP_404_NOT_FOUND,
  321. detail="Model not found",
  322. )
  323. model = app.state.MODELS[model_id]
  324. # Check if the model has any filters
  325. if "info" in model and "meta" in model["info"]:
  326. for filter_id in model["info"]["meta"].get("filterIds", []):
  327. filter = Functions.get_function_by_id(filter_id)
  328. if filter:
  329. if filter_id in webui_app.state.FUNCTIONS:
  330. function_module = webui_app.state.FUNCTIONS[filter_id]
  331. else:
  332. function_module, function_type = load_function_module_by_id(
  333. filter_id
  334. )
  335. webui_app.state.FUNCTIONS[filter_id] = function_module
  336. # Check if the function has a file_handler variable
  337. if hasattr(function_module, "file_handler"):
  338. skip_files = function_module.file_handler
  339. try:
  340. if hasattr(function_module, "inlet"):
  341. inlet = function_module.inlet
  342. if inspect.iscoroutinefunction(inlet):
  343. data = await inlet(
  344. data,
  345. {
  346. "id": user.id,
  347. "email": user.email,
  348. "name": user.name,
  349. "role": user.role,
  350. },
  351. )
  352. else:
  353. data = inlet(
  354. data,
  355. {
  356. "id": user.id,
  357. "email": user.email,
  358. "name": user.name,
  359. "role": user.role,
  360. },
  361. )
  362. except Exception as e:
  363. print(f"Error: {e}")
  364. return JSONResponse(
  365. status_code=status.HTTP_400_BAD_REQUEST,
  366. content={"detail": str(e)},
  367. )
  368. # Set the task model
  369. task_model_id = data["model"]
  370. # Check if the user has a custom task model and use that model
  371. if app.state.MODELS[task_model_id]["owned_by"] == "ollama":
  372. if (
  373. app.state.config.TASK_MODEL
  374. and app.state.config.TASK_MODEL in app.state.MODELS
  375. ):
  376. task_model_id = app.state.config.TASK_MODEL
  377. else:
  378. if (
  379. app.state.config.TASK_MODEL_EXTERNAL
  380. and app.state.config.TASK_MODEL_EXTERNAL in app.state.MODELS
  381. ):
  382. task_model_id = app.state.config.TASK_MODEL_EXTERNAL
  383. prompt = get_last_user_message(data["messages"])
  384. context = ""
  385. # If tool_ids field is present, call the functions
  386. if "tool_ids" in data:
  387. print(data["tool_ids"])
  388. for tool_id in data["tool_ids"]:
  389. print(tool_id)
  390. try:
  391. response, citation, file_handler = (
  392. await get_function_call_response(
  393. messages=data["messages"],
  394. files=data.get("files", []),
  395. tool_id=tool_id,
  396. template=app.state.config.TOOLS_FUNCTION_CALLING_PROMPT_TEMPLATE,
  397. task_model_id=task_model_id,
  398. user=user,
  399. )
  400. )
  401. print(file_handler)
  402. if isinstance(response, str):
  403. context += ("\n" if context != "" else "") + response
  404. if citation:
  405. citations.append(citation)
  406. show_citations = True
  407. if file_handler:
  408. skip_files = True
  409. except Exception as e:
  410. print(f"Error: {e}")
  411. del data["tool_ids"]
  412. print(f"tool_context: {context}")
  413. # If files field is present, generate RAG completions
  414. # If skip_files is True, skip the RAG completions
  415. if "files" in data:
  416. if not skip_files:
  417. data = {**data}
  418. rag_context, rag_citations = get_rag_context(
  419. files=data["files"],
  420. messages=data["messages"],
  421. embedding_function=rag_app.state.EMBEDDING_FUNCTION,
  422. k=rag_app.state.config.TOP_K,
  423. reranking_function=rag_app.state.sentence_transformer_rf,
  424. r=rag_app.state.config.RELEVANCE_THRESHOLD,
  425. hybrid_search=rag_app.state.config.ENABLE_RAG_HYBRID_SEARCH,
  426. )
  427. if rag_context:
  428. context += ("\n" if context != "" else "") + rag_context
  429. log.debug(f"rag_context: {rag_context}, citations: {citations}")
  430. if rag_citations:
  431. citations.extend(rag_citations)
  432. del data["files"]
  433. if show_citations and len(citations) > 0:
  434. data_items.append({"citations": citations})
  435. if context != "":
  436. system_prompt = rag_template(
  437. rag_app.state.config.RAG_TEMPLATE, context, prompt
  438. )
  439. print(system_prompt)
  440. data["messages"] = add_or_update_system_message(
  441. system_prompt, data["messages"]
  442. )
  443. modified_body_bytes = json.dumps(data).encode("utf-8")
  444. # Replace the request body with the modified one
  445. request._body = modified_body_bytes
  446. # Set custom header to ensure content-length matches new body length
  447. request.headers.__dict__["_list"] = [
  448. (b"content-length", str(len(modified_body_bytes)).encode("utf-8")),
  449. *[
  450. (k, v)
  451. for k, v in request.headers.raw
  452. if k.lower() != b"content-length"
  453. ],
  454. ]
  455. response = await call_next(request)
  456. if isinstance(response, StreamingResponse):
  457. # If it's a streaming response, inject it as SSE event or NDJSON line
  458. content_type = response.headers.get("Content-Type")
  459. if "text/event-stream" in content_type:
  460. return StreamingResponse(
  461. self.openai_stream_wrapper(response.body_iterator, data_items),
  462. )
  463. if "application/x-ndjson" in content_type:
  464. return StreamingResponse(
  465. self.ollama_stream_wrapper(response.body_iterator, data_items),
  466. )
  467. else:
  468. return response
  469. # If it's not a chat completion request, just pass it through
  470. response = await call_next(request)
  471. return response
  472. async def _receive(self, body: bytes):
  473. return {"type": "http.request", "body": body, "more_body": False}
  474. async def openai_stream_wrapper(self, original_generator, data_items):
  475. for item in data_items:
  476. yield f"data: {json.dumps(item)}\n\n"
  477. async for data in original_generator:
  478. yield data
  479. async def ollama_stream_wrapper(self, original_generator, data_items):
  480. for item in data_items:
  481. yield f"{json.dumps(item)}\n"
  482. async for data in original_generator:
  483. yield data
  484. app.add_middleware(ChatCompletionMiddleware)
  485. ##################################
  486. #
  487. # Pipeline Middleware
  488. #
  489. ##################################
  490. def filter_pipeline(payload, user):
  491. user = {"id": user.id, "email": user.email, "name": user.name, "role": user.role}
  492. model_id = payload["model"]
  493. filters = [
  494. model
  495. for model in app.state.MODELS.values()
  496. if "pipeline" in model
  497. and "type" in model["pipeline"]
  498. and model["pipeline"]["type"] == "filter"
  499. and (
  500. model["pipeline"]["pipelines"] == ["*"]
  501. or any(
  502. model_id == target_model_id
  503. for target_model_id in model["pipeline"]["pipelines"]
  504. )
  505. )
  506. ]
  507. sorted_filters = sorted(filters, key=lambda x: x["pipeline"]["priority"])
  508. model = app.state.MODELS[model_id]
  509. if "pipeline" in model:
  510. sorted_filters.append(model)
  511. for filter in sorted_filters:
  512. r = None
  513. try:
  514. urlIdx = filter["urlIdx"]
  515. url = openai_app.state.config.OPENAI_API_BASE_URLS[urlIdx]
  516. key = openai_app.state.config.OPENAI_API_KEYS[urlIdx]
  517. if key != "":
  518. headers = {"Authorization": f"Bearer {key}"}
  519. r = requests.post(
  520. f"{url}/{filter['id']}/filter/inlet",
  521. headers=headers,
  522. json={
  523. "user": user,
  524. "body": payload,
  525. },
  526. )
  527. r.raise_for_status()
  528. payload = r.json()
  529. except Exception as e:
  530. # Handle connection error here
  531. print(f"Connection error: {e}")
  532. if r is not None:
  533. try:
  534. res = r.json()
  535. except:
  536. pass
  537. if "detail" in res:
  538. raise Exception(r.status_code, res["detail"])
  539. else:
  540. pass
  541. if "pipeline" not in app.state.MODELS[model_id]:
  542. if "chat_id" in payload:
  543. del payload["chat_id"]
  544. if "title" in payload:
  545. del payload["title"]
  546. if "task" in payload:
  547. del payload["task"]
  548. return payload
  549. class PipelineMiddleware(BaseHTTPMiddleware):
  550. async def dispatch(self, request: Request, call_next):
  551. if request.method == "POST" and (
  552. "/ollama/api/chat" in request.url.path
  553. or "/chat/completions" in request.url.path
  554. ):
  555. log.debug(f"request.url.path: {request.url.path}")
  556. # Read the original request body
  557. body = await request.body()
  558. # Decode body to string
  559. body_str = body.decode("utf-8")
  560. # Parse string to JSON
  561. data = json.loads(body_str) if body_str else {}
  562. user = get_current_user(
  563. request,
  564. get_http_authorization_cred(request.headers.get("Authorization")),
  565. )
  566. try:
  567. data = filter_pipeline(data, user)
  568. except Exception as e:
  569. return JSONResponse(
  570. status_code=e.args[0],
  571. content={"detail": e.args[1]},
  572. )
  573. modified_body_bytes = json.dumps(data).encode("utf-8")
  574. # Replace the request body with the modified one
  575. request._body = modified_body_bytes
  576. # Set custom header to ensure content-length matches new body length
  577. request.headers.__dict__["_list"] = [
  578. (b"content-length", str(len(modified_body_bytes)).encode("utf-8")),
  579. *[
  580. (k, v)
  581. for k, v in request.headers.raw
  582. if k.lower() != b"content-length"
  583. ],
  584. ]
  585. response = await call_next(request)
  586. return response
  587. async def _receive(self, body: bytes):
  588. return {"type": "http.request", "body": body, "more_body": False}
  589. app.add_middleware(PipelineMiddleware)
  590. app.add_middleware(
  591. CORSMiddleware,
  592. allow_origins=origins,
  593. allow_credentials=True,
  594. allow_methods=["*"],
  595. allow_headers=["*"],
  596. )
  597. @app.middleware("http")
  598. async def check_url(request: Request, call_next):
  599. if len(app.state.MODELS) == 0:
  600. await get_all_models()
  601. else:
  602. pass
  603. start_time = int(time.time())
  604. response = await call_next(request)
  605. process_time = int(time.time()) - start_time
  606. response.headers["X-Process-Time"] = str(process_time)
  607. return response
  608. @app.middleware("http")
  609. async def update_embedding_function(request: Request, call_next):
  610. response = await call_next(request)
  611. if "/embedding/update" in request.url.path:
  612. webui_app.state.EMBEDDING_FUNCTION = rag_app.state.EMBEDDING_FUNCTION
  613. return response
  614. app.mount("/ws", socket_app)
  615. app.mount("/ollama", ollama_app)
  616. app.mount("/openai", openai_app)
  617. app.mount("/images/api/v1", images_app)
  618. app.mount("/audio/api/v1", audio_app)
  619. app.mount("/rag/api/v1", rag_app)
  620. app.mount("/api/v1", webui_app)
  621. webui_app.state.EMBEDDING_FUNCTION = rag_app.state.EMBEDDING_FUNCTION
  622. async def get_all_models():
  623. pipe_models = []
  624. openai_models = []
  625. ollama_models = []
  626. pipe_models = await get_pipe_models()
  627. if app.state.config.ENABLE_OPENAI_API:
  628. openai_models = await get_openai_models()
  629. openai_models = openai_models["data"]
  630. if app.state.config.ENABLE_OLLAMA_API:
  631. ollama_models = await get_ollama_models()
  632. ollama_models = [
  633. {
  634. "id": model["model"],
  635. "name": model["name"],
  636. "object": "model",
  637. "created": int(time.time()),
  638. "owned_by": "ollama",
  639. "ollama": model,
  640. }
  641. for model in ollama_models["models"]
  642. ]
  643. models = pipe_models + openai_models + ollama_models
  644. custom_models = Models.get_all_models()
  645. for custom_model in custom_models:
  646. if custom_model.base_model_id == None:
  647. for model in models:
  648. if (
  649. custom_model.id == model["id"]
  650. or custom_model.id == model["id"].split(":")[0]
  651. ):
  652. model["name"] = custom_model.name
  653. model["info"] = custom_model.model_dump()
  654. else:
  655. owned_by = "openai"
  656. for model in models:
  657. if (
  658. custom_model.base_model_id == model["id"]
  659. or custom_model.base_model_id == model["id"].split(":")[0]
  660. ):
  661. owned_by = model["owned_by"]
  662. break
  663. models.append(
  664. {
  665. "id": custom_model.id,
  666. "name": custom_model.name,
  667. "object": "model",
  668. "created": custom_model.created_at,
  669. "owned_by": owned_by,
  670. "info": custom_model.model_dump(),
  671. "preset": True,
  672. }
  673. )
  674. app.state.MODELS = {model["id"]: model for model in models}
  675. webui_app.state.MODELS = app.state.MODELS
  676. return models
  677. @app.get("/api/models")
  678. async def get_models(user=Depends(get_verified_user)):
  679. models = await get_all_models()
  680. # Filter out filter pipelines
  681. models = [
  682. model
  683. for model in models
  684. if "pipeline" not in model or model["pipeline"].get("type", None) != "filter"
  685. ]
  686. if app.state.config.ENABLE_MODEL_FILTER:
  687. if user.role == "user":
  688. models = list(
  689. filter(
  690. lambda model: model["id"] in app.state.config.MODEL_FILTER_LIST,
  691. models,
  692. )
  693. )
  694. return {"data": models}
  695. return {"data": models}
  696. @app.post("/api/chat/completions")
  697. async def generate_chat_completions(form_data: dict, user=Depends(get_verified_user)):
  698. model_id = form_data["model"]
  699. if model_id not in app.state.MODELS:
  700. raise HTTPException(
  701. status_code=status.HTTP_404_NOT_FOUND,
  702. detail="Model not found",
  703. )
  704. model = app.state.MODELS[model_id]
  705. print(model)
  706. pipe = model.get("pipe")
  707. if pipe:
  708. form_data["user"] = {
  709. "id": user.id,
  710. "email": user.email,
  711. "name": user.name,
  712. "role": user.role,
  713. }
  714. async def job():
  715. pipe_id = form_data["model"]
  716. if "." in pipe_id:
  717. pipe_id, sub_pipe_id = pipe_id.split(".", 1)
  718. print(pipe_id)
  719. pipe = webui_app.state.FUNCTIONS[pipe_id].pipe
  720. if form_data["stream"]:
  721. async def stream_content():
  722. if inspect.iscoroutinefunction(pipe):
  723. res = await pipe(body=form_data)
  724. else:
  725. res = pipe(body=form_data)
  726. if isinstance(res, str):
  727. message = stream_message_template(form_data["model"], res)
  728. yield f"data: {json.dumps(message)}\n\n"
  729. if isinstance(res, Iterator):
  730. for line in res:
  731. if isinstance(line, BaseModel):
  732. line = line.model_dump_json()
  733. line = f"data: {line}"
  734. try:
  735. line = line.decode("utf-8")
  736. except:
  737. pass
  738. if line.startswith("data:"):
  739. yield f"{line}\n\n"
  740. else:
  741. line = stream_message_template(form_data["model"], line)
  742. yield f"data: {json.dumps(line)}\n\n"
  743. if isinstance(res, str) or isinstance(res, Generator):
  744. finish_message = {
  745. "id": f"{form_data['model']}-{str(uuid.uuid4())}",
  746. "object": "chat.completion.chunk",
  747. "created": int(time.time()),
  748. "model": form_data["model"],
  749. "choices": [
  750. {
  751. "index": 0,
  752. "delta": {},
  753. "logprobs": None,
  754. "finish_reason": "stop",
  755. }
  756. ],
  757. }
  758. yield f"data: {json.dumps(finish_message)}\n\n"
  759. yield f"data: [DONE]"
  760. return StreamingResponse(
  761. stream_content(), media_type="text/event-stream"
  762. )
  763. else:
  764. if inspect.iscoroutinefunction(pipe):
  765. res = await pipe(body=form_data)
  766. else:
  767. res = pipe(body=form_data)
  768. if isinstance(res, dict):
  769. return res
  770. elif isinstance(res, BaseModel):
  771. return res.model_dump()
  772. else:
  773. message = ""
  774. if isinstance(res, str):
  775. message = res
  776. if isinstance(res, Generator):
  777. for stream in res:
  778. message = f"{message}{stream}"
  779. return {
  780. "id": f"{form_data['model']}-{str(uuid.uuid4())}",
  781. "object": "chat.completion",
  782. "created": int(time.time()),
  783. "model": form_data["model"],
  784. "choices": [
  785. {
  786. "index": 0,
  787. "message": {
  788. "role": "assistant",
  789. "content": message,
  790. },
  791. "logprobs": None,
  792. "finish_reason": "stop",
  793. }
  794. ],
  795. }
  796. return await job()
  797. if model["owned_by"] == "ollama":
  798. return await generate_ollama_chat_completion(form_data, user=user)
  799. else:
  800. return await generate_openai_chat_completion(form_data, user=user)
  801. @app.post("/api/chat/completed")
  802. async def chat_completed(form_data: dict, user=Depends(get_verified_user)):
  803. data = form_data
  804. model_id = data["model"]
  805. if model_id not in app.state.MODELS:
  806. raise HTTPException(
  807. status_code=status.HTTP_404_NOT_FOUND,
  808. detail="Model not found",
  809. )
  810. model = app.state.MODELS[model_id]
  811. filters = [
  812. model
  813. for model in app.state.MODELS.values()
  814. if "pipeline" in model
  815. and "type" in model["pipeline"]
  816. and model["pipeline"]["type"] == "filter"
  817. and (
  818. model["pipeline"]["pipelines"] == ["*"]
  819. or any(
  820. model_id == target_model_id
  821. for target_model_id in model["pipeline"]["pipelines"]
  822. )
  823. )
  824. ]
  825. sorted_filters = sorted(filters, key=lambda x: x["pipeline"]["priority"])
  826. if "pipeline" in model:
  827. sorted_filters = [model] + sorted_filters
  828. for filter in sorted_filters:
  829. r = None
  830. try:
  831. urlIdx = filter["urlIdx"]
  832. url = openai_app.state.config.OPENAI_API_BASE_URLS[urlIdx]
  833. key = openai_app.state.config.OPENAI_API_KEYS[urlIdx]
  834. if key != "":
  835. headers = {"Authorization": f"Bearer {key}"}
  836. r = requests.post(
  837. f"{url}/{filter['id']}/filter/outlet",
  838. headers=headers,
  839. json={
  840. "user": {"id": user.id, "name": user.name, "role": user.role},
  841. "body": data,
  842. },
  843. )
  844. r.raise_for_status()
  845. data = r.json()
  846. except Exception as e:
  847. # Handle connection error here
  848. print(f"Connection error: {e}")
  849. if r is not None:
  850. try:
  851. res = r.json()
  852. if "detail" in res:
  853. return JSONResponse(
  854. status_code=r.status_code,
  855. content=res,
  856. )
  857. except:
  858. pass
  859. else:
  860. pass
  861. # Check if the model has any filters
  862. if "info" in model and "meta" in model["info"]:
  863. for filter_id in model["info"]["meta"].get("filterIds", []):
  864. filter = Functions.get_function_by_id(filter_id)
  865. if filter:
  866. if filter_id in webui_app.state.FUNCTIONS:
  867. function_module = webui_app.state.FUNCTIONS[filter_id]
  868. else:
  869. function_module, function_type = load_function_module_by_id(
  870. filter_id
  871. )
  872. webui_app.state.FUNCTIONS[filter_id] = function_module
  873. try:
  874. if hasattr(function_module, "outlet"):
  875. outlet = function_module.outlet
  876. if inspect.iscoroutinefunction(outlet):
  877. data = await outlet(
  878. data,
  879. {
  880. "id": user.id,
  881. "email": user.email,
  882. "name": user.name,
  883. "role": user.role,
  884. },
  885. )
  886. else:
  887. data = outlet(
  888. data,
  889. {
  890. "id": user.id,
  891. "email": user.email,
  892. "name": user.name,
  893. "role": user.role,
  894. },
  895. )
  896. except Exception as e:
  897. print(f"Error: {e}")
  898. return JSONResponse(
  899. status_code=status.HTTP_400_BAD_REQUEST,
  900. content={"detail": str(e)},
  901. )
  902. return data
  903. ##################################
  904. #
  905. # Task Endpoints
  906. #
  907. ##################################
  908. # TODO: Refactor task API endpoints below into a separate file
  909. @app.get("/api/task/config")
  910. async def get_task_config(user=Depends(get_verified_user)):
  911. return {
  912. "TASK_MODEL": app.state.config.TASK_MODEL,
  913. "TASK_MODEL_EXTERNAL": app.state.config.TASK_MODEL_EXTERNAL,
  914. "TITLE_GENERATION_PROMPT_TEMPLATE": app.state.config.TITLE_GENERATION_PROMPT_TEMPLATE,
  915. "SEARCH_QUERY_GENERATION_PROMPT_TEMPLATE": app.state.config.SEARCH_QUERY_GENERATION_PROMPT_TEMPLATE,
  916. "SEARCH_QUERY_PROMPT_LENGTH_THRESHOLD": app.state.config.SEARCH_QUERY_PROMPT_LENGTH_THRESHOLD,
  917. "TOOLS_FUNCTION_CALLING_PROMPT_TEMPLATE": app.state.config.TOOLS_FUNCTION_CALLING_PROMPT_TEMPLATE,
  918. }
  919. class TaskConfigForm(BaseModel):
  920. TASK_MODEL: Optional[str]
  921. TASK_MODEL_EXTERNAL: Optional[str]
  922. TITLE_GENERATION_PROMPT_TEMPLATE: str
  923. SEARCH_QUERY_GENERATION_PROMPT_TEMPLATE: str
  924. SEARCH_QUERY_PROMPT_LENGTH_THRESHOLD: int
  925. TOOLS_FUNCTION_CALLING_PROMPT_TEMPLATE: str
  926. @app.post("/api/task/config/update")
  927. async def update_task_config(form_data: TaskConfigForm, user=Depends(get_admin_user)):
  928. app.state.config.TASK_MODEL = form_data.TASK_MODEL
  929. app.state.config.TASK_MODEL_EXTERNAL = form_data.TASK_MODEL_EXTERNAL
  930. app.state.config.TITLE_GENERATION_PROMPT_TEMPLATE = (
  931. form_data.TITLE_GENERATION_PROMPT_TEMPLATE
  932. )
  933. app.state.config.SEARCH_QUERY_GENERATION_PROMPT_TEMPLATE = (
  934. form_data.SEARCH_QUERY_GENERATION_PROMPT_TEMPLATE
  935. )
  936. app.state.config.SEARCH_QUERY_PROMPT_LENGTH_THRESHOLD = (
  937. form_data.SEARCH_QUERY_PROMPT_LENGTH_THRESHOLD
  938. )
  939. app.state.config.TOOLS_FUNCTION_CALLING_PROMPT_TEMPLATE = (
  940. form_data.TOOLS_FUNCTION_CALLING_PROMPT_TEMPLATE
  941. )
  942. return {
  943. "TASK_MODEL": app.state.config.TASK_MODEL,
  944. "TASK_MODEL_EXTERNAL": app.state.config.TASK_MODEL_EXTERNAL,
  945. "TITLE_GENERATION_PROMPT_TEMPLATE": app.state.config.TITLE_GENERATION_PROMPT_TEMPLATE,
  946. "SEARCH_QUERY_GENERATION_PROMPT_TEMPLATE": app.state.config.SEARCH_QUERY_GENERATION_PROMPT_TEMPLATE,
  947. "SEARCH_QUERY_PROMPT_LENGTH_THRESHOLD": app.state.config.SEARCH_QUERY_PROMPT_LENGTH_THRESHOLD,
  948. "TOOLS_FUNCTION_CALLING_PROMPT_TEMPLATE": app.state.config.TOOLS_FUNCTION_CALLING_PROMPT_TEMPLATE,
  949. }
  950. @app.post("/api/task/title/completions")
  951. async def generate_title(form_data: dict, user=Depends(get_verified_user)):
  952. print("generate_title")
  953. model_id = form_data["model"]
  954. if model_id not in app.state.MODELS:
  955. raise HTTPException(
  956. status_code=status.HTTP_404_NOT_FOUND,
  957. detail="Model not found",
  958. )
  959. # Check if the user has a custom task model
  960. # If the user has a custom task model, use that model
  961. if app.state.MODELS[model_id]["owned_by"] == "ollama":
  962. if app.state.config.TASK_MODEL:
  963. task_model_id = app.state.config.TASK_MODEL
  964. if task_model_id in app.state.MODELS:
  965. model_id = task_model_id
  966. else:
  967. if app.state.config.TASK_MODEL_EXTERNAL:
  968. task_model_id = app.state.config.TASK_MODEL_EXTERNAL
  969. if task_model_id in app.state.MODELS:
  970. model_id = task_model_id
  971. print(model_id)
  972. model = app.state.MODELS[model_id]
  973. template = app.state.config.TITLE_GENERATION_PROMPT_TEMPLATE
  974. content = title_generation_template(
  975. template,
  976. form_data["prompt"],
  977. {
  978. "name": user.name,
  979. "location": user.info.get("location") if user.info else None,
  980. },
  981. )
  982. payload = {
  983. "model": model_id,
  984. "messages": [{"role": "user", "content": content}],
  985. "stream": False,
  986. "max_tokens": 50,
  987. "chat_id": form_data.get("chat_id", None),
  988. "title": True,
  989. }
  990. log.debug(payload)
  991. try:
  992. payload = filter_pipeline(payload, user)
  993. except Exception as e:
  994. return JSONResponse(
  995. status_code=e.args[0],
  996. content={"detail": e.args[1]},
  997. )
  998. if model["owned_by"] == "ollama":
  999. return await generate_ollama_chat_completion(payload, user=user)
  1000. else:
  1001. return await generate_openai_chat_completion(payload, user=user)
  1002. @app.post("/api/task/query/completions")
  1003. async def generate_search_query(form_data: dict, user=Depends(get_verified_user)):
  1004. print("generate_search_query")
  1005. if len(form_data["prompt"]) < app.state.config.SEARCH_QUERY_PROMPT_LENGTH_THRESHOLD:
  1006. raise HTTPException(
  1007. status_code=status.HTTP_400_BAD_REQUEST,
  1008. detail=f"Skip search query generation for short prompts (< {app.state.config.SEARCH_QUERY_PROMPT_LENGTH_THRESHOLD} characters)",
  1009. )
  1010. model_id = form_data["model"]
  1011. if model_id not in app.state.MODELS:
  1012. raise HTTPException(
  1013. status_code=status.HTTP_404_NOT_FOUND,
  1014. detail="Model not found",
  1015. )
  1016. # Check if the user has a custom task model
  1017. # If the user has a custom task model, use that model
  1018. if app.state.MODELS[model_id]["owned_by"] == "ollama":
  1019. if app.state.config.TASK_MODEL:
  1020. task_model_id = app.state.config.TASK_MODEL
  1021. if task_model_id in app.state.MODELS:
  1022. model_id = task_model_id
  1023. else:
  1024. if app.state.config.TASK_MODEL_EXTERNAL:
  1025. task_model_id = app.state.config.TASK_MODEL_EXTERNAL
  1026. if task_model_id in app.state.MODELS:
  1027. model_id = task_model_id
  1028. print(model_id)
  1029. model = app.state.MODELS[model_id]
  1030. template = app.state.config.SEARCH_QUERY_GENERATION_PROMPT_TEMPLATE
  1031. content = search_query_generation_template(
  1032. template, form_data["prompt"], {"name": user.name}
  1033. )
  1034. payload = {
  1035. "model": model_id,
  1036. "messages": [{"role": "user", "content": content}],
  1037. "stream": False,
  1038. "max_tokens": 30,
  1039. "task": True,
  1040. }
  1041. print(payload)
  1042. try:
  1043. payload = filter_pipeline(payload, user)
  1044. except Exception as e:
  1045. return JSONResponse(
  1046. status_code=e.args[0],
  1047. content={"detail": e.args[1]},
  1048. )
  1049. if model["owned_by"] == "ollama":
  1050. return await generate_ollama_chat_completion(payload, user=user)
  1051. else:
  1052. return await generate_openai_chat_completion(payload, user=user)
  1053. @app.post("/api/task/emoji/completions")
  1054. async def generate_emoji(form_data: dict, user=Depends(get_verified_user)):
  1055. print("generate_emoji")
  1056. model_id = form_data["model"]
  1057. if model_id not in app.state.MODELS:
  1058. raise HTTPException(
  1059. status_code=status.HTTP_404_NOT_FOUND,
  1060. detail="Model not found",
  1061. )
  1062. # Check if the user has a custom task model
  1063. # If the user has a custom task model, use that model
  1064. if app.state.MODELS[model_id]["owned_by"] == "ollama":
  1065. if app.state.config.TASK_MODEL:
  1066. task_model_id = app.state.config.TASK_MODEL
  1067. if task_model_id in app.state.MODELS:
  1068. model_id = task_model_id
  1069. else:
  1070. if app.state.config.TASK_MODEL_EXTERNAL:
  1071. task_model_id = app.state.config.TASK_MODEL_EXTERNAL
  1072. if task_model_id in app.state.MODELS:
  1073. model_id = task_model_id
  1074. print(model_id)
  1075. model = app.state.MODELS[model_id]
  1076. template = '''
  1077. Your task is to reflect the speaker's likely facial expression through a fitting emoji. Interpret emotions from the message and reflect their facial expression using fitting, diverse emojis (e.g., 😊, 😢, 😡, 😱).
  1078. Message: """{{prompt}}"""
  1079. '''
  1080. content = title_generation_template(
  1081. template,
  1082. form_data["prompt"],
  1083. {
  1084. "name": user.name,
  1085. "location": user.info.get("location") if user.info else None,
  1086. },
  1087. )
  1088. payload = {
  1089. "model": model_id,
  1090. "messages": [{"role": "user", "content": content}],
  1091. "stream": False,
  1092. "max_tokens": 4,
  1093. "chat_id": form_data.get("chat_id", None),
  1094. "task": True,
  1095. }
  1096. log.debug(payload)
  1097. try:
  1098. payload = filter_pipeline(payload, user)
  1099. except Exception as e:
  1100. return JSONResponse(
  1101. status_code=e.args[0],
  1102. content={"detail": e.args[1]},
  1103. )
  1104. if model["owned_by"] == "ollama":
  1105. return await generate_ollama_chat_completion(payload, user=user)
  1106. else:
  1107. return await generate_openai_chat_completion(payload, user=user)
  1108. @app.post("/api/task/tools/completions")
  1109. async def get_tools_function_calling(form_data: dict, user=Depends(get_verified_user)):
  1110. print("get_tools_function_calling")
  1111. model_id = form_data["model"]
  1112. if model_id not in app.state.MODELS:
  1113. raise HTTPException(
  1114. status_code=status.HTTP_404_NOT_FOUND,
  1115. detail="Model not found",
  1116. )
  1117. # Check if the user has a custom task model
  1118. # If the user has a custom task model, use that model
  1119. if app.state.MODELS[model_id]["owned_by"] == "ollama":
  1120. if app.state.config.TASK_MODEL:
  1121. task_model_id = app.state.config.TASK_MODEL
  1122. if task_model_id in app.state.MODELS:
  1123. model_id = task_model_id
  1124. else:
  1125. if app.state.config.TASK_MODEL_EXTERNAL:
  1126. task_model_id = app.state.config.TASK_MODEL_EXTERNAL
  1127. if task_model_id in app.state.MODELS:
  1128. model_id = task_model_id
  1129. print(model_id)
  1130. template = app.state.config.TOOLS_FUNCTION_CALLING_PROMPT_TEMPLATE
  1131. try:
  1132. context, citation, file_handler = await get_function_call_response(
  1133. form_data["messages"],
  1134. form_data.get("files", []),
  1135. form_data["tool_id"],
  1136. template,
  1137. model_id,
  1138. user,
  1139. )
  1140. return context
  1141. except Exception as e:
  1142. return JSONResponse(
  1143. status_code=e.args[0],
  1144. content={"detail": e.args[1]},
  1145. )
  1146. ##################################
  1147. #
  1148. # Pipelines Endpoints
  1149. #
  1150. ##################################
  1151. # TODO: Refactor pipelines API endpoints below into a separate file
  1152. @app.get("/api/pipelines/list")
  1153. async def get_pipelines_list(user=Depends(get_admin_user)):
  1154. responses = await get_openai_models(raw=True)
  1155. print(responses)
  1156. urlIdxs = [
  1157. idx
  1158. for idx, response in enumerate(responses)
  1159. if response != None and "pipelines" in response
  1160. ]
  1161. return {
  1162. "data": [
  1163. {
  1164. "url": openai_app.state.config.OPENAI_API_BASE_URLS[urlIdx],
  1165. "idx": urlIdx,
  1166. }
  1167. for urlIdx in urlIdxs
  1168. ]
  1169. }
  1170. @app.post("/api/pipelines/upload")
  1171. async def upload_pipeline(
  1172. urlIdx: int = Form(...), file: UploadFile = File(...), user=Depends(get_admin_user)
  1173. ):
  1174. print("upload_pipeline", urlIdx, file.filename)
  1175. # Check if the uploaded file is a python file
  1176. if not file.filename.endswith(".py"):
  1177. raise HTTPException(
  1178. status_code=status.HTTP_400_BAD_REQUEST,
  1179. detail="Only Python (.py) files are allowed.",
  1180. )
  1181. upload_folder = f"{CACHE_DIR}/pipelines"
  1182. os.makedirs(upload_folder, exist_ok=True)
  1183. file_path = os.path.join(upload_folder, file.filename)
  1184. try:
  1185. # Save the uploaded file
  1186. with open(file_path, "wb") as buffer:
  1187. shutil.copyfileobj(file.file, buffer)
  1188. url = openai_app.state.config.OPENAI_API_BASE_URLS[urlIdx]
  1189. key = openai_app.state.config.OPENAI_API_KEYS[urlIdx]
  1190. headers = {"Authorization": f"Bearer {key}"}
  1191. with open(file_path, "rb") as f:
  1192. files = {"file": f}
  1193. r = requests.post(f"{url}/pipelines/upload", headers=headers, files=files)
  1194. r.raise_for_status()
  1195. data = r.json()
  1196. return {**data}
  1197. except Exception as e:
  1198. # Handle connection error here
  1199. print(f"Connection error: {e}")
  1200. detail = "Pipeline not found"
  1201. if r is not None:
  1202. try:
  1203. res = r.json()
  1204. if "detail" in res:
  1205. detail = res["detail"]
  1206. except:
  1207. pass
  1208. raise HTTPException(
  1209. status_code=(r.status_code if r is not None else status.HTTP_404_NOT_FOUND),
  1210. detail=detail,
  1211. )
  1212. finally:
  1213. # Ensure the file is deleted after the upload is completed or on failure
  1214. if os.path.exists(file_path):
  1215. os.remove(file_path)
  1216. class AddPipelineForm(BaseModel):
  1217. url: str
  1218. urlIdx: int
  1219. @app.post("/api/pipelines/add")
  1220. async def add_pipeline(form_data: AddPipelineForm, user=Depends(get_admin_user)):
  1221. r = None
  1222. try:
  1223. urlIdx = form_data.urlIdx
  1224. url = openai_app.state.config.OPENAI_API_BASE_URLS[urlIdx]
  1225. key = openai_app.state.config.OPENAI_API_KEYS[urlIdx]
  1226. headers = {"Authorization": f"Bearer {key}"}
  1227. r = requests.post(
  1228. f"{url}/pipelines/add", headers=headers, json={"url": form_data.url}
  1229. )
  1230. r.raise_for_status()
  1231. data = r.json()
  1232. return {**data}
  1233. except Exception as e:
  1234. # Handle connection error here
  1235. print(f"Connection error: {e}")
  1236. detail = "Pipeline not found"
  1237. if r is not None:
  1238. try:
  1239. res = r.json()
  1240. if "detail" in res:
  1241. detail = res["detail"]
  1242. except:
  1243. pass
  1244. raise HTTPException(
  1245. status_code=(r.status_code if r is not None else status.HTTP_404_NOT_FOUND),
  1246. detail=detail,
  1247. )
  1248. class DeletePipelineForm(BaseModel):
  1249. id: str
  1250. urlIdx: int
  1251. @app.delete("/api/pipelines/delete")
  1252. async def delete_pipeline(form_data: DeletePipelineForm, user=Depends(get_admin_user)):
  1253. r = None
  1254. try:
  1255. urlIdx = form_data.urlIdx
  1256. url = openai_app.state.config.OPENAI_API_BASE_URLS[urlIdx]
  1257. key = openai_app.state.config.OPENAI_API_KEYS[urlIdx]
  1258. headers = {"Authorization": f"Bearer {key}"}
  1259. r = requests.delete(
  1260. f"{url}/pipelines/delete", headers=headers, json={"id": form_data.id}
  1261. )
  1262. r.raise_for_status()
  1263. data = r.json()
  1264. return {**data}
  1265. except Exception as e:
  1266. # Handle connection error here
  1267. print(f"Connection error: {e}")
  1268. detail = "Pipeline not found"
  1269. if r is not None:
  1270. try:
  1271. res = r.json()
  1272. if "detail" in res:
  1273. detail = res["detail"]
  1274. except:
  1275. pass
  1276. raise HTTPException(
  1277. status_code=(r.status_code if r is not None else status.HTTP_404_NOT_FOUND),
  1278. detail=detail,
  1279. )
  1280. @app.get("/api/pipelines")
  1281. async def get_pipelines(urlIdx: Optional[int] = None, user=Depends(get_admin_user)):
  1282. r = None
  1283. try:
  1284. urlIdx
  1285. url = openai_app.state.config.OPENAI_API_BASE_URLS[urlIdx]
  1286. key = openai_app.state.config.OPENAI_API_KEYS[urlIdx]
  1287. headers = {"Authorization": f"Bearer {key}"}
  1288. r = requests.get(f"{url}/pipelines", headers=headers)
  1289. r.raise_for_status()
  1290. data = r.json()
  1291. return {**data}
  1292. except Exception as e:
  1293. # Handle connection error here
  1294. print(f"Connection error: {e}")
  1295. detail = "Pipeline not found"
  1296. if r is not None:
  1297. try:
  1298. res = r.json()
  1299. if "detail" in res:
  1300. detail = res["detail"]
  1301. except:
  1302. pass
  1303. raise HTTPException(
  1304. status_code=(r.status_code if r is not None else status.HTTP_404_NOT_FOUND),
  1305. detail=detail,
  1306. )
  1307. @app.get("/api/pipelines/{pipeline_id}/valves")
  1308. async def get_pipeline_valves(
  1309. urlIdx: Optional[int], pipeline_id: str, user=Depends(get_admin_user)
  1310. ):
  1311. models = await get_all_models()
  1312. r = None
  1313. try:
  1314. url = openai_app.state.config.OPENAI_API_BASE_URLS[urlIdx]
  1315. key = openai_app.state.config.OPENAI_API_KEYS[urlIdx]
  1316. headers = {"Authorization": f"Bearer {key}"}
  1317. r = requests.get(f"{url}/{pipeline_id}/valves", headers=headers)
  1318. r.raise_for_status()
  1319. data = r.json()
  1320. return {**data}
  1321. except Exception as e:
  1322. # Handle connection error here
  1323. print(f"Connection error: {e}")
  1324. detail = "Pipeline not found"
  1325. if r is not None:
  1326. try:
  1327. res = r.json()
  1328. if "detail" in res:
  1329. detail = res["detail"]
  1330. except:
  1331. pass
  1332. raise HTTPException(
  1333. status_code=(r.status_code if r is not None else status.HTTP_404_NOT_FOUND),
  1334. detail=detail,
  1335. )
  1336. @app.get("/api/pipelines/{pipeline_id}/valves/spec")
  1337. async def get_pipeline_valves_spec(
  1338. urlIdx: Optional[int], pipeline_id: str, user=Depends(get_admin_user)
  1339. ):
  1340. models = await get_all_models()
  1341. r = None
  1342. try:
  1343. url = openai_app.state.config.OPENAI_API_BASE_URLS[urlIdx]
  1344. key = openai_app.state.config.OPENAI_API_KEYS[urlIdx]
  1345. headers = {"Authorization": f"Bearer {key}"}
  1346. r = requests.get(f"{url}/{pipeline_id}/valves/spec", headers=headers)
  1347. r.raise_for_status()
  1348. data = r.json()
  1349. return {**data}
  1350. except Exception as e:
  1351. # Handle connection error here
  1352. print(f"Connection error: {e}")
  1353. detail = "Pipeline not found"
  1354. if r is not None:
  1355. try:
  1356. res = r.json()
  1357. if "detail" in res:
  1358. detail = res["detail"]
  1359. except:
  1360. pass
  1361. raise HTTPException(
  1362. status_code=(r.status_code if r is not None else status.HTTP_404_NOT_FOUND),
  1363. detail=detail,
  1364. )
  1365. @app.post("/api/pipelines/{pipeline_id}/valves/update")
  1366. async def update_pipeline_valves(
  1367. urlIdx: Optional[int],
  1368. pipeline_id: str,
  1369. form_data: dict,
  1370. user=Depends(get_admin_user),
  1371. ):
  1372. models = await get_all_models()
  1373. r = None
  1374. try:
  1375. url = openai_app.state.config.OPENAI_API_BASE_URLS[urlIdx]
  1376. key = openai_app.state.config.OPENAI_API_KEYS[urlIdx]
  1377. headers = {"Authorization": f"Bearer {key}"}
  1378. r = requests.post(
  1379. f"{url}/{pipeline_id}/valves/update",
  1380. headers=headers,
  1381. json={**form_data},
  1382. )
  1383. r.raise_for_status()
  1384. data = r.json()
  1385. return {**data}
  1386. except Exception as e:
  1387. # Handle connection error here
  1388. print(f"Connection error: {e}")
  1389. detail = "Pipeline not found"
  1390. if r is not None:
  1391. try:
  1392. res = r.json()
  1393. if "detail" in res:
  1394. detail = res["detail"]
  1395. except:
  1396. pass
  1397. raise HTTPException(
  1398. status_code=(r.status_code if r is not None else status.HTTP_404_NOT_FOUND),
  1399. detail=detail,
  1400. )
  1401. ##################################
  1402. #
  1403. # Config Endpoints
  1404. #
  1405. ##################################
  1406. @app.get("/api/config")
  1407. async def get_app_config():
  1408. # Checking and Handling the Absence of 'ui' in CONFIG_DATA
  1409. default_locale = "en-US"
  1410. if "ui" in CONFIG_DATA:
  1411. default_locale = CONFIG_DATA["ui"].get("default_locale", "en-US")
  1412. # The Rest of the Function Now Uses the Variables Defined Above
  1413. return {
  1414. "status": True,
  1415. "name": WEBUI_NAME,
  1416. "version": VERSION,
  1417. "default_locale": default_locale,
  1418. "default_models": webui_app.state.config.DEFAULT_MODELS,
  1419. "default_prompt_suggestions": webui_app.state.config.DEFAULT_PROMPT_SUGGESTIONS,
  1420. "features": {
  1421. "auth": WEBUI_AUTH,
  1422. "auth_trusted_header": bool(webui_app.state.AUTH_TRUSTED_EMAIL_HEADER),
  1423. "enable_signup": webui_app.state.config.ENABLE_SIGNUP,
  1424. "enable_web_search": rag_app.state.config.ENABLE_RAG_WEB_SEARCH,
  1425. "enable_image_generation": images_app.state.config.ENABLED,
  1426. "enable_community_sharing": webui_app.state.config.ENABLE_COMMUNITY_SHARING,
  1427. "enable_admin_export": ENABLE_ADMIN_EXPORT,
  1428. },
  1429. "audio": {
  1430. "tts": {
  1431. "engine": audio_app.state.config.TTS_ENGINE,
  1432. "voice": audio_app.state.config.TTS_VOICE,
  1433. },
  1434. "stt": {
  1435. "engine": audio_app.state.config.STT_ENGINE,
  1436. },
  1437. },
  1438. "oauth": {
  1439. "providers": {
  1440. name: config.get("name", name)
  1441. for name, config in OAUTH_PROVIDERS.items()
  1442. }
  1443. },
  1444. }
  1445. @app.get("/api/config/model/filter")
  1446. async def get_model_filter_config(user=Depends(get_admin_user)):
  1447. return {
  1448. "enabled": app.state.config.ENABLE_MODEL_FILTER,
  1449. "models": app.state.config.MODEL_FILTER_LIST,
  1450. }
  1451. class ModelFilterConfigForm(BaseModel):
  1452. enabled: bool
  1453. models: List[str]
  1454. @app.post("/api/config/model/filter")
  1455. async def update_model_filter_config(
  1456. form_data: ModelFilterConfigForm, user=Depends(get_admin_user)
  1457. ):
  1458. app.state.config.ENABLE_MODEL_FILTER = form_data.enabled
  1459. app.state.config.MODEL_FILTER_LIST = form_data.models
  1460. return {
  1461. "enabled": app.state.config.ENABLE_MODEL_FILTER,
  1462. "models": app.state.config.MODEL_FILTER_LIST,
  1463. }
  1464. # TODO: webhook endpoint should be under config endpoints
  1465. @app.get("/api/webhook")
  1466. async def get_webhook_url(user=Depends(get_admin_user)):
  1467. return {
  1468. "url": app.state.config.WEBHOOK_URL,
  1469. }
  1470. class UrlForm(BaseModel):
  1471. url: str
  1472. @app.post("/api/webhook")
  1473. async def update_webhook_url(form_data: UrlForm, user=Depends(get_admin_user)):
  1474. app.state.config.WEBHOOK_URL = form_data.url
  1475. webui_app.state.WEBHOOK_URL = app.state.config.WEBHOOK_URL
  1476. return {"url": app.state.config.WEBHOOK_URL}
  1477. @app.get("/api/version")
  1478. async def get_app_config():
  1479. return {
  1480. "version": VERSION,
  1481. }
  1482. @app.get("/api/changelog")
  1483. async def get_app_changelog():
  1484. return {key: CHANGELOG[key] for idx, key in enumerate(CHANGELOG) if idx < 5}
  1485. @app.get("/api/version/updates")
  1486. async def get_app_latest_release_version():
  1487. try:
  1488. async with aiohttp.ClientSession(trust_env=True) as session:
  1489. async with session.get(
  1490. "https://api.github.com/repos/open-webui/open-webui/releases/latest"
  1491. ) as response:
  1492. response.raise_for_status()
  1493. data = await response.json()
  1494. latest_version = data["tag_name"]
  1495. return {"current": VERSION, "latest": latest_version[1:]}
  1496. except aiohttp.ClientError as e:
  1497. raise HTTPException(
  1498. status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
  1499. detail=ERROR_MESSAGES.RATE_LIMIT_EXCEEDED,
  1500. )
  1501. ############################
  1502. # OAuth Login & Callback
  1503. ############################
  1504. oauth = OAuth()
  1505. for provider_name, provider_config in OAUTH_PROVIDERS.items():
  1506. oauth.register(
  1507. name=provider_name,
  1508. client_id=provider_config["client_id"],
  1509. client_secret=provider_config["client_secret"],
  1510. server_metadata_url=provider_config["server_metadata_url"],
  1511. client_kwargs={
  1512. "scope": provider_config["scope"],
  1513. },
  1514. )
  1515. # SessionMiddleware is used by authlib for oauth
  1516. if len(OAUTH_PROVIDERS) > 0:
  1517. app.add_middleware(
  1518. SessionMiddleware,
  1519. secret_key=WEBUI_SECRET_KEY,
  1520. session_cookie="oui-session",
  1521. same_site=WEBUI_SESSION_COOKIE_SAME_SITE,
  1522. https_only=WEBUI_SESSION_COOKIE_SECURE,
  1523. )
  1524. @app.get("/oauth/{provider}/login")
  1525. async def oauth_login(provider: str, request: Request):
  1526. if provider not in OAUTH_PROVIDERS:
  1527. raise HTTPException(404)
  1528. redirect_uri = request.url_for("oauth_callback", provider=provider)
  1529. return await oauth.create_client(provider).authorize_redirect(request, redirect_uri)
  1530. # OAuth login logic is as follows:
  1531. # 1. Attempt to find a user with matching subject ID, tied to the provider
  1532. # 2. If OAUTH_MERGE_ACCOUNTS_BY_EMAIL is true, find a user with the email address provided via OAuth
  1533. # - This is considered insecure in general, as OAuth providers do not always verify email addresses
  1534. # 3. If there is no user, and ENABLE_OAUTH_SIGNUP is true, create a user
  1535. # - Email addresses are considered unique, so we fail registration if the email address is alreayd taken
  1536. @app.get("/oauth/{provider}/callback")
  1537. async def oauth_callback(provider: str, request: Request, response: Response):
  1538. if provider not in OAUTH_PROVIDERS:
  1539. raise HTTPException(404)
  1540. client = oauth.create_client(provider)
  1541. try:
  1542. token = await client.authorize_access_token(request)
  1543. except Exception as e:
  1544. log.warning(f"OAuth callback error: {e}")
  1545. raise HTTPException(400, detail=ERROR_MESSAGES.INVALID_CRED)
  1546. user_data: UserInfo = token["userinfo"]
  1547. sub = user_data.get("sub")
  1548. if not sub:
  1549. log.warning(f"OAuth callback failed, sub is missing: {user_data}")
  1550. raise HTTPException(400, detail=ERROR_MESSAGES.INVALID_CRED)
  1551. provider_sub = f"{provider}@{sub}"
  1552. email = user_data.get("email", "").lower()
  1553. # We currently mandate that email addresses are provided
  1554. if not email:
  1555. log.warning(f"OAuth callback failed, email is missing: {user_data}")
  1556. raise HTTPException(400, detail=ERROR_MESSAGES.INVALID_CRED)
  1557. # Check if the user exists
  1558. user = Users.get_user_by_oauth_sub(provider_sub)
  1559. if not user:
  1560. # If the user does not exist, check if merging is enabled
  1561. if OAUTH_MERGE_ACCOUNTS_BY_EMAIL.value:
  1562. # Check if the user exists by email
  1563. user = Users.get_user_by_email(email)
  1564. if user:
  1565. # Update the user with the new oauth sub
  1566. Users.update_user_oauth_sub_by_id(user.id, provider_sub)
  1567. if not user:
  1568. # If the user does not exist, check if signups are enabled
  1569. if ENABLE_OAUTH_SIGNUP.value:
  1570. # Check if an existing user with the same email already exists
  1571. existing_user = Users.get_user_by_email(user_data.get("email", "").lower())
  1572. if existing_user:
  1573. raise HTTPException(400, detail=ERROR_MESSAGES.EMAIL_TAKEN)
  1574. picture_url = user_data.get("picture", "")
  1575. if picture_url:
  1576. # Download the profile image into a base64 string
  1577. try:
  1578. async with aiohttp.ClientSession() as session:
  1579. async with session.get(picture_url) as resp:
  1580. picture = await resp.read()
  1581. base64_encoded_picture = base64.b64encode(picture).decode(
  1582. "utf-8"
  1583. )
  1584. guessed_mime_type = mimetypes.guess_type(picture_url)[0]
  1585. if guessed_mime_type is None:
  1586. # assume JPG, browsers are tolerant enough of image formats
  1587. guessed_mime_type = "image/jpeg"
  1588. picture_url = f"data:{guessed_mime_type};base64,{base64_encoded_picture}"
  1589. except Exception as e:
  1590. log.error(f"Error downloading profile image '{picture_url}': {e}")
  1591. picture_url = ""
  1592. if not picture_url:
  1593. picture_url = "/user.png"
  1594. user = Auths.insert_new_auth(
  1595. email=email,
  1596. password=get_password_hash(
  1597. str(uuid.uuid4())
  1598. ), # Random password, not used
  1599. name=user_data.get("name", "User"),
  1600. profile_image_url=picture_url,
  1601. role=webui_app.state.config.DEFAULT_USER_ROLE,
  1602. oauth_sub=provider_sub,
  1603. )
  1604. if webui_app.state.config.WEBHOOK_URL:
  1605. post_webhook(
  1606. webui_app.state.config.WEBHOOK_URL,
  1607. WEBHOOK_MESSAGES.USER_SIGNUP(user.name),
  1608. {
  1609. "action": "signup",
  1610. "message": WEBHOOK_MESSAGES.USER_SIGNUP(user.name),
  1611. "user": user.model_dump_json(exclude_none=True),
  1612. },
  1613. )
  1614. else:
  1615. raise HTTPException(
  1616. status.HTTP_403_FORBIDDEN, detail=ERROR_MESSAGES.ACCESS_PROHIBITED
  1617. )
  1618. jwt_token = create_token(
  1619. data={"id": user.id},
  1620. expires_delta=parse_duration(webui_app.state.config.JWT_EXPIRES_IN),
  1621. )
  1622. # Set the cookie token
  1623. response.set_cookie(
  1624. key="token",
  1625. value=token,
  1626. httponly=True, # Ensures the cookie is not accessible via JavaScript
  1627. )
  1628. # Redirect back to the frontend with the JWT token
  1629. redirect_url = f"{request.base_url}auth#token={jwt_token}"
  1630. return RedirectResponse(url=redirect_url)
  1631. @app.get("/manifest.json")
  1632. async def get_manifest_json():
  1633. return {
  1634. "name": WEBUI_NAME,
  1635. "short_name": WEBUI_NAME,
  1636. "start_url": "/",
  1637. "display": "standalone",
  1638. "background_color": "#343541",
  1639. "theme_color": "#343541",
  1640. "orientation": "portrait-primary",
  1641. "icons": [{"src": "/static/logo.png", "type": "image/png", "sizes": "500x500"}],
  1642. }
  1643. @app.get("/opensearch.xml")
  1644. async def get_opensearch_xml():
  1645. xml_content = rf"""
  1646. <OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/" xmlns:moz="http://www.mozilla.org/2006/browser/search/">
  1647. <ShortName>{WEBUI_NAME}</ShortName>
  1648. <Description>Search {WEBUI_NAME}</Description>
  1649. <InputEncoding>UTF-8</InputEncoding>
  1650. <Image width="16" height="16" type="image/x-icon">{WEBUI_URL}/favicon.png</Image>
  1651. <Url type="text/html" method="get" template="{WEBUI_URL}/?q={"{searchTerms}"}"/>
  1652. <moz:SearchForm>{WEBUI_URL}</moz:SearchForm>
  1653. </OpenSearchDescription>
  1654. """
  1655. return Response(content=xml_content, media_type="application/xml")
  1656. @app.get("/health")
  1657. async def healthcheck():
  1658. return {"status": True}
  1659. app.mount("/static", StaticFiles(directory=STATIC_DIR), name="static")
  1660. app.mount("/cache", StaticFiles(directory=CACHE_DIR), name="cache")
  1661. if os.path.exists(FRONTEND_BUILD_DIR):
  1662. mimetypes.add_type("text/javascript", ".js")
  1663. app.mount(
  1664. "/",
  1665. SPAStaticFiles(directory=FRONTEND_BUILD_DIR, html=True),
  1666. name="spa-static-files",
  1667. )
  1668. else:
  1669. log.warning(
  1670. f"Frontend build directory not found at '{FRONTEND_BUILD_DIR}'. Serving API only."
  1671. )