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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
//! `llmprism` -- a command-line wrapper around the `llmprism` library, so
//! every capability it supports (text generation, streaming, structured
//! output, moderation, embeddings, rerank, images, audio) is reachable
//! straight from the shell, not just from Rust code.
//!
//! A thin wrapper, deliberately: [`Registry::from_env`] already only
//! registers a provider when both its Cargo feature and its API-key
//! environment variable are present, so this binary needs no
//! provider-specific logic of its own -- it just calls the same public API
//! any other `llmprism` consumer would.
//!
//! Build/install with the providers you want compiled in, e.g.:
//!
//! ```sh
//! cargo install llmprism --features cli,openai,anthropic
//! ```

use std::io::Read;
use std::process::ExitCode;

use clap::{Args, Parser, Subcommand};
use futures::StreamExt;
use llmprism::schema::ObjectSchema;
use llmprism::text::PendingTextRequest;
use llmprism::{Registry, StreamEvent};
use serde::Serialize;

#[derive(Parser)]
#[command(
    name = "llmprism",
    version,
    about = "Talk to LLM providers from the command line."
)]
struct Cli {
    #[command(subcommand)]
    command: Command,
}

#[derive(Subcommand)]
enum Command {
    /// Generate text.
    Text(TextArgs),
    /// Generate text, printing the reply as it streams in.
    Stream(TextArgs),
    /// Generate a reply matching a JSON Schema.
    Structured(StructuredArgs),
    /// Check text against a provider's content-safety classifier.
    Moderate(ModerateArgs),
    /// Turn text into embedding vectors.
    Embed(EmbedArgs),
    /// Score and sort documents by relevance to a query.
    Rerank(RerankArgs),
    /// Generate an image from a text prompt.
    Image(ImageArgs),
    /// Turn text into spoken audio.
    Speak(SpeakArgs),
    /// Transcribe spoken audio into text.
    Transcribe(TranscribeArgs),
    /// List providers compiled in and configured with an API key.
    Providers,
}

#[derive(Args)]
struct CommonArgs {
    /// Provider to use (e.g. "openai", "anthropic").
    #[arg(short, long)]
    provider: String,
    /// Model to use (e.g. "gpt-4o-mini").
    #[arg(short, long)]
    model: String,
    /// Print the full response as JSON instead of a plain-text summary.
    #[arg(long)]
    json: bool,
}

#[derive(Args)]
struct TextArgs {
    #[command(flatten)]
    common: CommonArgs,
    /// The prompt to send. Reads from stdin if omitted.
    #[arg(short = 'P', long)]
    prompt: Option<String>,
    /// A system-level instruction.
    #[arg(short, long)]
    system: Option<String>,
    #[arg(long)]
    temperature: Option<f32>,
    #[arg(long)]
    max_tokens: Option<u32>,
    /// How many tool-calling round trips to allow. Only meaningful with
    /// --mcp-stdio/--mcp-http attached; irrelevant otherwise.
    #[arg(long, default_value_t = 1)]
    max_steps: u32,
    /// Connect to an MCP server over stdio and attach its tools -- the
    /// command and its arguments as one string (e.g. "npx -y some-server").
    /// Repeatable.
    #[cfg(feature = "mcp")]
    #[arg(long = "mcp-stdio")]
    mcp_stdio: Vec<String>,
    /// Connect to an MCP server over Streamable HTTP and attach its tools.
    /// Repeatable.
    #[cfg(feature = "mcp")]
    #[arg(long = "mcp-http")]
    mcp_http: Vec<String>,
}

#[derive(Args)]
struct StructuredArgs {
    #[command(flatten)]
    common: CommonArgs,
    #[arg(short = 'P', long)]
    prompt: Option<String>,
    #[arg(short, long)]
    system: Option<String>,
    /// Path to a JSON Schema file describing the required reply shape.
    #[arg(long)]
    schema_file: std::path::PathBuf,
}

#[derive(Args)]
struct ModerateArgs {
    #[command(flatten)]
    common: CommonArgs,
    /// Text to check. Reads from stdin if omitted.
    #[arg(short = 'i', long)]
    input: Option<String>,
}

#[derive(Args)]
struct EmbedArgs {
    #[command(flatten)]
    common: CommonArgs,
    /// Text to embed. Pass more than once for several inputs in one call.
    /// Reads a single input from stdin if none are given.
    #[arg(short = 'i', long = "input")]
    inputs: Vec<String>,
    #[arg(long)]
    dimensions: Option<u32>,
}

#[derive(Args)]
struct RerankArgs {
    #[command(flatten)]
    common: CommonArgs,
    #[arg(short, long)]
    query: String,
    /// A document to score. Pass more than once.
    #[arg(short, long = "document")]
    documents: Vec<String>,
    #[arg(long)]
    top_k: Option<u32>,
}

#[derive(Args)]
struct ImageArgs {
    #[command(flatten)]
    common: CommonArgs,
    #[arg(short = 'P', long)]
    prompt: Option<String>,
    #[arg(long)]
    size: Option<String>,
    #[arg(long)]
    quality: Option<String>,
    #[arg(long)]
    style: Option<String>,
}

#[derive(Args)]
struct SpeakArgs {
    #[command(flatten)]
    common: CommonArgs,
    /// Text to speak. Reads from stdin if omitted.
    #[arg(short = 'i', long)]
    input: Option<String>,
    #[arg(long)]
    voice: Option<String>,
    /// Where to write the generated audio.
    #[arg(short, long)]
    out: std::path::PathBuf,
}

#[derive(Args)]
struct TranscribeArgs {
    #[command(flatten)]
    common: CommonArgs,
    /// Path to the audio file to transcribe.
    #[arg(short, long)]
    file: std::path::PathBuf,
}

// `current_thread`, not the default `multi_thread`: this binary runs one
// command and exits, with no need for a thread pool, and it keeps the
// crate's own `tokio` dependency (used by the library too, where a
// long-running server *would* want `multi_thread`) from having to pull in
// `rt-multi-thread` just to satisfy this binary.
#[tokio::main(flavor = "current_thread")]
async fn main() -> ExitCode {
    let cli = Cli::parse();
    match run(cli).await {
        Ok(()) => ExitCode::SUCCESS,
        Err(e) => {
            eprintln!("error: {e}");
            ExitCode::FAILURE
        }
    }
}

async fn run(cli: Cli) -> Result<(), Box<dyn std::error::Error>> {
    let registry = Registry::from_env();

    match cli.command {
        Command::Text(args) => run_text(&registry, args, false).await,
        Command::Stream(args) => run_text(&registry, args, true).await,
        Command::Structured(args) => run_structured(&registry, args).await,
        Command::Moderate(args) => run_moderate(&registry, args).await,
        Command::Embed(args) => run_embed(&registry, args).await,
        Command::Rerank(args) => run_rerank(&registry, args).await,
        Command::Image(args) => run_image(&registry, args).await,
        Command::Speak(args) => run_speak(&registry, args).await,
        Command::Transcribe(args) => run_transcribe(&registry, args).await,
        Command::Providers => run_providers(&registry),
    }
}

/// Reads `value` if given, otherwise the whole of stdin (trimmed of a
/// trailing newline) -- the fallback that makes every text-taking command
/// pipeable, e.g. `echo "..." | llmprism text -p openai -m gpt-4o-mini`.
fn value_or_stdin(value: Option<String>) -> Result<String, std::io::Error> {
    match value {
        Some(value) => Ok(value),
        None => {
            let mut buf = String::new();
            std::io::stdin().read_to_string(&mut buf)?;
            Ok(buf.trim_end_matches('\n').to_string())
        }
    }
}

fn print_result<T: Serialize>(value: &T, summary: impl FnOnce(&T) -> String, json: bool) {
    if json {
        println!(
            "{}",
            serde_json::to_string_pretty(value).expect("response types always serialize")
        );
    } else {
        println!("{}", summary(value));
    }
}

/// The MCP connections a text/stream request attached, if any -- kept alive
/// by the caller until the request finishes, so each connection shuts down
/// normally afterward instead of being force-killed by process exit.
#[cfg(feature = "mcp")]
type McpConnections = Vec<llmprism::mcp::McpToolset>;
#[cfg(not(feature = "mcp"))]
type McpConnections = ();

async fn build_text_request(
    registry: &Registry,
    args: &TextArgs,
) -> Result<(PendingTextRequest, McpConnections), Box<dyn std::error::Error>> {
    let prompt = value_or_stdin(args.prompt.clone())?;
    let mut request = registry.text(&args.common.provider, &args.common.model)?;

    if let Some(system) = &args.system {
        request = request.with_system_prompt(system);
    }
    request = request.with_prompt(prompt);
    if let Some(temperature) = args.temperature {
        request = request.with_temperature(temperature);
    }
    if let Some(max_tokens) = args.max_tokens {
        request = request.with_max_tokens(max_tokens);
    }
    request = request.with_max_steps(args.max_steps);

    #[cfg(feature = "mcp")]
    let mut connections = Vec::new();

    #[cfg(feature = "mcp")]
    {
        for target in &args.mcp_stdio {
            let mut parts = target.split_whitespace();
            let command = parts
                .next()
                .ok_or("--mcp-stdio needs a command, e.g. \"npx -y some-server\"")?;
            let toolset = llmprism::mcp::McpToolset::connect_stdio(command, parts).await?;
            request = request.with_tools(toolset.tools());
            connections.push(toolset);
        }
        for url in &args.mcp_http {
            let toolset = llmprism::mcp::McpToolset::connect_http(url.clone()).await?;
            request = request.with_tools(toolset.tools());
            connections.push(toolset);
        }
    }

    #[cfg(not(feature = "mcp"))]
    let connections = ();

    Ok((request, connections))
}

async fn run_text(
    registry: &Registry,
    args: TextArgs,
    stream: bool,
) -> Result<(), Box<dyn std::error::Error>> {
    let json = args.common.json;
    let (request, _connections) = build_text_request(registry, &args).await?;

    if stream {
        let mut events = request.stream();
        let mut final_response = None;
        while let Some(event) = events.next().await {
            match event? {
                StreamEvent::TextDelta { text } => {
                    if !json {
                        print!("{text}");
                        use std::io::Write;
                        std::io::stdout().flush().ok();
                    }
                }
                StreamEvent::StreamEnd { response } => final_response = Some(response),
                _ => {}
            }
        }
        if !json {
            println!();
        }
        if let Some(response) = final_response {
            if json {
                print_result(&response, |_| String::new(), true);
            }
        }
    } else {
        let response = request.generate().await?;
        print_result(&response, |r| r.text.clone().unwrap_or_default(), json);
    }

    Ok(())
}

async fn run_structured(
    registry: &Registry,
    args: StructuredArgs,
) -> Result<(), Box<dyn std::error::Error>> {
    let prompt = value_or_stdin(args.prompt)?;
    let schema_text = std::fs::read_to_string(&args.schema_file)
        .map_err(|e| format!("{}: {e}", args.schema_file.display()))?;
    let schema_json: serde_json::Value = serde_json::from_str(&schema_text)
        .map_err(|e| format!("{}: {e}", args.schema_file.display()))?;
    let schema = ObjectSchema::from_raw_json_schema("response", schema_json);

    let mut request = registry.structured(&args.common.provider, &args.common.model, schema)?;
    if let Some(system) = &args.system {
        request = request.with_system_prompt(system);
    }
    let response = request.with_prompt(prompt).generate().await?;

    print_result(
        &response,
        |r| serde_json::to_string_pretty(&r.data).unwrap_or_default(),
        args.common.json,
    );
    Ok(())
}

async fn run_moderate(
    registry: &Registry,
    args: ModerateArgs,
) -> Result<(), Box<dyn std::error::Error>> {
    let input = value_or_stdin(args.input)?;
    let response = registry
        .moderation(&args.common.provider, &args.common.model)?
        .with_input(input)
        .generate()
        .await?;

    print_result(
        &response,
        |r| {
            r.results
                .iter()
                .map(|result| format!("flagged: {}", result.flagged))
                .collect::<Vec<_>>()
                .join("\n")
        },
        args.common.json,
    );
    Ok(())
}

async fn run_embed(registry: &Registry, args: EmbedArgs) -> Result<(), Box<dyn std::error::Error>> {
    let inputs = if args.inputs.is_empty() {
        vec![value_or_stdin(None)?]
    } else {
        args.inputs
    };

    let mut request = registry.embeddings(&args.common.provider, &args.common.model)?;
    for input in inputs {
        request = request.with_input(input);
    }
    if let Some(dimensions) = args.dimensions {
        request = request.with_dimensions(dimensions);
    }
    let response = request.generate().await?;

    print_result(
        &response,
        |r| {
            format!(
                "{} embedding(s), {} dimension(s) each",
                r.embeddings.len(),
                r.embeddings.first().map(|e| e.vector.len()).unwrap_or(0)
            )
        },
        args.common.json,
    );
    Ok(())
}

async fn run_rerank(
    registry: &Registry,
    args: RerankArgs,
) -> Result<(), Box<dyn std::error::Error>> {
    let mut request = registry.rerank(&args.common.provider, &args.common.model, args.query)?;
    request = request.with_documents(args.documents);
    if let Some(top_k) = args.top_k {
        request = request.with_top_k(top_k);
    }
    let response = request.generate().await?;

    print_result(
        &response,
        |r| {
            r.results
                .iter()
                .map(|result| format!("[{}] {:.4}", result.index, result.relevance_score))
                .collect::<Vec<_>>()
                .join("\n")
        },
        args.common.json,
    );
    Ok(())
}

async fn run_image(registry: &Registry, args: ImageArgs) -> Result<(), Box<dyn std::error::Error>> {
    let prompt = value_or_stdin(args.prompt)?;
    let mut request = registry.images(&args.common.provider, &args.common.model, prompt)?;
    if let Some(size) = args.size {
        request = request.with_size(size);
    }
    if let Some(quality) = args.quality {
        request = request.with_quality(quality);
    }
    if let Some(style) = args.style {
        request = request.with_style(style);
    }
    let response = request.generate().await?;

    print_result(
        &response,
        |r| {
            r.images
                .iter()
                .map(|image| match &image.data {
                    llmprism::value_objects::MediaData::Url(url) => url.clone(),
                    llmprism::value_objects::MediaData::Base64(_) => {
                        "<base64-encoded image data -- use --json to get it>".to_string()
                    }
                })
                .collect::<Vec<_>>()
                .join("\n")
        },
        args.common.json,
    );
    Ok(())
}

async fn run_speak(registry: &Registry, args: SpeakArgs) -> Result<(), Box<dyn std::error::Error>> {
    let input = value_or_stdin(args.input)?;
    let mut request = registry.text_to_speech(&args.common.provider, &args.common.model, input)?;
    if let Some(voice) = args.voice {
        request = request.with_voice(voice);
    }
    let response = request.generate().await?;

    std::fs::write(&args.out, &response.audio.data)
        .map_err(|e| format!("{}: {e}", args.out.display()))?;
    if args.common.json {
        // Deliberately not the full `AudioResponse` here -- that would dump
        // the whole audio payload as a JSON array of numbers, which is both
        // enormous and pointless once it's already on disk at `args.out`.
        let summary = serde_json::json!({
            "path": args.out,
            "bytes_written": response.audio.data.len(),
            "mime_type": response.audio.mime_type,
            "meta": response.meta,
        });
        println!("{}", serde_json::to_string_pretty(&summary)?);
    } else {
        println!(
            "wrote {} bytes ({}) to {}",
            response.audio.data.len(),
            response.audio.mime_type,
            args.out.display()
        );
    }
    Ok(())
}

async fn run_transcribe(
    registry: &Registry,
    args: TranscribeArgs,
) -> Result<(), Box<dyn std::error::Error>> {
    let data = std::fs::read(&args.file).map_err(|e| format!("{}: {e}", args.file.display()))?;
    let mime_type = mime_type_for(&args.file);
    let filename = args
        .file
        .file_name()
        .map(|n| n.to_string_lossy().to_string())
        .unwrap_or_else(|| "audio".to_string());

    let audio = llmprism::audio::AudioInput::new(data, mime_type).with_filename(filename);
    let response = registry
        .speech_to_text(&args.common.provider, &args.common.model, audio)?
        .generate()
        .await?;

    print_result(&response, |r| r.text.clone(), args.common.json);
    Ok(())
}

fn run_providers(registry: &Registry) -> Result<(), Box<dyn std::error::Error>> {
    let mut names = registry.provider_names();
    names.sort_unstable();
    if names.is_empty() {
        println!("no providers configured -- set an API key (e.g. OPENAI_API_KEY) and rebuild with that provider's feature enabled");
    } else {
        for name in names {
            println!("{name}");
        }
    }
    Ok(())
}

/// A best-effort MIME type from a file extension, for
/// [`llmprism::audio::AudioInput`]. Falls back to a generic audio type
/// rather than failing outright -- most providers only use this as a hint.
fn mime_type_for(path: &std::path::Path) -> &'static str {
    match path.extension().and_then(|e| e.to_str()) {
        Some("mp3") => "audio/mpeg",
        Some("wav") => "audio/wav",
        Some("m4a") => "audio/mp4",
        Some("flac") => "audio/flac",
        Some("ogg") => "audio/ogg",
        Some("webm") => "audio/webm",
        _ => "application/octet-stream",
    }
}