main.py 4.88 KB
Newer Older
1
2
from fastapi import FastAPI, Depends
from fastapi.routing import APIRoute
3
from fastapi.middleware.cors import CORSMiddleware
4
from apps.webui.routers import (
Timothy J. Baek's avatar
Timothy J. Baek committed
5
6
7
8
    auths,
    users,
    chats,
    documents,
Timothy J. Baek's avatar
Timothy J. Baek committed
9
    tools,
Timothy J. Baek's avatar
Timothy J. Baek committed
10
    models,
Timothy J. Baek's avatar
Timothy J. Baek committed
11
12
    prompts,
    configs,
Timothy J. Baek's avatar
Timothy J. Baek committed
13
    memories,
Timothy J. Baek's avatar
Timothy J. Baek committed
14
    utils,
Timothy J. Baek's avatar
Timothy J. Baek committed
15
    files,
Timothy J. Baek's avatar
Timothy J. Baek committed
16
    functions,
Timothy J. Baek's avatar
Timothy J. Baek committed
17
)
Timothy J. Baek's avatar
Timothy J. Baek committed
18
19
20
from apps.webui.models.functions import Functions
from apps.webui.utils import load_function_module_by_id

Timothy J. Baek's avatar
Timothy J. Baek committed
21
from config import (
22
    WEBUI_BUILD_HASH,
23
24
    SHOW_ADMIN_DETAILS,
    ADMIN_EMAIL,
Timothy J. Baek's avatar
Timothy J. Baek committed
25
26
27
28
29
30
    WEBUI_AUTH,
    DEFAULT_MODELS,
    DEFAULT_PROMPT_SUGGESTIONS,
    DEFAULT_USER_ROLE,
    ENABLE_SIGNUP,
    USER_PERMISSIONS,
Timothy J. Baek's avatar
Timothy J. Baek committed
31
    WEBHOOK_URL,
32
    WEBUI_AUTH_TRUSTED_EMAIL_HEADER,
33
    WEBUI_AUTH_TRUSTED_NAME_HEADER,
34
    JWT_EXPIRES_IN,
Timothy J. Baek's avatar
Timothy J. Baek committed
35
    WEBUI_BANNERS,
36
    ENABLE_COMMUNITY_SHARING,
Timothy J. Baek's avatar
Timothy J. Baek committed
37
    AppConfig,
Timothy J. Baek's avatar
Timothy J. Baek committed
38
)
39
40
41
42
43

app = FastAPI()

origins = ["*"]

44
app.state.config = AppConfig()
Timothy J. Baek's avatar
Timothy J. Baek committed
45

46
47
app.state.config.ENABLE_SIGNUP = ENABLE_SIGNUP
app.state.config.JWT_EXPIRES_IN = JWT_EXPIRES_IN
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
48
app.state.AUTH_TRUSTED_EMAIL_HEADER = WEBUI_AUTH_TRUSTED_EMAIL_HEADER
49
app.state.AUTH_TRUSTED_NAME_HEADER = WEBUI_AUTH_TRUSTED_NAME_HEADER
50

51
52
53
54
55

app.state.config.SHOW_ADMIN_DETAILS = SHOW_ADMIN_DETAILS
app.state.config.ADMIN_EMAIL = ADMIN_EMAIL


56
57
58
59
60
app.state.config.DEFAULT_MODELS = DEFAULT_MODELS
app.state.config.DEFAULT_PROMPT_SUGGESTIONS = DEFAULT_PROMPT_SUGGESTIONS
app.state.config.DEFAULT_USER_ROLE = DEFAULT_USER_ROLE
app.state.config.USER_PERMISSIONS = USER_PERMISSIONS
app.state.config.WEBHOOK_URL = WEBHOOK_URL
Timothy J. Baek's avatar
Timothy J. Baek committed
61
app.state.config.BANNERS = WEBUI_BANNERS
Timothy J. Baek's avatar
Timothy J. Baek committed
62

63
app.state.config.ENABLE_COMMUNITY_SHARING = ENABLE_COMMUNITY_SHARING
Timothy J. Baek's avatar
Timothy J. Baek committed
64
65

app.state.MODELS = {}
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
66
app.state.TOOLS = {}
Timothy J. Baek's avatar
Timothy J. Baek committed
67
app.state.FUNCTIONS = {}
Timothy J. Baek's avatar
Timothy J. Baek committed
68

69
70
71
72
73
74
75
76
app.add_middleware(
    CORSMiddleware,
    allow_origins=origins,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

Timothy J. Baek's avatar
Timothy J. Baek committed
77
78

app.include_router(configs.router, prefix="/configs", tags=["configs"])
79
app.include_router(auths.router, prefix="/auths", tags=["auths"])
80
81
app.include_router(users.router, prefix="/users", tags=["users"])
app.include_router(chats.router, prefix="/chats", tags=["chats"])
Timothy J. Baek's avatar
Timothy J. Baek committed
82

Timothy J. Baek's avatar
Timothy J. Baek committed
83
app.include_router(documents.router, prefix="/documents", tags=["documents"])
Timothy J. Baek's avatar
Timothy J. Baek committed
84
app.include_router(models.router, prefix="/models", tags=["models"])
85
app.include_router(prompts.router, prefix="/prompts", tags=["prompts"])
Timothy J. Baek's avatar
Timothy J. Baek committed
86

Timothy J. Baek's avatar
Timothy J. Baek committed
87
app.include_router(memories.router, prefix="/memories", tags=["memories"])
Timothy J. Baek's avatar
Timothy J. Baek committed
88
89
90
app.include_router(files.router, prefix="/files", tags=["files"])
app.include_router(tools.router, prefix="/tools", tags=["tools"])
app.include_router(functions.router, prefix="/functions", tags=["functions"])
Timothy J. Baek's avatar
Timothy J. Baek committed
91

Timothy J. Baek's avatar
Timothy J. Baek committed
92
app.include_router(utils.router, prefix="/utils", tags=["utils"])
93
94
95
96


@app.get("/")
async def get_status():
Timothy J. Baek's avatar
Timothy J. Baek committed
97
98
99
    return {
        "status": True,
        "auth": WEBUI_AUTH,
100
101
        "default_models": app.state.config.DEFAULT_MODELS,
        "default_prompt_suggestions": app.state.config.DEFAULT_PROMPT_SUGGESTIONS,
Timothy J. Baek's avatar
Timothy J. Baek committed
102
    }
Timothy J. Baek's avatar
Timothy J. Baek committed
103
104
105


async def get_pipe_models():
106
    pipes = Functions.get_functions_by_type("pipe", active_only=True)
Timothy J. Baek's avatar
Timothy J. Baek committed
107
108
109
110
111
    pipe_models = []

    for pipe in pipes:
        # Check if function is already loaded
        if pipe.id not in app.state.FUNCTIONS:
112
113
114
            function_module, function_type, frontmatter = load_function_module_by_id(
                pipe.id
            )
Timothy J. Baek's avatar
Timothy J. Baek committed
115
116
117
118
119
120
121
122
123
124
            app.state.FUNCTIONS[pipe.id] = function_module
        else:
            function_module = app.state.FUNCTIONS[pipe.id]

        # Check if function is a manifold
        if hasattr(function_module, "type"):
            if function_module.type == "manifold":
                manifold_pipes = []

                # Check if pipes is a function or a list
Timothy J. Baek's avatar
Timothy J. Baek committed
125
126
                if callable(function_module.pipes):
                    manifold_pipes = function_module.pipes()
Timothy J. Baek's avatar
Timothy J. Baek committed
127
                else:
Timothy J. Baek's avatar
Timothy J. Baek committed
128
                    manifold_pipes = function_module.pipes
Timothy J. Baek's avatar
Timothy J. Baek committed
129
130
131
132
133

                for p in manifold_pipes:
                    manifold_pipe_id = f'{pipe.id}.{p["id"]}'
                    manifold_pipe_name = p["name"]

Timothy J. Baek's avatar
Timothy J. Baek committed
134
                    if hasattr(function_module, "name"):
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
135
136
137
                        manifold_pipe_name = (
                            f"{function_module.name}{manifold_pipe_name}"
                        )
Timothy J. Baek's avatar
Timothy J. Baek committed
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161

                    pipe_models.append(
                        {
                            "id": manifold_pipe_id,
                            "name": manifold_pipe_name,
                            "object": "model",
                            "created": pipe.created_at,
                            "owned_by": "openai",
                            "pipe": {"type": pipe.type},
                        }
                    )
        else:
            pipe_models.append(
                {
                    "id": pipe.id,
                    "name": pipe.name,
                    "object": "model",
                    "created": pipe.created_at,
                    "owned_by": "openai",
                    "pipe": {"type": "pipe"},
                }
            )

    return pipe_models