Skip to main content

swarm_engine_core/learn/record/
llm.rs

1//! LlmCallRecord - LLM呼び出しの記録
2
3use serde::{Deserialize, Serialize};
4
5use crate::util::epoch_millis;
6
7/// LLM呼び出しの記録
8///
9/// LlmDebugEvent から変換可能(swarm-engine-llm crate で実装)。
10#[derive(Debug, Clone, Serialize, Deserialize)]
11pub struct LlmCallRecord {
12    /// 呼び出し種別("decide", "call_raw" 等)
13    pub call_type: String,
14    /// プロンプト
15    pub prompt: String,
16    /// レスポンス
17    pub response: String,
18    /// モデル名
19    pub model: String,
20    /// エンドポイント
21    pub endpoint: String,
22    /// LoRAアダプター名
23    pub lora: Option<String>,
24    /// レイテンシ(ミリ秒)
25    pub latency_ms: u64,
26    /// タイムスタンプ(Unix epoch ms)
27    pub timestamp_ms: u64,
28    /// Worker ID
29    pub worker_id: Option<usize>,
30    /// エラー(あれば)
31    pub error: Option<String>,
32}
33
34impl LlmCallRecord {
35    pub fn new(call_type: impl Into<String>, model: impl Into<String>) -> Self {
36        Self {
37            call_type: call_type.into(),
38            prompt: String::new(),
39            response: String::new(),
40            model: model.into(),
41            endpoint: String::new(),
42            lora: None,
43            latency_ms: 0,
44            timestamp_ms: epoch_millis(),
45            worker_id: None,
46            error: None,
47        }
48    }
49
50    pub fn prompt(mut self, prompt: impl Into<String>) -> Self {
51        self.prompt = prompt.into();
52        self
53    }
54
55    pub fn response(mut self, response: impl Into<String>) -> Self {
56        self.response = response.into();
57        self
58    }
59
60    pub fn endpoint(mut self, endpoint: impl Into<String>) -> Self {
61        self.endpoint = endpoint.into();
62        self
63    }
64
65    pub fn lora(mut self, lora: impl Into<String>) -> Self {
66        self.lora = Some(lora.into());
67        self
68    }
69
70    pub fn latency_ms(mut self, latency: u64) -> Self {
71        self.latency_ms = latency;
72        self
73    }
74
75    pub fn worker_id(mut self, id: usize) -> Self {
76        self.worker_id = Some(id);
77        self
78    }
79
80    pub fn error(mut self, error: impl Into<String>) -> Self {
81        self.error = Some(error.into());
82        self
83    }
84
85    /// 成功したか(エラーがなく、レスポンスがある)
86    pub fn is_success(&self) -> bool {
87        self.error.is_none() && !self.response.is_empty()
88    }
89}