DiceTales Main AI System Documentation
Overview
The Main AI System (advanced/js/ai.js
) is the core storytelling engine of DiceTales, powered by HuggingFace’s advanced language models. This system provides human-like conversational AI with comprehensive memory integration, plot grounding, and dynamic storytelling capabilities.
Architecture
Core Components
- AIManager Class: Primary AI coordination and management
- HuggingFace Integration: Modern conversational models for human-like responses
- Memory Integration: Seamless connection with Memory Manager for context
- Response Enhancement: Post-processing for immersive storytelling
- Model Fallback System: Robust model switching for reliability
Model Priority Queue
The system uses a prioritized list of HuggingFace models:
microsoft/GODEL-v1_1-large-seq2seq
- Primary conversational AI
facebook/blenderbot-400M-distill
- Optimized dialogue generation
microsoft/GODEL-v1_1-base-seq2seq
- Balanced performance model
facebook/blenderbot-1B-distill
- Large-scale conversational model
microsoft/DialoGPT-large
- Proven conversational model
microsoft/DialoGPT-medium
- Medium-sized reliable model
gpt2-large
/ distilgpt2
- General purpose fallback models
Key Features
🤖 Human-Like Conversational AI
Advanced Models: Uses state-of-the-art conversational models specifically designed for natural dialogue generation.
Context Integration: Builds comprehensive prompts including:
- Character stats, equipment, and current health
- Past decisions and their consequences
- NPC relationships and interaction history
- Discovered locations and world state
- Active plot threads for story continuity
💭 Enhanced Response Processing
Quality Enhancement Pipeline:
- Context Assembly: Builds comprehensive prompt with character and memory data
- API Request: Sends request to HuggingFace models with fallback handling
- Response Enhancement: Cleans and enhances raw AI output for immersion
- Memory Extraction: Automatically extracts important information for storage
- Display Processing: Formats response for game interface display
Meta-Commentary Filtering: Removes AI artifacts and improves immersion:
- Filters out planning statements and meta-analysis
- Removes system-level commentary
- Enhances descriptive language
- Maintains proper narrative voice
🧠 Memory System Integration
Comprehensive Context Building:
🎭 CHARACTER PROFILE: [Name, Class, Level, Background, Current Status]
📊 ABILITY SCORES: [STR, DEX, CON, INT, WIS, CHA with current values]
❤️ HEALTH STATUS: [Current/Max HP, conditions, injuries]
🎒 INVENTORY: [Equipped items, carried equipment, special items]
🧠 RECENT DECISIONS: [Last 3 significant choices and consequences]
👥 KEY RELATIONSHIPS: [Important NPCs and relationship status]
🔍 DISCOVERIES: [Recent findings and their significance]
🎯 SKILLS USED: [Frequently used skills and success patterns]
📖 ACTIVE PLOTS: [Current story threads and objectives]
🗺️ LOCATION CONTEXT: [Current area and relevant locations]
Automatic Information Tracking:
- Detects new NPCs and tracks relationships
- Records location descriptions for consistency
- Monitors quest progression and objectives
- Tracks inventory changes and item acquisition
- Identifies skill usage patterns
⚙️ Configuration Options
Conversation Settings:
conversationConfig: {
maxContextLength: 2048,
temperature: 0.8,
topP: 0.9,
repetitionPenalty: 1.1,
maxNewTokens: 150,
doSample: true,
numBeams: 3
}
Memory Configuration:
memoryConfig: {
maxConversationHistory: 20,
plotContextWindow: 5,
characterMemoryDepth: 10
}
API Reference
Constructor
const aiManager = new AIManager();
Core Methods
async initialize()
Initializes the AI system and tests model connections.
await aiManager.initialize();
async startCampaign()
Starts a new campaign with initial story generation.
await aiManager.startCampaign();
async processPlayerAction(actionData)
Processes player actions and generates AI responses.
await aiManager.processPlayerAction({
action: "Search the ancient ruins",
type: "exploration",
diceRoll: { result: 15, sides: 20 }
});
buildMemoryContext(character, campaign)
Builds comprehensive memory context for AI prompts.
const context = aiManager.buildMemoryContext(character, campaign);
async callAI(messages)
Makes API calls to HuggingFace models with fallback system.
const response = await aiManager.callAI([
{ role: 'system', content: 'You are a helpful DM.' },
{ role: 'user', content: 'What happens next?' }
]);
Testing and Debugging
Browser Console Functions
// Test AI response system
testAI()
// Test specific player action
testPlayerAction("examine the room")
// Test memory system functionality
testMemory()
// Toggle fallback mode for testing
enableFallbackMode()
disableFallbackMode()
Setting-Specific Storytelling
The AI system adapts its storytelling style based on campaign settings:
Medieval Fantasy
- Traditional D&D elements and terminology
- Fantasy races, magic, and medieval technology
- Classic adventure tropes and scenarios
Modern/Urban Fantasy
- Contemporary setting with supernatural elements
- Modern technology and urban environments
- Conspiracy and investigation themes
Sci-Fi Space
- Futuristic technology and space travel
- Alien encounters and cosmic mysteries
- Science fiction terminology and concepts
Eldritch Horror
- Cosmic horror and sanity mechanics
- Academic investigation themes
- Lovecraftian atmosphere and terminology
Model Warmup
await aiManager.warmupHuggingFaceModel();
Context Management
- Automatic conversation history pruning
- Intelligent memory prioritization
- Optimized prompt construction
Error Handling
- Graceful model fallback system
- Comprehensive error logging
- Emergency fallback responses
Integration Points
Memory Manager
- Automatic memory context integration
- Real-time information extraction
- Persistent story element tracking
Character System
- Character data integration in prompts
- Skill usage detection and tracking
- Equipment and inventory awareness
Game State
- Campaign setting integration
- Turn-based action processing
- Save/load state compatibility
Best Practices
Prompt Engineering
- Include specific character context
- Reference recent decisions and consequences
- Maintain consistent world state information
- Use setting-appropriate language and terminology
Response Quality
- Monitor response coherence and relevance
- Validate against character and world consistency
- Ensure proper narrative voice and tone
- Remove meta-commentary and system artifacts
- Track model response times
- Monitor API usage and rate limits
- Log errors for debugging and improvement
- Test fallback systems regularly
Troubleshooting
Common Issues
No AI Response:
- Check HuggingFace API availability
- Verify model accessibility
- Test fallback model queue
- Check browser console for errors
Poor Response Quality:
- Verify memory context is building correctly
- Check character data integration
- Ensure proper setting configuration
- Monitor conversation history length
Memory Integration Issues:
- Verify Memory Manager initialization
- Check game state persistence
- Validate context building process
- Test information extraction patterns
Debug Functions
// Test AI system status
aiManager.debugTest()
// Check memory integration
aiManager.testMemorySystem()
// Validate response pipeline
aiManager.testAIResponse("look around")
Future Enhancements
Planned Features
- Fine-tuned Models: Custom models trained on RPG content
- Emotion Tracking: Character emotional state integration
- Multi-Character Voices: Different AI personalities for NPCs
- Advanced Plot Generation: AI-driven story arc development
- Real-time Learning: System adaptation based on player preferences
Configuration Extensions
- Custom model integration support
- Advanced memory tuning options
- Response quality metrics
- Performance optimization settings
Conclusion
The Main AI System provides the core intelligence behind DiceTales’ immersive storytelling experience. By combining modern conversational AI with comprehensive memory management and context awareness, it delivers human-like interactions that maintain plot consistency and character development throughout extended gaming sessions.