Setup
Copy
# Create environment
python3 -m venv venv && source venv/bin/activate
# Install packages
pip install praisonaiagents praisonai
# Set API key
export OPENAI_API_KEY="your-key"
Create Sample Data
Copy
# Create mission parameters
cat > missions.json << 'EOF'
{
"ARTEMIS-III": {"destination": "Moon", "duration_days": 30, "crew": 4, "payload_kg": 2500, "delta_v_km_s": 12.5},
"MARS-2030": {"destination": "Mars", "duration_days": 900, "crew": 6, "payload_kg": 50000, "delta_v_km_s": 15.0},
"ISS-RESUPPLY": {"destination": "LEO", "duration_days": 3, "crew": 0, "payload_kg": 3000, "delta_v_km_s": 9.4}
}
EOF
# Create resource requirements
cat > resources.json << 'EOF'
{
"life_support": {"oxygen_kg_per_day": 0.84, "water_kg_per_day": 2.5, "food_kg_per_day": 1.8},
"power": {"base_kw": 10, "per_crew_kw": 2},
"propellant": {"isp_s": 450, "dry_mass_ratio": 0.1}
}
EOF
Run: Python Code
Copy
from praisonaiagents import Agent, Agents, Task, tool
from pydantic import BaseModel
from typing import List
import json
import math
# Structured output
class MissionPlan(BaseModel):
mission_id: str
destination: str
duration_days: int
crew_size: int
total_mass_kg: float
propellant_kg: float
life_support_kg: float
power_requirement_kw: float
contingency_reserves_pct: float
risk_level: str
# Database persistence is configured via memory={} parameter
# Tools
@tool
def get_mission(mission_id: str) -> str:
"""Get mission parameters."""
with open("missions.json") as f:
missions = json.load(f)
return json.dumps(missions.get(mission_id, {}))
@tool
def calculate_life_support(crew: int, duration_days: int) -> str:
"""Calculate life support requirements."""
with open("resources.json") as f:
res = json.load(f)["life_support"]
oxygen = crew * duration_days * res["oxygen_kg_per_day"]
water = crew * duration_days * res["water_kg_per_day"]
food = crew * duration_days * res["food_kg_per_day"]
total = oxygen + water + food
return json.dumps({
"oxygen_kg": round(oxygen, 1),
"water_kg": round(water, 1),
"food_kg": round(food, 1),
"total_kg": round(total, 1)
})
@tool
def calculate_propellant(payload_kg: float, delta_v_km_s: float) -> str:
"""Calculate propellant using Tsiolkovsky equation."""
with open("resources.json") as f:
res = json.load(f)["propellant"]
isp = res["isp_s"]
g0 = 9.81 # m/s^2
ve = isp * g0 / 1000 # km/s
# Tsiolkovsky: delta_v = ve * ln(m0/mf)
mass_ratio = math.exp(delta_v_km_s / ve)
dry_mass = payload_kg / (1 - res["dry_mass_ratio"])
wet_mass = dry_mass * mass_ratio
propellant = wet_mass - dry_mass
return json.dumps({
"propellant_kg": round(propellant, 1),
"wet_mass_kg": round(wet_mass, 1),
"mass_ratio": round(mass_ratio, 2)
})
@tool
def assess_risk(destination: str, duration_days: int, crew: int) -> str:
"""Assess mission risk level."""
risk_score = 0
if destination == "Mars":
risk_score += 5
elif destination == "Moon":
risk_score += 3
else:
risk_score += 1
risk_score += duration_days / 100
risk_score += crew * 0.5
if risk_score > 8:
level = "critical"
elif risk_score > 5:
level = "high"
elif risk_score > 3:
level = "medium"
else:
level = "low"
return json.dumps({"risk_score": round(risk_score, 1), "level": level, "contingency_pct": 15 + risk_score * 2})
# Agents
planner = Agent(
name="MissionPlanner",
instructions="Plan space missions. Use get_mission tool.",
tools=[get_mission],
memory={
"db": "sqlite:///space_missions.db",
"session_id": "space-mission"
}
)
engineer = Agent(
name="SystemsEngineer",
instructions="Calculate resources. Use calculate_life_support and calculate_propellant tools.",
tools=[calculate_life_support, calculate_propellant]
)
safety = Agent(
name="SafetyOfficer",
instructions="Assess risks. Use assess_risk tool.",
tools=[assess_risk]
)
# Tasks
plan_task = Task(
description="Analyze mission: {mission_id}",
agent=planner,
expected_output="Mission parameters"
)
resource_task = Task(
description="Calculate resource requirements",
agent=engineer,
expected_output="Resource calculations"
)
risk_task = Task(
description="Assess mission risks and contingencies",
agent=safety,
expected_output="Mission plan",
output_pydantic=MissionPlan
)
# Run
agents = Agents(agents=[planner, engineer, safety], tasks=[plan_task, resource_task, risk_task])
result = agents.start(mission_id="ARTEMIS-III")
print(result)
Run: CLI
Copy
# Plan single mission
praisonai "Plan ARTEMIS-III lunar mission" --verbose
# With persistence
praisonai "Compare Mars vs Moon mission requirements" --memory --user-id mission_control
# Resource optimization
praisonai "Optimize payload for ISS resupply mission" --telemetry
Run: agents.yaml
Createagents.yaml:
Copy
framework: praisonai
topic: "space mission planning"
roles:
planner:
role: Mission Planner
goal: Define mission parameters
backstory: Expert at space mission design
tasks:
plan:
description: |
Define mission:
- Destination and duration
- Crew size
- Payload requirements
- Delta-v budget
expected_output: Mission parameters
engineer:
role: Systems Engineer
goal: Calculate resource requirements
backstory: Expert at spacecraft systems
tasks:
calculate:
description: |
Calculate resources:
- Life support (O2, H2O, food)
- Power requirements
- Propellant mass
expected_output: Resource requirements
safety:
role: Safety Officer
goal: Assess risks and contingencies
backstory: Expert at mission safety
tasks:
assess:
description: |
Assess risks:
- Mission criticality
- Contingency reserves
- Abort scenarios
expected_output: Risk assessment
Copy
praisonai agents.yaml --verbose
Monitor & Verify
Copy
# View mission history
praisonai --history 10 --user-id mission_control
# Check metrics
praisonai --metrics
# Export plans
praisonai --save mission_plans
Serve API
Copy
from praisonaiagents import Agent, tool
import json
import math
@tool
def quick_mission_estimate(destination: str, crew: int, duration_days: int) -> str:
"""Quick mission resource estimate."""
# Life support
life_support_kg = crew * duration_days * 5.14 # O2 + H2O + food
# Power
power_kw = 10 + crew * 2
# Delta-v estimates
delta_v = {"LEO": 9.4, "Moon": 12.5, "Mars": 15.0}.get(destination, 10)
# Propellant (simplified)
propellant_kg = life_support_kg * (math.exp(delta_v / 4.4) - 1)
return json.dumps({
"destination": destination,
"life_support_kg": round(life_support_kg, 1),
"propellant_kg": round(propellant_kg, 1),
"power_kw": power_kw,
"total_mass_kg": round(life_support_kg + propellant_kg, 1)
})
agent = Agent(
name="SpaceMissionAPI",
instructions="Estimate space mission requirements.",
tools=[quick_mission_estimate]
)
agent.launch(path="/mission-estimate", port=8000)
Copy
curl -X POST http://localhost:8000/mission-estimate \
-H "Content-Type: application/json" \
-d '{"message": "Estimate Moon mission: 4 crew, 30 days"}'
Cleanup
Copy
rm -f space_missions.db missions.json resources.json
deactivate
Features Demonstrated
| Feature | Implementation |
|---|---|
| Multi-agent | Planner → Engineer → Safety |
| Structured Output | Pydantic MissionPlan |
| Orbital Mechanics | Tsiolkovsky equation |
| Risk Assessment | Destination-based scoring |
| DB Persistence | SQLite via db() |
| CLI | --telemetry for tracking |
| YAML Config | 3-agent mission pipeline |
| API Endpoint | agent.launch() |

