Skip to content

Latest commit

 

History

History
472 lines (361 loc) · 13.1 KB

lesson1_first_agent.md

File metadata and controls

472 lines (361 loc) · 13.1 KB

Lesson 1: Creating Your First Agent

This lesson builds Morgan, a friendly sales specialist who helps customers with PC building recommendations. Morgan is a simple AI-powered voice agent built with the SignalWire Agents SDK.

Table of Contents

  1. Understanding Agent Architecture
  2. Creating a Basic Agent
  3. Configuring Agent Prompts
  4. Adding Voice and Language
  5. Running Your Agent
  6. Testing Your Agent
  7. Enabling SSL/HTTPS
  8. Summary

Understanding Agent Architecture

SignalWire Agents are Python classes that inherit from AgentBase. Every agent has:

Core Components:

  • Name: A descriptive name for your agent
  • Route: The HTTP endpoint where the agent listens (e.g., /, /sales)
  • Host/Port: Network configuration for the agent server
  • Prompt: Instructions that define the agent's behavior
  • Language/Voice: Text-to-speech configuration

Key Concepts:

  • Agents handle voice conversations through the SignalWire platform
  • Each agent exposes an HTTP endpoint that returns SWML (SignalWire Markup Language)
  • The SWML document defines how the agent behaves during calls

Creating a Basic Agent

This section creates the first agent: Morgan, a sales specialist for PC Builder Pro.

Step 1: Import Required Modules

Create sales_agent.py and define an agent class that inherits from AgentBase:

#!/usr/bin/env python3
"""
Simple Sales Agent - Morgan
A friendly PC building sales specialist
"""

from signalwire import AgentBase

class SalesAgent(AgentBase):
    def __init__(self):
        super().__init__(
            name="PC Builder Sales Agent - Morgan",
            route="/",
            host="0.0.0.0",
            port=3000
        )

Step 2: Understanding the Parameters

Constructor Parameters:

  • name: Identifies your agent in logs and debugging
  • route: The URL path where the agent listens (use / for root)
  • host: Network interface to bind to (0.0.0.0 listens on all interfaces)
  • port: TCP port for the HTTP server (default: 3000)

Configuring Agent Prompts

The Prompt Object Model (POM) provides a structured way to define agent behavior. Instead of one long prompt, you organize instructions into logical sections.

Step 3: Add the Agent's Role

Add a prompt section that names Morgan and sets the character:

def __init__(self):
    super().__init__(
        name="PC Builder Sales Agent - Morgan",
        route="/",
        host="0.0.0.0",
        port=3000
    )
    
    # Configure the agent's personality and role
    self.prompt_add_section(
        "AI Role",
        body=(
            "You are Morgan, a passionate PC building expert and sales specialist "
            "at PC Builder Pro. You're known for your deep knowledge of components "
            "and your ability to match customers with their perfect build. You get "
            "excited about the latest hardware and love sharing that enthusiasm. "
            "Always introduce yourself by name."
        )
    )

Step 4: Define Areas of Expertise

Add a second section listing what Morgan specializes in:

    # Add expertise section
    self.prompt_add_section(
        "Your Expertise",
        body="Areas of specialization:",
        bullets=[
            "Custom PC builds for all budgets",
            "Component compatibility and optimization",
            "Performance recommendations",
            "Price/performance analysis",
            "Current market trends"
        ]
    )

Step 5: Define Tasks and Workflow

Add the steps Morgan should follow during a sales call:

    # Define the sales workflow
    self.prompt_add_section(
        "Your Tasks",
        body="Complete sales process workflow with passion and expertise:",
        bullets=[
            "Greet customers warmly and introduce yourself",
            "Understand their specific PC building requirements",
            "Ask about budget, intended use, and preferences",
            "Provide knowledgeable recommendations",
            "Share your enthusiasm for PC building",
            "Offer to explain technical details when helpful"
        ]
    )

Step 6: Add Voice Instructions

Add a section that describes how Morgan should sound while speaking:

    # Voice and tone instructions
    self.prompt_add_section(
        "Voice Instructions",
        body=(
            "Share your passion for PC building and get excited about "
            "helping customers create their perfect system. Your enthusiasm "
            "should be genuine and infectious."
        )
    )

Adding Voice and Language

SignalWire supports multiple text-to-speech voices.

Step 7: Configure Voice Settings

Configure Morgan's voice with add_language():

    # Configure language and voice
    self.add_language(
        name="English",
        code="en-US",
        voice="rime.marsh"  # A friendly, enthusiastic voice
    )

Available Voices:

  • rime.spore - Energetic and upbeat
  • rime.marsh - Warm and friendly
  • rime.cove - Calm and professional
  • And many more...

Running Your Agent

Complete Code

Here's the complete sales_agent.py:

#!/usr/bin/env python3
"""
Simple Sales Agent - Morgan
A friendly PC building sales specialist
"""

from signalwire import AgentBase

class SalesAgent(AgentBase):
    def __init__(self):
        super().__init__(
            name="PC Builder Sales Agent - Morgan",
            route="/",
            host="0.0.0.0",
            port=3000
        )
        
        # Configure the agent's personality and role
        self.prompt_add_section(
            "AI Role",
            body=(
                "You are Morgan, a passionate PC building expert and sales specialist "
                "at PC Builder Pro. You're known for your deep knowledge of components "
                "and your ability to match customers with their perfect build. You get "
                "excited about the latest hardware and love sharing that enthusiasm. "
                "Always introduce yourself by name."
            )
        )
        
        # Add expertise section
        self.prompt_add_section(
            "Your Expertise",
            body="Areas of specialization:",
            bullets=[
                "Custom PC builds for all budgets",
                "Component compatibility and optimization",
                "Performance recommendations",
                "Price/performance analysis",
                "Current market trends"
            ]
        )
        
        # Define the sales workflow
        self.prompt_add_section(
            "Your Tasks",
            body="Complete sales process workflow with passion and expertise:",
            bullets=[
                "Greet customers warmly and introduce yourself",
                "Understand their specific PC building requirements",
                "Ask about budget, intended use, and preferences",
                "Provide knowledgeable recommendations",
                "Share your enthusiasm for PC building",
                "Offer to explain technical details when helpful"
            ]
        )
        
        # Voice and tone instructions
        self.prompt_add_section(
            "Voice Instructions",
            body=(
                "Share your passion for PC building and get excited about "
                "helping customers create their perfect system. Your enthusiasm "
                "should be genuine and infectious."
            )
        )
        
        # Configure language and voice
        self.add_language(
            name="English",
            code="en-US",
            voice="rime.marsh"  # A friendly, enthusiastic voice
        )

def main():
    """Main function to run the agent"""
    agent = SalesAgent()
    
    print("Starting PC Builder Sales Agent - Morgan")
    print("=" * 50)
    print("Agent running at: http://localhost:3000/")
    print("Press Ctrl+C to stop")
    print("=" * 50)
    
    try:
        agent.run()
    except KeyboardInterrupt:
        print("\nShutting down agent...")

if __name__ == "__main__":
    main()

Step 8: Run the Agent

Run the script from the repository root:

# Make sure you're in the signalwire-python directory
cd /path/to/signalwire-python

# Run the agent
python tutorial/multi_agents/sales_agent.py

You should see:

Starting PC Builder Sales Agent - Morgan
==================================================
Agent running at: http://localhost:3000/
Press Ctrl+C to stop
==================================================

Every request to the agent needs basic auth. Without SWML_BASIC_AUTH_USER and SWML_BASIC_AUTH_PASSWORD set, the SDK generates a password and keeps it out of the logs. Set both before running the agent, so you know what to pass to curl:

export SWML_BASIC_AUTH_USER=devuser
export SWML_BASIC_AUTH_PASSWORD=devpassword
python tutorial/multi_agents/sales_agent.py

Testing Your Agent

Method 1: Get SWML Document

Request the SWML document directly with curl, while the agent is running:

# Get the SWML configuration
curl -u devuser:devpassword http://localhost:3000/

# With pretty printing
curl -u devuser:devpassword http://localhost:3000/ | python -m json.tool

Method 2: Test with swaig-test

swaig-test builds the agent without starting a server, which is faster for quick checks:

# Install the SDK if not already done
pip install -e .

# Test the agent
swaig-test tutorial/multi_agents/sales_agent.py --dump-swml

Understanding the SWML Output

The agent returns a JSON document that SignalWire uses to control the call. answer picks up the call, and ai carries the prompt and the voice configuration:

{
  "version": "1.0.0",
  "sections": {
    "main": [
      {
        "answer": {}
      },
      {
        "ai": {
          "prompt": {
            "pom": [
              {
                "title": "AI Role",
                "body": "You are Morgan..."
              }
              // ... other sections
            ]
          },
          "params": {},
          "languages": [
            {
              "name": "English",
              "code": "en-US",
              "voice": "rime.marsh"
            }
          ]
        }
      }
    ]
  }
}

Enabling SSL/HTTPS

For production deployments, you'll want to enable SSL/HTTPS:

Method 1: Using Environment Variables

Export the SSL variables in the shell, then run the agent:

# Set SSL environment variables
export SWML_SSL_ENABLED=true
export SWML_SSL_CERT_PATH=/path/to/your/certificate.pem
export SWML_SSL_KEY_PATH=/path/to/your/private-key.pem
export SWML_DOMAIN=yourdomain.com

# Run the agent with SSL
python tutorial/multi_agents/sales_agent.py

Method 2: Using a Single Command

Set the same variables inline, on the command that starts the agent:

SWML_SSL_ENABLED=true \
SWML_SSL_CERT_PATH=/path/to/cert.pem \
SWML_SSL_KEY_PATH=/path/to/key.pem \
SWML_DOMAIN=yourdomain.com \
python tutorial/multi_agents/sales_agent.py

Self-Signed Certificates (Development Only)

For testing, you can create a self-signed certificate:

# Generate a self-signed certificate
openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365 -nodes \
  -subj "/C=US/ST=State/L=City/O=Organization/CN=localhost"

# Run with the self-signed cert
SWML_SSL_ENABLED=true \
SWML_SSL_CERT_PATH=cert.pem \
SWML_SSL_KEY_PATH=key.pem \
python tutorial/multi_agents/sales_agent.py

Note: Self-signed certificates will show security warnings. Use proper certificates from a Certificate Authority (CA) for production.


Summary

This lesson built a working SignalWire agent. It covered:

Key Concepts:

  • How to create an agent by inheriting from AgentBase
  • Using the Prompt Object Model (POM) to structure agent behavior
  • Configuring voice and language settings
  • Running agents locally and with SSL/HTTPS
  • Testing agents with curl and swaig-test

What's Next?

In the next lesson, you'll learn how to make Morgan smarter by adding a searchable knowledge base. This will allow the agent to look up product information, pricing, and technical specifications in real-time.

Practice Exercises

Before moving on, try these exercises:

  1. Change Morgan's Voice: Try different voices like rime.spore or rime.cove
  2. Modify the Personality: Make Morgan more formal or casual
  3. Add New Sections: Add a section about return policies or warranties
  4. Change the Port: Run the agent on port 3001 instead of 3000

Troubleshooting Tips

Common Issues:

  • Port in use: Change the port in the constructor or kill the process using the port
  • Import errors: Make sure you've installed the SDK with pip install -e .
  • SSL errors: Check that your certificate and key paths are correct

Tutorial Overview | Next: Lesson 2 - Adding Intelligence with Knowledge Bases