swarm-engine-core 0.1.6

Core types and orchestration for SwarmEngine
Documentation
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
//! TrainingData - LoRA 学習用データ形式
//!
//! ## 設計思想
//!
//! - **DPO/SFT 両対応**: 将来の DPO 対応を見据えた設計
//! - **メタデータ付き**: モデル名、LoRA、Episode ID など追跡情報を保持
//! - **Builder パターン**: 柔軟な構築と可読性の両立
//!
//! ## 使用例
//!
//! ```rust
//! use swarm_engine_core::learn::{TrainingData, TrainingFormat};
//!
//! // SFT 形式(シンプル)
//! let sft = TrainingData::sft_simple("What action?", "CheckStatus");
//!
//! // SFT 形式(システムプロンプト付き)
//! let sft_with_system = TrainingData::sft(
//!     "You are an agent.",
//!     "What should I do?",
//!     "CheckStatus"
//! );
//!
//! // メタデータ付加
//! let with_meta = TrainingData::sft_simple("prompt", "response")
//!     .with_episode_id("ep_001".to_string())
//!     .with_model("qwen2.5")
//!     .with_outcome_score(1.0);
//! ```

use serde::{Deserialize, Serialize};

// ============================================================================
// TrainingFormat
// ============================================================================

/// 学習形式
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
pub enum TrainingFormat {
    /// Supervised Fine-Tuning(chosen のみ使用)
    #[default]
    Sft,
    /// Direct Preference Optimization(chosen + rejected)
    Dpo,
}

// ============================================================================
// TrainingMetadata
// ============================================================================

/// 学習データのメタデータ
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct TrainingMetadata {
    /// 元の Episode ID
    pub episode_id: Option<String>,

    /// Outcome スコア(0.0-1.0)
    pub outcome_score: Option<f64>,

    /// 使用モデル名
    pub model: Option<String>,

    /// 使用 LoRA 名
    pub lora: Option<String>,

    /// Strategy 名
    pub strategy_name: Option<String>,

    /// シナリオ名
    pub scenario_name: Option<String>,

    /// 追加のカスタムメタデータ
    #[serde(default, skip_serializing_if = "std::collections::HashMap::is_empty")]
    pub custom: std::collections::HashMap<String, String>,
}

impl TrainingMetadata {
    pub fn new() -> Self {
        Self::default()
    }
}

// ============================================================================
// TrainingData
// ============================================================================

/// LoRA 学習用データ形式
///
/// DPO/SFT 両対応を想定した設計。
/// 初期実装は SFT のみで開始し、データが蓄積されたら DPO に移行予定。
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TrainingData {
    /// システムプロンプト(オプション)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub system: Option<String>,

    /// 入力プロンプト(ユーザー入力)
    pub prompt: String,

    /// 選択された応答(成功ケース)
    pub chosen: String,

    /// 拒否された応答(失敗ケース、DPO 用)
    /// SFT の場合は None
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub rejected: Option<String>,

    /// 学習形式
    pub format: TrainingFormat,

    /// メタデータ
    #[serde(default)]
    pub metadata: TrainingMetadata,
}

impl TrainingData {
    // ========================================================================
    // Constructors
    // ========================================================================

    /// SFT 形式のデータを作成(システムプロンプト付き)
    ///
    /// # Arguments
    /// * `system` - システムプロンプト
    /// * `prompt` - ユーザープロンプト
    /// * `response` - アシスタントレスポンス(chosen)
    pub fn sft(system: &str, prompt: &str, response: &str) -> Self {
        Self {
            system: Some(system.to_string()),
            prompt: prompt.to_string(),
            chosen: response.to_string(),
            rejected: None,
            format: TrainingFormat::Sft,
            metadata: TrainingMetadata::default(),
        }
    }

    /// SFT 形式のデータを作成(シンプル)
    ///
    /// # Arguments
    /// * `prompt` - プロンプト
    /// * `response` - レスポンス(chosen)
    pub fn sft_simple(prompt: &str, response: &str) -> Self {
        Self {
            system: None,
            prompt: prompt.to_string(),
            chosen: response.to_string(),
            rejected: None,
            format: TrainingFormat::Sft,
            metadata: TrainingMetadata::default(),
        }
    }

    /// DPO 形式のデータを作成
    ///
    /// # Arguments
    /// * `prompt` - プロンプト
    /// * `chosen` - 選択された応答(成功ケース)
    /// * `rejected` - 拒否された応答(失敗ケース)
    pub fn dpo(prompt: &str, chosen: &str, rejected: &str) -> Self {
        Self {
            system: None,
            prompt: prompt.to_string(),
            chosen: chosen.to_string(),
            rejected: Some(rejected.to_string()),
            format: TrainingFormat::Dpo,
            metadata: TrainingMetadata::default(),
        }
    }

    /// DPO 形式のデータを作成(システムプロンプト付き)
    pub fn dpo_with_system(system: &str, prompt: &str, chosen: &str, rejected: &str) -> Self {
        Self {
            system: Some(system.to_string()),
            prompt: prompt.to_string(),
            chosen: chosen.to_string(),
            rejected: Some(rejected.to_string()),
            format: TrainingFormat::Dpo,
            metadata: TrainingMetadata::default(),
        }
    }

    // ========================================================================
    // Builder methods
    // ========================================================================

    /// Episode ID を設定
    pub fn with_episode_id(mut self, episode_id: String) -> Self {
        self.metadata.episode_id = Some(episode_id);
        self
    }

    /// Outcome スコアを設定
    pub fn with_outcome_score(mut self, score: f64) -> Self {
        self.metadata.outcome_score = Some(score);
        self
    }

    /// モデル名を設定
    pub fn with_model(mut self, model: &str) -> Self {
        self.metadata.model = Some(model.to_string());
        self
    }

    /// LoRA 名を設定
    pub fn with_lora(mut self, lora: Option<String>) -> Self {
        self.metadata.lora = lora;
        self
    }

    /// Strategy 名を設定
    pub fn with_strategy(mut self, strategy: &str) -> Self {
        self.metadata.strategy_name = Some(strategy.to_string());
        self
    }

    /// シナリオ名を設定
    pub fn with_scenario(mut self, scenario: &str) -> Self {
        self.metadata.scenario_name = Some(scenario.to_string());
        self
    }

    /// カスタムメタデータを追加
    pub fn with_custom(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.metadata.custom.insert(key.into(), value.into());
        self
    }

    // ========================================================================
    // Accessors
    // ========================================================================

    /// SFT 形式かどうか
    pub fn is_sft(&self) -> bool {
        matches!(self.format, TrainingFormat::Sft)
    }

    /// DPO 形式かどうか
    pub fn is_dpo(&self) -> bool {
        matches!(self.format, TrainingFormat::Dpo)
    }

    /// 有効なデータかどうか(prompt と chosen が非空)
    pub fn is_valid(&self) -> bool {
        !self.prompt.is_empty() && !self.chosen.is_empty()
    }

    /// DPO として有効かどうか(rejected も必要)
    pub fn is_valid_dpo(&self) -> bool {
        self.is_valid()
            && self
                .rejected
                .as_ref()
                .map(|r| !r.is_empty())
                .unwrap_or(false)
    }
}

// ============================================================================
// Conversation Format (for JSONL output)
// ============================================================================

/// 会話形式の学習データ(JSONL 出力用)
///
/// Hugging Face の conversations 形式に準拠。
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConversationData {
    /// 会話のターン
    pub conversations: Vec<ConversationTurn>,

    /// メタデータ(オプション)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub metadata: Option<TrainingMetadata>,
}

/// 会話のターン
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConversationTurn {
    /// 発話者の役割
    pub role: ConversationRole,

    /// 発話内容
    pub content: String,
}

/// 発話者の役割
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum ConversationRole {
    System,
    User,
    Assistant,
}

impl From<&TrainingData> for ConversationData {
    fn from(data: &TrainingData) -> Self {
        let mut conversations = Vec::new();

        // System prompt (optional)
        if let Some(system) = &data.system {
            conversations.push(ConversationTurn {
                role: ConversationRole::System,
                content: system.clone(),
            });
        }

        // User prompt
        conversations.push(ConversationTurn {
            role: ConversationRole::User,
            content: data.prompt.clone(),
        });

        // Assistant response (chosen)
        conversations.push(ConversationTurn {
            role: ConversationRole::Assistant,
            content: data.chosen.clone(),
        });

        Self {
            conversations,
            metadata: Some(data.metadata.clone()),
        }
    }
}

impl TrainingData {
    /// Conversation 形式に変換
    pub fn to_conversation(&self) -> ConversationData {
        ConversationData::from(self)
    }
}

// ============================================================================
// Tests
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_sft_simple() {
        let data = TrainingData::sft_simple("What action?", "CheckStatus");

        assert_eq!(data.prompt, "What action?");
        assert_eq!(data.chosen, "CheckStatus");
        assert!(data.system.is_none());
        assert!(data.rejected.is_none());
        assert!(data.is_sft());
        assert!(data.is_valid());
    }

    #[test]
    fn test_sft_with_system() {
        let data = TrainingData::sft("You are an agent.", "What to do?", "CheckStatus");

        assert_eq!(data.system, Some("You are an agent.".to_string()));
        assert_eq!(data.prompt, "What to do?");
        assert_eq!(data.chosen, "CheckStatus");
        assert!(data.is_sft());
    }

    #[test]
    fn test_dpo() {
        let data = TrainingData::dpo("What action?", "CheckStatus", "InvalidAction");

        assert_eq!(data.chosen, "CheckStatus");
        assert_eq!(data.rejected, Some("InvalidAction".to_string()));
        assert!(data.is_dpo());
        assert!(data.is_valid_dpo());
    }

    #[test]
    fn test_builder_methods() {
        let data = TrainingData::sft_simple("prompt", "response")
            .with_episode_id("ep_001".to_string())
            .with_outcome_score(0.85)
            .with_model("qwen2.5")
            .with_lora(Some("my_lora".to_string()))
            .with_strategy("worker_action")
            .with_scenario("troubleshooting")
            .with_custom("key", "value");

        assert_eq!(data.metadata.episode_id, Some("ep_001".to_string()));
        assert_eq!(data.metadata.outcome_score, Some(0.85));
        assert_eq!(data.metadata.model, Some("qwen2.5".to_string()));
        assert_eq!(data.metadata.lora, Some("my_lora".to_string()));
        assert_eq!(
            data.metadata.strategy_name,
            Some("worker_action".to_string())
        );
        assert_eq!(
            data.metadata.scenario_name,
            Some("troubleshooting".to_string())
        );
        assert_eq!(data.metadata.custom.get("key"), Some(&"value".to_string()));
    }

    #[test]
    fn test_to_conversation() {
        let data = TrainingData::sft("System prompt", "User prompt", "Assistant response");

        let conv = data.to_conversation();

        assert_eq!(conv.conversations.len(), 3);
        assert_eq!(conv.conversations[0].role, ConversationRole::System);
        assert_eq!(conv.conversations[0].content, "System prompt");
        assert_eq!(conv.conversations[1].role, ConversationRole::User);
        assert_eq!(conv.conversations[1].content, "User prompt");
        assert_eq!(conv.conversations[2].role, ConversationRole::Assistant);
        assert_eq!(conv.conversations[2].content, "Assistant response");
    }

    #[test]
    fn test_to_conversation_no_system() {
        let data = TrainingData::sft_simple("prompt", "response");

        let conv = data.to_conversation();

        assert_eq!(conv.conversations.len(), 2);
        assert_eq!(conv.conversations[0].role, ConversationRole::User);
        assert_eq!(conv.conversations[1].role, ConversationRole::Assistant);
    }

    #[test]
    fn test_serialization() {
        let data =
            TrainingData::sft_simple("prompt", "response").with_episode_id("ep_001".to_string());

        let json = serde_json::to_string(&data).unwrap();
        let deserialized: TrainingData = serde_json::from_str(&json).unwrap();

        assert_eq!(deserialized.prompt, data.prompt);
        assert_eq!(deserialized.chosen, data.chosen);
        assert_eq!(deserialized.metadata.episode_id, data.metadata.episode_id);
    }

    #[test]
    fn test_conversation_serialization() {
        let data = TrainingData::sft("System", "User", "Assistant");
        let conv = data.to_conversation();

        let json = serde_json::to_string(&conv).unwrap();

        // conversations 形式で出力されることを確認
        assert!(json.contains("\"conversations\""));
        assert!(json.contains("\"role\""));
        assert!(json.contains("\"system\""));
        assert!(json.contains("\"user\""));
        assert!(json.contains("\"assistant\""));
    }
}