The KBA Drafter is an LLM-powered Knowledge Base Article generator that uses OpenAI to automatically create structured KBA drafts from support tickets.
backend/
├── llm_service.py # OpenAI client with structured output
├── kba_service.py # Core business logic
├── kba_models.py # Pydantic data models
├── kba_schemas.py # JSON Schema for LLM validation
├── kba_prompts.py # Prompt engineering
├── kba_audit.py # Audit logging service
├── kba_exceptions.py # Custom exceptions
├── guidelines_loader.py # Load .md guideline files
└── operations.py # REST/MCP operations (@operation decorator)
frontend/src/features/kba-drafter/
└── KBADrafterPage.jsx # Main UI (input, editor, list)
docs/kba_guidelines/
├── README.md # System overview
├── GENERAL.md # Universal KBA structure
├── VPN.md # VPN-specific patterns
├── PASSWORD_RESET.md # Password/account procedures
└── NETWORK.md # Network diagnostics
{
"id": "uuid",
"incident_id": "INC123456",
"ticket_uuid": "550e8400-...",
"title": "VPN Connection Issues on Windows",
"problem_description": "...",
"solution_steps": ["Step 1", "Step 2", ...],
"additional_notes": "...",
"tags": ["VPN", "Windows", "Network"],
"status": "draft|reviewed|published|failed",
"created_by": "user@example.com",
"reviewed_by": null,
"llm_generation_time_ms": 1234,
"created_at": "2025-01-15T10:00:00Z"
}- Input: User enters ticket UUID
- Data Loading: Backend fetches ticket from CSV (
csv/data.csv) - Context Building:
- Load relevant guidelines based on ticket categorization
- Build prompt with ticket data + guidelines + JSON schema
- LLM Generation: Send to OpenAI (gpt-4o-mini)
- Uses native structured output via beta.chat.completions.parse()
- Automatic validation and retry
- Parsing: Extract validated output from OpenAI
- Save: Store draft in database (
backend/data/kba.db) - Review: User reviews and edits draft
- Publish: Export to target system (file/SharePoint/etc.)
POST /api/kba/drafts
Content-Type: application/json
{
"ticket_id": "550e8400-e29b-41d4-a716-446655440000",
"user_id": "user@example.com"
}GET /api/kba/drafts/{draft_id}PATCH /api/kba/drafts/{draft_id}
Content-Type: application/json
{
"title": "Updated title",
"user_id": "user@example.com"
}POST /api/kba/drafts/{draft_id}/publish
Content-Type: application/json
{
"target_system": "file",
"user_id": "user@example.com"
}GET /api/kba/drafts?status=draft&limit=10GET /api/kba/drafts/{draft_id}/auditGET /api/kba/guidelines
GET /api/kba/guidelines/{category}GET /api/kba/health# OpenAI Configuration
OPENAI_API_KEY=sk-proj-your-key-here
OPENAI_MODEL=gpt-4o-mini- Get API key from: https://platform.openai.com/api-keys
- Add to
.envfile - Verify:
curl http://localhost:5001/api/kba/health
Guidelines are markdown files in docs/kba_guidelines/ that provide LLM context:
- Frontmatter: Metadata (category, priority, tags)
- Content: Instructions, patterns, examples
The system automatically loads relevant guidelines based on:
- Ticket categorization (Tier 1/2/3)
- Keywords in summary/description
- Configurable mappings in
guidelines_loader.py
- Create
.mdfile indocs/kba_guidelines/ - Add frontmatter:
--- category: EMAIL priority: 10 tags: [outlook, exchange, email] ---
- Add content with examples
- Update
CATEGORY_MAPinguidelines_loader.py
The system uses a structured prompt format:
# TASK
Generate a Knowledge Base Article from this support ticket.
# TICKET DATA
{ticket information}
# GUIDELINES
{relevant guidelines}
# OUTPUT FORMAT
{JSON schema}
# INSTRUCTIONS
- Follow the guidelines precisely
- Extract root cause analysis
- Provide actionable steps
- Use clear, concise language
- 3 attempts with exponential backoff
- Error feedback loop (LLM sees validation errors)
- Fallback parsing strategies
KBAException
├── KBANotFoundException
├── KBAServiceException
│ ├── LLMUnavailableError
│ └── InvalidLLMOutputError
└── KBAValidationException
Every action is logged to audit trail:
{
"event_type": "draft_created|draft_updated|...",
"user_id": "user@example.com",
"draft_id": "uuid",
"changes": {"field": "old -> new"},
"metadata": {"llm_model": "gpt-4o-mini"},
"timestamp": "2025-01-15T10:00:00Z"
}cd backend
pytest tests/test_kba_*.py -vcd frontend
npm testnpx playwright test tests/e2e/kba-drafter.spec.js- Input Validation: All inputs validated via Pydantic
- SQL Injection: SQLModel prevents SQL injection
- XSS: React auto-escapes output
- Audit Trail: All changes logged with user ID
- API Key Security: Store OpenAI API key in
.env(never commit)
- Typical Generation Time: 2-5 seconds (gpt-4o-mini)
- Database: SQLite (production: PostgreSQL recommended)
- Caching: Guidelines cached in memory
- Concurrency: Async/await throughout
# Check OpenAI API key is set
echo $OPENAI_API_KEY
# Test health endpoint
curl http://localhost:5001/api/kba/health- Check prompt length (model context limit)
- Verify JSON schema validity
- Review audit trail for error details
- Check browser console for API errors
- Verify backend is running
- Check CORS configuration
- Multi-language support
- SharePoint integration
- Advanced search/filter
- Batch generation
- Template customization
- A/B testing different prompts
- Fine-tuned models
- OpenAI: https://platform.openai.com/docs
- Pydantic: https://docs.pydantic.dev/
- Quart: https://quart.palletsprojects.com/
- FluentUI: https://react.fluentui.dev/