Skip to content

Commit

Permalink
adjust endpoint guards
Browse files Browse the repository at this point in the history
  • Loading branch information
Unischneider committed Nov 7, 2024
1 parent f7a4b00 commit ebc8613
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
22 changes: 12 additions & 10 deletions app/api/question_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
from app.utils.dependencies import request_handler, auth_handler, weaviate_manager, model
from app.utils.environment import config

router = APIRouter(prefix="/api/v1/question", tags=["response"], dependencies=[Depends(auth_handler.verify_token)])
auth = APIRouter(prefix="/api", tags=["response"], dependencies=[Depends(auth_handler.verify_api_key)])
question_router = APIRouter(prefix="/api/v1/question", tags=["response"])
admin_router = APIRouter(prefix="/api/admin", tags=["settings", "admin"],
dependencies=[Depends(auth_handler.verify_token)])
auth_router = APIRouter(prefix="/api", tags=["authorization"], dependencies=[Depends(auth_handler.verify_api_key)])


@auth.post("/token")
@auth_router.post("/token")
async def login(login_request: LoginRequest):
if config.WITHOUT_USER_LOGIN == "true" or (
login_request.username == config.EXPECTED_USERNAME and login_request.password == config.EXPECTED_PASSWORD):
Expand All @@ -23,7 +25,7 @@ async def login(login_request: LoginRequest):
raise HTTPException(status_code=401, detail="Invalid username or password")


@router.post("/ask", dependencies=[Depends(auth_handler.verify_api_key)])
@question_router.post("/ask", tags=["email"], dependencies=[Depends(auth_handler.verify_api_key)])
async def ask(request: UserRequest):
question = request.message
classification = request.study_program
Expand All @@ -50,7 +52,7 @@ async def ask(request: UserRequest):
return {"answer": answer}


@router.post("/chat")
@question_router.post("/chat", tags=["chatbot"], dependencies=[Depends(auth_handler.verify_token)])
async def chat(request: UserChat):
messages = request.messages
if not messages:
Expand All @@ -68,14 +70,14 @@ async def chat(request: UserChat):
return {"answer": answer}


@router.get("/initSchema",
status_code=status.HTTP_202_ACCEPTED, )
@admin_router.get("/initSchema",
status_code=status.HTTP_202_ACCEPTED, )
async def initializeDb():
initialize_vectorstores(config.KNOWLEDGE_BASE_FOLDER, config.QA_FOLDER, weaviate_manager)
return


@router.post("/document")
@admin_router.post("/document")
async def add_document(request: UserRequest):
question = request.message
classification = request.study_program
Expand All @@ -92,13 +94,13 @@ async def add_document(request: UserRequest):
raise HTTPException(status_code=500, detail="Failed to add document")


@router.get("/ping")
@admin_router.get("/ping")
async def ping():
logging.info(config.GPU_URL)
return {"answer": "Server running."}


@router.get("/hi")
@admin_router.get("/hi")
async def ping():
logging.info("hi")
return model.complete([{"role": "user", "content": "Hi"}])
5 changes: 3 additions & 2 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging

from app.api.question_router import question_router, admin_router, auth_router
from app.utils.setup_logging import setup_logging

setup_logging()
Expand All @@ -12,7 +13,6 @@
from fastapi.responses import ORJSONResponse
from starlette.responses import JSONResponse

from app.api.question_router import router as question_router, auth
from app.utils.dependencies import shutdown_model

logging.info("Starting application...")
Expand Down Expand Up @@ -48,7 +48,8 @@ async def validation_exception_handler(request: Request, exc: RequestValidationE


app.include_router(router=question_router)
app.include_router(router=auth)
app.include_router(router=admin_router)
app.include_router(router=auth_router)

if __name__ == "__main__":
logging.info("Starting FastAPI server")
Expand Down

0 comments on commit ebc8613

Please sign in to comment.