-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda_agent.py
More file actions
135 lines (105 loc) · 3.96 KB
/
lambda_agent.py
File metadata and controls
135 lines (105 loc) · 3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/env python3
"""
Copyright (c) 2025 SignalWire
This file is part of the SignalWire AI Agents SDK.
Licensed under the MIT License.
See LICENSE file in the project root for full license information.
"""
"""
AWS Lambda deployment example for SignalWire AI Agents
This example shows how to deploy a SignalWire AI Agent to AWS Lambda using Mangum.
Mangum is included in requirements.txt, so no additional installation is needed.
Requirements:
- signalwire-agents (includes mangum>=0.19.0)
Usage:
1. Deploy this file to AWS Lambda
2. Configure API Gateway to route all requests to this function
3. Set environment variables:
- SWML_BASIC_AUTH_USER (optional, defaults to 'dev')
- SWML_BASIC_AUTH_PASSWORD (optional, defaults to 'w00t')
Features:
- Full SignalWire AI agent functionality in serverless
- Same routing, authentication, SWAIG functions as regular deployment
- Health checks work (/health, /ready)
- Structured logging compatible with CloudWatch
- Environment-based configuration
"""
import os
import sys
from datetime import datetime
# Add the signalwire_agents module to the path if needed
# (not needed if installed via pip)
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
from signalwire_agents import AgentBase
from signalwire_agents.core.function_result import SwaigFunctionResult
# Import Mangum for Lambda/API Gateway integration
try:
from mangum import Mangum
except ImportError:
print("ERROR: Mangum not installed. Run: pip install mangum")
sys.exit(1)
class HealthCheckAgent(AgentBase):
"""
Example agent for Lambda deployment
This agent demonstrates all the standard AgentBase features:
- SWAIG functions with SwaigFunctionResult
- Prompt configuration via prompt_add_section
- Voice configuration via add_language
- Basic auth and health endpoints
"""
def __init__(self, name="lambda-agent", route="/", **kwargs):
super().__init__(name=name, route=route, **kwargs)
# Configure voice
self.add_language(name="English", code="en-US", voice="inworld.Mark")
# Configure prompt using prompt_add_section (the public API)
self.prompt_add_section(
"Role",
body="You are a helpful AI assistant running in AWS Lambda."
)
self.prompt_add_section(
"Instructions",
bullets=[
"Greet users warmly and offer help",
"Use the greet_user function when asked to greet someone",
"Use the get_time function when asked about the current time",
]
)
@AgentBase.tool("Greet a user by name")
def greet_user(self, name: str = "friend"):
"""Greet a user by name"""
return SwaigFunctionResult(f"Hello {name}! I'm running in AWS Lambda!")
@AgentBase.tool("Get the current time")
def get_time(self):
"""Get the current time"""
return SwaigFunctionResult(f"Current time: {datetime.now().isoformat()}")
# Create the agent instance
# This works exactly the same as local development!
agent = HealthCheckAgent(
name="lambda-agent",
route="/", # Lambda usually serves from root
)
# Get the FastAPI app from the agent
app = agent.get_app()
# Create the Lambda handler using Mangum
# This handles all the API Gateway <-> FastAPI translation
handler = Mangum(app)
def lambda_handler(event, context):
"""
AWS Lambda entry point
This function receives API Gateway events and returns responses.
Mangum handles all the translation between Lambda/API Gateway format
and FastAPI's expected format.
Args:
event: API Gateway event
context: Lambda context
Returns:
dict: API Gateway response format
"""
return handler(event, context)
# For local testing (optional)
def main():
print("\nStarting agent server...")
print("Note: Works in any deployment mode (server/CGI/Lambda)")
agent.run()
if __name__ == "__main__":
main()