https://github.com/991o2o9/ai_agent_backend
Urban Future: Voices of the City โ Backend API and core logic for the interactive city simulation. Handles AI-driven NPC behaviors, dialogue management, memory storage, and game state, enabling dynamic social interactions and decision-making in the game world.
https://github.com/991o2o9/ai_agent_backend
ai-agents fastapi npc postgresql python redis
Last synced: about 1 month ago
JSON representation
Urban Future: Voices of the City โ Backend API and core logic for the interactive city simulation. Handles AI-driven NPC behaviors, dialogue management, memory storage, and game state, enabling dynamic social interactions and decision-making in the game world.
- Host: GitHub
- URL: https://github.com/991o2o9/ai_agent_backend
- Owner: 991o2o9
- Created: 2025-09-02T02:58:24.000Z (9 months ago)
- Default Branch: dev
- Last Pushed: 2025-09-04T15:16:39.000Z (9 months ago)
- Last Synced: 2025-09-04T17:28:49.409Z (9 months ago)
- Topics: ai-agents, fastapi, npc, postgresql, python, redis
- Language: Python
- Homepage:
- Size: 87.9 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# ๐ฎ NPC Game Backend
[](https://fastapi.tiangolo.com/)
[](https://www.python.org/)
[](https://www.postgresql.org/)
[](https://redis.io/)
[](https://www.docker.com/)
**A modern, high-performance FastAPI backend service for NPC (Non-Player Character) game interactions**
[๐ API Docs](http://localhost:8000/docs) โข [๐ง Configuration](#-configuration) โข [๐ Quick Start](#-quick-start) โข [๐ค Contributing](#-contributing)
---
## โจ Features
๐ **High Performance**
- FastAPI async framework
- Optimized database queries
- Redis caching layer
๐ **Security First**
- JWT-based authentication
- Password hashing with Passlib
- Session management
๐ฎ **Game Systems**
- NPC management & interactions
- Session handling & state
- User progress tracking
๐ ๏ธ **Developer Experience**
- Auto-generated API docs
- Database migrations
- Docker containerization
## ๐๏ธ Architecture
```mermaid
graph TB
A[Client] -->|HTTP Requests| B[FastAPI Backend]
B --> C[Authentication Service]
B --> D[NPC Service]
B --> E[Session Service]
B --> F[User Service]
C --> G[(PostgreSQL)]
D --> G
E --> G
F --> G
B --> H[(Redis Cache)]
style A fill:#e1f5fe
style B fill:#f3e5f5
style G fill:#e8f5e8
style H fill:#fff3e0
```
## ๐ ๏ธ Tech Stack
| Category | Technology |
|----------|------------|
| **Backend** |   |
| **Database** |   |
| **Cache** |  |
| **Auth** |   |
| **DevOps** |   |
| **Tools** |  |
## ๐ Prerequisites
> **Note**: Make sure you have the following installed on your system:
- ๐ **Python 3.11+**
- ๐ฆ **Poetry** (dependency management)
- ๐ณ **Docker & Docker Compose**
- ๐ **PostgreSQL 15** (for local development)
- ๐ด **Redis 7** (for local development)
## ๐ Quick Start
### ๐ณ Docker Setup (Recommended)
```bash
# 1. Clone the repository
git clone
cd ai_agent_backend
# 2. Environment setup
# Create .env file with your configuration (see Configuration section below)
# 3. Launch all services
docker-compose up -d
# 4. Run database migrations
docker-compose exec backend alembic upgrade head
# 5. ๐ You're ready to go!
# API: http://localhost:8000
# Docs: http://localhost:8000/docs
```
### ๐ง Local Development
Click to expand local development setup
```bash
# 1. Install Poetry
curl -sSL https://install.python-poetry.org | python3 -
# 2. Install dependencies
poetry install
# 3. Activate virtual environment
poetry shell
# 4. Setup environment
cp .env.example .env
# 5. Start databases
docker-compose up -d db redis
# 6. Run migrations
alembic upgrade head
# 7. Start development server
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000
```
## ๐ Project Structure
```
ai_agent_backend/
โโโ ๐ alembic/ # Database migrations
โ โโโ env.py
โ โโโ script.py.mako
โ โโโ versions/
โโโ ๐ app/
โ โโโ ๐ api/ # ๐ฃ๏ธ API route handlers
โ โ โโโ npc.py
โ โ โโโ sessions.py
โ โ โโโ users.py
โ โโโ ๐ core/ # โ๏ธ Core configuration
โ โ โโโ config.py
โ โ โโโ db.py
โ โ โโโ redis.py
โ โ โโโ security.py
โ โโโ ๐ models/ # ๐๏ธ SQLAlchemy models
โ โ โโโ npc.py
โ โ โโโ session.py
โ โ โโโ user.py
โ โโโ ๐ repositories/ # ๐พ Data access layer
โ โ โโโ npc_repo.py
โ โ โโโ session_repo.py
โ โ โโโ user_repo.py
โ โโโ ๐ schemas/ # ๐ Pydantic schemas
โ โ โโโ npc.py
โ โ โโโ session.py
โ โ โโโ user.py
โ โโโ ๐ services/ # ๐ง Business logic
โ โ โโโ npc_service.py
โ โ โโโ session_service.py
โ โ โโโ user_service.py
โ โโโ ๐ main.py # ๐ FastAPI application
โโโ ๐ณ docker-compose.yml
โโโ ๐ฆ pyproject.toml
โโโ ๐ README.md
```
## โ๏ธ Configuration
Create a `.env` file in the root directory:
```env
# Database Configuration
DATABASE_URL=postgresql://username:password@localhost:5432/npc_game_db
REDIS_URL=redis://localhost:6379/0
# Security Configuration
SECRET_KEY=your-super-secret-key-here-change-this-in-production
ALGORITHM=HS256
ACCESS_TOKEN_EXPIRE_MINUTES=30
# Hugging Face Configuration (Required for AI NPC responses)
HUGGINGFACE_API_TOKEN=your-huggingface-api-token-here
HUGGINGFACE_MODEL=mistralai/Mistral-7B-Instruct-v0.2
HUGGINGFACE_MODEL_PROVIDER=featherless-ai
# AI API Endpoints (optional - these have sensible defaults)
HUGGINGFACE_CHAT_API_URL=https://router.huggingface.co/v1/chat/completions
HUGGINGFACE_INFERENCE_API_URL=https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.2
# AI Generation Settings
MAX_TOKENS=512
TEMPERATURE=0.7
TOP_P=0.9
AI_TIMEOUT=30
# Memory Settings
SHORT_MEMORY_TTL=3600
LONG_MEMORY_LIMIT=100
# Rate Limiting
MAX_REQUESTS_PER_MINUTE=60
MAX_REQUESTS_PER_HOUR=1000
```
**Important Security Notes:**
- ๐ Never commit your `.env` file to version control
- ๐ Generate a strong, unique `SECRET_KEY` for production
- ๐ซ Get your Hugging Face API token from [Hugging Face Settings](https://huggingface.co/settings/tokens)
- ๐ In production, use environment variables instead of `.env` files
## ๐ API Documentation
Once the server is running, explore the API:
| Service | URL | Description |
|---------|-----|-------------|
| ๐ **Home** | http://localhost:8000 | Health check endpoint |
| ๐ **Swagger UI** | http://localhost:8000/docs | Interactive API documentation |
| ๐ **ReDoc** | http://localhost:8000/redoc | Alternative API documentation |
### ๐ฃ๏ธ Key Endpoints
๐ฅ User Management
- `POST /users/register` - Register new user
- `POST /users/login` - User authentication
- `GET /users/me` - Get current user profile
๐ฎ Session Management
- `POST /sessions/` - Create new game session
- `GET /sessions/` - List user sessions
- `GET /sessions/{id}` - Get session details
๐ค NPC Management
- `POST /npc/` - Create new NPC
- `GET /npc/` - List all NPCs
- `GET /npc/{id}` - Get NPC details
## ๐๏ธ Database Operations
### Migrations
```bash
# Create new migration
alembic revision --autogenerate -m "Add new feature"
# Apply migrations
alembic upgrade head
# Rollback last migration
alembic downgrade -1
```
## ๐งช Development & Testing
```bash
# Install development dependencies
poetry install --with dev
# Run tests
pytest
# Code formatting
black .
isort .
flake8 .
```
## ๐ณ Docker Commands
```bash
# ๐ Start all services
docker-compose up -d
# ๐ View logs
docker-compose logs -f backend
# ๐ Stop services
docker-compose down
# ๐ Rebuild and restart
docker-compose up -d --build
# ๐ป Access backend container
docker-compose exec backend bash
```
## ๐ฎ Roadmap
- [ ] ๐ WebSocket support for real-time interactions
- [ ] ๐ง Advanced NPC AI behaviors using LLMs
- [ ] ๐พ Enhanced game state persistence
- [ ] ๐ฅ Multiplayer session support
- [ ] ๐ Performance monitoring and metrics
- [ ] ๐ฆ API rate limiting
- [ ] ๐งช Comprehensive test coverage
- [ ] ๐จ Admin dashboard
- [ ] ๐ฑ Mobile API optimizations
## ๐ค Contributing
We welcome contributions! Here's how you can help:
1. ๐ด Fork the repository
2. ๐ฟ Create your feature branch (`git checkout -b feature/amazing-feature`)
3. โ
Commit your changes (`git commit -m 'Add some amazing feature'`)
4. ๐ค Push to the branch (`git push origin feature/amazing-feature`)
5. ๐ Open a Pull Request
### ๐ Contribution Guidelines
- Follow PEP 8 style guidelines
- Add tests for new features
- Update documentation as needed
- Use conventional commit messages
## ๐ Support
Need help? We're here for you!
- ๐ [Create an Issue](https://github.com/your-username/ai_agent_backend/issues)
- ๐ฌ [Discussions](https://github.com/your-username/ai_agent_backend/discussions)
- ๐ง Contact the maintainer
## ๐จโ๐ป Author
**Amin Soltobekov**
[](https://www.linkedin.com/in/amin-soltobekov-482b5430b/)
[](https://github.com/yourusername)
---
## ๐ License
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
---
**โญ Star this repo if you find it helpful!**
Made with โค๏ธ and โ by [Amin Soltobekov](https://www.linkedin.com/in/amin-soltobekov-482b5430b/)