File size: 12,876 Bytes
fcc38f9 | 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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 | # How to Create Services
This guide explains how to create service layers for VAGEN environments using **Blackjack** as an example. Services enable batch processing and distributed training by managing multiple environment instances simultaneously.
## Directory Structure
```
vagen/env/
βββ __init__.py # Main environment registry (REGISTERED_ENV)
βββ blackjack/
βββ service.py # BlackjackService - batch environment manager
βββ service_config.py # BlackjackServiceConfig - service configuration
βββ env.py # BlackjackEnv (dependency)
βββ env_config.py # BlackjackEnvConfig (dependency)
βββ __init__.py # Environment info definition (BLACKJACK_ENV_INFO)
```
**File Responsibilities:**
- `blackjack/service_config.py`: Configuration parameters for batch processing and service management
- `blackjack/service.py`: Implementation of batch environment operations following BaseService interface
- `blackjack/__init__.py`: Defines BLACKJACK_ENV_INFO with service classes included
- `env/__init__.py`: Main registry where BLACKJACK_ENV_INFO gets added to REGISTERED_ENV
## Component Hierarchy
```
VAGEN Service Architecture
β
βββ Infrastructure Layer (vagen/server/)
β βββ BatchEnvServer # Flask server hosting all services
β β βββ Service Management # Route requests to appropriate services
β β βββ HTTP API Endpoints # RESTful interface (/batch/*, /environments)
β β βββ Request Serialization # Handle complex observation data
β β βββ Multi-Service Coordination # Manage different environment types
β β
β βββ BatchEnvClient # Client for connecting to servers
β βββ HTTP Communication # Connect to remote BatchEnvServer
β βββ Batch Method Wrappers # Same interface as BaseService
β βββ Automatic Serialization # Handle observation deserialization
β βββ Convenience Methods # Single-environment operations
β
βββ Service Interface Layer (vagen/env/base/)
β βββ BaseService (Abstract Interface)
β βββ create_environments_batch() # Create multiple env instances
β βββ reset_batch() # Reset multiple environments
β βββ step_batch() # Execute actions across environments
β βββ compute_reward_batch() # Calculate final rewards
β βββ get_system_prompts_batch() # Get prompts for environments
β βββ close_batch() # Clean up environment resources
β
βββ Implementation Layer (vagen/env/your_env/)
βββ YourService (e.g., BlackjackService)
βββ Environment Instance Pool # Manage multiple env instances
βββ Configuration Management # Handle per-environment configs
βββ State Tracking # Monitor environment states
βββ Optional: LLM-as-Judge # State reward evaluation
```
## Understanding BaseService Interface
Services inherit from `BaseService`, which defines batch operations for environment management. Here's what each required method does:
### Core Methods Overview
**`create_environments_batch(ids2configs)`** - Initialize environment pool
- Takes a dictionary mapping environment IDs to their configurations
- Creates multiple environment instances with different settings
- Each environment can have unique parameters (render mode, difficulty, etc.)
**`reset_batch(ids2seeds)`** - Reset multiple environments
- Resets specified environments with given seeds for reproducibility
- Returns initial observations for all environments
- Handles serialization for network transfer
**`step_batch(ids2actions)`** - Execute actions across environments
- Takes LLM responses for multiple environments simultaneously
- Processes each action in its respective environment
- Returns observations, rewards, done flags, and info for all environments
**`compute_reward_batch(env_ids)`** - Calculate final rewards
- Computes episode-ending rewards for specified environments
- Usually returns 0.0 unless you need custom final scoring
**`get_system_prompts_batch(env_ids)`** - Retrieve system prompts
- Gets the system prompt for each specified environment
- Used by training system to set up LLM context
**`close_batch(env_ids)`** - Clean up resources
- Closes specified environments and frees resources
- If no IDs provided, closes all managed environments
## Implementation Components
### 1. Service Configuration (`service_config.py`)
Defines service-level parameters:
```python
from vagen.env.base.base_service_config import BaseServiceConfig
from dataclasses import dataclass
@dataclass
class BlackjackServiceConfig(BaseServiceConfig):
# Inherited: max_workers = 10
# Optional state reward features
use_state_reward: bool = False
```
### 2. Service Implementation (`service.py`)
The main service class implementing BaseService:
```python
from typing import Dict, List, Tuple, Optional, Any
from vagen.env.base.base_service import BaseService
from vagen.server.serial import serialize_observation
from .env import BlackjackEnv
from .env_config import BlackjackEnvConfig
class BlackjackService(BaseService):
def __init__(self, config: BaseServiceConfig):
self.environments = {} # env_id -> BlackjackEnv instance
self.env_configs = {} # env_id -> BlackjackEnvConfig
self.config = config
# Optional: Initialize state reward tracking
if self.config.use_state_reward:
from vagen.env.utils.top_string_tracker import TopKStringTracker
self.top_strings_tracker = TopKStringTracker(self.config.top_strings_m)
def create_environments_batch(self, ids2configs: Dict[Any, Any]) -> None:
"""Create multiple Blackjack environments with different configurations"""
for env_id, config in ids2configs.items():
# Extract environment-specific config
env_config_dict = config.get('env_config', {})
env_config = BlackjackEnvConfig(**env_config_dict)
# Create environment instance
env = BlackjackEnv(env_config)
# Store in service pools
self.environments[env_id] = env
self.env_configs[env_id] = env_config
def reset_batch(self, ids2seeds: Dict[Any, Any]) -> Dict[Any, Tuple[Any, Any]]:
"""Reset multiple environments and return serialized observations"""
results = {}
for env_id, seed in ids2seeds.items():
env = self.environments[env_id]
observation, info = env.reset(seed=seed)
# Serialize for network transfer
serialized_observation = serialize_observation(observation)
results[env_id] = (serialized_observation, info)
return results
def step_batch(self, ids2actions: Dict[Any, Any]) -> Dict[Any, Tuple[Dict, float, bool, Dict]]:
"""Execute LLM actions across multiple environments"""
results = {}
for env_id, action in ids2actions.items():
env = self.environments[env_id]
observation, reward, done, info = env.step(action)
# Serialize observation for network transfer
serialized_observation = serialize_observation(observation)
results[env_id] = (serialized_observation, reward, done, info)
return results
def compute_reward_batch(self, env_ids: List[str]) -> Dict[Any, float]:
"""Get final rewards for multiple environments"""
results = {}
for env_id in env_ids:
env = self.environments[env_id]
results[env_id] = env.compute_reward()
return results
def get_system_prompts_batch(self, env_ids: List[str]) -> Dict[Any, str]:
"""Get system prompts for multiple environments"""
results = {}
for env_id in env_ids:
env = self.environments[env_id]
results[env_id] = env.system_prompt()
return results
def close_batch(self, env_ids: Optional[List[str]] = None) -> None:
"""Clean up multiple environments"""
if env_ids is None:
env_ids = list(self.environments.keys())
for env_id in env_ids:
if env_id in self.environments:
self.environments[env_id].close()
del self.environments[env_id]
del self.env_configs[env_id]
```
## Service Registration
Update your `blackjack/__init__.py` to include service information:
```python
from .env import BlackjackEnv
from .env_config import BlackjackEnvConfig
from .service import BlackjackService
from .service_config import BlackjackServiceConfig
# Complete registration with service support
BLACKJACK_ENV_INFO = {
"env_cls": BlackjackEnv,
"config_cls": BlackjackEnvConfig,
"service_cls": BlackjackService,
"service_config_cls": BlackjackServiceConfig,
"description": "Classic Blackjack with batch processing support"
}
```
Then add to the main registry in `vagen/env/__init__.py`:
```python
from .blackjack import BLACKJACK_ENV_INFO
REGISTERED_ENV = {
# ... other environments ...
"blackjack": BLACKJACK_ENV_INFO,
# ... other environments ...
}
```
## Server Configuration
Add your service to the server config in `vagen/server/config/server.yaml`:
```yaml
blackjack:
max_workers: 48
use_state_reward: false
```
## Advanced Features: State Rewards (Optional)
If you want LLM-as-judge evaluation, add these methods to your service:
```python
# Add to your service class if using state rewards
@service_state_reward_wrapper # Decorator enables LLM judging
def step_batch(self, ids2actions: Dict[Any, Any]) -> Dict[Any, Tuple[Dict, float, bool, Dict]]:
# Same implementation as above
# Decorator automatically calls reward methods when needed
pass
def gen_reasoning_prompt(self, content, **kwargs) -> str:
"""Generate prompt for LLM judge to evaluate reasoning"""
return f"""Evaluate this Blackjack decision reasoning:
{content}
Rate the strategic quality from 0.0 to 1.0."""
def calculate_reasoning_reward(self, **kwargs) -> float:
"""Calculate reward based on LLM judge evaluation"""
response = kwargs.get("response")
content = kwargs.get("content")
# Parse LLM judge response
try:
reward = response.get("quality", 0.0) if isinstance(response, dict) else 0.5
except:
reward = 0.0
# Anti-repetition penalty
if hasattr(self, 'top_strings_tracker'):
top_k_strings = self.top_strings_tracker.get_top_k(self.config.top_strings_k)
if content in top_k_strings and reward < 0.6:
return -0.1 # Penalty for repetitive low-quality responses
return reward
```
## Usage Patterns
### Direct Service Usage
```python
from vagen.env.blackjack import BlackjackService, BlackjackServiceConfig
# Create service with 4 environments
config = BlackjackServiceConfig(max_workers=4)
service = BlackjackService(config)
# Create environments with different settings
ids2configs = {
"easy": {"env_config": {"natural": False, "render_mode": "text"}},
"hard": {"env_config": {"natural": True, "render_mode": "vision"}}
}
service.create_environments_batch(ids2configs)
# Batch operations
observations = service.reset_batch({"easy": 42, "hard": 123})
results = service.step_batch({
"easy": "<answer>Stand</answer>",
"hard": "<answer>Hit</answer>"
})
service.close_batch()
```
### Client-Server Usage
```python
from vagen.server.client import BatchEnvClient
# Connect to remote service
client = BatchEnvClient(base_url="http://localhost:5000")
# Same interface as direct service
client.create_environments_batch(ids2configs)
observations = client.reset_batch(ids2seeds)
results = client.step_batch(ids2actions)
client.close_batch()
```
## Testing Your Service
```python
# Test service functionality
config = BlackjackServiceConfig()
service = BlackjackService(config)
# Test environment creation
ids2configs = {"test_env": {"env_config": {"render_mode": "text"}}}
service.create_environments_batch(ids2configs)
# Test batch operations
obs = service.reset_batch({"test_env": 42})
results = service.step_batch({"test_env": "<answer>Hit</answer>"})
print("Service test passed:", len(obs) == 1 and len(results) == 1)
service.close_batch()
```
The service layer enables efficient scaling from single environments to hundreds of parallel instances, essential for large-scale LLM training while maintaining clean separation from your core game logic. |