main.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. from fastapi import FastAPI, Depends
  2. from fastapi.routing import APIRoute
  3. from fastapi.middleware.cors import CORSMiddleware
  4. from starlette.middleware.sessions import SessionMiddleware
  5. from apps.webui.routers import (
  6. auths,
  7. users,
  8. chats,
  9. documents,
  10. tools,
  11. models,
  12. prompts,
  13. configs,
  14. memories,
  15. utils,
  16. files,
  17. functions,
  18. )
  19. from apps.webui.models.functions import Functions
  20. from apps.webui.utils import load_function_module_by_id
  21. from config import (
  22. WEBUI_BUILD_HASH,
  23. SHOW_ADMIN_DETAILS,
  24. ADMIN_EMAIL,
  25. WEBUI_AUTH,
  26. DEFAULT_MODELS,
  27. DEFAULT_PROMPT_SUGGESTIONS,
  28. DEFAULT_USER_ROLE,
  29. ENABLE_SIGNUP,
  30. USER_PERMISSIONS,
  31. WEBHOOK_URL,
  32. WEBUI_AUTH_TRUSTED_EMAIL_HEADER,
  33. WEBUI_AUTH_TRUSTED_NAME_HEADER,
  34. JWT_EXPIRES_IN,
  35. WEBUI_BANNERS,
  36. ENABLE_COMMUNITY_SHARING,
  37. AppConfig,
  38. )
  39. app = FastAPI()
  40. origins = ["*"]
  41. app.state.config = AppConfig()
  42. app.state.config.ENABLE_SIGNUP = ENABLE_SIGNUP
  43. app.state.config.JWT_EXPIRES_IN = JWT_EXPIRES_IN
  44. app.state.AUTH_TRUSTED_EMAIL_HEADER = WEBUI_AUTH_TRUSTED_EMAIL_HEADER
  45. app.state.AUTH_TRUSTED_NAME_HEADER = WEBUI_AUTH_TRUSTED_NAME_HEADER
  46. app.state.config.SHOW_ADMIN_DETAILS = SHOW_ADMIN_DETAILS
  47. app.state.config.ADMIN_EMAIL = ADMIN_EMAIL
  48. app.state.config.DEFAULT_MODELS = DEFAULT_MODELS
  49. app.state.config.DEFAULT_PROMPT_SUGGESTIONS = DEFAULT_PROMPT_SUGGESTIONS
  50. app.state.config.DEFAULT_USER_ROLE = DEFAULT_USER_ROLE
  51. app.state.config.USER_PERMISSIONS = USER_PERMISSIONS
  52. app.state.config.WEBHOOK_URL = WEBHOOK_URL
  53. app.state.config.BANNERS = WEBUI_BANNERS
  54. app.state.config.ENABLE_COMMUNITY_SHARING = ENABLE_COMMUNITY_SHARING
  55. app.state.MODELS = {}
  56. app.state.TOOLS = {}
  57. app.state.FUNCTIONS = {}
  58. app.add_middleware(
  59. CORSMiddleware,
  60. allow_origins=origins,
  61. allow_credentials=True,
  62. allow_methods=["*"],
  63. allow_headers=["*"],
  64. )
  65. app.include_router(configs.router, prefix="/configs", tags=["configs"])
  66. app.include_router(auths.router, prefix="/auths", tags=["auths"])
  67. app.include_router(users.router, prefix="/users", tags=["users"])
  68. app.include_router(chats.router, prefix="/chats", tags=["chats"])
  69. app.include_router(documents.router, prefix="/documents", tags=["documents"])
  70. app.include_router(models.router, prefix="/models", tags=["models"])
  71. app.include_router(prompts.router, prefix="/prompts", tags=["prompts"])
  72. app.include_router(memories.router, prefix="/memories", tags=["memories"])
  73. app.include_router(files.router, prefix="/files", tags=["files"])
  74. app.include_router(tools.router, prefix="/tools", tags=["tools"])
  75. app.include_router(functions.router, prefix="/functions", tags=["functions"])
  76. app.include_router(utils.router, prefix="/utils", tags=["utils"])
  77. @app.get("/")
  78. async def get_status():
  79. return {
  80. "status": True,
  81. "auth": WEBUI_AUTH,
  82. "default_models": app.state.config.DEFAULT_MODELS,
  83. "default_prompt_suggestions": app.state.config.DEFAULT_PROMPT_SUGGESTIONS,
  84. }
  85. async def get_pipe_models():
  86. pipes = Functions.get_functions_by_type("pipe")
  87. pipe_models = []
  88. for pipe in pipes:
  89. # Check if function is already loaded
  90. if pipe.id not in app.state.FUNCTIONS:
  91. function_module, function_type = load_function_module_by_id(pipe.id)
  92. app.state.FUNCTIONS[pipe.id] = function_module
  93. else:
  94. function_module = app.state.FUNCTIONS[pipe.id]
  95. # Check if function is a manifold
  96. if hasattr(function_module, "type"):
  97. if function_module.type == "manifold":
  98. manifold_pipes = []
  99. # Check if pipes is a function or a list
  100. if callable(function_module.pipes):
  101. manifold_pipes = function_module.pipes()
  102. else:
  103. manifold_pipes = function_module.pipes
  104. for p in manifold_pipes:
  105. manifold_pipe_id = f'{pipe.id}.{p["id"]}'
  106. manifold_pipe_name = p["name"]
  107. if hasattr(function_module, "name"):
  108. manifold_pipe_name = f"{pipe.name}{manifold_pipe_name}"
  109. pipe_models.append(
  110. {
  111. "id": manifold_pipe_id,
  112. "name": manifold_pipe_name,
  113. "object": "model",
  114. "created": pipe.created_at,
  115. "owned_by": "openai",
  116. "pipe": {"type": pipe.type},
  117. }
  118. )
  119. else:
  120. pipe_models.append(
  121. {
  122. "id": pipe.id,
  123. "name": pipe.name,
  124. "object": "model",
  125. "created": pipe.created_at,
  126. "owned_by": "openai",
  127. "pipe": {"type": "pipe"},
  128. }
  129. )
  130. return pipe_models