Skip to main content

llm_test_bench_core/api/
models.rs

1// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
2// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
3// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
4// option. This file may not be copied, modified, or distributed
5// except according to those terms.
6
7//! API data models.
8
9use serde::{Deserialize, Serialize};
10use utoipa::ToSchema;
11use chrono::{DateTime, Utc};
12use std::collections::HashMap;
13
14/// Health check response
15#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
16pub struct HealthResponse {
17    /// Service status
18    pub status: String,
19    /// API version
20    pub version: String,
21    /// Timestamp
22    pub timestamp: DateTime<Utc>,
23    /// Service uptime in seconds
24    pub uptime_seconds: u64,
25}
26
27/// Completion request
28#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
29pub struct CompletionRequest {
30    /// Provider name
31    pub provider: String,
32    /// Model name
33    pub model: String,
34    /// Prompt text
35    pub prompt: String,
36    /// Maximum tokens to generate
37    #[serde(skip_serializing_if = "Option::is_none")]
38    pub max_tokens: Option<usize>,
39    /// Temperature (0.0 to 2.0)
40    #[serde(skip_serializing_if = "Option::is_none")]
41    pub temperature: Option<f32>,
42    /// Enable streaming
43    #[serde(default)]
44    pub stream: bool,
45}
46
47/// Completion response
48#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
49pub struct CompletionResponse {
50    /// Generated text
51    pub text: String,
52    /// Model used
53    pub model: String,
54    /// Token usage
55    #[serde(skip_serializing_if = "Option::is_none")]
56    pub usage: Option<TokenUsage>,
57    /// Request ID
58    pub request_id: String,
59    /// Timestamp
60    pub created_at: DateTime<Utc>,
61}
62
63/// Token usage information
64#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
65pub struct TokenUsage {
66    pub prompt_tokens: u64,
67    pub completion_tokens: u64,
68    pub total_tokens: u64,
69}
70
71/// Evaluation request
72#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
73pub struct EvaluationRequest {
74    /// Provider name
75    pub provider: String,
76    /// Model name
77    pub model: String,
78    /// Input text
79    pub input: String,
80    /// Output text
81    pub output: String,
82    /// Expected output (optional)
83    #[serde(skip_serializing_if = "Option::is_none")]
84    pub expected: Option<String>,
85    /// Metrics to evaluate
86    pub metrics: Vec<String>,
87}
88
89/// Evaluation response
90#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
91pub struct EvaluationResponse {
92    /// Overall score
93    pub score: f64,
94    /// Individual metric scores
95    pub metrics: HashMap<String, f64>,
96    /// Evaluation ID
97    pub evaluation_id: String,
98    /// Timestamp
99    pub created_at: DateTime<Utc>,
100}
101
102/// Benchmark request
103#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
104pub struct BenchmarkRequest {
105    /// Benchmark name
106    pub name: String,
107    /// Providers to benchmark
108    pub providers: Vec<String>,
109    /// Models to benchmark
110    pub models: Vec<String>,
111    /// Dataset to use
112    pub dataset: String,
113    /// Number of examples
114    #[serde(skip_serializing_if = "Option::is_none")]
115    pub num_examples: Option<usize>,
116}
117
118/// Benchmark response
119#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
120pub struct BenchmarkResponse {
121    /// Benchmark ID
122    pub benchmark_id: String,
123    /// Status
124    pub status: BenchmarkStatus,
125    /// Progress (0-100)
126    pub progress: f64,
127    /// Results (when completed)
128    #[serde(skip_serializing_if = "Option::is_none")]
129    pub results: Option<BenchmarkResults>,
130}
131
132/// Benchmark status
133#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
134#[serde(rename_all = "snake_case")]
135pub enum BenchmarkStatus {
136    Pending,
137    Running,
138    Completed,
139    Failed,
140}
141
142/// Benchmark results
143#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
144pub struct BenchmarkResults {
145    /// Results by provider
146    pub by_provider: HashMap<String, ProviderResults>,
147    /// Total duration in seconds
148    pub duration_seconds: f64,
149}
150
151/// Provider benchmark results
152#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
153pub struct ProviderResults {
154    /// Provider name
155    pub provider: String,
156    /// Model name
157    pub model: String,
158    /// Average score
159    pub avg_score: f64,
160    /// Average latency (seconds)
161    pub avg_latency: f64,
162    /// Total cost (USD)
163    pub total_cost: f64,
164    /// Error rate
165    pub error_rate: f64,
166}
167
168/// Plugin information
169#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
170pub struct PluginInfo {
171    /// Plugin ID
172    pub id: String,
173    /// Plugin name
174    pub name: String,
175    /// Plugin version
176    pub version: String,
177    /// Plugin type
178    pub plugin_type: String,
179    /// Status
180    pub status: String,
181    /// Execution count
182    pub execution_count: u64,
183}
184
185/// Plugin execution request
186#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
187pub struct PluginExecuteRequest {
188    /// Plugin ID
189    pub plugin_id: String,
190    /// Input data
191    pub input: serde_json::Value,
192}
193
194/// Plugin execution response
195#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
196pub struct PluginExecuteResponse {
197    /// Output data
198    pub output: serde_json::Value,
199    /// Execution time (ms)
200    pub execution_time_ms: u64,
201    /// Request ID
202    pub request_id: String,
203}
204
205/// Paginated response
206#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
207pub struct PaginatedResponse<T> {
208    /// Items
209    pub items: Vec<T>,
210    /// Total count
211    pub total: usize,
212    /// Page number (1-indexed)
213    pub page: usize,
214    /// Page size
215    pub page_size: usize,
216    /// Total pages
217    pub total_pages: usize,
218}
219
220impl<T> PaginatedResponse<T> {
221    pub fn new(items: Vec<T>, total: usize, page: usize, page_size: usize) -> Self {
222        let total_pages = (total + page_size - 1) / page_size;
223        Self {
224            items,
225            total,
226            page,
227            page_size,
228            total_pages,
229        }
230    }
231}
232
233/// API error response
234#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
235pub struct ErrorResponse {
236    /// Error message
237    pub error: String,
238    /// Error code
239    pub code: String,
240    /// Detailed message
241    #[serde(skip_serializing_if = "Option::is_none")]
242    pub detail: Option<String>,
243    /// Request ID
244    #[serde(skip_serializing_if = "Option::is_none")]
245    pub request_id: Option<String>,
246    /// Timestamp
247    pub timestamp: DateTime<Utc>,
248}
249
250impl ErrorResponse {
251    pub fn new(error: impl Into<String>, code: impl Into<String>) -> Self {
252        Self {
253            error: error.into(),
254            code: code.into(),
255            detail: None,
256            request_id: None,
257            timestamp: Utc::now(),
258        }
259    }
260
261    pub fn with_detail(mut self, detail: impl Into<String>) -> Self {
262        self.detail = Some(detail.into());
263        self
264    }
265
266    pub fn with_request_id(mut self, request_id: impl Into<String>) -> Self {
267        self.request_id = Some(request_id.into());
268        self
269    }
270}
271
272#[cfg(test)]
273mod tests {
274    use super::*;
275
276    #[test]
277    fn test_paginated_response() {
278        let items = vec![1, 2, 3, 4, 5];
279        let response = PaginatedResponse::new(items.clone(), 15, 1, 5);
280
281        assert_eq!(response.items, items);
282        assert_eq!(response.total, 15);
283        assert_eq!(response.page, 1);
284        assert_eq!(response.total_pages, 3);
285    }
286
287    #[test]
288    fn test_error_response() {
289        let error = ErrorResponse::new("Test error", "TEST_ERROR")
290            .with_detail("Detailed message")
291            .with_request_id("req_123");
292
293        assert_eq!(error.error, "Test error");
294        assert_eq!(error.code, "TEST_ERROR");
295        assert_eq!(error.detail, Some("Detailed message".to_string()));
296        assert_eq!(error.request_id, Some("req_123".to_string()));
297    }
298}