DEV Community

Thesius Code
Thesius Code

Posted on • Originally published at datanest-stores.pages.dev

Conversational AI Templates

Conversational AI Templates

Production-ready chatbot frameworks that handle the hard parts of conversation design — multi-turn context management, intent classification, graceful topic switching, and seamless human escalation. These templates give you a working conversational AI system in minutes, not months. Built for customer support, internal tools, and domain-specific assistants.

Key Features

  • Multi-Turn Context Management — Sliding window and summary-based context strategies that keep conversations coherent without exploding token costs
  • Intent Classification — Rule-based and LLM-powered intent detection with confidence scoring and ambiguity resolution
  • Conversation State Machine — Define conversation flows as state machines with transitions, guards, and side effects
  • Escalation Workflows — Automatic handoff to human agents when confidence drops, sentiment turns negative, or the user explicitly requests it
  • Slot Filling — Extract structured data from natural language with validation, confirmation prompts, and partial-fill handling
  • Persona System — Configure tone, vocabulary, and behavioral constraints to match your brand voice
  • Conversation Analytics — Track resolution rates, escalation frequency, average turns to resolution, and user satisfaction

Quick Start

from conversational_ai import Chatbot, ConversationConfig, IntentRouter

# 1. Define intents and their handlers
router = IntentRouter()

@router.intent("order_status", examples=[
    "Where is my order?",
    "Track my package",
    "When will my order arrive?",
])
def handle_order_status(context):
    order_id = context.slots.get("order_id")
    if not order_id:
        return context.ask_slot("order_id", "What's your order number?")
    return context.respond(f"Let me look up order {order_id} for you...")

@router.intent("refund_request", examples=[
    "I want a refund",
    "Can I return this item?",
])
def handle_refund(context):
    return context.respond(
        "I can help with that. Let me connect you with our returns team.",
        escalate=True,
        reason="refund_request",
    )

# 2. Create chatbot
bot = Chatbot(
    router=router,
    config=ConversationConfig(
        persona="You are a friendly customer support agent for Acme Corp.",
        context_strategy="sliding_window",
        context_window=10,
        escalation_threshold=0.4,
    ),
)

# 3. Run conversation
session = bot.new_session(user_id="user_123")
response = session.message("Hi, where's my order #ACM-7842?")
print(response.text)        # "Let me look up order ACM-7842 for you..."
print(response.intent)      # "order_status"
print(response.confidence)  # 0.94
Enter fullscreen mode Exit fullscreen mode

Architecture

User Message
      │
      ▼
┌──────────────┐
│  Preprocessor │──── Normalize, spell-check, language detect
└──────┬───────┘
       ▼
┌──────────────┐
│Intent Classif.│──── Match intent + extract entities
└──────┬───────┘
       │
       ├── High confidence ──▶ Route to intent handler
       ├── Low confidence ───▶ Clarification prompt
       └── Very low ─────────▶ Escalation to human
                                      │
Intent Handler                        ▼
       │                      Human Agent Queue
       ▼
┌──────────────┐
│ Slot Filler  │──── Extract & validate required data
└──────┬───────┘
       ▼
┌──────────────┐
│  Response    │──── Apply persona, format, disclaimers
│  Generator   │
└──────────────┘
Enter fullscreen mode Exit fullscreen mode

Usage Examples

Conversation State Machine

from conversational_ai import StateMachine, State, Transition

booking_flow = StateMachine(
    initial="greeting",
    states=[
        State("greeting", prompt="Welcome! Would you like to book an appointment?"),
        State("collect_date", prompt="What date works best for you?"),
        State("collect_time", prompt="And what time?"),
        State("confirm", prompt="I have {date} at {time}. Shall I confirm?"),
        State("done", prompt="All set! You'll receive a confirmation email."),
    ],
    transitions=[
        Transition("greeting", "collect_date", on_intent="affirm"),
        Transition("collect_date", "collect_time", on_slot="date"),
        Transition("collect_time", "confirm", on_slot="time"),
        Transition("confirm", "done", on_intent="affirm"),
        Transition("confirm", "collect_date", on_intent="deny"),
    ],
)
Enter fullscreen mode Exit fullscreen mode

Context Management Strategies

from conversational_ai.context import SlidingWindow, SummarizingContext

# Simple: Keep last N turns
config = ConversationConfig(
    context_strategy=SlidingWindow(max_turns=15),
)

# Advanced: Summarize older turns, keep recent ones verbatim
config = ConversationConfig(
    context_strategy=SummarizingContext(
        recent_turns=5,
        summary_model="gpt-4o-mini",
        summary_interval=10,
    ),
)
Enter fullscreen mode Exit fullscreen mode

Human Escalation with Context Transfer

from conversational_ai.escalation import EscalationManager

escalation = EscalationManager(
    triggers=[
        {"type": "low_confidence", "threshold": 0.4},
        {"type": "negative_sentiment", "threshold": -0.6},
        {"type": "keyword", "patterns": ["speak to a human", "real person"]},
        {"type": "max_turns_without_resolution", "turns": 8},
    ],
    handoff_format="markdown",
    queue_backend="redis",
)
Enter fullscreen mode Exit fullscreen mode

Configuration

# chatbot_config.yaml
persona:
  name: "Acme Assistant"
  system_prompt: |
    You are a helpful customer support agent for Acme Corp.
    Be friendly but professional. Never make promises about
    refund timelines. Always verify order numbers before lookup.
  tone: "professional_friendly"
  max_response_length: 300

context:
  strategy: "summarizing"
  recent_turns: 5
  summary_model: "gpt-4o-mini"
  max_context_tokens: 4000

intent_classification:
  method: "hybrid"               # rule | llm | hybrid
  model: "gpt-4o-mini"
  confidence_threshold: 0.6
  clarification_prompt: "I'm not sure I understand. Could you rephrase that?"

slots:
  order_id:
    type: "string"
    pattern: '[A-Z]{3}-\d{4}'
    prompt: "Could you share your order number? It looks like ABC-1234."
    required: true
  email:
    type: "email"
    prompt: "What email address is on the account?"

escalation:
  enabled: true
  queue: "redis://localhost:6379/1"
  include_summary: true
  include_sentiment_score: true
  notify_channel: "slack"
Enter fullscreen mode Exit fullscreen mode

Best Practices

  1. Define fallback intents — Always have a graceful "I don't understand" path rather than forcing a bad classification.
  2. Confirm before acting — For any destructive action (cancellation, deletion), always confirm with the user first.
  3. Track conversation quality — Monitor average turns-to-resolution. If it's climbing, your intents or handlers need work.
  4. Use cheap models for classification — Intent classification doesn't need GPT-4. Use gpt-4o-mini or a fine-tuned small model.
  5. Persist sessions — Store conversation state in Redis so users can resume across page reloads or devices.
  6. A/B test personas — Small changes in system prompt wording can significantly impact resolution rates.

Troubleshooting

Problem Cause Fix
Bot gives long, rambling responses No max_response_length set or persona too vague Add explicit length constraints in persona config and system prompt
Intent classification is inaccurate Too few examples or overlapping intent definitions Add 10+ diverse examples per intent; merge intents that overlap
Context window overflows on long conversations Using sliding_window with high turn count Switch to summarizing context strategy to compress older turns
Escalation floods human agents Confidence threshold too high Lower confidence_threshold to 0.5 and add more training examples

This is 1 of 11 resources in the AI Builder Pro toolkit. Get the complete [Conversational AI Templates] with all files, templates, and documentation for $49.

Get the Full Kit →

Or grab the entire AI Builder Pro bundle (11 products) for $169 — save 30%.

Get the Complete Bundle →


Related Articles

Top comments (0)