Spaces:
Sleeping
Sleeping
Update private_gpt/launcher.py
Browse files- private_gpt/launcher.py +71 -13
private_gpt/launcher.py
CHANGED
|
@@ -2,10 +2,11 @@
|
|
| 2 |
import logging
|
| 3 |
from typing import Any
|
| 4 |
|
| 5 |
-
from fastapi import Depends, FastAPI, Request
|
| 6 |
from fastapi.middleware.cors import CORSMiddleware
|
| 7 |
from fastapi.openapi.utils import get_openapi
|
| 8 |
from injector import Injector
|
|
|
|
| 9 |
|
| 10 |
from private_gpt.paths import docs_path
|
| 11 |
from private_gpt.server.chat.chat_router import chat_router
|
|
@@ -14,7 +15,21 @@ from private_gpt.server.completions.completions_router import completions_router
|
|
| 14 |
from private_gpt.server.embeddings.embeddings_router import embeddings_router
|
| 15 |
from private_gpt.server.health.health_router import health_router
|
| 16 |
from private_gpt.server.ingest.ingest_router import ingest_router
|
|
|
|
| 17 |
from private_gpt.settings.settings import Settings
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
logger = logging.getLogger(__name__)
|
| 20 |
|
|
@@ -66,21 +81,49 @@ def create_app(root_injector: Injector) -> FastAPI:
|
|
| 66 |
request.state.injector = root_injector
|
| 67 |
|
| 68 |
app = FastAPI(dependencies=[Depends(bind_injector_to_request)])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
def custom_openapi() -> dict[str, Any]:
|
| 71 |
if app.openapi_schema:
|
| 72 |
return app.openapi_schema
|
| 73 |
openapi_schema = get_openapi(
|
| 74 |
-
title="
|
| 75 |
description=description,
|
| 76 |
version="0.1.0",
|
| 77 |
-
summary="
|
| 78 |
"ask questions to your documents using the power of Large Language "
|
| 79 |
"Models (LLMs), even in scenarios without Internet connection. "
|
| 80 |
"100% private, no data leaves your execution environment at any point.",
|
| 81 |
-
contact={
|
| 82 |
-
"url": "https://github.com/imartinez/privateGPT",
|
| 83 |
-
},
|
| 84 |
license_info={
|
| 85 |
"name": "Apache 2.0",
|
| 86 |
"url": "https://www.apache.org/licenses/LICENSE-2.0.html",
|
|
@@ -99,16 +142,34 @@ def create_app(root_injector: Injector) -> FastAPI:
|
|
| 99 |
|
| 100 |
app.openapi = custom_openapi # type: ignore[method-assign]
|
| 101 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
app.include_router(completions_router)
|
| 103 |
app.include_router(chat_router)
|
| 104 |
app.include_router(chunks_router)
|
| 105 |
app.include_router(ingest_router)
|
| 106 |
app.include_router(embeddings_router)
|
| 107 |
app.include_router(health_router)
|
| 108 |
-
|
| 109 |
-
@app.get("/")
|
| 110 |
-
async def root():
|
| 111 |
-
return {"message": "Chatbot API"}
|
| 112 |
|
| 113 |
settings = root_injector.get(Settings)
|
| 114 |
if settings.server.cors.enabled:
|
|
@@ -121,7 +182,6 @@ def create_app(root_injector: Injector) -> FastAPI:
|
|
| 121 |
allow_methods=settings.server.cors.allow_methods,
|
| 122 |
allow_headers=settings.server.cors.allow_headers,
|
| 123 |
)
|
| 124 |
-
|
| 125 |
|
| 126 |
if settings.ui.enabled:
|
| 127 |
logger.debug("Importing the UI module")
|
|
@@ -130,6 +190,4 @@ def create_app(root_injector: Injector) -> FastAPI:
|
|
| 130 |
ui = root_injector.get(PrivateGptUi)
|
| 131 |
ui.mount_in_app(app, settings.ui.path)
|
| 132 |
|
| 133 |
-
|
| 134 |
-
|
| 135 |
return app
|
|
|
|
| 2 |
import logging
|
| 3 |
from typing import Any
|
| 4 |
|
| 5 |
+
from fastapi import Depends, FastAPI, Request, status, HTTPException
|
| 6 |
from fastapi.middleware.cors import CORSMiddleware
|
| 7 |
from fastapi.openapi.utils import get_openapi
|
| 8 |
from injector import Injector
|
| 9 |
+
from fastapi import APIRouter
|
| 10 |
|
| 11 |
from private_gpt.paths import docs_path
|
| 12 |
from private_gpt.server.chat.chat_router import chat_router
|
|
|
|
| 15 |
from private_gpt.server.embeddings.embeddings_router import embeddings_router
|
| 16 |
from private_gpt.server.health.health_router import health_router
|
| 17 |
from private_gpt.server.ingest.ingest_router import ingest_router
|
| 18 |
+
from private_gpt.server.utils import authentication
|
| 19 |
from private_gpt.settings.settings import Settings
|
| 20 |
+
from private_gpt.components.llm.llm_component import LLMComponent
|
| 21 |
+
|
| 22 |
+
#databse for user authentication integration
|
| 23 |
+
from private_gpt.server.utils import models
|
| 24 |
+
#from private_gpt.server.utils.database import engine, SessionLocal
|
| 25 |
+
from typing import Annotated
|
| 26 |
+
from sqlalchemy.orm import Session
|
| 27 |
+
from private_gpt.server.utils.authentication import get_current_user
|
| 28 |
+
|
| 29 |
+
from fastapi import Depends, HTTPException, status
|
| 30 |
+
from fastapi.security import OAuth2AuthorizationCodeBearer
|
| 31 |
+
|
| 32 |
+
|
| 33 |
|
| 34 |
logger = logging.getLogger(__name__)
|
| 35 |
|
|
|
|
| 81 |
request.state.injector = root_injector
|
| 82 |
|
| 83 |
app = FastAPI(dependencies=[Depends(bind_injector_to_request)])
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
model_router = APIRouter(prefix ='/v1',dependencies=[Depends(get_current_user)])
|
| 87 |
+
|
| 88 |
+
@model_router.post("/switch_model")
|
| 89 |
+
async def switch_model(
|
| 90 |
+
new_model: str, current_user: dict = Depends(get_current_user)
|
| 91 |
+
):
|
| 92 |
+
# Check if the user has the "admin" role
|
| 93 |
+
if "admin" not in current_user.get("role", []):
|
| 94 |
+
raise HTTPException(
|
| 95 |
+
status_code=status.HTTP_403_FORBIDDEN,
|
| 96 |
+
detail="You are not an admin and cannot use this API.",
|
| 97 |
+
)
|
| 98 |
+
|
| 99 |
+
#logic to switch the LLM model based on the user's request
|
| 100 |
+
|
| 101 |
+
llm_component = root_injector.get(LLMComponent)
|
| 102 |
+
llm_component.switch_model(new_model, settings=settings)
|
| 103 |
+
|
| 104 |
+
return {"message": f"Model switched to {new_model}"}
|
| 105 |
+
|
| 106 |
+
# @model_router.post("/switch_model")
|
| 107 |
+
# async def switch_model(new_model: str):
|
| 108 |
+
# # Implement logic to switch the LLM model based on the user's request
|
| 109 |
+
# # Example: Change the LLM model in LLMComponent based on the new_model parameter
|
| 110 |
+
# llm_component = root_injector.get(LLMComponent)
|
| 111 |
+
# llm_component.switch_model(new_model, settings=settings)
|
| 112 |
+
|
| 113 |
+
# return {"message": f"Model switched to {new_model}"}
|
| 114 |
+
|
| 115 |
|
| 116 |
def custom_openapi() -> dict[str, Any]:
|
| 117 |
if app.openapi_schema:
|
| 118 |
return app.openapi_schema
|
| 119 |
openapi_schema = get_openapi(
|
| 120 |
+
title="Invenxion-Chatbot",
|
| 121 |
description=description,
|
| 122 |
version="0.1.0",
|
| 123 |
+
summary="This is a production-ready AI project that allows you to "
|
| 124 |
"ask questions to your documents using the power of Large Language "
|
| 125 |
"Models (LLMs), even in scenarios without Internet connection. "
|
| 126 |
"100% private, no data leaves your execution environment at any point.",
|
|
|
|
|
|
|
|
|
|
| 127 |
license_info={
|
| 128 |
"name": "Apache 2.0",
|
| 129 |
"url": "https://www.apache.org/licenses/LICENSE-2.0.html",
|
|
|
|
| 142 |
|
| 143 |
app.openapi = custom_openapi # type: ignore[method-assign]
|
| 144 |
|
| 145 |
+
#models.Base.metadata.create_all(bind=engine)
|
| 146 |
+
|
| 147 |
+
# def get_db():
|
| 148 |
+
# db = SessionLocal()
|
| 149 |
+
# try:
|
| 150 |
+
# yield db
|
| 151 |
+
# finally:
|
| 152 |
+
# db.close()
|
| 153 |
+
|
| 154 |
+
#db_dependency = Annotated[Session, Depends(get_db)]
|
| 155 |
+
#user_dependency = Annotated[dict, Depends(get_current_user)]
|
| 156 |
+
|
| 157 |
+
@app.get("/v1/", status_code=status.HTTP_200_OK)
|
| 158 |
+
async def user(current_user: dict = Depends(get_current_user)):
|
| 159 |
+
if current_user is None:
|
| 160 |
+
raise HTTPException(status_code=401, detail="Authentication Failed")
|
| 161 |
+
return {"User": current_user}
|
| 162 |
+
|
| 163 |
+
|
| 164 |
+
app.include_router(authentication.router)
|
| 165 |
+
|
| 166 |
app.include_router(completions_router)
|
| 167 |
app.include_router(chat_router)
|
| 168 |
app.include_router(chunks_router)
|
| 169 |
app.include_router(ingest_router)
|
| 170 |
app.include_router(embeddings_router)
|
| 171 |
app.include_router(health_router)
|
| 172 |
+
app.include_router(model_router)
|
|
|
|
|
|
|
|
|
|
| 173 |
|
| 174 |
settings = root_injector.get(Settings)
|
| 175 |
if settings.server.cors.enabled:
|
|
|
|
| 182 |
allow_methods=settings.server.cors.allow_methods,
|
| 183 |
allow_headers=settings.server.cors.allow_headers,
|
| 184 |
)
|
|
|
|
| 185 |
|
| 186 |
if settings.ui.enabled:
|
| 187 |
logger.debug("Importing the UI module")
|
|
|
|
| 190 |
ui = root_injector.get(PrivateGptUi)
|
| 191 |
ui.mount_in_app(app, settings.ui.path)
|
| 192 |
|
|
|
|
|
|
|
| 193 |
return app
|