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