AI fields generate dynamic content using AI models during flow execution. Instead of static text, you provide prompts that are resolved at runtime with context and variables.

How it works

  1. Define prompt: Use AIField.prompt() with your generation prompt
  2. Runtime resolution: AI generates content based on prompt and flow context
  3. Variable access: Use {{variable_name}} to include flow variables
  4. Context aware: AI has access to conversation history and user data

Basic usage

from kapso.builder.ai.field import AIField

# Three equivalent ways to create AI fields:
AIField(prompt="Generate a greeting")          # Recommended
AIField.prompt("Generate a greeting")          # Also works
AIField("Generate a greeting")                 # Positional argument

# Example usage
node = SendTextNode(
    id="greeting",
    whatsapp_config_id="config_123",
    message=AIField(prompt="Generate a warm, professional greeting"),
    provider_model_name="claude-3-5-sonnet"
)

Supported nodes

SendTextNode
SendTextNode(
    message=AIField(prompt="Your prompt here")
)
SendTemplateNode
SendTemplateNode(
    parameters=AIField(prompt="Generate template parameters as JSON")
)
SendInteractiveNode
SendInteractiveNode(
    header_text=AIField(prompt="Generate header text"),
    body_text=AIField(prompt="Generate body content"),
    footer_text=AIField(prompt="Generate footer"),
    buttons=[
        Button(text=AIField(prompt="Generate button text"))
    ]
)

Variable interpolation

Access flow variables in your prompts:
# Using flow variables
node = SendTextNode(
    id="personalized_greeting",
    message=AIField(prompt=
        "Generate a personalized greeting for {{customer_name}} who is interested in {{product_category}}"
    ),
    provider_model_name="gpt-4"
)
Available variables:
  • Flow variables: {{variable_name}}
  • User context: {{phone_number}}, {{conversation_id}}
  • System info: {{flow_id}}, {{current_time}}

Examples

Dynamic support response
support_response = SendTextNode(
    id="support_help",
    message=AIField(prompt=
        "Based on the user's issue about {{issue_type}}, provide a helpful solution. "
        "Keep it concise and actionable."
    ),
    provider_model_name="claude-3-5-sonnet"
)
Template with AI parameters
# AI generates template parameters
confirmation = SendTemplateNode(
    id="order_confirmation",
    template_id="order_template_123",
    parameters=AIField(prompt=
        "Generate order confirmation parameters for customer {{customer_name}} "
        "with order total {{order_total}}. Return as JSON: "
        "{\"customer_name\": \"...\", \"order_total\": \"...\", \"estimated_delivery\": \"...\"}"
    ),
    provider_model_name="gpt-4"
)
Interactive buttons
# Dynamic button options
interactive = SendInteractiveNode(
    id="product_options",
    header_text="Product Categories",
    body_text=AIField(prompt=
        "Generate engaging body text about our {{featured_category}} products"
    ),
    buttons=[
        Button(
            id="option_1",
            text=AIField(prompt="Generate button text for {{category_1}}")
        ),
        Button(
            id="option_2", 
            text=AIField(prompt="Generate button text for {{category_2}}")
        )
    ],
    provider_model_name="claude-3-5-sonnet"
)
Conditional content
# Content varies based on user data
response = SendTextNode(
    id="status_update",
    message=AIField(prompt=
        "Generate a status update message. "
        "User type: {{user_type}}, Account status: {{account_status}}. "
        "If premium user, mention exclusive benefits. "
        "If basic user, suggest upgrade options."
    ),
    provider_model_name="gpt-4"
)

Requirements

  • AI model required: Must specify provider_model_name when using AI fields
  • Credits consumption: AI field resolution consumes processing credits
  • Runtime resolution: Fields are resolved during flow execution, not at build time