langchain-rust 7.0.0

LangChain for Rust, the easiest way to write LLM-based programs in Rust
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
use serde_json::Value;

use crate::schemas::{
    messages::{ImageContent, Message},
    prompt::PromptValue,
};

use super::{
    FormatPrompter, MessageFormatter, PromptArgs, PromptError, PromptFromatter, PromptTemplate,
};

/// Struct `HumanMessagePromptTemplate` defines a template for creating human (user) messages.
/// `PromptTemplate` is used to generate the message template.
///
/// # Usage
/// ```rust,ignore
/// let human_message_prompt = HumanMessagePromptTemplate::new(template_fstring!(
///    "User says: {content}",
///    "content",
/// ));
/// ```
///
/// # Images
///
/// [`with_images`](Self::with_images) names an input variable that holds images to
/// attach to the message, so a chain or agent can take them like any other input:
///
/// ```rust,ignore
/// let prompt = HumanMessagePromptTemplate::new(template_fstring!("{input}", "input"))
///     .with_images("images");
/// chain.invoke(prompt_args! {
///     "input" => "What is in this picture?",
///     "images" => ["https://example.com/cat.png"],
/// }).await?;
/// ```
#[derive(Clone)]
pub struct HumanMessagePromptTemplate {
    prompt: PromptTemplate,
    images_key: Option<String>,
}

impl HumanMessagePromptTemplate {
    pub fn new(prompt: PromptTemplate) -> Self {
        Self {
            prompt,
            images_key: None,
        }
    }

    /// Attaches the images found in the `key` input variable to the message.
    ///
    /// The variable can be one image or a list of them. An image is either a string
    /// (an `http(s)` URL, a `data:` URL or bare base64 data) or an
    /// [`ImageContent`] object (`{"image_url": "...", "detail": "low"}`).
    /// A missing variable, `null` or an empty list means no images.
    pub fn with_images<S: Into<String>>(mut self, key: S) -> Self {
        self.images_key = Some(key.into());
        self
    }
}

/// Reads the images held in an input variable (see
/// [`HumanMessagePromptTemplate::with_images`]).
pub(crate) fn images_from_value(value: &Value) -> Result<Vec<ImageContent>, PromptError> {
    fn one(value: &Value) -> Result<ImageContent, PromptError> {
        match value {
            Value::String(url) if !url.trim().is_empty() => Ok(ImageContent::from(url)),
            Value::Object(_) => Ok(serde_json::from_value(value.clone())?),
            other => Err(PromptError::OtherError(format!(
                "expected an image URL, base64 string or {{\"image_url\": ...}} object, got {other}"
            ))),
        }
    }
    match value {
        Value::Null => Ok(Vec::new()),
        Value::Array(items) => items.iter().map(one).collect(),
        other => Ok(vec![one(other)?]),
    }
}

/// Input variable the built-in chain and agent prompts read images from.
pub const DEFAULT_IMAGES_KEY: &str = "images";

/// The human turn of a chain or agent call, as it is saved to memory: the text of
/// `input_key` plus any images in `images_key`.
pub(crate) fn human_message_from_inputs(
    inputs: &PromptArgs,
    input_key: &str,
    images_key: &str,
) -> Result<Message, PromptError> {
    let input = inputs
        .get(input_key)
        .ok_or_else(|| PromptError::MissingVariable(input_key.to_string()))?;
    let message = Message::new_human_message(crate::schemas::messages::value_to_text(input));
    let images = match inputs.get(images_key) {
        Some(value) => images_from_value(value)?,
        None => Vec::new(),
    };
    Ok(if images.is_empty() {
        message
    } else {
        message.with_images(images)
    })
}

impl MessageFormatter for HumanMessagePromptTemplate {
    fn format_messages(&self, input_variables: PromptArgs) -> Result<Vec<Message>, PromptError> {
        let images = match &self.images_key {
            Some(key) => match input_variables.get(key) {
                Some(value) => images_from_value(value)?,
                None => Vec::new(),
            },
            None => Vec::new(),
        };
        let mut message = Message::new_human_message(self.prompt.format(input_variables)?);
        if !images.is_empty() {
            message = message.with_images(images);
        }
        tracing::debug!("message: {:?}", message);
        Ok(vec![message])
    }
    fn input_variables(&self) -> Vec<String> {
        self.prompt.variables().clone()
    }
}

impl FormatPrompter for HumanMessagePromptTemplate {
    fn format_prompt(&self, input_variables: PromptArgs) -> Result<PromptValue, PromptError> {
        let messages = self.format_messages(input_variables)?;
        Ok(PromptValue::from_messages(messages))
    }
    fn get_input_variables(&self) -> Vec<String> {
        self.input_variables()
    }
}

/// Struct `SystemMessagePromptTemplate` defines a template for creating system-level messages.
/// `PromptTemplate` is used to generate the message template.
///
/// # Usage
/// ```rust,ignore
/// let system_message_prompt = SystemMessagePromptTemplate::new(template_fstring!(
///    "System alert: {alert_type} {alert_detail}",
///    "alert_type",
///    "alert_detail"
/// ));
/// ```
#[derive(Clone)]
pub struct SystemMessagePromptTemplate {
    prompt: PromptTemplate,
}

impl SystemMessagePromptTemplate {
    pub fn new(prompt: PromptTemplate) -> Self {
        Self { prompt }
    }
}

impl FormatPrompter for SystemMessagePromptTemplate {
    fn format_prompt(&self, input_variables: PromptArgs) -> Result<PromptValue, PromptError> {
        let messages = self.format_messages(input_variables)?;
        Ok(PromptValue::from_messages(messages))
    }
    fn get_input_variables(&self) -> Vec<String> {
        self.input_variables()
    }
}

impl MessageFormatter for SystemMessagePromptTemplate {
    fn format_messages(&self, input_variables: PromptArgs) -> Result<Vec<Message>, PromptError> {
        let message = Message::new_system_message(self.prompt.format(input_variables)?);
        tracing::debug!("message: {:?}", message);
        Ok(vec![message])
    }
    fn input_variables(&self) -> Vec<String> {
        self.prompt.variables().clone()
    }
}

/// Struct `AIMessagePromptTemplate` defines a template for creating AI (assistant) messages.
/// `PromptTemplate` is used to generate the message template.
///
/// # Usage
/// ```rust,ignore
/// let ai_message_prompt = AIMessagePromptTemplate::new(template_fstring!(
///    "AI response: {content} {additional_info}",
///    "content",
///    "additional_info"
/// ));
#[derive(Clone)]
pub struct AIMessagePromptTemplate {
    prompt: PromptTemplate,
}

impl FormatPrompter for AIMessagePromptTemplate {
    fn format_prompt(&self, input_variables: PromptArgs) -> Result<PromptValue, PromptError> {
        let messages = self.format_messages(input_variables)?;
        Ok(PromptValue::from_messages(messages))
    }
    fn get_input_variables(&self) -> Vec<String> {
        self.input_variables()
    }
}

impl MessageFormatter for AIMessagePromptTemplate {
    fn format_messages(&self, input_variables: PromptArgs) -> Result<Vec<Message>, PromptError> {
        let message = Message::new_ai_message(self.prompt.format(input_variables)?);
        tracing::debug!("message: {:?}", message);
        Ok(vec![message])
    }
    fn input_variables(&self) -> Vec<String> {
        self.prompt.variables().clone()
    }
}

impl AIMessagePromptTemplate {
    pub fn new(prompt: PromptTemplate) -> Self {
        Self { prompt }
    }
}

pub enum MessageOrTemplate {
    Message(Message),
    Template(Box<dyn MessageFormatter>),
    MessagesPlaceholder(String),
}

/// `fmt_message` is a utility macro used to create a `MessageOrTemplate::Message` variant.
///
/// # Usage
/// The macro is called with a `Message` object. For example:
/// ```rust,ignore
/// let message = Message::new_human_message("Hello World");
/// fmt_message!(message) // Returns a `MessageOrTemplate::Message` variant that wraps the `Message` object
/// ```
#[macro_export]
macro_rules! fmt_message {
    ($msg:expr) => {
        $crate::prompt::MessageOrTemplate::Message($msg)
    };
}

/// `fmt_template` is a utility macro used to create a `MessageOrTemplate::Template` variant.
///
/// # Usage
/// The macro is called with a `MessageFormatter` object, for instance `HumanMessagePromptTemplate`,
/// `SystemMessagePromptTemplate`, `AIMessagePromptTemplate` or any other implementation of `MessageFormatter`.
///
/// ```rust,ignore
/// let prompt_template = HumanMessagePromptTemplate::new(template);
/// fmt_template!(prompt_template)
/// ```
/// This returns a `MessageOrTemplate::Template` variant that wraps the `MessageFormatter` object within a Box.
#[macro_export]
macro_rules! fmt_template {
    ($template:expr) => {
        $crate::prompt::MessageOrTemplate::Template(Box::new($template))
    };
}

/// `fmt_placeholder` is a utility macro used to create a `MessageOrTemplate::MessagesPlaceholder` variant.
///
/// # Usage
/// The macro is called with a string literal or a String object:
/// ```rust,ignore
/// fmt_placeholder!("Placeholder message")
/// ```
/// This returns a `MessageOrTemplate::MessagesPlaceholder` variant that wraps the given string.
#[macro_export]
macro_rules! fmt_placeholder {
    ($placeholder:expr) => {
        $crate::prompt::MessageOrTemplate::MessagesPlaceholder($placeholder.into())
    };
}

pub struct MessageFormatterStruct {
    items: Vec<MessageOrTemplate>,
}

impl MessageFormatterStruct {
    pub fn new() -> Self {
        Self { items: Vec::new() }
    }

    pub fn add_message(&mut self, message: Message) {
        self.items.push(MessageOrTemplate::Message(message));
    }

    pub fn add_template(&mut self, template: Box<dyn MessageFormatter>) {
        self.items.push(MessageOrTemplate::Template(template));
    }

    pub fn add_messages_placeholder(&mut self, placeholder: &str) {
        self.items.push(MessageOrTemplate::MessagesPlaceholder(
            placeholder.to_string(),
        ));
    }

    fn format(&self, input_variables: PromptArgs) -> Result<Vec<Message>, PromptError> {
        let mut result: Vec<Message> = Vec::new();
        for item in &self.items {
            match item {
                MessageOrTemplate::Message(msg) => result.push(msg.clone()),
                MessageOrTemplate::Template(tmpl) => {
                    result.extend(tmpl.format_messages(input_variables.clone())?)
                }
                MessageOrTemplate::MessagesPlaceholder(placeholder) => {
                    let messages = input_variables
                        .get(placeholder)
                        .ok_or_else(|| PromptError::MissingVariable(placeholder.clone()))?;
                    result.extend(Message::messages_from_value(messages)?);
                }
            }
        }
        Ok(result)
    }
}

impl MessageFormatter for MessageFormatterStruct {
    fn format_messages(&self, input_variables: PromptArgs) -> Result<Vec<Message>, PromptError> {
        self.format(input_variables)
    }
    fn input_variables(&self) -> Vec<String> {
        let mut variables = Vec::new();
        for item in &self.items {
            match item {
                MessageOrTemplate::Message(_) => {}
                MessageOrTemplate::Template(tmpl) => {
                    variables.extend(tmpl.input_variables());
                }
                MessageOrTemplate::MessagesPlaceholder(placeholder) => {
                    variables.extend(vec![placeholder.clone()]);
                }
            }
        }
        variables
    }
}

impl FormatPrompter for MessageFormatterStruct {
    fn format_prompt(&self, input_variables: PromptArgs) -> Result<PromptValue, PromptError> {
        let messages = self.format(input_variables)?;
        Ok(PromptValue::from_messages(messages))
    }
    fn get_input_variables(&self) -> Vec<String> {
        self.input_variables()
    }
}

#[macro_export]
// A macro for creating a new MessageFormatterStruct with various types of messages.
///
///# Example
/// ```rust,ignore
/// // Create an AI message prompt template
/// let ai_message_prompt = AIMessagePromptTemplate::new(
/// template_fstring!(
///     "AI response: {content} {test}",
///     "content",
///     "test"
/// ));
///
///
/// let human_msg = Message::new_human_message("Hello from user");
///
/// // Use the `message_formatter` macro to construct the formatter.
/// let formatter = message_formatter![
///     fmt_message!(human_msg),
///     fmt_template!(ai_message_prompt),
///     fmt_placeholder!("history")
/// ];
/// ```
macro_rules! message_formatter {
($($item:expr),* $(,)?) => {{
    let mut formatter = $crate::prompt::MessageFormatterStruct::new();
    $(
        match $item {
            $crate::prompt::MessageOrTemplate::Message(msg) => formatter.add_message(msg),
            $crate::prompt::MessageOrTemplate::Template(tmpl) => formatter.add_template(tmpl),
            $crate::prompt::MessageOrTemplate::MessagesPlaceholder(placeholder) => formatter.add_messages_placeholder(&placeholder.clone()),
        }
    )*
    formatter
}};
}

#[cfg(test)]
mod tests {
    use crate::{
        message_formatter,
        prompt::{chat::AIMessagePromptTemplate, FormatPrompter, PromptError},
        prompt_args,
        schemas::messages::{Message, MessageType},
        template_fstring,
    };

    #[test]
    fn test_message_formatter_macro() {
        // Create a human message and system message
        let human_msg = Message::new_human_message("Hello from user");

        // Create an AI message prompt template
        let ai_message_prompt = AIMessagePromptTemplate::new(template_fstring!(
            "AI response: {content} {test}",
            "content",
            "test"
        ));

        // Use the `message_formatter` macro to construct the formatter
        let formatter = message_formatter![
            fmt_message!(human_msg),
            fmt_template!(ai_message_prompt),
            fmt_placeholder!("history")
        ];

        // Define input variables for the AI message template
        let input_variables = prompt_args! {
            "content" => "This is a test",
            "test" => "test2",
            "history" => vec![
                Message::new_human_message("Placeholder message 1"),
                Message::new_ai_message("Placeholder message 2"),
            ],


        };

        // Format messages
        let formatted_messages = formatter
            .format_prompt(input_variables)
            .unwrap()
            .to_chat_messages();

        // Verify the number of messages
        assert_eq!(formatted_messages.len(), 4);

        // Verify the content of each message
        assert_eq!(formatted_messages[0].content, "Hello from user");
        assert_eq!(
            formatted_messages[1].content,
            "AI response: This is a test test2"
        );
        assert_eq!(formatted_messages[2].content, "Placeholder message 1");
        assert_eq!(formatted_messages[3].content, "Placeholder message 2");
    }

    #[test]
    fn message_templates_roles_and_variables() {
        use crate::{
            prompt::{
                HumanMessagePromptTemplate, MessageFormatter, PromptError,
                SystemMessagePromptTemplate,
            },
            schemas::MessageType,
        };
        let formatter = message_formatter![
            fmt_template!(SystemMessagePromptTemplate::new(template_fstring!(
                "You are {role}",
                "role"
            ))),
            fmt_placeholder!("history"),
            fmt_template!(HumanMessagePromptTemplate::new(template_fstring!(
                "{q}", "q"
            ))),
        ];
        assert_eq!(
            formatter.input_variables(),
            vec!["role".to_string(), "history".into(), "q".into()]
        );

        let messages = formatter
            .format_messages(prompt_args! {
                "role" => "a bot", "q" => "hi", "history" => Vec::<Message>::new()
            })
            .unwrap();
        assert_eq!(messages.len(), 2);
        assert_eq!(messages[0].message_type, MessageType::SystemMessage);
        assert_eq!(messages[0].content, "You are a bot");
        assert_eq!(messages[1].message_type, MessageType::HumanMessage);

        // missing placeholder is an error, not a panic
        let err = formatter
            .format_messages(prompt_args! {"role" => "x", "q" => "y"})
            .unwrap_err();
        assert!(matches!(err, PromptError::MissingVariable(v) if v == "history"));

        // missing template variable
        let err = formatter
            .format_messages(prompt_args! {"history" => Vec::<Message>::new(), "q" => "y"})
            .unwrap_err();
        assert!(matches!(err, PromptError::MissingVariable(v) if v == "role"));

        // placeholder with wrong type
        let err = formatter
            .format_messages(prompt_args! {"role" => "x", "q" => "y", "history" => "nope"})
            .unwrap_err();
        assert!(matches!(err, PromptError::SerializationError(_)));
    }

    #[test]
    fn human_template_attaches_images_from_a_variable() {
        use crate::prompt::{HumanMessagePromptTemplate, MessageFormatter};
        use crate::schemas::messages::ImageContent;
        use serde_json::json;

        let template = HumanMessagePromptTemplate::new(template_fstring!("Q: {input}", "input"))
            .with_images("images");
        // Images are not prompt variables: the template only needs `input`.
        assert_eq!(template.input_variables(), vec!["input".to_string()]);

        let urls = |messages: &[Message]| -> Vec<String> {
            messages[0]
                .images
                .iter()
                .flatten()
                .map(|i| i.image_url.clone())
                .collect()
        };

        // A list mixing strings and ImageContent objects.
        let messages = template
            .format_messages(prompt_args! {
                "input" => "what is it?",
                "images" => json!(["https://a/cat.png", {"image_url": "iVBORw0KGgo", "detail": "low"}]),
            })
            .unwrap();
        assert_eq!(messages.len(), 1);
        assert_eq!(messages[0].content, "Q: what is it?");
        assert_eq!(urls(&messages), vec!["https://a/cat.png", "iVBORw0KGgo"]);
        assert_eq!(
            messages[0].images.as_ref().unwrap()[1],
            ImageContent::from("iVBORw0KGgo").with_detail("low")
        );

        // A single string works too.
        let messages = template
            .format_messages(prompt_args! {"input" => "x", "images" => "https://a/b.png"})
            .unwrap();
        assert_eq!(urls(&messages), vec!["https://a/b.png"]);

        // Missing, null or empty: a plain text message (images stays None).
        for args in [
            prompt_args! {"input" => "x"},
            prompt_args! {"input" => "x", "images" => json!(null)},
            prompt_args! {"input" => "x", "images" => json!([])},
        ] {
            assert_eq!(template.format_messages(args).unwrap()[0].images, None);
        }

        // Without with_images the variable is ignored.
        let plain = HumanMessagePromptTemplate::new(template_fstring!("{input}", "input"));
        let messages = plain
            .format_messages(prompt_args! {"input" => "x", "images" => "https://a/b.png"})
            .unwrap();
        assert_eq!(messages[0].images, None);

        // Bad values are errors, not silently dropped.
        for bad in [
            json!(42),
            json!([""]),
            json!([{"detail": "low"}]),
            json!([[]]),
        ] {
            let err = template
                .format_messages(prompt_args! {"input" => "x", "images" => bad.clone()})
                .unwrap_err();
            assert!(
                matches!(
                    err,
                    PromptError::OtherError(_) | PromptError::SerializationError(_)
                ),
                "{bad}: {err:?}"
            );
        }
    }

    #[test]
    fn human_message_from_inputs_reads_text_and_images() {
        use crate::prompt::human_message_from_inputs;
        use serde_json::json;

        let m = human_message_from_inputs(
            &prompt_args! {"input" => "hi", "images" => json!(["https://a/b.png"])},
            "input",
            "images",
        )
        .unwrap();
        assert_eq!(m.message_type, MessageType::HumanMessage);
        assert_eq!(m.content, "hi");
        assert_eq!(m.images.unwrap()[0].image_url, "https://a/b.png");

        // Non-string input is rendered without JSON quotes for strings only.
        let m = human_message_from_inputs(
            &prompt_args! {"input" => json!({"a": 1})},
            "input",
            "images",
        )
        .unwrap();
        assert_eq!(m.content, r#"{"a":1}"#);
        assert_eq!(m.images, None);

        let err = human_message_from_inputs(&prompt_args! {}, "input", "images").unwrap_err();
        assert!(matches!(err, PromptError::MissingVariable(v) if v == "input"));
    }
}