main.py 64 KB

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