VersacheX/FastAPIAdventureInAI

0

stars

22

commits

Python

primary language

Nov 27, 2025

updated

README

FastAPI Adventure in AI

An AI-powered interactive text adventure game with dynamic story generation using local LLM models.

Features

  • ๐ŸŽฎ Interactive text-based adventures with AI-generated narratives
  • ๐Ÿค– Local LLM integration (MythoMax-L2-13B-GPTQ)
  • ๐Ÿ“š Smart memory management with three-tier compression system
  • ๐ŸŒ Multiple pre-built worlds (Terminator Nexus, Mad Max Wasteland)
  • ๐ŸŽญ Content rating system (Family Friendly, Mature, Unrestricted)
  • ๐Ÿ‘ฅ User accounts with different tier levels
  • ๐Ÿ’พ Save/load game functionality
  • โšก Real-time story generation with token budget management

Architecture

Backend

  • FastAPI - Modern async Python web framework
  • SQLAlchemy - ORM for database management
  • SQLite - Embedded database (can be swapped for PostgreSQL/MySQL)
  • PyTorch + Transformers - AI model loading and inference (local GPTQ models)
  • CUDA - GPU acceleration for model inference (optional)

Frontend

  • React - UI framework
  • Axios - API communication
  • CSS - Custom styling

Directory Structure (actual repository layout)

FastAPIAdventureInAI/ # repo root
โ”œโ”€โ”€ .env.example
โ”œโ”€โ”€ README.md
โ”œโ”€โ”€ requirements.txt
โ”œโ”€โ”€ SETUP.md
โ”œโ”€โ”€ SETUP_database.py
โ”œโ”€โ”€ FastAPIAdventureInAI.sln
โ”œโ”€โ”€ FastAPIAdventureInAI.pyproj
โ”œโ”€โ”€ quick_setup.bat
โ”œโ”€โ”€ quick_setup.sh
โ”œโ”€โ”€ tools/ # utility scripts
โ”‚ โ”œโ”€โ”€ run_extractor.py
โ”‚ โ”œโ”€โ”€ scan_site_dumps.py
โ”‚ โ”œโ”€โ”€ scan_site_dumps_fixed.py
โ”‚ โ”œโ”€โ”€ generate_dom_json.py
โ”‚ โ””โ”€โ”€ analyze_hosts.py
โ”œโ”€โ”€ ai_main.py # helper entry that runs the AI server (runs `ai_server:app`)
โ””โ”€โ”€ FastAPIAdventureInAI/ # backend package
 โ”œโ”€โ”€ __init__.py
 โ”œโ”€โ”€ aiadventureinpythonconstants.py
 โ”œโ”€โ”€ config.py
 โ”œโ”€โ”€ data_server.py # FastAPI app wiring (includes routers)
 โ”œโ”€โ”€ main.py # helper entry that runs the backend (runs `data_server:app`)
 โ”œโ”€โ”€ ai_server.py # standalone AI inference server (optional separate process)
 โ”œโ”€โ”€ seed_data.py
 โ”œโ”€โ”€ setup_database.py
 โ”œโ”€โ”€ api/
 โ”‚ โ”œโ”€โ”€ __init__.py
 โ”‚ โ”œโ”€โ”€ ai_client_requests.py
 โ”‚ โ”œโ”€โ”€ routers/
 โ”‚ โ”‚ โ”œโ”€โ”€ __init__.py
 โ”‚ โ”‚ โ”œโ”€โ”€ auth_router.py
 โ”‚ โ”‚ โ”œโ”€โ”€ users_router.py
 โ”‚ โ”‚ โ”œโ”€โ”€ worlds_router.py
 โ”‚ โ”‚ โ”œโ”€โ”€ game_ratings_router.py
 โ”‚ โ”‚ โ”œโ”€โ”€ history_router.py
 โ”‚ โ”‚ โ”œโ”€โ”€ saved_games_router.py
 โ”‚ โ”‚ โ”œโ”€โ”€ tokenized_history_router.py
 โ”‚ โ”‚ โ””โ”€โ”€ deep_memory_router.py
 โ”‚ โ””โ”€โ”€ services/
 โ”‚ โ”œโ”€โ”€ __init__.py
 โ”‚ โ”œโ”€โ”€ data_api_auth_service.py
 โ”‚ โ”œโ”€โ”€ users_service.py
 โ”‚ โ”œโ”€โ”€ worlds_service.py
 โ”‚ โ”œโ”€โ”€ history_service.py
 โ”‚ โ”œโ”€โ”€ tokenized_history_service.py
 โ”‚ โ”œโ”€โ”€ deep_memory_service.py
 โ”‚ โ””โ”€โ”€ saved_games_service.py
 โ”œโ”€โ”€ ai/
 โ”‚ โ”œโ”€โ”€ __init__.py
 โ”‚ โ”œโ”€โ”€ schemas_ai_server.py
 โ”‚ โ”œโ”€โ”€ routers/
 โ”‚ โ”‚ โ”œโ”€โ”€ __init__.py
 โ”‚ โ”‚ โ”œโ”€โ”€ root_router.py
 โ”‚ โ”‚ โ”œโ”€โ”€ tokens_router.py
 โ”‚ โ”‚ โ””โ”€โ”€ lore_router.py
 โ”‚ โ”œโ”€โ”€ services/
 โ”‚ โ”‚ โ”œโ”€โ”€ __init__.py
 โ”‚ โ”‚ โ”œโ”€โ”€ ai_api_service.py
 โ”‚ โ”‚ โ”œโ”€โ”€ ai_modeler_service.py
 โ”‚ โ”‚ โ”œโ”€โ”€ lookup_ai_service.py
 โ”‚ โ”‚ โ”œโ”€โ”€ http_service.py
 โ”‚ โ”‚ โ”œโ”€โ”€ ddgs_service.py
 โ”‚ โ”‚ โ””โ”€โ”€ extractors/
 โ”‚ โ”‚ โ”œโ”€โ”€ __init__.py
 โ”‚ โ”‚ โ”œโ”€โ”€ common.py
 โ”‚ โ”‚ โ””โ”€โ”€ generic_extractor.py
 โ”‚ โ””โ”€โ”€ lookup_ai/
 โ”‚ โ”œโ”€โ”€ __init__.py
 โ”‚ โ”œโ”€โ”€ fetch_sources.py
 โ”‚ โ”œโ”€โ”€ section_selector.py
 โ”‚ โ”œโ”€โ”€ query_terms.py
 โ”‚ โ””โ”€โ”€ services/
 โ”‚ โ”œโ”€โ”€ __init__.py
 โ”‚ โ”œโ”€โ”€ wikipedia_service.py
 โ”‚ โ”œโ”€โ”€ fandom_service.py
 โ”‚ โ”œโ”€โ”€ lol_wiki_service.py
 โ”‚ โ”œโ”€โ”€ leagueoflegends_service.py
 โ”‚ โ”œโ”€โ”€ product_page_service.py
 โ”‚ โ”œโ”€โ”€ fanlore_service.py
 โ”‚ โ”œโ”€โ”€ gluwee_service.py
 โ”‚ โ”œโ”€โ”€ halloweencostumes_service.py
 โ”‚ โ”œโ”€โ”€ costumerealm_service.py
 โ”‚ โ””โ”€โ”€ animecharacters_service.py
 โ”œโ”€โ”€ business/
 โ”‚ โ”œโ”€โ”€ __init__.py
 โ”‚ โ”œโ”€โ”€ converters/
 โ”‚ โ”‚ โ”œโ”€โ”€ __init__.py
 โ”‚ โ”‚ โ””โ”€โ”€ converters.py
 โ”‚ โ”œโ”€โ”€ dtos/
 โ”‚ โ”‚ โ”œโ”€โ”€ __init__.py
 โ”‚ โ”‚ โ””โ”€โ”€ dtos.py
 โ”‚ โ”œโ”€โ”€ models/
 โ”‚ โ”‚ โ”œโ”€โ”€ __init__.py
 โ”‚ โ”‚ โ””โ”€โ”€ models.py
 โ”‚ โ””โ”€โ”€ schemas/
 โ”‚ โ”œโ”€โ”€ __init__.py
 โ”‚ โ””โ”€โ”€ schemas_api.py
 โ”œโ”€โ”€ shared/
 โ”‚ โ”œโ”€โ”€ __init__.py
 โ”‚ โ”œโ”€โ”€ helpers/
 โ”‚ โ”‚ โ”œโ”€โ”€ __init__.py
 โ”‚ โ”‚ โ”œโ”€โ”€ ai_settings.py
 โ”‚ โ”‚ โ””โ”€โ”€ memory_helper.py
 โ”‚ โ””โ”€โ”€ services/
 โ”‚ โ”œโ”€โ”€ __init__.py
 โ”‚ โ”œโ”€โ”€ auth_service.py
 โ”‚ โ””โ”€โ”€ orm_service.py
 โ””โ”€โ”€ tools/
 โ””โ”€โ”€ (project-specific helpers and scripts)

adventure-client/ # React frontend
โ”œโ”€โ”€ package.json
โ”œโ”€โ”€ package-lock.json
โ”œโ”€โ”€ public/
โ”‚ โ”œโ”€โ”€ index.html
โ”‚ โ”œโ”€โ”€ manifest.json
โ”‚ โ””โ”€โ”€ robots.txt
โ””โ”€โ”€ src/
 โ”œโ”€โ”€ index.js
 โ”œโ”€โ”€ index.css
 โ”œโ”€โ”€ App.js
 โ”œโ”€โ”€ App.css
 โ”œโ”€โ”€ Login.js
 โ”œโ”€โ”€ NewGame.js
 โ”œโ”€โ”€ CreateWorld.js
 โ”œโ”€โ”€ Game.js
 โ”œโ”€โ”€ LoadGame.js
 โ”œโ”€โ”€ ManageWorlds.js
 โ”œโ”€โ”€ ManageWorlds.js
 โ”œโ”€โ”€ config.js
 โ””โ”€โ”€ tests/
 โ””โ”€โ”€ (react tests)

Prerequisites

System Requirements

  • Python:3.10+ (recommended3.10 for best compatibility)
  • Node.js:16+ and npm (for frontend)
  • CUDA: Optional, required for GPU acceleration
  • GPU: Recommended for local model inference (8GB+ VRAM suggested)
  • RAM:16GB+ recommended
  • Storage:20GB+ free space for models

Software Dependencies

  • Git
  • Python virtual environment (venv)
  • Build tools for native packages (Visual Studio Build Tools on Windows or gcc/g++ on Linux)

Installation

###1. Clone the Repository

git clone https://github.com/VersacheX/FastAPIAdventureInAI.git
cd FastAPIAdventureInAI

###2. Backend Setup

Create Python Virtual Environment

cd FastAPIAdventureInAI
python -m venv env

Activate Virtual Environment

Windows (PowerShell):

.\\env\\Scripts\\Activate.ps1

Windows (CMD):

.\\env\\Scripts\\activate.bat

Linux/Mac:

source env/bin/activate

Install Python Dependencies

pip install -r requirements.txt

If you have CUDA/PyTorch compatibility issues, install PyTorch separately using the instructions from the PyTorch website for your CUDA version.

Configure Database

Edit FastAPIAdventureInAI/config.py to set your database connection (default is SQLite):

DATABASE_URL = "sqlite:///./adventure.db"

Create Database Tables

python -c "from dependencies import engine; from business.models import Base; Base.metadata.create_all(bind=engine)"

Seed Initial Data

python seed_data.py

This will create default game ratings, pre-built worlds, AI directive settings, account levels, and an admin user.

Download AI Model

The project uses a local GPTQ-compatible model. The active model constant is defined in FastAPIAdventureInAI/ai/services/ai_modeler_service.py as AI_MODEL.

Recommended model: TheBloke/MythoMax-L2-13B-GPTQ (or another compatible GPTQ model). Update AI_MODEL in ai/services/ai_modeler_service.py if you place the model locally or want to switch models.

Running the Application

You typically run two processes (three if you run the frontend locally):

Terminal1: AI Inference Server (optional separate process)

You can start the AI server either by running ai_server.py directly or using ai_main.py which launches the same app with Uvicorn. Run the standalone AI server which loads the local model and serves inference on port9000:

cd FastAPIAdventureInAI
.\\env\\Scripts\\Activate.ps1 # or activate your venv
python ai_main.py

This starts the AI model server on http://localhost:9000.

Note: ai_server.py and the model loader in ai/services/ai_modeler_service.py are responsible for loading the local GPTQ model. If you prefer the backend to directly load the model into FastAPI app state, the code already supports loading the model into app.state.

Terminal2: FastAPI Backend

Start the backend API (uses data_server.py via main.py):

cd FastAPIAdventureInAI
.\\env\\Scripts\\Activate.ps1
python main.py

This starts the API server (default host 0.0.0.0) on port8080 by default.

Alternatively you can run:

python data_server.py

Terminal3: React Frontend

cd adventure-client
npm install
npm start

This starts the frontend development server on http://localhost:3000.

Usage

  1. Open browser to http://localhost:3000
  2. Login with the seeded admin account (if present) or register a user
  3. Create a new game and start playing

Configuration

API Settings (FastAPIAdventureInAI/config.py)

SECRET_KEY = "your-secret-key-here" # Change in production!
DATABASE_URL = "sqlite:///./adventure.db"
AI_SERVER_URL = "http://127.0.0.1:9000"
CORS_ORIGINS = ["http://localhost:3000"]

AI Model Settings

Token budgets and memory limits can be adjusted in the AI helpers and settings files under ai/ and shared/helpers/.

Memory Management System

The application uses a three-tier memory compression system:

  1. Recent History (uncompressed): last entries kept in full
  2. Tokenized Chunks (compressed): older entries summarized into token-sized blocks
  3. Deep Memory (ultra-compressed): ancient history compressed further

This keeps prompts within model context windows while preserving key story information.

Development

Adding API Endpoints

  1. Create/edit a router under api/routers/ or ai/routers/ for AI-specific endpoints
  2. Register the router in data_server.py (or main wiring)

Database Migrations

When changing models:

  1. Update models under business/models/
  2. Update the database schema manually or integrate Alembic

Troubleshooting

Common issues

  • Ensure virtual environment is activated and dependencies installed
  • For CUDA OOM errors: use a smaller model, reduce memory usage, or run on CPU
  • If ports are in use, change the port in main.py or ai_server.py

API Documentation

Once running backend, visit:

  • Swagger UI: http://localhost:8080/docs
  • ReDoc: http://localhost:8080/redoc

Security Notes

  • Change SECRET_KEY and default passwords before production
  • Use HTTPS and proper credentials in production

License

Add your license here.

Contributors

VersacheX

22 commits

VersacheX/FastAPIAdventureInAI

0

stars

22

commits

Python

primary language

Nov 27, 2025

updated

README

FastAPI Adventure in AI

An AI-powered interactive text adventure game with dynamic story generation using local LLM models.

Features

  • ๐ŸŽฎ Interactive text-based adventures with AI-generated narratives
  • ๐Ÿค– Local LLM integration (MythoMax-L2-13B-GPTQ)
  • ๐Ÿ“š Smart memory management with three-tier compression system
  • ๐ŸŒ Multiple pre-built worlds (Terminator Nexus, Mad Max Wasteland)
  • ๐ŸŽญ Content rating system (Family Friendly, Mature, Unrestricted)
  • ๐Ÿ‘ฅ User accounts with different tier levels
  • ๐Ÿ’พ Save/load game functionality
  • โšก Real-time story generation with token budget management

Architecture

Backend

  • FastAPI - Modern async Python web framework
  • SQLAlchemy - ORM for database management
  • SQLite - Embedded database (can be swapped for PostgreSQL/MySQL)
  • PyTorch + Transformers - AI model loading and inference (local GPTQ models)
  • CUDA - GPU acceleration for model inference (optional)

Frontend

  • React - UI framework
  • Axios - API communication
  • CSS - Custom styling

Directory Structure (actual repository layout)

FastAPIAdventureInAI/ # repo root
โ”œโ”€โ”€ .env.example
โ”œโ”€โ”€ README.md
โ”œโ”€โ”€ requirements.txt
โ”œโ”€โ”€ SETUP.md
โ”œโ”€โ”€ SETUP_database.py
โ”œโ”€โ”€ FastAPIAdventureInAI.sln
โ”œโ”€โ”€ FastAPIAdventureInAI.pyproj
โ”œโ”€โ”€ quick_setup.bat
โ”œโ”€โ”€ quick_setup.sh
โ”œโ”€โ”€ tools/ # utility scripts
โ”‚ โ”œโ”€โ”€ run_extractor.py
โ”‚ โ”œโ”€โ”€ scan_site_dumps.py
โ”‚ โ”œโ”€โ”€ scan_site_dumps_fixed.py
โ”‚ โ”œโ”€โ”€ generate_dom_json.py
โ”‚ โ””โ”€โ”€ analyze_hosts.py
โ”œโ”€โ”€ ai_main.py # helper entry that runs the AI server (runs `ai_server:app`)
โ””โ”€โ”€ FastAPIAdventureInAI/ # backend package
 โ”œโ”€โ”€ __init__.py
 โ”œโ”€โ”€ aiadventureinpythonconstants.py
 โ”œโ”€โ”€ config.py
 โ”œโ”€โ”€ data_server.py # FastAPI app wiring (includes routers)
 โ”œโ”€โ”€ main.py # helper entry that runs the backend (runs `data_server:app`)
 โ”œโ”€โ”€ ai_server.py # standalone AI inference server (optional separate process)
 โ”œโ”€โ”€ seed_data.py
 โ”œโ”€โ”€ setup_database.py
 โ”œโ”€โ”€ api/
 โ”‚ โ”œโ”€โ”€ __init__.py
 โ”‚ โ”œโ”€โ”€ ai_client_requests.py
 โ”‚ โ”œโ”€โ”€ routers/
 โ”‚ โ”‚ โ”œโ”€โ”€ __init__.py
 โ”‚ โ”‚ โ”œโ”€โ”€ auth_router.py
 โ”‚ โ”‚ โ”œโ”€โ”€ users_router.py
 โ”‚ โ”‚ โ”œโ”€โ”€ worlds_router.py
 โ”‚ โ”‚ โ”œโ”€โ”€ game_ratings_router.py
 โ”‚ โ”‚ โ”œโ”€โ”€ history_router.py
 โ”‚ โ”‚ โ”œโ”€โ”€ saved_games_router.py
 โ”‚ โ”‚ โ”œโ”€โ”€ tokenized_history_router.py
 โ”‚ โ”‚ โ””โ”€โ”€ deep_memory_router.py
 โ”‚ โ””โ”€โ”€ services/
 โ”‚ โ”œโ”€โ”€ __init__.py
 โ”‚ โ”œโ”€โ”€ data_api_auth_service.py
 โ”‚ โ”œโ”€โ”€ users_service.py
 โ”‚ โ”œโ”€โ”€ worlds_service.py
 โ”‚ โ”œโ”€โ”€ history_service.py
 โ”‚ โ”œโ”€โ”€ tokenized_history_service.py
 โ”‚ โ”œโ”€โ”€ deep_memory_service.py
 โ”‚ โ””โ”€โ”€ saved_games_service.py
 โ”œโ”€โ”€ ai/
 โ”‚ โ”œโ”€โ”€ __init__.py
 โ”‚ โ”œโ”€โ”€ schemas_ai_server.py
 โ”‚ โ”œโ”€โ”€ routers/
 โ”‚ โ”‚ โ”œโ”€โ”€ __init__.py
 โ”‚ โ”‚ โ”œโ”€โ”€ root_router.py
 โ”‚ โ”‚ โ”œโ”€โ”€ tokens_router.py
 โ”‚ โ”‚ โ””โ”€โ”€ lore_router.py
 โ”‚ โ”œโ”€โ”€ services/
 โ”‚ โ”‚ โ”œโ”€โ”€ __init__.py
 โ”‚ โ”‚ โ”œโ”€โ”€ ai_api_service.py
 โ”‚ โ”‚ โ”œโ”€โ”€ ai_modeler_service.py
 โ”‚ โ”‚ โ”œโ”€โ”€ lookup_ai_service.py
 โ”‚ โ”‚ โ”œโ”€โ”€ http_service.py
 โ”‚ โ”‚ โ”œโ”€โ”€ ddgs_service.py
 โ”‚ โ”‚ โ””โ”€โ”€ extractors/
 โ”‚ โ”‚ โ”œโ”€โ”€ __init__.py
 โ”‚ โ”‚ โ”œโ”€โ”€ common.py
 โ”‚ โ”‚ โ””โ”€โ”€ generic_extractor.py
 โ”‚ โ””โ”€โ”€ lookup_ai/
 โ”‚ โ”œโ”€โ”€ __init__.py
 โ”‚ โ”œโ”€โ”€ fetch_sources.py
 โ”‚ โ”œโ”€โ”€ section_selector.py
 โ”‚ โ”œโ”€โ”€ query_terms.py
 โ”‚ โ””โ”€โ”€ services/
 โ”‚ โ”œโ”€โ”€ __init__.py
 โ”‚ โ”œโ”€โ”€ wikipedia_service.py
 โ”‚ โ”œโ”€โ”€ fandom_service.py
 โ”‚ โ”œโ”€โ”€ lol_wiki_service.py
 โ”‚ โ”œโ”€โ”€ leagueoflegends_service.py
 โ”‚ โ”œโ”€โ”€ product_page_service.py
 โ”‚ โ”œโ”€โ”€ fanlore_service.py
 โ”‚ โ”œโ”€โ”€ gluwee_service.py
 โ”‚ โ”œโ”€โ”€ halloweencostumes_service.py
 โ”‚ โ”œโ”€โ”€ costumerealm_service.py
 โ”‚ โ””โ”€โ”€ animecharacters_service.py
 โ”œโ”€โ”€ business/
 โ”‚ โ”œโ”€โ”€ __init__.py
 โ”‚ โ”œโ”€โ”€ converters/
 โ”‚ โ”‚ โ”œโ”€โ”€ __init__.py
 โ”‚ โ”‚ โ””โ”€โ”€ converters.py
 โ”‚ โ”œโ”€โ”€ dtos/
 โ”‚ โ”‚ โ”œโ”€โ”€ __init__.py
 โ”‚ โ”‚ โ””โ”€โ”€ dtos.py
 โ”‚ โ”œโ”€โ”€ models/
 โ”‚ โ”‚ โ”œโ”€โ”€ __init__.py
 โ”‚ โ”‚ โ””โ”€โ”€ models.py
 โ”‚ โ””โ”€โ”€ schemas/
 โ”‚ โ”œโ”€โ”€ __init__.py
 โ”‚ โ””โ”€โ”€ schemas_api.py
 โ”œโ”€โ”€ shared/
 โ”‚ โ”œโ”€โ”€ __init__.py
 โ”‚ โ”œโ”€โ”€ helpers/
 โ”‚ โ”‚ โ”œโ”€โ”€ __init__.py
 โ”‚ โ”‚ โ”œโ”€โ”€ ai_settings.py
 โ”‚ โ”‚ โ””โ”€โ”€ memory_helper.py
 โ”‚ โ””โ”€โ”€ services/
 โ”‚ โ”œโ”€โ”€ __init__.py
 โ”‚ โ”œโ”€โ”€ auth_service.py
 โ”‚ โ””โ”€โ”€ orm_service.py
 โ””โ”€โ”€ tools/
 โ””โ”€โ”€ (project-specific helpers and scripts)

adventure-client/ # React frontend
โ”œโ”€โ”€ package.json
โ”œโ”€โ”€ package-lock.json
โ”œโ”€โ”€ public/
โ”‚ โ”œโ”€โ”€ index.html
โ”‚ โ”œโ”€โ”€ manifest.json
โ”‚ โ””โ”€โ”€ robots.txt
โ””โ”€โ”€ src/
 โ”œโ”€โ”€ index.js
 โ”œโ”€โ”€ index.css
 โ”œโ”€โ”€ App.js
 โ”œโ”€โ”€ App.css
 โ”œโ”€โ”€ Login.js
 โ”œโ”€โ”€ NewGame.js
 โ”œโ”€โ”€ CreateWorld.js
 โ”œโ”€โ”€ Game.js
 โ”œโ”€โ”€ LoadGame.js
 โ”œโ”€โ”€ ManageWorlds.js
 โ”œโ”€โ”€ ManageWorlds.js
 โ”œโ”€โ”€ config.js
 โ””โ”€โ”€ tests/
 โ””โ”€โ”€ (react tests)

Prerequisites

System Requirements

  • Python:3.10+ (recommended3.10 for best compatibility)
  • Node.js:16+ and npm (for frontend)
  • CUDA: Optional, required for GPU acceleration
  • GPU: Recommended for local model inference (8GB+ VRAM suggested)
  • RAM:16GB+ recommended
  • Storage:20GB+ free space for models

Software Dependencies

  • Git
  • Python virtual environment (venv)
  • Build tools for native packages (Visual Studio Build Tools on Windows or gcc/g++ on Linux)

Installation

###1. Clone the Repository

git clone https://github.com/VersacheX/FastAPIAdventureInAI.git
cd FastAPIAdventureInAI

###2. Backend Setup

Create Python Virtual Environment

cd FastAPIAdventureInAI
python -m venv env

Activate Virtual Environment

Windows (PowerShell):

.\\env\\Scripts\\Activate.ps1

Windows (CMD):

.\\env\\Scripts\\activate.bat

Linux/Mac:

source env/bin/activate

Install Python Dependencies

pip install -r requirements.txt

If you have CUDA/PyTorch compatibility issues, install PyTorch separately using the instructions from the PyTorch website for your CUDA version.

Configure Database

Edit FastAPIAdventureInAI/config.py to set your database connection (default is SQLite):

DATABASE_URL = "sqlite:///./adventure.db"

Create Database Tables

python -c "from dependencies import engine; from business.models import Base; Base.metadata.create_all(bind=engine)"

Seed Initial Data

python seed_data.py

This will create default game ratings, pre-built worlds, AI directive settings, account levels, and an admin user.

Download AI Model

The project uses a local GPTQ-compatible model. The active model constant is defined in FastAPIAdventureInAI/ai/services/ai_modeler_service.py as AI_MODEL.

Recommended model: TheBloke/MythoMax-L2-13B-GPTQ (or another compatible GPTQ model). Update AI_MODEL in ai/services/ai_modeler_service.py if you place the model locally or want to switch models.

Running the Application

You typically run two processes (three if you run the frontend locally):

Terminal1: AI Inference Server (optional separate process)

You can start the AI server either by running ai_server.py directly or using ai_main.py which launches the same app with Uvicorn. Run the standalone AI server which loads the local model and serves inference on port9000:

cd FastAPIAdventureInAI
.\\env\\Scripts\\Activate.ps1 # or activate your venv
python ai_main.py

This starts the AI model server on http://localhost:9000.

Note: ai_server.py and the model loader in ai/services/ai_modeler_service.py are responsible for loading the local GPTQ model. If you prefer the backend to directly load the model into FastAPI app state, the code already supports loading the model into app.state.

Terminal2: FastAPI Backend

Start the backend API (uses data_server.py via main.py):

cd FastAPIAdventureInAI
.\\env\\Scripts\\Activate.ps1
python main.py

This starts the API server (default host 0.0.0.0) on port8080 by default.

Alternatively you can run:

python data_server.py

Terminal3: React Frontend

cd adventure-client
npm install
npm start

This starts the frontend development server on http://localhost:3000.

Usage

  1. Open browser to http://localhost:3000
  2. Login with the seeded admin account (if present) or register a user
  3. Create a new game and start playing

Configuration

API Settings (FastAPIAdventureInAI/config.py)

SECRET_KEY = "your-secret-key-here" # Change in production!
DATABASE_URL = "sqlite:///./adventure.db"
AI_SERVER_URL = "http://127.0.0.1:9000"
CORS_ORIGINS = ["http://localhost:3000"]

AI Model Settings

Token budgets and memory limits can be adjusted in the AI helpers and settings files under ai/ and shared/helpers/.

Memory Management System

The application uses a three-tier memory compression system:

  1. Recent History (uncompressed): last entries kept in full
  2. Tokenized Chunks (compressed): older entries summarized into token-sized blocks
  3. Deep Memory (ultra-compressed): ancient history compressed further

This keeps prompts within model context windows while preserving key story information.

Development

Adding API Endpoints

  1. Create/edit a router under api/routers/ or ai/routers/ for AI-specific endpoints
  2. Register the router in data_server.py (or main wiring)

Database Migrations

When changing models:

  1. Update models under business/models/
  2. Update the database schema manually or integrate Alembic

Troubleshooting

Common issues

  • Ensure virtual environment is activated and dependencies installed
  • For CUDA OOM errors: use a smaller model, reduce memory usage, or run on CPU
  • If ports are in use, change the port in main.py or ai_server.py

API Documentation

Once running backend, visit:

  • Swagger UI: http://localhost:8080/docs
  • ReDoc: http://localhost:8080/redoc

Security Notes

  • Change SECRET_KEY and default passwords before production
  • Use HTTPS and proper credentials in production

License

Add your license here.

Contributors

VersacheX

22 commits

Languages

Python

73.5%

JavaScript

18.6%

CSS

6.6%