An open API service indexing awesome lists of open source software.

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.

Awesome Lists containing this project

README

          

# ๐ŸŽฎ NPC Game Backend



[![FastAPI](https://img.shields.io/badge/FastAPI-005571?style=for-the-badge&logo=fastapi)](https://fastapi.tiangolo.com/)
[![Python](https://img.shields.io/badge/python-3670A0?style=for-the-badge&logo=python&logoColor=ffdd54)](https://www.python.org/)
[![PostgreSQL](https://img.shields.io/badge/postgresql-%23316192.svg?style=for-the-badge&logo=postgresql&logoColor=white)](https://www.postgresql.org/)
[![Redis](https://img.shields.io/badge/redis-%23DD0031.svg?style=for-the-badge&logo=redis&logoColor=white)](https://redis.io/)
[![Docker](https://img.shields.io/badge/docker-%230db7ed.svg?style=for-the-badge&logo=docker&logoColor=white)](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** | ![FastAPI](https://img.shields.io/badge/-FastAPI-009688?style=flat&logo=fastapi&logoColor=white) ![Python](https://img.shields.io/badge/-Python%203.11+-3776ab?style=flat&logo=python&logoColor=white) |
| **Database** | ![PostgreSQL](https://img.shields.io/badge/-PostgreSQL%2015-336791?style=flat&logo=postgresql&logoColor=white) ![SQLAlchemy](https://img.shields.io/badge/-SQLAlchemy%202.0-red?style=flat) |
| **Cache** | ![Redis](https://img.shields.io/badge/-Redis%207-dc382d?style=flat&logo=redis&logoColor=white) |
| **Auth** | ![JWT](https://img.shields.io/badge/-JWT-000000?style=flat&logo=jsonwebtokens&logoColor=white) ![Passlib](https://img.shields.io/badge/-Passlib-green?style=flat) |
| **DevOps** | ![Docker](https://img.shields.io/badge/-Docker-2496ed?style=flat&logo=docker&logoColor=white) ![Alembic](https://img.shields.io/badge/-Alembic-orange?style=flat) |
| **Tools** | ![Poetry](https://img.shields.io/badge/-Poetry-60a5fa?style=flat&logo=poetry&logoColor=white) |

## ๐Ÿ“‹ 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**

[![LinkedIn](https://img.shields.io/badge/LinkedIn-0077B5?style=for-the-badge&logo=linkedin&logoColor=white)](https://www.linkedin.com/in/amin-soltobekov-482b5430b/)
[![GitHub](https://img.shields.io/badge/GitHub-100000?style=for-the-badge&logo=github&logoColor=white)](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/)