Skip to content

Commit

Permalink
upd: admin mode to enable singup
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexisVLRT committed Jan 26, 2024
1 parent 1918cea commit 8f5de4c
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 8 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
10 changes: 9 additions & 1 deletion backend/main.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -30,6 +32,9 @@
user_exists,
)

load_dotenv()
ADMIN_MODE = bool(int(os.getenv("ADMIN_MODE", False)))

app = FastAPI()
logger = get_logger()

Expand Down Expand Up @@ -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(
Expand Down
8 changes: 8 additions & 0 deletions docs/admin_mode.md
Original file line number Diff line number Diff line change
@@ -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.
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
15 changes: 8 additions & 7 deletions frontend/lib/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down
2 changes: 2 additions & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8f5de4c

Please sign in to comment.