SWAIG (SignalWire AI Gateway) 🔗 ↑ TOC

Introduction 🔗 ↑ TOC

SWAIG (SignalWire AI Gateway) is a serverless function framework within SignalWire's conversational AI stack that allows developers to build AI agents capable of automating complex conversations, executing functions, using tools, and interacting with external APIs, databases, or business logic in a seamless and low-latency manner.

Core Concepts 🔗 ↑ TOC

What SWAIG Enables 🔗 ↑ TOC

SWAIG bridges the gap between conversational AI and real-world business systems by providing: - Function-based AI execution during live conversations - Tool integration with CRMs, CDPs, e-commerce backends, support ticketing systems - RAG database connectivity for enhanced knowledge retrieval - Real-time call orchestration and dynamic workflow modification - Context management across channels and sessions

Integration Philosophy 🔗 ↑ TOC

SWAIG operates on two core strategies that share a common foundation: defining functions with desired arguments that the AI can call when appropriate during conversations.

Execution Strategies 🔗 ↑ TOC

1. Webhook-Based Execution 🔗 ↑ TOC

The AI agent sends structured function calls to an external server where additional logic is processed.

How it works: 1. AI determines a function should be called based on conversation context 2. Function name and collected arguments are sent to a webhook endpoint 3. External CGI script processes the request and can perform any server-side logic 4. Server responds with appropriate data or SWML instructions 5. Response can include telephony commands, prompt updates, or conversation modifications

Use cases: - Complex business logic requiring custom code - Integration with proprietary systems - Real-time data processing and analysis - Multi-step workflows requiring server state

2. Serverless JSON Templates 🔗 ↑ TOC

Instead of external CGI, developers define JSON-based execution templates that process function calls locally without requiring a hosted backend.

How it works: 1. Function signature includes JSON template definition 2. Parser enters JSON structure when function is called 3. Common paradigms like pattern matching, REST API calls, and data fetching are handled 4. Variable expansion and result looping occur within the template 5. Templated JSON response is returned without external hosting

Advantages: - Completely serverless deployment - Reduced latency (no external network calls) - Simplified architecture for common integration patterns - Built-in support for popular API patterns

Use cases: - Classic third-party API integration (Slack, Salesforce, etc.) - Simple data retrieval and formatting - Pattern matching and conditional responses - Rapid prototyping and development

Key Features 🔗 ↑ TOC

Function-Based AI Execution 🔗 ↑ TOC

Developers can define functions that AI agents call during live conversations: - External REST API triggers: Connect to any web service - CRM system interactions: Salesforce, HubSpot, custom CRMs - Database record retrieval: Customer data, product catalogs, order history - Calculation engines: Pricing, availability, complex business rules

Real-Time Orchestration 🔗 ↑ TOC

SWAIG allows AI agents to modify call logic dynamically: - Call transfers: Route to human agents or other systems - Media playback: Play messages, videos, or hold music - IVR transitions: Switch between AI and traditional menu systems - Context switching: Change conversation focus based on user needs

Low-Latency Tool Use 🔗 ↑ TOC

Because SWAIG functions execute directly from conversation logic: - No middleware latency: Direct integration with telephony stack - Parallel processing: Multiple functions can execute simultaneously - Optimized networking: Minimal network hops for external calls - Efficient caching: Frequently accessed data stored locally

Advanced Context and Memory Management 🔗 ↑ TOC

SWAIG maintains sophisticated conversation state: - Cross-session context: Remember previous interactions - Multi-channel continuity: Maintain context across voice, video, text - Conversation history: Full transcript and decision tracking - User preferences: Personalized responses and behavior

Dynamic Context Switching 🔗 ↑ TOC

Multiple conversation contexts within a single agent: - Role-based contexts: Sales, support, technical assistance - Specialized system prompts: Different instructions per context - Security permissions: Context-specific access controls - Tool availability: Different functions per conversation type

Integration Scenarios 🔗 ↑ TOC

CRM Integration Example 🔗 ↑ TOC

# SWAIG function for customer lookup
customer_lookup:
  function: get_customer_info
  purpose: Retrieve customer account details
  argument:
    customer_id:
      type: string
      description: Customer account identifier
  data_map:
    expressions:
      - pattern: ".*"
        url: "https://api.crm.com/customers/${customer_id}"
        headers:
          Authorization: "Bearer ${api_token}"
        output:
          customer_name: "${response.data.name}"
          account_status: "${response.data.status}"
          last_order: "${response.data.orders[0].date}"

E-commerce Integration 🔗 ↑ TOC

Support Ticketing Systems 🔗 ↑ TOC

DataSphere Integration 🔗 ↑ TOC

SWAIG works seamlessly with SignalWire's DataSphere RAG stack: - Document loading: Any format documents indexed for search - Semantic search: Find relevant information during conversations - Response incorporation: Blend retrieved data with AI responses - Real-time updates: Fresh data available immediately

Guardrails and Data Validation 🔗 ↑ TOC

Behavioral Controls 🔗 ↑ TOC

Data Validation 🔗 ↑ TOC

Anti-Hallucination Measures 🔗 ↑ TOC

Technical Architecture 🔗 ↑ TOC

Latency Optimization 🔗 ↑ TOC

Scalability Features 🔗 ↑ TOC

Security and Compliance 🔗 ↑ TOC

Development Workflow 🔗 ↑ TOC

Function Definition 🔗 ↑ TOC

  1. Identify use case: Determine what external integration is needed
  2. Define function signature: Specify parameters and expected outputs
  3. Choose execution strategy: Webhook vs. serverless template
  4. Implement logic: Write the integration code or template
  5. Test integration: Validate function behavior in conversations

Testing and Debugging 🔗 ↑ TOC

Deployment and Monitoring 🔗 ↑ TOC

Best Practices 🔗 ↑ TOC

Function Design 🔗 ↑ TOC

Performance Optimization 🔗 ↑ TOC

Security Considerations 🔗 ↑ TOC

Use Case Examples 🔗 ↑ TOC

Healthcare Assistant 🔗 ↑ TOC

Financial Services 🔗 ↑ TOC

Retail and E-commerce 🔗 ↑ TOC