swarm_engine_core/learn/record/
llm.rs1use serde::{Deserialize, Serialize};
4
5use crate::util::epoch_millis;
6
7#[derive(Debug, Clone, Serialize, Deserialize)]
11pub struct LlmCallRecord {
12 pub call_type: String,
14 pub prompt: String,
16 pub response: String,
18 pub model: String,
20 pub endpoint: String,
22 pub lora: Option<String>,
24 pub latency_ms: u64,
26 pub timestamp_ms: u64,
28 pub worker_id: Option<usize>,
30 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 pub fn is_success(&self) -> bool {
87 self.error.is_none() && !self.response.is_empty()
88 }
89}