Skip to content

Latest commit

 

History

History
345 lines (277 loc) · 11.5 KB

File metadata and controls

345 lines (277 loc) · 11.5 KB

AI System Review & Bug Fix Report

Executive Summary

CRITICAL BUG FIXED: The /ai ask slash command was passing incorrect parameters to AIManager.chat(), causing undefined serverDocument and channel values, which prevented proper context building and conversation history retrieval. This resulted in repetitive AI responses.

Status: ✅ Fixed in Internals/SlashCommands/commands/ai.js

Your AI system is well-architected with multi-provider support (OpenAI, Anthropic, Groq, Ollama) and comprehensive features. The system prompt is limited at 4,000 characters while modern LLMs support 128K-200K context windows, creating an opportunity to enhance AI capabilities with richer prompting.

Current System Architecture

Core Components

AIManager.js (26.5 KB)

  • Central orchestration for all AI operations
  • Manages provider resolution, context building, memory integration
  • Handles conversation history, vector memory, and rate limiting
  • Integrates with 5+ LLM providers

Providers (7 files)

  • OpenAI: gpt-4o (128K context), gpt-4o-mini, gpt-4-turbo
  • Anthropic: Claude 3.5 Sonnet (200K context), Claude 3 variants
  • Groq: Llama 3.1, Mixtral (8K-32K context)
  • Ollama: Local model support
  • OpenAI-Compatible: Generic API support

Memory Systems

  • ConversationMemory: Per-channel/per-user history (configurable 1-50 messages)
  • VectorMemory: Qdrant integration for semantic search (optional)
  • RateLimiter: Cooldowns, per-user/channel limits
  • UsageTracker: Token counting, cost estimation, budget enforcement

Database Schema (serverAISchema.js)

  • 287 lines, comprehensive configuration
  • Supports per-server customization
  • Governance, budget controls, tool access management

System Prompt Current State

Location: serverDocument.config.ai.systemPrompt

Limits:

  • Max length: 4,000 characters
  • Default: "You are a helpful AI assistant in a Discord server. Be concise, friendly, and helpful."
  • UI: 10-row textarea in admin panel

Context Building Flow (AIManager.js:236-268):

1. Load base system prompt (4K max)
2. Optionally inject vector memory context
3. Append conversation history
4. Send to provider

Problem: 4,000 characters is ~1,000 tokens, leaving 127K unused in gpt-4o's context window.

Provider Context Window Analysis

Provider Model Context Utilization
OpenAI gpt-4o 128,000 3.1%
OpenAI gpt-4o-mini 128,000 3.1%
Anthropic Claude 3.5 Sonnet 200,000 2%
Groq Mixtral 32,768 12%
Groq Llama 3.1 8,192 48%

Current system prompt uses only 2-12% of available context.

Enhancement Opportunities

1. Increase System Prompt Limit (Quick Win)

  • Current: 4,000 characters
  • Recommended: 16,000-32,000 characters
  • Effort: 30 minutes
  • Impact: High

Changes needed:

  • Update serverAISchema.js maxlength
  • Update admin-ai-personality.ejs textarea rows & maxlength
  • Update Web/controllers/dashboard/ai.js substring limit

2. System Prompt Templates (Medium Effort)

Create pre-built templates for common use cases:

  • Community Manager (moderation, engagement)
  • Support Bot (customer service, FAQ)
  • Creative Assistant (brainstorming, content)
  • Technical Helper (code, debugging)
  • Gaming Bot (game knowledge, events)
  • Educational (teaching, learning)

Storage: New aiPromptTemplates collection UI: Dropdown selector with preview Effort: 2-3 hours Impact: High (reduces setup friction, improves quality)

3. Dynamic Server Context Injection (Medium Effort)

Automatically inject server-specific context:

  • Server name, member count, creation date
  • Roles and their purposes
  • Channel structure and purposes
  • Server rules/guidelines
  • Enabled features
  • Language preference

Implementation: New _buildServerContext() method in AIManager Effort: 1-2 hours Impact: Medium (improves relevance, reduces manual setup)

4. Prompt History & Versioning (Low Effort)

Track system prompt changes:

  • Version history in database
  • Rollback capability
  • Change reasons/audit trail
  • UI history browser

Effort: 1 hour Impact: Low (operational benefit, debugging)

5. Provider-Specific Optimizations (Medium Effort)

Tailor prompts based on provider:

  • Claude: Leverage 200K context, detailed reasoning
  • GPT-4o: Structured outputs, complex instructions
  • Groq: Concise prompts, fast inference

Effort: 1-2 hours Impact: Medium (better quality per provider)

6. Advanced Prompt Engineering (Low Effort)

Add to system prompt:

  • Few-shot examples
  • Constraint specifications
  • Tone & style guidelines
  • Output format requirements

Effort: 30 minutes Impact: High (improves response quality)

7. Dashboard Enhancements (Medium Effort)

  • Character counter with visual indicator
  • Token usage estimator
  • Template preview modal
  • Test console for prompt validation
  • Prompt analytics

Effort: 2-3 hours Impact: Medium (UX improvement)

Recommended Implementation Plan

Phase 1: Foundation (1-2 hours)

  1. ✅ Increase system prompt limit to 16,000 characters
  2. ✅ Update UI textarea (25 rows, maxlength="16000")
  3. ✅ Add character counter in UI
  4. ✅ Add token estimator (chars / 4)

Phase 2: Templates & Context (3-4 hours)

  1. Create aiPromptTemplates collection
  2. Seed 6 default templates
  3. Add template selector UI
  4. Implement server context injection
  5. Add prompt history tracking

Phase 3: Polish (2-3 hours)

  1. Provider-specific optimizations
  2. Advanced prompt engineering features
  3. Test console for validation
  4. Documentation updates

Code Changes Required

1. Schema Update

File: Database/Schemas/serverAISchema.js

systemPrompt: {
    type: String,
    default: "You are a helpful AI assistant in a Discord server. Be concise, friendly, and helpful.",
    maxlength: 16000,  // Increased from 4000
}

2. Controller Update

File: Web/controllers/dashboard/ai.js

controllers.personality.post = async (req, res) => {
    const serverQueryDocument = req.svr.queryDocument;
    serverQueryDocument.set(
        "config.ai.systemPrompt", 
        req.body.systemPrompt ? req.body.systemPrompt.substring(0, 16000) : ""
    );
    save(req, res, true);
};

3. UI Update

File: Web/views/pages/admin-ai-personality.ejs

<textarea name="systemPrompt" 
    class="textarea is-primary" 
    rows="25"
    maxlength="16000"
    placeholder="You are a helpful AI assistant...">
    <%= configData.ai.systemPrompt %>
</textarea>
<p class="help">
    <span id="charCount">0</span> / 16000 characters
    (~<span id="tokenEstimate">0</span> tokens)
</p>

<script>
const textarea = document.querySelector('[name="systemPrompt"]');
function updateCounts() {
    const chars = textarea.value.length;
    const tokens = Math.ceil(chars / 4);
    document.getElementById('charCount').textContent = chars;
    document.getElementById('tokenEstimate').textContent = tokens;
}
textarea.addEventListener('input', updateCounts);
updateCounts();
</script>

4. AIManager Enhancement

File: Modules/AI/AIManager.js (buildContext method)

async buildContext (serverDocument, channelId, user, currentMessage = null) {
    const aiConfig = serverDocument.config.ai || {};
    const memoryConfig = aiConfig.memory || {};
    const vectorConfig = aiConfig.vectorMemory || {};

    // Get system prompt
    let systemPrompt = aiConfig.systemPrompt ||
        "You are a helpful AI assistant in a Discord server. Be concise and helpful.";

    // Inject server context (NEW)
    const serverContext = await this._buildServerContext(serverDocument, channelId);
    if (serverContext) {
        systemPrompt += `\n\n${serverContext}`;
    }

    // Inject vector memory context
    if (vectorConfig.enabled && vectorConfig.injectContext && currentMessage) {
        const vectorContext = await this.searchVectorMemory(...);
        if (vectorContext && vectorContext.length > 0) {
            const contextPrefix = vectorConfig.contextPrefix || "Relevant context from memory:";
            const contextText = vectorContext.map(m => `- ${m.content}`).join("\n");
            systemPrompt += `\n\n${contextPrefix}\n${contextText}`;
        }
    }

    const messages = [{ role: "system", content: systemPrompt }];
    const history = await this.memory.getHistory(...);
    messages.push(...history);

    return messages;
}

async _buildServerContext (serverDocument, channelId) {
    const guild = this.client.guilds.cache.get(serverDocument._id);
    if (!guild) return null;

    const channel = guild.channels.cache.get(channelId);
    const rolesList = guild.roles.cache
        .filter(r => !r.isManaged() && r.id !== guild.id)
        .map(r => `- ${r.name}`)
        .join("\n");

    return `## Server Context
**Server**: ${guild.name} (${guild.memberCount} members)
**Channel**: ${channel?.name || "unknown"}
**Roles**: 
${rolesList}`;
}

Benefits of Enhancement

Immediate (Limit Increase)

  • Support detailed instructions and guidelines
  • Add constraint specifications
  • Include tone & style guidelines
  • Better moderation capabilities

Short-term (Templates)

  • Reduce setup friction for new servers
  • Improve consistency across servers
  • Enable specialized use cases (support, moderation, gaming)
  • Better out-of-box experience

Long-term (Full Implementation)

  • Leverage full power of 128K-200K context windows
  • Enable complex multi-step reasoning
  • Support advanced prompt engineering techniques
  • Better AI quality across all use cases

Security & Compliance

Considerations

  1. Prompt Injection: Validate system prompts don't contain malicious instructions
  2. Audit Trail: Log all system prompt changes
  3. Rate Limiting: Apply to system prompt updates
  4. Tier Gating: Limit prompt size by subscription tier
  5. Content Filtering: Scan for inappropriate content

Recommendations

  • Add systemPromptHistory to track changes
  • Implement change reason field
  • Log user ID and timestamp
  • Consider tier-based limits (starter: 8K, premium: 16K+)

Testing Strategy

  1. Unit Tests: Token estimation accuracy
  2. Integration Tests: Context building with large prompts
  3. Load Tests: Maximum prompt size handling
  4. Provider Tests: Each provider with large prompts
  5. UI Tests: Textarea behavior, character counter

Backward Compatibility

Fully backward compatible

  • Existing prompts under 4,000 chars continue to work
  • Default prompt unchanged
  • No breaking API changes
  • Gradual UI migration

Next Steps

  1. Review this document - Confirm approach aligns with your vision
  2. Implement Phase 1 - Quick wins (1-2 hours)
  3. Test thoroughly - Ensure no regressions
  4. Implement Phase 2 - Templates & context (3-4 hours)
  5. Gather feedback - Monitor usage patterns
  6. Iterate - Refine based on real-world usage

Conclusion

Your AI system is solid and well-designed. The main opportunity is leveraging the massive context windows available in modern LLMs. By increasing the system prompt limit from 4,000 to 16,000+ characters and implementing templates/dynamic context injection, you can significantly improve AI quality with minimal effort.

The recommended approach is:

  • Phase 1: Increase limit to 16,000 chars (quick win)
  • Phase 2: Add templates and server context (medium effort, high impact)
  • Phase 3: Polish and advanced features (optional enhancements)

This positions your bot to provide superior AI experiences compared to competitors while maintaining full backward compatibility.