from fastapi import FastAPI, HTTPException, Depends, status from fastapi.staticfiles import StaticFiles from fastapi.responses import HTMLResponse, JSONResponse from fastapi.security import HTTPBasic, HTTPBasicCredentials from pydantic import BaseModel import httpx import secrets import os from typing import List, Dict, Optional # Initialize FastAPI app app = FastAPI(title="AI Chat Application") app.mount("/static", StaticFiles(directory="static"), name="static") # Basic auth setup for admin security = HTTPBasic() ADMIN_USERNAME = os.environ.get("ADMIN_USERNAME", "admin") ADMIN_PASSWORD = os.environ.get("ADMIN_PASSWORD", "password") # change in prod def get_admin_user(credentials: HTTPBasicCredentials = Depends(security)): if not (secrets.compare_digest(credentials.username, ADMIN_USERNAME) and secrets.compare_digest(credentials.password, ADMIN_PASSWORD)): raise HTTPException( status_code=status.HTTP_401_UNAUTHORIZED, detail="Incorrect username or password", headers={"WWW-Authenticate": "Basic"}, ) return credentials.username # In-memory data stores chat_history: Dict[str, List[Dict[str, str]]] = {} rules: List[str] = [ "You are Vetra, an office bot that answers based on given rules", "Be respectful and helpful", "Do not generate harmful content", "Provide accurate information" ] available_models: List[str] = ["mistral-large-latest", "gemini", "openai-xlarge"] current_model: str = available_models[0] # Pydantic models class ChatMessage(BaseModel): message: str chat_id: Optional[str] = None class Rule(BaseModel): rule: str class ModelRequest(BaseModel): model: str # Serve index.html at root @app.get("/", response_class=HTMLResponse) async def read_root(): try: with open("index.html", "r") as f: return HTMLResponse(content=f.read()) except FileNotFoundError: raise HTTPException(status_code=404, detail="index.html not found") # Helper function to create system message from rules def create_system_message(): """Create a system message by combining all active rules.""" rules_text = "\n".join([f"- {rule}" for rule in rules]) return { "role": "system", "content": f"You are an AI assistant following these rules:\n{rules_text}" } # Chat endpoint @app.post("/api/chat", response_class=JSONResponse) async def process_chat(chat_request: ChatMessage): chat_id = chat_request.chat_id or secrets.token_hex(8) # Initialize chat history with system message if it's a new chat if chat_id not in chat_history: chat_history[chat_id] = [create_system_message()] # Add user message to history chat_history[chat_id].append({"role": "user", "content": chat_request.message}) # Check block rules for rule in rules: if rule.startswith("Block:") and rule[6:].lower() in chat_request.message.lower(): return JSONResponse({"response": "I cannot respond due to content rules.", "chat_id": chat_id}) # Always use the admin-selected model - no user choice api_request = {"model": current_model, "messages": chat_history[chat_id], "temperature": 0.7, "max_tokens": 1000} try: async with httpx.AsyncClient(timeout=60.0) as client: resp = await client.post( "https://parthsadaria-lokiai.hf.space/chat/completions", json=api_request, headers={"Authorization": "sigma"} ) data = resp.json() if choices := data.get("choices"): msg = choices[0]["message"]["content"] chat_history[chat_id].append({"role": "assistant", "content": msg}) return {"response": msg, "chat_id": chat_id, "model": current_model} else: err = "No response from AI model" chat_history[chat_id].append({"role": "assistant", "content": err}) return {"response": err, "chat_id": chat_id} except httpx.RequestError as e: err = f"Error communicating with AI service: {e}" chat_history[chat_id].append({"role": "assistant", "content": err}) return {"response": err, "chat_id": chat_id} # Update system message when rules change def update_system_message_in_chats(): """Update the system message in all active chats when rules change.""" new_system_msg = create_system_message() for chat_id in chat_history: # Replace the first message if it's a system message, or insert at beginning if chat_history[chat_id] and chat_history[chat_id][0].get("role") == "system": chat_history[chat_id][0] = new_system_msg else: chat_history[chat_id].insert(0, new_system_msg) # Admin panel HTML @app.get("/admin", response_class=HTMLResponse) async def admin_panel(username: str = Depends(get_admin_user)): opts = "".join( f'' for m in available_models ) rl = "".join(f'
Current model: {current_model} (Applies to all chats)