llmprism 0.4.0

A unified, fluent Rust interface for integrating Large Language Model providers -- a Rust port of the PHP Prism library.
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
use std::collections::HashMap;

use serde_json::Value;

use crate::audio::{AudioOutput, AudioResponse, TranscriptionResponse};
use crate::embeddings::{Embedding, EmbeddingsResponse};
use crate::images::{GeneratedImage, ImagesResponse};
use crate::moderation::{ModerationResponse, ModerationResult};
use crate::rerank::{RankedDocument, RerankResponse};
use crate::structured::StructuredResponse;
use crate::text::Step;
use crate::value_objects::{EmbeddingsUsage, FinishReason, MediaData, Meta, ToolCall, Usage};

/// A fluent builder for a canned [`Step`], for scripting what a
/// [`FakeProvider`](super::FakeProvider) should return in a test.
///
/// # Example
///
/// ```
/// use llmprism::testing::FakeTextResponse;
///
/// let step = FakeTextResponse::new("Hello!").build();
/// assert_eq!(step.text.as_deref(), Some("Hello!"));
/// ```
pub struct FakeTextResponse {
    step: Step,
}

impl FakeTextResponse {
    /// Starts a canned response with the given reply text and a `Stop` finish
    /// reason. Pass an empty string if you only want a tool call and no visible
    /// text (see [`with_tool_call`](Self::with_tool_call)).
    pub fn new(text: impl Into<String>) -> Self {
        let text = text.into();
        Self {
            step: Step {
                text: if text.is_empty() { None } else { Some(text) },
                tool_calls: Vec::new(),
                finish_reason: FinishReason::Stop,
                usage: Usage::default(),
                meta: Meta::default(),
            },
        }
    }

    /// Scripts this response as also requesting a tool call, and switches the
    /// finish reason to [`FinishReason::ToolCalls`] to match. Call this more than
    /// once to script several tool calls in one round trip.
    pub fn with_tool_call(
        mut self,
        id: impl Into<String>,
        name: impl Into<String>,
        arguments: Value,
    ) -> Self {
        self.step.tool_calls.push(ToolCall {
            id: id.into(),
            name: name.into(),
            arguments,
        });
        self.step.finish_reason = FinishReason::ToolCalls;
        self
    }

    /// Overrides the token usage reported for this canned response, if your test
    /// needs to check usage accounting.
    pub fn with_usage(mut self, usage: Usage) -> Self {
        self.step.usage = usage;
        self
    }

    /// Overrides the finish reason, for testing how your application handles a
    /// specific one (e.g. [`FinishReason::Length`]).
    pub fn with_finish_reason(mut self, finish_reason: FinishReason) -> Self {
        self.step.finish_reason = finish_reason;
        self
    }

    /// Finishes building and returns the underlying [`Step`].
    pub fn build(self) -> Step {
        self.step
    }
}

impl From<FakeTextResponse> for Step {
    fn from(fixture: FakeTextResponse) -> Self {
        fixture.build()
    }
}

/// A fluent builder for a canned [`StructuredResponse`], for scripting what a
/// [`FakeProvider`](super::FakeProvider) should return from a structured
/// -output request in a test.
///
/// # Example
///
/// ```
/// use llmprism::testing::FakeStructuredResponse;
/// use serde_json::json;
///
/// let response = FakeStructuredResponse::new(json!({"greeting": "hi"})).build();
/// assert_eq!(response.data["greeting"], "hi");
/// ```
pub struct FakeStructuredResponse {
    response: StructuredResponse,
}

impl FakeStructuredResponse {
    /// Starts a canned response that will hand back `data` as-is, with a
    /// `Stop` finish reason.
    pub fn new(data: Value) -> Self {
        Self {
            response: StructuredResponse {
                data,
                finish_reason: FinishReason::Stop,
                usage: Usage::default(),
                meta: Meta::default(),
            },
        }
    }

    /// Overrides the token usage reported for this canned response.
    pub fn with_usage(mut self, usage: Usage) -> Self {
        self.response.usage = usage;
        self
    }

    /// Overrides the finish reason, for testing how your application handles
    /// a truncated (`FinishReason::Length`) or otherwise non-`Stop` result.
    pub fn with_finish_reason(mut self, finish_reason: FinishReason) -> Self {
        self.response.finish_reason = finish_reason;
        self
    }

    /// Finishes building and returns the underlying [`StructuredResponse`].
    pub fn build(self) -> StructuredResponse {
        self.response
    }
}

impl From<FakeStructuredResponse> for StructuredResponse {
    fn from(fixture: FakeStructuredResponse) -> Self {
        fixture.build()
    }
}

/// A fluent builder for a canned [`ModerationResponse`], for scripting what a
/// [`FakeProvider`](super::FakeProvider) should return from a moderation
/// request in a test.
///
/// # Example
///
/// ```
/// use llmprism::testing::FakeModerationResponse;
///
/// let response = FakeModerationResponse::new().flagged(true).build();
/// assert!(response.results[0].flagged);
/// ```
pub struct FakeModerationResponse {
    response: ModerationResponse,
}

impl FakeModerationResponse {
    /// Starts a canned response with no results yet.
    pub fn new() -> Self {
        Self {
            response: ModerationResponse {
                results: Vec::new(),
                meta: Meta::default(),
            },
        }
    }

    /// Appends a result for the next input, flagged (or not) as given, with no
    /// specific categories set -- covers the common case of checking a single
    /// input without needing to spell out a full [`ModerationResult`].
    pub fn flagged(mut self, flagged: bool) -> Self {
        self.response.results.push(ModerationResult {
            flagged,
            categories: HashMap::new(),
            category_scores: HashMap::new(),
        });
        self
    }

    /// Appends a fully custom result -- use this when a test needs specific
    /// categories/scores, or to script results for more than one input.
    pub fn with_result(mut self, result: ModerationResult) -> Self {
        self.response.results.push(result);
        self
    }

    /// Finishes building and returns the underlying [`ModerationResponse`].
    pub fn build(self) -> ModerationResponse {
        self.response
    }
}

impl Default for FakeModerationResponse {
    fn default() -> Self {
        Self::new()
    }
}

impl From<FakeModerationResponse> for ModerationResponse {
    fn from(fixture: FakeModerationResponse) -> Self {
        fixture.build()
    }
}

/// A fluent builder for a canned [`EmbeddingsResponse`], for scripting what a
/// [`FakeProvider`](super::FakeProvider) should return from an embeddings
/// request in a test.
///
/// # Example
///
/// ```
/// use llmprism::testing::FakeEmbeddingsResponse;
///
/// let response = FakeEmbeddingsResponse::new().with_embedding(vec![0.1, 0.2, 0.3]).build();
/// assert_eq!(response.embeddings[0].vector.len(), 3);
/// ```
pub struct FakeEmbeddingsResponse {
    response: EmbeddingsResponse,
}

impl FakeEmbeddingsResponse {
    /// Starts a canned response with no embeddings yet.
    pub fn new() -> Self {
        Self {
            response: EmbeddingsResponse {
                embeddings: Vec::new(),
                usage: EmbeddingsUsage::default(),
                meta: Meta::default(),
            },
        }
    }

    /// Appends one embedding vector, for the next input in order. Call this
    /// once per input your test expects to be embedded.
    pub fn with_embedding(mut self, vector: Vec<f32>) -> Self {
        self.response.embeddings.push(Embedding { vector });
        self
    }

    /// Overrides the token usage reported for this canned response.
    pub fn with_usage(mut self, usage: EmbeddingsUsage) -> Self {
        self.response.usage = usage;
        self
    }

    /// Finishes building and returns the underlying [`EmbeddingsResponse`].
    pub fn build(self) -> EmbeddingsResponse {
        self.response
    }
}

impl Default for FakeEmbeddingsResponse {
    fn default() -> Self {
        Self::new()
    }
}

impl From<FakeEmbeddingsResponse> for EmbeddingsResponse {
    fn from(fixture: FakeEmbeddingsResponse) -> Self {
        fixture.build()
    }
}

/// A fluent builder for a canned [`RerankResponse`], for scripting what a
/// [`FakeProvider`](super::FakeProvider) should return from a rerank request
/// in a test.
///
/// # Example
///
/// ```
/// use llmprism::testing::FakeRerankResponse;
///
/// let response = FakeRerankResponse::new().with_result(0, 0.92).build();
/// assert_eq!(response.results[0].relevance_score, 0.92);
/// ```
pub struct FakeRerankResponse {
    response: RerankResponse,
}

impl FakeRerankResponse {
    /// Starts a canned response with no scored documents yet.
    pub fn new() -> Self {
        Self {
            response: RerankResponse {
                results: Vec::new(),
                usage: EmbeddingsUsage::default(),
                meta: Meta::default(),
            },
        }
    }

    /// Appends one scored document, referencing the original `documents`
    /// list by `index`. Call this once per document your test expects to be
    /// scored.
    pub fn with_result(mut self, index: usize, relevance_score: f32) -> Self {
        self.response.results.push(RankedDocument {
            index,
            relevance_score,
            document: None,
        });
        self
    }

    /// Overrides the token usage reported for this canned response.
    pub fn with_usage(mut self, usage: EmbeddingsUsage) -> Self {
        self.response.usage = usage;
        self
    }

    /// Finishes building and returns the underlying [`RerankResponse`].
    pub fn build(self) -> RerankResponse {
        self.response
    }
}

impl Default for FakeRerankResponse {
    fn default() -> Self {
        Self::new()
    }
}

impl From<FakeRerankResponse> for RerankResponse {
    fn from(fixture: FakeRerankResponse) -> Self {
        fixture.build()
    }
}

/// A fluent builder for a canned [`ImagesResponse`], for scripting what a
/// [`FakeProvider`](super::FakeProvider) should return from an
/// image-generation request in a test.
///
/// # Example
///
/// ```
/// use llmprism::testing::FakeImagesResponse;
///
/// let response = FakeImagesResponse::new().with_url("https://example.com/cat.png").build();
/// assert_eq!(response.images.len(), 1);
/// ```
pub struct FakeImagesResponse {
    response: ImagesResponse,
}

impl FakeImagesResponse {
    /// Starts a canned response with no images yet.
    pub fn new() -> Self {
        Self {
            response: ImagesResponse {
                images: Vec::new(),
                meta: Meta::default(),
            },
        }
    }

    /// Appends one image, given as a URL.
    pub fn with_url(mut self, url: impl Into<String>) -> Self {
        self.response.images.push(GeneratedImage {
            data: MediaData::Url(url.into()),
            revised_prompt: None,
        });
        self
    }

    /// Appends one image, given as base64-encoded data.
    pub fn with_base64(mut self, data: impl Into<String>) -> Self {
        self.response.images.push(GeneratedImage {
            data: MediaData::Base64(data.into()),
            revised_prompt: None,
        });
        self
    }

    /// Finishes building and returns the underlying [`ImagesResponse`].
    pub fn build(self) -> ImagesResponse {
        self.response
    }
}

impl Default for FakeImagesResponse {
    fn default() -> Self {
        Self::new()
    }
}

impl From<FakeImagesResponse> for ImagesResponse {
    fn from(fixture: FakeImagesResponse) -> Self {
        fixture.build()
    }
}

/// A canned [`AudioResponse`], for scripting what a
/// [`FakeProvider`](super::FakeProvider) should return from a
/// text-to-speech request in a test.
///
/// # Example
///
/// ```
/// use llmprism::testing::FakeAudioResponse;
///
/// let response = FakeAudioResponse::new(vec![0, 1, 2, 3], "audio/mpeg").build();
/// assert_eq!(response.audio.mime_type, "audio/mpeg");
/// ```
pub struct FakeAudioResponse {
    response: AudioResponse,
}

impl FakeAudioResponse {
    /// Starts a canned response that will hand back `data`/`mime_type`
    /// as-is.
    pub fn new(data: Vec<u8>, mime_type: impl Into<String>) -> Self {
        Self {
            response: AudioResponse {
                audio: AudioOutput {
                    data,
                    mime_type: mime_type.into(),
                },
                meta: Meta::default(),
            },
        }
    }

    /// Finishes building and returns the underlying [`AudioResponse`].
    pub fn build(self) -> AudioResponse {
        self.response
    }
}

impl From<FakeAudioResponse> for AudioResponse {
    fn from(fixture: FakeAudioResponse) -> Self {
        fixture.build()
    }
}

/// A canned [`TranscriptionResponse`], for scripting what a
/// [`FakeProvider`](super::FakeProvider) should return from a
/// speech-to-text request in a test.
///
/// # Example
///
/// ```
/// use llmprism::testing::FakeTranscriptionResponse;
///
/// let response = FakeTranscriptionResponse::new("hello there").build();
/// assert_eq!(response.text, "hello there");
/// ```
pub struct FakeTranscriptionResponse {
    response: TranscriptionResponse,
}

impl FakeTranscriptionResponse {
    /// Starts a canned response that will hand back `text` as-is.
    pub fn new(text: impl Into<String>) -> Self {
        Self {
            response: TranscriptionResponse {
                text: text.into(),
                meta: Meta::default(),
            },
        }
    }

    /// Finishes building and returns the underlying [`TranscriptionResponse`].
    pub fn build(self) -> TranscriptionResponse {
        self.response
    }
}

impl From<FakeTranscriptionResponse> for TranscriptionResponse {
    fn from(fixture: FakeTranscriptionResponse) -> Self {
        fixture.build()
    }
}