Skip to content

alipala/ai_fantasy_rpg

Repository files navigation

Fantasy RPG Game with CrewAI Agents 🎮

A text-based RPG game that uses CrewAI agents and Together AI to create dynamic, interactive fantasy worlds. The game features hierarchical content generation, state management, and an immersive chat interface.

System Architecture

graph TD
    A[Player Input] --> B[Game Master Agent]
    B --> C[World Builder Agent]
    B --> D[Inventory Manager Agent]
    B --> E[Safety Checker Agent]
    
    C --> F[World Generation]
    F --> G[Kingdoms]
    G --> H[Towns]
    H --> I[NPCs]
    
    D --> J[Inventory State]
    J --> K[Item Management]
    
    B --> L[Game State]
    L --> M[Current Location]
    L --> N[Player History]
    L --> O[World State]
Loading

CrewAI Agent Framework 🤖

The game uses four main agents that work together:

  1. World Builder Agent
class WorldBuilderAgent:
    def __init__(self, api_key):
        self.agent = Agent(
            role='World Builder',
            goal='Create rich fantasy worlds',
            backstory='Expert at worldbuilding',
            allow_delegation=False,
            llm=CustomTogetherModel(api_key)
        )
  1. Game Master Agent
class GameMasterAgent:
    def __init__(self, api_key):
        self.agent = Agent(
            role='Game Master',
            goal='Manage game flow',
            backstory='Expert storyteller',
            allow_delegation=True,
            llm=CustomTogetherModel(api_key)
        )

Agent interaction flow:

Player Input -> Game Master -> World Builder -> Game State Update
                    ↓
              Safety Checker
                    ↓
             Inventory Manager

Game State Management 🎲

Game state is managed through a Pydantic model:

class GameState(BaseModel):
    world: Dict
    current_location: Dict
    inventory: Dict[str, int]
    history: List[Dict]

State components:

  • World: Contains all generated content
  • Location: Player's current position
  • Inventory: Player's items
  • History: Action/response log

Hierarchical Content Generation 🏰

Content is generated in layers:

  1. World Generation
World
  ├── Description
  └── Start Message
  1. Kingdom Generation
Kingdom
  ├── Name
  ├── Description
  └── Unique Features
  1. Town Generation
Town
  ├── Name
  ├── Description
  ├── Location Details 
  └── Points of Interest
  1. NPC Generation
NPC
  ├── Name
  ├── Description
  ├── Background
  └── Motivations

Getting Started 🚀

  1. Install requirements:
pip install -r requirements.txt
  1. Set up environment:
# .env file
TOGETHER_API_KEY=your_api_key_here
  1. Initialize world:
python create_world.py
  1. Run the game:
python main.py

Key Features ✨

  1. Dynamic World Generation

    • Procedurally generated content
    • Consistent world logic
    • Rich NPC interactions
  2. Inventory System

    • Item management
    • Drag-and-drop interface
    • Real-time updates
  3. State Management

    • Persistent game state
    • History tracking
    • Save/load functionality
  4. Safety Checks

    • Content filtering
    • Input validation
    • Error handling

Content Generation Example 🎨

# Generate a new kingdom
kingdom = world_builder.generate_kingdoms(world_data)

# Generate towns for the kingdom
towns = world_builder.generate_towns(world_data, kingdom)

# Generate NPCs for each town
npcs = world_builder.generate_npcs(world_data, kingdom, town)

Advanced Features 🔧

  1. Custom Together AI Integration
class CustomTogetherModel(BaseChatModel):
    def _generate(self, messages: List[Dict[str, Any]], stop: List[str] | None = None) -> str:
        response = self.client.chat.completions.create(
            model="meta-llama/Llama-3-70b-chat-hf",
            messages=messages
        )
        return response.choices[0].message.content
  1. Inventory Management
def update_inventory(self, item_updates):
    for update in item_updates:
        name = update['name']
        change = update['change_amount']
        
        if name not in self.inventory:
            self.inventory[name] = 0
        self.inventory[name] += change
  1. Event Logging
logging.info(f"Action processed: {action}")
logging.info(f"Current inventory: {game_state.inventory}")

License 📄

MIT License - feel free to use and modify for your own projects!

About

This is a text based fantasy AI game

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors