diff --git a/README.md b/README.md index fbacfd5..8e7544d 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,7 @@ pip install -r requirements.txt You will need to set some env vars, either in a .env file at the project root, or just by exporting them like so: ```shell export PYTHONPATH=. +export ADMIN_MODE=1 export OPENAI_API_KEY="xxx" # API key used to query the LLM export EMBEDDING_API_KEY="xxx" # API key used to query the embedding model export DATABASE_URL="sqlite:///$(pwd)/database/db.sqlite3" # For local developement only. You will need a real, cloud-based SQL database URL for prod. diff --git a/backend/main.py b/backend/main.py index 706a763..cd933dc 100644 --- a/backend/main.py +++ b/backend/main.py @@ -1,10 +1,12 @@ import asyncio import inspect +import os import traceback from datetime import datetime from pathlib import Path from typing import List from uuid import uuid4 +from dotenv import load_dotenv from fastapi import Depends, FastAPI, HTTPException, status from fastapi.responses import StreamingResponse @@ -30,6 +32,9 @@ user_exists, ) +load_dotenv() +ADMIN_MODE = bool(int(os.getenv("ADMIN_MODE", False))) + app = FastAPI() logger = get_logger() @@ -211,8 +216,11 @@ async def feedback_thumbs_down( ############################################ -@app.post("/user/signup") +@app.post("/user/signup", include_in_schema=ADMIN_MODE) async def signup(user: UnsecureUser) -> dict: + if not ADMIN_MODE: + raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="Signup is disabled") + user = User.from_unsecure_user(user) if user_exists(user.email): raise HTTPException( diff --git a/docs/admin_mode.md b/docs/admin_mode.md new file mode 100644 index 0000000..b19625f --- /dev/null +++ b/docs/admin_mode.md @@ -0,0 +1,8 @@ +Once you start deploying to a cloud, you may end up having to plublicly expose your backend and frontend to the internet. In this case you will need to disable the admin mode on the deployed components. + +Just deploy with the `ADMIN_MODE` env var as disabled. This could be in your Dockerfile, or any other deployment config. +```shell +ADMIN_MODE=0 +``` + +This will disable the signup endpoint, preventing people stumbling upon the API from creating an account and using your LLM tokens. \ No newline at end of file diff --git a/docs/index.md b/docs/index.md index 96a6cae..f523b32 100644 --- a/docs/index.md +++ b/docs/index.md @@ -24,6 +24,7 @@ pip install -r requirements.txt You will need to set some env vars, either in a .env file at the project root, or just by exporting them like so: ```shell export PYTHONPATH=. +export ADMIN_MODE=1 export OPENAI_API_KEY="xxx" # API key used to query the LLM export EMBEDDING_API_KEY="xxx" # API key used to query the embedding model export DATABASE_URL="sqlite:///$(pwd)/database/db.sqlite3" # For local developement only. You will need a real, cloud-based SQL database URL for prod. diff --git a/frontend/lib/auth.py b/frontend/lib/auth.py index b170398..3ae0350 100644 --- a/frontend/lib/auth.py +++ b/frontend/lib/auth.py @@ -2,22 +2,23 @@ from time import sleep from typing import Optional from urllib.parse import urljoin +from dotenv import load_dotenv import extra_streamlit_components as stx import requests import streamlit as st from requests.sessions import Session +load_dotenv() FASTAPI_URL = os.getenv("FASTAPI_URL", "http://localhost:8000/") +ADMIN_MODE = bool(int(os.getenv("ADMIN_MODE", False))) def auth() -> Optional[str]: - tab = stx.tab_bar( - data=[ - stx.TabBarItemData(id="Login", title="Login", description=""), - stx.TabBarItemData(id="Signup", title="Signup", description=""), - ], - default="Login", - ) + data = [stx.TabBarItemData(id="Login", title="Login", description="")] + if ADMIN_MODE: + data += [stx.TabBarItemData(id="Signup", title="Signup", description="")] + + tab = stx.tab_bar(data=data, default="Login") if tab == "Login": login_form() elif tab == "Signup": diff --git a/mkdocs.yml b/mkdocs.yml index 55d6c75..4851e55 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -15,6 +15,8 @@ nav: - Loading Docs in the RAG: loading_documents.md - The RAG object: rag_object.md - The RAGConfig object: rag_config.md + - Deployment: + - Admin Mode: admin_mode.md - Config cookbook: - LLMs: recipe_llms_configs.md - Vector Stores: recipe_vector_stores_configs.md