main.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. from bs4 import BeautifulSoup
  2. import json
  3. import markdown
  4. import time
  5. import os
  6. import sys
  7. import requests
  8. from fastapi import FastAPI, Request, Depends, status
  9. from fastapi.staticfiles import StaticFiles
  10. from fastapi import HTTPException
  11. from fastapi.middleware.wsgi import WSGIMiddleware
  12. from fastapi.middleware.cors import CORSMiddleware
  13. from starlette.exceptions import HTTPException as StarletteHTTPException
  14. from starlette.middleware.base import BaseHTTPMiddleware
  15. from apps.ollama.main import app as ollama_app
  16. from apps.openai.main import app as openai_app
  17. from apps.litellm.main import app as litellm_app, startup as litellm_app_startup
  18. from apps.audio.main import app as audio_app
  19. from apps.images.main import app as images_app
  20. from apps.rag.main import app as rag_app
  21. from apps.web.main import app as webui_app
  22. from apps.rag.utils import query_doc, query_collection, rag_template
  23. from config import WEBUI_NAME, ENV, VERSION, CHANGELOG, FRONTEND_BUILD_DIR
  24. from constants import ERROR_MESSAGES
  25. class SPAStaticFiles(StaticFiles):
  26. async def get_response(self, path: str, scope):
  27. try:
  28. return await super().get_response(path, scope)
  29. except (HTTPException, StarletteHTTPException) as ex:
  30. if ex.status_code == 404:
  31. return await super().get_response("index.html", scope)
  32. else:
  33. raise ex
  34. app = FastAPI(docs_url="/docs" if ENV == "dev" else None, redoc_url=None)
  35. origins = ["*"]
  36. app.add_middleware(
  37. CORSMiddleware,
  38. allow_origins=origins,
  39. allow_credentials=True,
  40. allow_methods=["*"],
  41. allow_headers=["*"],
  42. )
  43. @app.on_event("startup")
  44. async def on_startup():
  45. await litellm_app_startup()
  46. class RAGMiddleware(BaseHTTPMiddleware):
  47. async def dispatch(self, request: Request, call_next):
  48. if request.method == "POST" and (
  49. "/api/chat" in request.url.path or "/chat/completions" in request.url.path
  50. ):
  51. print(request.url.path)
  52. # Read the original request body
  53. body = await request.body()
  54. # Decode body to string
  55. body_str = body.decode("utf-8")
  56. # Parse string to JSON
  57. data = json.loads(body_str) if body_str else {}
  58. # Example: Add a new key-value pair or modify existing ones
  59. # data["modified"] = True # Example modification
  60. if "docs" in data:
  61. docs = data["docs"]
  62. print(docs)
  63. last_user_message_idx = None
  64. for i in range(len(data["messages"]) - 1, -1, -1):
  65. if data["messages"][i]["role"] == "user":
  66. last_user_message_idx = i
  67. break
  68. user_message = data["messages"][last_user_message_idx]
  69. if isinstance(user_message["content"], list):
  70. # Handle list content input
  71. content_type = "list"
  72. query = ""
  73. for content_item in user_message["content"]:
  74. if content_item["type"] == "text":
  75. query = content_item["text"]
  76. break
  77. elif isinstance(user_message["content"], str):
  78. # Handle text content input
  79. content_type = "text"
  80. query = user_message["content"]
  81. else:
  82. # Fallback in case the input does not match expected types
  83. content_type = None
  84. query = ""
  85. relevant_contexts = []
  86. for doc in docs:
  87. context = None
  88. try:
  89. if doc["type"] == "collection":
  90. context = query_collection(
  91. collection_names=doc["collection_names"],
  92. query=query,
  93. k=rag_app.state.TOP_K,
  94. embedding_function=rag_app.state.sentence_transformer_ef,
  95. )
  96. else:
  97. context = query_doc(
  98. collection_name=doc["collection_name"],
  99. query=query,
  100. k=rag_app.state.TOP_K,
  101. embedding_function=rag_app.state.sentence_transformer_ef,
  102. )
  103. except Exception as e:
  104. print(e)
  105. context = None
  106. relevant_contexts.append(context)
  107. context_string = ""
  108. for context in relevant_contexts:
  109. if context:
  110. context_string += " ".join(context["documents"][0]) + "\n"
  111. ra_content = rag_template(
  112. template=rag_app.state.RAG_TEMPLATE,
  113. context=context_string,
  114. query=query,
  115. )
  116. if content_type == "list":
  117. new_content = []
  118. for content_item in user_message["content"]:
  119. if content_item["type"] == "text":
  120. # Update the text item's content with ra_content
  121. new_content.append({"type": "text", "text": ra_content})
  122. else:
  123. # Keep other types of content as they are
  124. new_content.append(content_item)
  125. new_user_message = {**user_message, "content": new_content}
  126. else:
  127. new_user_message = {
  128. **user_message,
  129. "content": ra_content,
  130. }
  131. data["messages"][last_user_message_idx] = new_user_message
  132. del data["docs"]
  133. print(data["messages"])
  134. modified_body_bytes = json.dumps(data).encode("utf-8")
  135. # Create a new request with the modified body
  136. scope = request.scope
  137. scope["body"] = modified_body_bytes
  138. request = Request(scope, receive=lambda: self._receive(modified_body_bytes))
  139. response = await call_next(request)
  140. return response
  141. async def _receive(self, body: bytes):
  142. return {"type": "http.request", "body": body, "more_body": False}
  143. app.add_middleware(RAGMiddleware)
  144. @app.middleware("http")
  145. async def check_url(request: Request, call_next):
  146. start_time = int(time.time())
  147. response = await call_next(request)
  148. process_time = int(time.time()) - start_time
  149. response.headers["X-Process-Time"] = str(process_time)
  150. return response
  151. app.mount("/api/v1", webui_app)
  152. app.mount("/litellm/api", litellm_app)
  153. app.mount("/ollama", ollama_app)
  154. app.mount("/openai/api", openai_app)
  155. app.mount("/images/api/v1", images_app)
  156. app.mount("/audio/api/v1", audio_app)
  157. app.mount("/rag/api/v1", rag_app)
  158. @app.get("/api/config")
  159. async def get_app_config():
  160. return {
  161. "status": True,
  162. "name": WEBUI_NAME,
  163. "version": VERSION,
  164. "images": images_app.state.ENABLED,
  165. "default_models": webui_app.state.DEFAULT_MODELS,
  166. "default_prompt_suggestions": webui_app.state.DEFAULT_PROMPT_SUGGESTIONS,
  167. }
  168. @app.get("/api/version")
  169. async def get_app_config():
  170. return {
  171. "version": VERSION,
  172. }
  173. @app.get("/api/changelog")
  174. async def get_app_changelog():
  175. return CHANGELOG
  176. @app.get("/api/version/updates")
  177. async def get_app_latest_release_version():
  178. try:
  179. response = requests.get(
  180. f"https://api.github.com/repos/open-webui/open-webui/releases/latest"
  181. )
  182. response.raise_for_status()
  183. latest_version = response.json()["tag_name"]
  184. return {"current": VERSION, "latest": latest_version[1:]}
  185. except Exception as e:
  186. raise HTTPException(
  187. status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
  188. detail=ERROR_MESSAGES.RATE_LIMIT_EXCEEDED,
  189. )
  190. app.mount("/static", StaticFiles(directory="static"), name="static")
  191. app.mount("/cache", StaticFiles(directory="data/cache"), name="cache")
  192. app.mount(
  193. "/",
  194. SPAStaticFiles(directory=FRONTEND_BUILD_DIR, html=True),
  195. name="spa-static-files",
  196. )