dynamo_llm/preprocessor/prompt/template/
context.rs

1// SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2// SPDX-License-Identifier: Apache-2.0
3
4use super::{ContextMixins, PromptContextMixin};
5
6use chrono::{DateTime, Utc};
7use minijinja::value::{Object, Value};
8use std::sync::Arc;
9
10impl Object for ContextMixins {
11    fn get_value(self: &Arc<Self>, field: &Value) -> Option<Value> {
12        match field.as_str()? {
13            "datetime" => self.datetime(),
14            _ => None,
15        }
16    }
17}
18
19impl ContextMixins {
20    pub fn new(allowed_mixins: &[PromptContextMixin]) -> Self {
21        ContextMixins {
22            context_mixins: allowed_mixins.iter().cloned().collect(),
23        }
24    }
25
26    /// Implements the `datetime` context mixin.
27    /// Different mixins can be implemented here for the same key.
28    /// We need to valiate that multiple mixins do not conflict with each other.
29    fn datetime(&self) -> Option<Value> {
30        if self
31            .context_mixins
32            .contains(&PromptContextMixin::Llama3DateTime)
33        {
34            let now = chrono::Utc::now();
35            Some(Value::from(llama3_datetime(now)))
36        } else {
37            None
38        }
39    }
40}
41
42fn llama3_datetime(datetime: DateTime<Utc>) -> String {
43    datetime.format("%d, %B, %Y").to_string()
44}