Modal Compute Agents
Run managed agents on Modal's serverless compute platform for scalable workloads
Modal compute agents leverage serverless infrastructure for high-performance, scalable computing workloads.
Quick Start
- Setup Modal Authentication
bash pip install modal modal token newSet up Modal token with your credentials from Modal Dashboard
- Basic Modal Agent
python import asyncio from praisonaiagents import Agent from praisonai.integrations.managed_agents import ManagedAgent, ManagedConfig managed = ManagedAgent( provider="local", config=ManagedConfig( model="gpt-4o-mini", name="ModalAgent" ), compute="modal" # Requires Modal authentication ) agent = Agent(name="modal-agent", backend=managed) # Provision serverless compute info = asyncio.run(managed.provision_compute()) print(f"Function: {info.instance_id}, Status: {info.status}") # Execute high-performance computation result = asyncio.run(managed.execute_in_compute("python3 -c \"print(sum(range(1000001)))\"")) print(f"Sum of 1-1M: {result['stdout']}")
How It Works
Modal provides serverless compute with automatic scaling, custom environments, and high-performance execution.
Serverless Compute
Function Deployment
import asyncio
from praisonaiagents import Agent
from praisonai.integrations.managed_agents import ManagedAgent, ManagedConfig
managed = ManagedAgent(
provider="local",
config=ManagedConfig(model="gpt-4o-mini"),
compute="modal"
)
# Deploy function with custom image
info = asyncio.run(managed.provision_compute(
image="python:3.12",
packages=["numpy", "scipy", "numba"] # High-performance packages
))
print(f"Function deployed: {info.instance_id}")
print(f"Status: {info.status}")High-Performance Computing
# Execute computationally intensive tasks
result = asyncio.run(managed.execute_in_compute("""
python3 -c "
import numpy as np
import time
# Large matrix multiplication
start = time.time()
a = np.random.rand(1000, 1000)
b = np.random.rand(1000, 1000)
c = np.dot(a, b)
end = time.time()
print(f'Matrix multiplication (1000x1000): {end - start:.3f} seconds')
print(f'Result shape: {c.shape}')
print(f'Sample result: {c[0, 0]:.6f}')
"
"""))
print(result["stdout"])Parallel Processing
# Execute parallel computations
result = asyncio.run(managed.execute_in_compute("""
python3 -c "
import multiprocessing as mp
import time
def worker(n):
return sum(i * i for i in range(n))
start = time.time()
with mp.Pool() as pool:
results = pool.map(worker, [100000] * 8)
end = time.time()
print(f'Parallel computation: {end - start:.3f} seconds')
print(f'Results: {results[:3]}...')
"
"""))
print(result["stdout"])LLM Integration
import asyncio
from praisonaiagents import Agent
from praisonai.integrations.managed_agents import ManagedAgent, ManagedConfig
managed = ManagedAgent(
provider="local",
config=ManagedConfig(
model="gpt-4o-mini",
system="You are a computational scientist. Use Modal's serverless platform for intensive computations."
),
compute="modal"
)
agent = Agent(name="scientist", backend=managed)
# Provision high-performance environment
info = asyncio.run(managed.provision_compute(
packages=["numpy", "scipy", "matplotlib", "pandas", "scikit-learn"]
))
# LLM directs high-performance computing
result = agent.start("""
Perform a Monte Carlo simulation to estimate π using 10 million random points.
Show the convergence process and final estimate.
""", stream=True)Common Patterns
Machine Learning Pipeline
import asyncio
from praisonaiagents import Agent
from praisonai.integrations.managed_agents import ManagedAgent, ManagedConfig
managed = ManagedAgent(
provider="local",
config=ManagedConfig(
model="gpt-4o-mini",
system="You are an ML engineer using Modal for scalable model training."
),
compute="modal"
)
agent = Agent(name="ml-engineer", backend=managed)
# ML environment with GPU support (if available)
info = asyncio.run(managed.provision_compute(
packages=["torch", "scikit-learn", "numpy", "pandas"]
))
# Train models at scale
result = agent.start("""
Create and train a simple neural network on synthetic data:
1. Generate 10,000 samples with 10 features
2. Create a 3-layer neural network
3. Train for 100 epochs
4. Report final accuracy and training time
""")Data Processing Pipeline
import asyncio
from praisonaiagents import Agent
from praisonai.integrations.managed_agents import ManagedAgent, ManagedConfig
managed = ManagedAgent(
provider="local",
config=ManagedConfig(
model="gpt-4o-mini",
system="You are a data engineer processing large datasets efficiently."
),
compute="modal"
)
agent = Agent(name="data-engineer", backend=managed)
# Big data environment
info = asyncio.run(managed.provision_compute(
packages=["pandas", "polars", "pyarrow", "dask"]
))
# Process large datasets
result = agent.start("""
Simulate processing a large dataset:
1. Create a 1M row DataFrame with random data
2. Perform groupby operations and aggregations
3. Calculate percentiles and statistics
4. Measure processing time and memory usage
""")Scientific Computing
import asyncio
from praisonaiagents import Agent
from praisonai.integrations.managed_agents import ManagedAgent, ManagedConfig
managed = ManagedAgent(
provider="local",
config=ManagedConfig(model="gpt-4o-mini"),
compute="modal"
)
agent = Agent(name="physicist", backend=managed)
# Scientific computing environment
info = asyncio.run(managed.provision_compute(
packages=["numpy", "scipy", "sympy", "matplotlib"]
))
# Complex calculations
result = agent.start("""
Solve a differential equation numerically:
1. dy/dx = -2*y + x with initial condition y(0) = 1
2. Solve over x = [0, 5]
3. Plot the solution
4. Compare with analytical solution
""")Configuration Options
Modal Configuration
| Option | Type | Default | Description |
|---|---|---|---|
image | str | "python:3.12" | Base container image |
packages | List[str] | [] | Python packages to install |
gpu | str | None | GPU type (if needed) |
memory | int | 1024 | Memory limit in MB |
cpu | float | 2.0 | CPU cores |
timeout | int | 300 | Function timeout |
Compute Resources
| Resource Tier | CPU | Memory | Use Case |
|---|---|---|---|
| Basic | 1 core | 1GB | Light computation |
| Standard | 2 cores | 4GB | General workloads |
| High-CPU | 8 cores | 16GB | CPU-intensive tasks |
| High-Memory | 4 cores | 32GB | Memory-intensive tasks |
GPU Support
import asyncio
from praisonaiagents import Agent
from praisonai.integrations.managed_agents import ManagedAgent, ManagedConfig
managed = ManagedAgent(
provider="local",
config=ManagedConfig(model="gpt-4o-mini"),
compute="modal"
)
# GPU-accelerated compute (if available in your Modal plan)
info = asyncio.run(managed.provision_compute(
packages=["torch", "transformers", "accelerate"],
gpu="T4", # or "A10G", "A100" based on availability
memory=8192 # 8GB for GPU workloads
))Best Practices
Resource Optimization
Choose appropriate CPU and memory based on workload. Use GPU instances only when needed for deep learning or parallel computation.
Package Management
Install packages during provisioning for better performance. Cache frequently used environments to reduce cold start times.
Cost Management
Modal charges for compute time. Shutdown functions promptly after use. Monitor usage in the Modal dashboard.
Scaling Considerations
Modal auto-scales serverless functions. Design stateless computations for better parallelization and scaling.
Advanced Features
Custom Images
# Use custom Docker images for specialized environments
info = asyncio.run(managed.provision_compute(
image="tensorflow/tensorflow:latest-gpu",
packages=["transformers", "datasets"]
))Shared Storage
# Access Modal's shared volumes (if configured)
result = asyncio.run(managed.execute_in_compute("""
# Upload/download files to/from Modal volumes
echo "Data processing complete" > /data/results.txt
cat /data/results.txt
"""))Environment Variables
# Set environment variables for functions
result = asyncio.run(managed.execute_in_compute("""
export COMPUTE_TYPE=modal
export WORKER_ID=$RANDOM
python3 -c "import os; print(f'Running on {os.environ.get(\"COMPUTE_TYPE\")}')"
"""))