The SGLang template provides structured generation capabilities for complex prompting, reasoning, and multi-step AI workflows. SGLang (Structured Generation Language) excels at complex reasoning tasks and controlled text generation.
What is SGLang?
SGLang is a domain-specific language for complex prompting and generation that provides:
🧠 Structured reasoning with multi-step prompts
🔄 Control flow for dynamic generation
📊 State management across generation steps
🎯 Guided generation with constraints
🔗 Chain-of-thought prompting patterns
Quick Start
from chutes.chute import NodeSelector
from chutes.chute.template.sglang import build_sglang_chute
chute = build_sglang_chute(
username="myuser",
model_name="microsoft/DialoGPT-medium",
revision="main",
node_selector=NodeSelector(
gpu_count=1,
min_vram_gb_per_gpu=16
)
)
curl -X POST https://myuser-sglang-chute.chutes.ai/reason \
-H "Content-Type: application/json" \
-d '{
"problem": "What are the environmental impacts of renewable energy?",
"steps": [
"analyze_benefits",
"identify_drawbacks",
"compare_alternatives",
"provide_conclusion"
]
}'
Chain-of-Thought
curl -X POST https://myuser-sglang-chute.chutes.ai/chain-of-thought \
-H "Content-Type: application/json" \
-d '{
"question": "If a train travels 60 mph for 2.5 hours, how far does it go?",
"show_reasoning": true
}'
SGLang Programs
Multi-Step Reasoning
@sglang.functiondefanalyze_problem(s, problem):
s += f"Problem: {problem}\n\n"
s += "Let me think about this step by step:\n\n"
s += "Step 1: Understanding the problem\n"
s += sglang.gen("understanding", max_tokens=100)
s += "\n\n"
s += "Step 2: Identifying key factors\n"
s += sglang.gen("factors", max_tokens=100)
s += "\n\n"
s += "Step 3: Analysis\n"
s += sglang.gen("analysis", max_tokens=150)
s += "\n\n"
s += "Conclusion:\n"
s += sglang.gen("conclusion", max_tokens=100)
return s
Structured Output
@sglang.functiondefextract_information(s, text):
s += f"Text: {text}\n\n"
s += "Extract the following information:\n\n"
s += "Name: "
s += sglang.gen("name", max_tokens=20, stop=["\n"])
s += "\n"
s += "Age: "
s += sglang.gen("age", max_tokens=10, regex=r"\d+")
s += "\n"
s += "Occupation: "
s += sglang.gen("occupation", max_tokens=30, stop=["\n"])
s += "\n"
s += "Summary: "
s += sglang.gen("summary", max_tokens=100)
return s
Guided Generation
@sglang.functiondefgenerate_story(s, theme, character):
s += f"Write a story about {character} with the theme of {theme}.\n\n"# Structured story generation
s += "Title: "
s += sglang.gen("title", max_tokens=20, stop=["\n"])
s += "\n\n"
s += "Setting: "
s += sglang.gen("setting", max_tokens=50, stop=["\n"])
s += "\n\n"
s += "Plot:\n"for i inrange(3):
s += f"Chapter {i+1}: "
s += sglang.gen(f"chapter_{i+1}", max_tokens=200)
s += "\n\n"
s += "Conclusion: "
s += sglang.gen("conclusion", max_tokens=100)
return s
Advanced Features
Custom Templates
# Define custom reasoning template
reasoning_template = """
Problem: {problem}
Analysis Framework:
1. Context: What background information is relevant?
2. Constraints: What limitations or requirements exist?
3. Options: What are the possible approaches or solutions?
4. Evaluation: What are the pros and cons of each option?
5. Conclusion: What is the best approach and why?
Let me work through this systematically:
"""
chute = build_sglang_chute(
username="myuser",
model_name="microsoft/DialoGPT-medium",
custom_templates={"reasoning": reasoning_template},
guidance_scale=1.5# Higher guidance for structured output
)