rsllm 0.1.0

Rust-native LLM client library with multi-provider support and streaming capabilities
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
//! # RSLLM Response Types
//! 
//! Response types for chat completions, embeddings, and other LLM operations.
//! Supports both streaming and non-streaming responses with usage tracking.

use crate::{MessageRole, ToolCall};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Response from a chat completion request
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChatResponse {
    /// Generated content
    pub content: String,
    
    /// Model used for generation
    pub model: String,
    
    /// Usage statistics
    pub usage: Option<Usage>,
    
    /// Finish reason
    pub finish_reason: Option<String>,
    
    /// Tool calls made by the assistant
    pub tool_calls: Option<Vec<ToolCall>>,
    
    /// Response metadata
    pub metadata: HashMap<String, serde_json::Value>,
    
    /// Response timestamp
    #[serde(with = "chrono::serde::ts_seconds_option")]
    pub timestamp: Option<chrono::DateTime<chrono::Utc>>,
    
    /// Response ID (if provided by provider)
    pub id: Option<String>,
}

impl ChatResponse {
    /// Create a new chat response
    pub fn new(content: impl Into<String>, model: impl Into<String>) -> Self {
        Self {
            content: content.into(),
            model: model.into(),
            usage: None,
            finish_reason: None,
            tool_calls: None,
            metadata: HashMap::new(),
            timestamp: Some(chrono::Utc::now()),
            id: None,
        }
    }
    
    /// Set usage statistics
    pub fn with_usage(mut self, usage: Usage) -> Self {
        self.usage = Some(usage);
        self
    }
    
    /// Set finish reason
    pub fn with_finish_reason(mut self, reason: impl Into<String>) -> Self {
        self.finish_reason = Some(reason.into());
        self
    }
    
    /// Set tool calls
    pub fn with_tool_calls(mut self, tool_calls: Vec<ToolCall>) -> Self {
        self.tool_calls = Some(tool_calls);
        self
    }
    
    /// Add metadata
    pub fn with_metadata(mut self, key: impl Into<String>, value: serde_json::Value) -> Self {
        self.metadata.insert(key.into(), value);
        self
    }
    
    /// Set response ID
    pub fn with_id(mut self, id: impl Into<String>) -> Self {
        self.id = Some(id.into());
        self
    }
    
    /// Check if the response contains tool calls
    pub fn has_tool_calls(&self) -> bool {
        self.tool_calls.as_ref().map_or(false, |calls| !calls.is_empty())
    }
    
    /// Check if the response finished successfully
    pub fn is_finished(&self) -> bool {
        matches!(
            self.finish_reason.as_deref(),
            Some("stop") | Some("end_turn") | Some("tool_calls")
        )
    }
    
    /// Check if the response was truncated due to length
    pub fn is_truncated(&self) -> bool {
        matches!(
            self.finish_reason.as_deref(),
            Some("length") | Some("max_tokens")
        )
    }
    
    /// Get the content length
    pub fn content_length(&self) -> usize {
        self.content.len()
    }
}

/// Response from a completion request (non-chat)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CompletionResponse {
    /// Generated text
    pub text: String,
    
    /// Model used for generation
    pub model: String,
    
    /// Usage statistics
    pub usage: Option<Usage>,
    
    /// Finish reason
    pub finish_reason: Option<String>,
    
    /// Log probabilities (if requested)
    pub logprobs: Option<LogProbs>,
    
    /// Response metadata
    pub metadata: HashMap<String, serde_json::Value>,
    
    /// Response timestamp
    #[serde(with = "chrono::serde::ts_seconds_option")]
    pub timestamp: Option<chrono::DateTime<chrono::Utc>>,
    
    /// Response ID (if provided by provider)
    pub id: Option<String>,
}

impl CompletionResponse {
    /// Create a new completion response
    pub fn new(text: impl Into<String>, model: impl Into<String>) -> Self {
        Self {
            text: text.into(),
            model: model.into(),
            usage: None,
            finish_reason: None,
            logprobs: None,
            metadata: HashMap::new(),
            timestamp: Some(chrono::Utc::now()),
            id: None,
        }
    }
    
    /// Set usage statistics
    pub fn with_usage(mut self, usage: Usage) -> Self {
        self.usage = Some(usage);
        self
    }
    
    /// Set finish reason
    pub fn with_finish_reason(mut self, reason: impl Into<String>) -> Self {
        self.finish_reason = Some(reason.into());
        self
    }
    
    /// Set log probabilities
    pub fn with_logprobs(mut self, logprobs: LogProbs) -> Self {
        self.logprobs = Some(logprobs);
        self
    }
    
    /// Add metadata
    pub fn with_metadata(mut self, key: impl Into<String>, value: serde_json::Value) -> Self {
        self.metadata.insert(key.into(), value);
        self
    }
    
    /// Set response ID
    pub fn with_id(mut self, id: impl Into<String>) -> Self {
        self.id = Some(id.into());
        self
    }
}

/// A single chunk in a streaming response
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StreamChunk {
    /// Content delta for this chunk
    pub content: String,
    
    /// Whether this is a delta (partial) or complete content
    pub is_delta: bool,
    
    /// Whether this is the final chunk
    pub is_done: bool,
    
    /// Model name
    pub model: String,
    
    /// Role of the message (if applicable)
    pub role: Option<MessageRole>,
    
    /// Tool calls delta (if applicable)
    pub tool_calls_delta: Option<Vec<ToolCallDelta>>,
    
    /// Finish reason (if this is the final chunk)
    pub finish_reason: Option<String>,
    
    /// Usage statistics (typically only in final chunk)
    pub usage: Option<Usage>,
    
    /// Chunk metadata
    pub metadata: HashMap<String, serde_json::Value>,
    
    /// Chunk timestamp
    #[serde(with = "chrono::serde::ts_seconds_option")]
    pub timestamp: Option<chrono::DateTime<chrono::Utc>>,
}

impl StreamChunk {
    /// Create a new stream chunk
    pub fn new(
        content: impl Into<String>,
        model: impl Into<String>,
        is_delta: bool,
        is_done: bool,
    ) -> Self {
        Self {
            content: content.into(),
            is_delta,
            is_done,
            model: model.into(),
            role: None,
            tool_calls_delta: None,
            finish_reason: None,
            usage: None,
            metadata: HashMap::new(),
            timestamp: Some(chrono::Utc::now()),
        }
    }
    
    /// Create a delta chunk
    pub fn delta(content: impl Into<String>, model: impl Into<String>) -> Self {
        Self::new(content, model, true, false)
    }
    
    /// Create a final chunk
    pub fn done(model: impl Into<String>) -> Self {
        Self::new("", model, false, true)
    }
    
    /// Set the role
    pub fn with_role(mut self, role: MessageRole) -> Self {
        self.role = Some(role);
        self
    }
    
    /// Set tool calls delta
    pub fn with_tool_calls_delta(mut self, delta: Vec<ToolCallDelta>) -> Self {
        self.tool_calls_delta = Some(delta);
        self
    }
    
    /// Set finish reason
    pub fn with_finish_reason(mut self, reason: impl Into<String>) -> Self {
        self.finish_reason = Some(reason.into());
        self
    }
    
    /// Set usage statistics
    pub fn with_usage(mut self, usage: Usage) -> Self {
        self.usage = Some(usage);
        self
    }
    
    /// Add metadata
    pub fn with_metadata(mut self, key: impl Into<String>, value: serde_json::Value) -> Self {
        self.metadata.insert(key.into(), value);
        self
    }
    
    /// Check if this chunk has content
    pub fn has_content(&self) -> bool {
        !self.content.is_empty()
    }
    
    /// Check if this chunk has tool calls
    pub fn has_tool_calls(&self) -> bool {
        self.tool_calls_delta.as_ref().map_or(false, |calls| !calls.is_empty())
    }
}

/// Tool call delta for streaming responses
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolCallDelta {
    /// Tool call index
    pub index: u32,
    
    /// Tool call ID (if starting a new call)
    pub id: Option<String>,
    
    /// Tool call type (if starting a new call)
    #[serde(rename = "type")]
    pub call_type: Option<String>,
    
    /// Function delta
    pub function: Option<ToolFunctionDelta>,
}

/// Tool function delta for streaming responses
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolFunctionDelta {
    /// Function name (if starting a new call)
    pub name: Option<String>,
    
    /// Arguments delta (partial JSON string)
    pub arguments: Option<String>,
}

/// Usage statistics for API calls
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Usage {
    /// Number of tokens in the prompt
    pub prompt_tokens: u32,
    
    /// Number of tokens in the completion
    pub completion_tokens: u32,
    
    /// Total number of tokens used
    pub total_tokens: u32,
    
    /// Number of cached tokens (if applicable)
    pub cached_tokens: Option<u32>,
    
    /// Reasoning tokens (for models with reasoning capabilities)
    pub reasoning_tokens: Option<u32>,
}

impl Usage {
    /// Create new usage statistics
    pub fn new(prompt_tokens: u32, completion_tokens: u32) -> Self {
        Self {
            prompt_tokens,
            completion_tokens,
            total_tokens: prompt_tokens + completion_tokens,
            cached_tokens: None,
            reasoning_tokens: None,
        }
    }
    
    /// Set cached tokens
    pub fn with_cached_tokens(mut self, cached_tokens: u32) -> Self {
        self.cached_tokens = Some(cached_tokens);
        self
    }
    
    /// Set reasoning tokens
    pub fn with_reasoning_tokens(mut self, reasoning_tokens: u32) -> Self {
        self.reasoning_tokens = Some(reasoning_tokens);
        self
    }
    
    /// Get effective prompt tokens (excluding cached)
    pub fn effective_prompt_tokens(&self) -> u32 {
        self.prompt_tokens - self.cached_tokens.unwrap_or(0)
    }
    
    /// Get total cost in tokens
    pub fn total_cost(&self) -> u32 {
        self.effective_prompt_tokens() + self.completion_tokens
    }
}

/// Log probabilities for completion responses
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LogProbs {
    /// Top log probabilities for each token
    pub token_logprobs: Vec<Option<f64>>,
    
    /// Top alternative tokens and their log probabilities
    pub top_logprobs: Vec<Option<HashMap<String, f64>>>,
    
    /// Text offset for each token
    pub text_offset: Vec<usize>,
}

/// Embedding response
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EmbeddingResponse {
    /// Embedding vectors
    pub embeddings: Vec<Vec<f32>>,
    
    /// Model used for embeddings
    pub model: String,
    
    /// Usage statistics
    pub usage: Option<Usage>,
    
    /// Response metadata
    pub metadata: HashMap<String, serde_json::Value>,
    
    /// Response timestamp
    #[serde(with = "chrono::serde::ts_seconds_option")]
    pub timestamp: Option<chrono::DateTime<chrono::Utc>>,
}

impl EmbeddingResponse {
    /// Create a new embedding response
    pub fn new(embeddings: Vec<Vec<f32>>, model: impl Into<String>) -> Self {
        Self {
            embeddings,
            model: model.into(),
            usage: None,
            metadata: HashMap::new(),
            timestamp: Some(chrono::Utc::now()),
        }
    }
    
    /// Set usage statistics
    pub fn with_usage(mut self, usage: Usage) -> Self {
        self.usage = Some(usage);
        self
    }
    
    /// Add metadata
    pub fn with_metadata(mut self, key: impl Into<String>, value: serde_json::Value) -> Self {
        self.metadata.insert(key.into(), value);
        self
    }
    
    /// Get the number of embeddings
    pub fn count(&self) -> usize {
        self.embeddings.len()
    }
    
    /// Get the embedding dimension (if any embeddings exist)
    pub fn dimension(&self) -> Option<usize> {
        self.embeddings.first().map(|emb| emb.len())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    
    #[test]
    fn test_chat_response_creation() {
        let response = ChatResponse::new("Hello!", "gpt-4")
            .with_finish_reason("stop")
            .with_usage(Usage::new(10, 5));
            
        assert_eq!(response.content, "Hello!");
        assert_eq!(response.model, "gpt-4");
        assert_eq!(response.finish_reason, Some("stop".to_string()));
        assert!(response.usage.is_some());
        assert!(response.is_finished());
    }
    
    #[test]
    fn test_stream_chunk() {
        let chunk = StreamChunk::delta("Hello", "gpt-4")
            .with_role(MessageRole::Assistant);
            
        assert_eq!(chunk.content, "Hello");
        assert!(chunk.is_delta);
        assert!(!chunk.is_done);
        assert_eq!(chunk.role, Some(MessageRole::Assistant));
        assert!(chunk.has_content());
    }
    
    #[test]
    fn test_usage_calculation() {
        let usage = Usage::new(100, 50)
            .with_cached_tokens(20);
            
        assert_eq!(usage.total_tokens, 150);
        assert_eq!(usage.effective_prompt_tokens(), 80);
        assert_eq!(usage.total_cost(), 130);
    }
}