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
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
//! The OpenAI provider. Text generation and structured output talk to the
//! Chat Completions API (`api.openai.com/v1/chat/completions`); moderation,
//! embeddings, image generation, and both audio endpoints each talk to their
//! own separate endpoint (`/v1/moderations`, `/v1/embeddings`,
//! `/v1/images/generations`, `/v1/audio/speech`, `/v1/audio/transcriptions`
//! -- see `moderation.rs`/`embeddings.rs`/`images.rs`/`audio.rs`). Enable
//! with the `openai` Cargo feature.

mod audio;
mod config;
mod embeddings;
mod images;
mod maps;
mod moderation;
mod wire;

use std::collections::BTreeMap;

use async_stream::try_stream;
use async_trait::async_trait;
use eventsource_stream::Eventsource;
use futures::stream::{BoxStream, StreamExt};
use reqwest_middleware::ClientWithMiddleware;
use serde_json::{json, Value};

use crate::audio::{
    AudioResponse, SpeechToTextRequest, TextToSpeechRequest, TranscriptionResponse,
};
use crate::client::{
    build_http_client, merge_provider_options, merge_provider_options_into_form, ErrorMapper,
};
use crate::embeddings::{EmbeddingsRequest, EmbeddingsResponse};
use crate::error::Error;
use crate::images::{ImagesRequest, ImagesResponse};
use crate::moderation::{ModerationRequest, ModerationResponse};
use crate::provider::Provider;
use crate::stream_event::StreamEvent;
use crate::structured::{StructuredRequest, StructuredResponse, StructuredStreamEvent};
use crate::text::{Step, TextRequest};
use crate::value_objects::{FinishReason, Meta, ToolCall, Usage};

use self::config::DEFAULT_BASE_URL;
use self::wire::{ChatResponse, ChatStreamChunk};

/// A [`Provider`] backed by OpenAI's Chat Completions API.
///
/// # Example
///
/// ```no_run
/// use llmprism::providers::openai::OpenAiProvider;
/// use llmprism::Registry;
///
/// let mut registry = Registry::new();
/// registry.register("openai", OpenAiProvider::new(std::env::var("OPENAI_API_KEY").unwrap()));
/// ```
///
/// If you're happy reading the API key from `OPENAI_API_KEY` yourself, you
/// likely don't need to construct this directly -- see
/// [`Registry::from_env`](crate::Registry::from_env).
pub struct OpenAiProvider {
    name: &'static str,
    api_key: String,
    base_url: String,
    client: ClientWithMiddleware,
}

impl OpenAiProvider {
    /// Creates a provider that talks to the real OpenAI API using `api_key`.
    pub fn new(api_key: impl Into<String>) -> Self {
        Self::with_base_url(api_key, DEFAULT_BASE_URL)
    }

    /// Creates a provider pointed at a different base URL -- useful for an
    /// OpenAI-compatible endpoint (a local model server, a proxy, an
    /// alternative provider that mirrors OpenAI's API shape) rather than
    /// `api.openai.com` itself. `base_url` should not include a trailing
    /// `/chat/completions`; that's appended automatically.
    pub fn with_base_url(api_key: impl Into<String>, base_url: impl Into<String>) -> Self {
        Self::with_name_and_base_url("openai", api_key, base_url)
    }

    /// Like [`with_base_url`](Self::with_base_url), but also overrides what
    /// [`Provider::name`] reports (and therefore what shows up in this
    /// provider's error messages). Not exposed publicly: the
    /// `providers::openai_compatible` family uses this internally so that,
    /// say, a Groq request's errors say `"groq"` rather than the misleading
    /// `"openai"` they'd otherwise inherit from reusing this provider's
    /// implementation.
    pub(crate) fn with_name_and_base_url(
        name: &'static str,
        api_key: impl Into<String>,
        base_url: impl Into<String>,
    ) -> Self {
        Self {
            name,
            api_key: api_key.into(),
            base_url: base_url.into(),
            client: build_http_client(),
        }
    }

    /// Replaces the underlying HTTP client -- an escape hatch for anything
    /// [`build_http_client`] doesn't cover: a request timeout (see that
    /// function's docs for why one isn't set by default), a proxy, a custom
    /// retry policy, or your application's own middleware.
    ///
    /// ```
    /// # #[cfg(feature = "openai")]
    /// # {
    /// use std::time::Duration;
    ///
    /// use llmprism::providers::openai::OpenAiProvider;
    /// use reqwest_middleware::ClientBuilder;
    ///
    /// let http_client = reqwest::Client::builder()
    ///     .connect_timeout(Duration::from_secs(10))
    ///     .build()
    ///     .unwrap();
    ///
    /// let provider = OpenAiProvider::new("sk-...").with_client(ClientBuilder::new(http_client).build());
    /// # }
    /// ```
    pub fn with_client(mut self, client: ClientWithMiddleware) -> Self {
        self.client = client;
        self
    }
}

#[async_trait]
impl Provider for OpenAiProvider {
    fn name(&self) -> &str {
        self.name
    }

    async fn text_step(&self, request: &TextRequest) -> Result<Step, Error> {
        let wire_request = maps::build_request(request);
        let body = merge_provider_options(&wire_request, &request.provider_options)?;

        let http_response = self
            .client
            .post(format!("{}/chat/completions", self.base_url))
            .bearer_auth(&self.api_key)
            .json(&body)
            .send()
            .await?;

        let status = http_response.status();
        let headers = http_response.headers().clone();
        let body_text = http_response.text().await?;

        if !status.is_success() {
            let mapper = ErrorMapper {
                provider: self.name(),
            };
            return Err(mapper.map_error_response(status, &headers, &body_text));
        }

        let wire_response: ChatResponse =
            serde_json::from_str(&body_text).map_err(|e| Error::Decode {
                provider: self.name().to_string(),
                message: e.to_string(),
            })?;

        Ok(maps::parse_response(wire_response))
    }

    async fn stream_text_once(
        &self,
        request: &TextRequest,
    ) -> Result<BoxStream<'static, Result<StreamEvent, Error>>, Error> {
        let mut wire_request = maps::build_request(request);
        wire_request.stream = Some(true);
        // Usage is otherwise omitted entirely from a streamed response; this asks
        // for one extra chunk at the end (empty `choices`, `usage` set) carrying
        // it, so streaming and non-streaming responses report usage the same way.
        wire_request.stream_options = Some(json!({"include_usage": true}));
        let body = merge_provider_options(&wire_request, &request.provider_options)?;

        let http_response = self
            .client
            .post(format!("{}/chat/completions", self.base_url))
            .bearer_auth(&self.api_key)
            .json(&body)
            .send()
            .await?;

        let status = http_response.status();
        if !status.is_success() {
            let headers = http_response.headers().clone();
            let body_text = http_response.text().await?;
            let mapper = ErrorMapper {
                provider: self.name(),
            };
            return Err(mapper.map_error_response(status, &headers, &body_text));
        }

        let provider_name = self.name().to_string();
        let mut events = http_response.bytes_stream().eventsource();

        let stream = try_stream! {
            let mut tool_calls: BTreeMap<usize, ToolCallAccumulator> = BTreeMap::new();
            let mut meta_sent = false;
            let mut finish_reason = FinishReason::Stop;
            let mut usage = Usage::default();

            while let Some(event) = events.next().await {
                let event = event.map_err(|e| Error::StreamDecode {
                    provider: provider_name.clone(),
                    message: e.to_string(),
                })?;

                let data = event.data.trim();
                if data.is_empty() || data == "[DONE]" {
                    continue;
                }

                let chunk: ChatStreamChunk =
                    serde_json::from_str(data).map_err(|e| Error::StreamDecode {
                        provider: provider_name.clone(),
                        message: e.to_string(),
                    })?;

                if !meta_sent {
                    yield StreamEvent::StreamStart {
                        meta: Meta {
                            id: chunk.id.clone(),
                            model: chunk.model.clone(),
                            rate_limits: Vec::new(),
                        },
                    };
                    meta_sent = true;
                }

                if let Some(choice) = chunk.choices.into_iter().next() {
                    if let Some(content) = choice.delta.content {
                        if !content.is_empty() {
                            yield StreamEvent::TextDelta { text: content };
                        }
                    }

                    for delta in choice.delta.tool_calls {
                        let index = delta.index;
                        let mut arguments_delta = None;

                        {
                            let entry = tool_calls.entry(index).or_default();
                            if let Some(id) = delta.id {
                                entry.id = Some(id);
                            }
                            if let Some(function) = delta.function {
                                if let Some(name) = function.name {
                                    entry.name = Some(name);
                                }
                                if let Some(args) = function.arguments {
                                    if !args.is_empty() {
                                        entry.arguments.push_str(&args);
                                        arguments_delta = Some(args);
                                    }
                                }
                            }
                        }

                        if let Some(arguments_delta) = arguments_delta {
                            let entry = &tool_calls[&index];
                            yield StreamEvent::ToolCallDelta {
                                index,
                                id: entry.id.clone(),
                                name: entry.name.clone(),
                                arguments_delta,
                            };
                        }
                    }

                    if let Some(fr) = choice.finish_reason {
                        finish_reason = maps::map_finish_reason(&fr);
                    }
                }

                if let Some(chunk_usage) = chunk.usage {
                    usage = maps::map_usage(chunk_usage);
                }
            }

            for (_, accumulated) in tool_calls {
                yield StreamEvent::ToolCall(ToolCall {
                    id: accumulated.id.unwrap_or_default(),
                    name: accumulated.name.unwrap_or_default(),
                    arguments: serde_json::from_str(&accumulated.arguments).unwrap_or(Value::Null),
                });
            }

            yield StreamEvent::StepFinish { usage, finish_reason };
        };

        Ok(stream.boxed())
    }

    async fn structured(&self, request: StructuredRequest) -> Result<StructuredResponse, Error> {
        let wire_request = maps::build_structured_request(&request);
        let body = merge_provider_options(&wire_request, &request.provider_options)?;

        let http_response = self
            .client
            .post(format!("{}/chat/completions", self.base_url))
            .bearer_auth(&self.api_key)
            .json(&body)
            .send()
            .await?;

        let status = http_response.status();
        let headers = http_response.headers().clone();
        let body_text = http_response.text().await?;

        if !status.is_success() {
            let mapper = ErrorMapper {
                provider: self.name(),
            };
            return Err(mapper.map_error_response(status, &headers, &body_text));
        }

        let wire_response: ChatResponse =
            serde_json::from_str(&body_text).map_err(|e| Error::Decode {
                provider: self.name().to_string(),
                message: e.to_string(),
            })?;

        maps::parse_structured_response(wire_response, self.name())
    }

    async fn stream_structured_once(
        &self,
        request: &StructuredRequest,
    ) -> Result<BoxStream<'static, Result<StructuredStreamEvent, Error>>, Error> {
        // OpenAI's `response_format: json_schema` mode streams exactly like a
        // plain chat completion -- the schema-constrained JSON arrives as
        // ordinary `choice.delta.content` string fragments, not via
        // `tool_calls` -- so this reuses the same wire request, the same
        // `ChatStreamChunk` SSE shape, and the same usage-tracking
        // `stream_options` flag `stream_text_once` already does.
        let mut wire_request = maps::build_structured_request(request);
        wire_request.stream = Some(true);
        wire_request.stream_options = Some(json!({"include_usage": true}));
        let body = merge_provider_options(&wire_request, &request.provider_options)?;

        let http_response = self
            .client
            .post(format!("{}/chat/completions", self.base_url))
            .bearer_auth(&self.api_key)
            .json(&body)
            .send()
            .await?;

        let status = http_response.status();
        if !status.is_success() {
            let headers = http_response.headers().clone();
            let body_text = http_response.text().await?;
            let mapper = ErrorMapper {
                provider: self.name(),
            };
            return Err(mapper.map_error_response(status, &headers, &body_text));
        }

        let provider_name = self.name().to_string();
        let mut events = http_response.bytes_stream().eventsource();

        let stream = try_stream! {
            let mut content = String::new();
            let mut meta = Meta::default();
            let mut finish_reason = FinishReason::Stop;
            let mut usage = Usage::default();

            while let Some(event) = events.next().await {
                let event = event.map_err(|e| Error::StreamDecode {
                    provider: provider_name.clone(),
                    message: e.to_string(),
                })?;

                let data = event.data.trim();
                if data.is_empty() || data == "[DONE]" {
                    continue;
                }

                let chunk: ChatStreamChunk =
                    serde_json::from_str(data).map_err(|e| Error::StreamDecode {
                        provider: provider_name.clone(),
                        message: e.to_string(),
                    })?;

                meta = Meta {
                    id: chunk.id.clone(),
                    model: chunk.model.clone(),
                    rate_limits: Vec::new(),
                };

                if let Some(choice) = chunk.choices.into_iter().next() {
                    if let Some(delta) = choice.delta.content {
                        if !delta.is_empty() {
                            content.push_str(&delta);
                            // A partial parse failing isn't a stream error --
                            // `fix_json` is designed to always succeed, but a
                            // defensive skip here just means this particular
                            // chunk contributes no preview, not that anything
                            // is wrong.
                            let fixed = partial_json_fixer::fix_json(&content);
                            if let Ok(data) = serde_json::from_str(&fixed) {
                                yield StructuredStreamEvent::PartialObject { data };
                            }
                        }
                    }

                    if let Some(fr) = choice.finish_reason {
                        finish_reason = maps::map_finish_reason(&fr);
                    }
                }

                if let Some(chunk_usage) = chunk.usage {
                    usage = maps::map_usage(chunk_usage);
                }
            }

            let data: Value = serde_json::from_str(&content).map_err(|e| Error::StructuredDecode {
                provider: provider_name.clone(),
                message: e.to_string(),
                context: Box::new(crate::error::StructuredDecodeContext {
                    raw: content.clone(),
                    finish_reason,
                    usage,
                    meta: meta.clone(),
                }),
            })?;

            yield StructuredStreamEvent::End {
                response: StructuredResponse { data, finish_reason, usage, meta },
            };
        };

        Ok(stream.boxed())
    }

    async fn moderation(&self, request: ModerationRequest) -> Result<ModerationResponse, Error> {
        let wire_request = moderation::build_request(&request);
        let body = merge_provider_options(&wire_request, &request.provider_options)?;

        let http_response = self
            .client
            .post(format!("{}/moderations", self.base_url))
            .bearer_auth(&self.api_key)
            .json(&body)
            .send()
            .await?;

        let status = http_response.status();
        let headers = http_response.headers().clone();
        let body_text = http_response.text().await?;

        if !status.is_success() {
            let mapper = ErrorMapper {
                provider: self.name(),
            };
            return Err(mapper.map_error_response(status, &headers, &body_text));
        }

        let wire_response: moderation::ApiResponse =
            serde_json::from_str(&body_text).map_err(|e| Error::Decode {
                provider: self.name().to_string(),
                message: e.to_string(),
            })?;

        Ok(moderation::parse_response(wire_response))
    }

    async fn embeddings(&self, request: EmbeddingsRequest) -> Result<EmbeddingsResponse, Error> {
        let wire_request = embeddings::build_request(&request);
        let body = merge_provider_options(&wire_request, &request.provider_options)?;

        let http_response = self
            .client
            .post(format!("{}/embeddings", self.base_url))
            .bearer_auth(&self.api_key)
            .json(&body)
            .send()
            .await?;

        let status = http_response.status();
        let headers = http_response.headers().clone();
        let body_text = http_response.text().await?;

        if !status.is_success() {
            let mapper = ErrorMapper {
                provider: self.name(),
            };
            return Err(mapper.map_error_response(status, &headers, &body_text));
        }

        let wire_response: embeddings::ApiResponse =
            serde_json::from_str(&body_text).map_err(|e| Error::Decode {
                provider: self.name().to_string(),
                message: e.to_string(),
            })?;

        Ok(embeddings::parse_response(wire_response))
    }

    async fn images(&self, request: ImagesRequest) -> Result<ImagesResponse, Error> {
        let wire_request = images::build_request(&request);
        let body = merge_provider_options(&wire_request, &request.provider_options)?;

        let http_response = self
            .client
            .post(format!("{}/images/generations", self.base_url))
            .bearer_auth(&self.api_key)
            .json(&body)
            .send()
            .await?;

        let status = http_response.status();
        let headers = http_response.headers().clone();
        let body_text = http_response.text().await?;

        if !status.is_success() {
            let mapper = ErrorMapper {
                provider: self.name(),
            };
            return Err(mapper.map_error_response(status, &headers, &body_text));
        }

        let wire_response: images::ApiResponse =
            serde_json::from_str(&body_text).map_err(|e| Error::Decode {
                provider: self.name().to_string(),
                message: e.to_string(),
            })?;

        images::parse_response(wire_response, self.name())
    }

    async fn text_to_speech(&self, request: TextToSpeechRequest) -> Result<AudioResponse, Error> {
        let wire_request = audio::build_speech_request(&request);
        let body = merge_provider_options(&wire_request, &request.provider_options)?;

        let http_response = self
            .client
            .post(format!("{}/audio/speech", self.base_url))
            .bearer_auth(&self.api_key)
            .json(&body)
            .send()
            .await?;

        let status = http_response.status();
        if !status.is_success() {
            let headers = http_response.headers().clone();
            let body_text = http_response.text().await?;
            let mapper = ErrorMapper {
                provider: self.name(),
            };
            return Err(mapper.map_error_response(status, &headers, &body_text));
        }

        let mime_type = http_response
            .headers()
            .get(reqwest::header::CONTENT_TYPE)
            .and_then(|value| value.to_str().ok())
            .map(|value| value.to_string());
        let data = http_response.bytes().await?;

        Ok(audio::parse_speech_response(data.to_vec(), mime_type))
    }

    async fn speech_to_text(
        &self,
        request: SpeechToTextRequest,
    ) -> Result<TranscriptionResponse, Error> {
        let part = reqwest::multipart::Part::bytes(request.audio.data.clone())
            .file_name(request.audio.filename.clone())
            .mime_str(&request.audio.mime_type)?;
        let form = reqwest::multipart::Form::new()
            .text("model", request.model.clone())
            .part("file", part);
        let form = merge_provider_options_into_form(form, &request.provider_options);

        let http_response = self
            .client
            .post(format!("{}/audio/transcriptions", self.base_url))
            .bearer_auth(&self.api_key)
            .multipart(form)
            .send()
            .await?;

        let status = http_response.status();
        let headers = http_response.headers().clone();
        let body_text = http_response.text().await?;

        if !status.is_success() {
            let mapper = ErrorMapper {
                provider: self.name(),
            };
            return Err(mapper.map_error_response(status, &headers, &body_text));
        }

        let wire_response: audio::TranscriptionApiResponse = serde_json::from_str(&body_text)
            .map_err(|e| Error::Decode {
                provider: self.name().to_string(),
                message: e.to_string(),
            })?;

        Ok(audio::parse_transcription_response(wire_response))
    }
}

/// Accumulates one tool call's `id`/`name`/`arguments` across however many
/// stream chunks it's spread over, keyed by `index` since a single chunk's
/// `tool_calls` array may contain deltas for several tool calls in progress at
/// once.
#[derive(Default)]
struct ToolCallAccumulator {
    id: Option<String>,
    name: Option<String>,
    arguments: String,
}