liteforge 0.2.4

Rust SDK for LiteForge - LLM completions via OpenAI-compatible API
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
//! Prompt templates and management.
//!
//! This module provides template-based prompt construction with
//! variable substitution and a library for managing reusable prompts.

mod template;

pub use template::{PromptTemplate, TemplateError, TemplateResult};

use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// A library for storing and retrieving prompt templates.
#[derive(Debug, Default)]
pub struct PromptLibrary {
    templates: HashMap<String, PromptTemplate>,
    categories: HashMap<String, Vec<String>>,
}

impl PromptLibrary {
    /// Create a new empty prompt library.
    pub fn new() -> Self {
        Self::default()
    }

    /// Add a template to the library.
    pub fn add(&mut self, name: impl Into<String>, template: PromptTemplate) {
        let name = name.into();
        self.templates.insert(name, template);
    }

    /// Add a template with a category.
    pub fn add_with_category(
        &mut self,
        name: impl Into<String>,
        template: PromptTemplate,
        category: impl Into<String>,
    ) {
        let name = name.into();
        let category = category.into();

        self.templates.insert(name.clone(), template);
        self.categories.entry(category).or_default().push(name);
    }

    /// Get a template by name.
    pub fn get(&self, name: &str) -> Option<&PromptTemplate> {
        self.templates.get(name)
    }

    /// Remove a template.
    pub fn remove(&mut self, name: &str) -> Option<PromptTemplate> {
        // Remove from categories too
        for templates in self.categories.values_mut() {
            templates.retain(|n| n != name);
        }
        self.templates.remove(name)
    }

    /// List all template names.
    pub fn list(&self) -> Vec<&str> {
        self.templates.keys().map(|s| s.as_str()).collect()
    }

    /// List templates in a category.
    pub fn list_by_category(&self, category: &str) -> Vec<&str> {
        self.categories
            .get(category)
            .map(|names| names.iter().map(|s| s.as_str()).collect())
            .unwrap_or_default()
    }

    /// List all categories.
    pub fn categories(&self) -> Vec<&str> {
        self.categories.keys().map(|s| s.as_str()).collect()
    }

    /// Check if a template exists.
    pub fn has(&self, name: &str) -> bool {
        self.templates.contains_key(name)
    }

    /// Get the number of templates.
    pub fn len(&self) -> usize {
        self.templates.len()
    }

    /// Check if the library is empty.
    pub fn is_empty(&self) -> bool {
        self.templates.is_empty()
    }

    /// Render a template by name with variables.
    pub fn render(
        &self,
        name: &str,
        variables: &HashMap<String, String>,
    ) -> TemplateResult<String> {
        let template = self
            .get(name)
            .ok_or_else(|| TemplateError::NotFound(name.to_string()))?;
        template.render(variables)
    }
}

/// Configuration for a prompt template.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PromptConfig {
    /// Template name.
    pub name: String,
    /// Template description.
    #[serde(default)]
    pub description: String,
    /// The template string.
    pub template: String,
    /// Default values for variables.
    #[serde(default)]
    pub defaults: HashMap<String, String>,
    /// Category for organization.
    #[serde(default)]
    pub category: Option<String>,
    /// Tags for searchability.
    #[serde(default)]
    pub tags: Vec<String>,
}

impl PromptConfig {
    /// Create a new prompt config.
    pub fn new(name: impl Into<String>, template: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            description: String::new(),
            template: template.into(),
            defaults: HashMap::new(),
            category: None,
            tags: Vec::new(),
        }
    }

    /// Set the description.
    pub fn with_description(mut self, description: impl Into<String>) -> Self {
        self.description = description.into();
        self
    }

    /// Add a default value.
    pub fn with_default(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.defaults.insert(key.into(), value.into());
        self
    }

    /// Set the category.
    pub fn with_category(mut self, category: impl Into<String>) -> Self {
        self.category = Some(category.into());
        self
    }

    /// Add a tag.
    pub fn with_tag(mut self, tag: impl Into<String>) -> Self {
        self.tags.push(tag.into());
        self
    }

    /// Convert to a PromptTemplate.
    pub fn into_template(self) -> PromptTemplate {
        let mut template = PromptTemplate::new(&self.template);
        for (key, value) in self.defaults {
            template = template.with_default(key, value);
        }
        template
    }
}

/// Builder for creating prompts with sections.
#[derive(Debug, Default)]
pub struct PromptBuilder {
    sections: Vec<(String, String)>,
    variables: HashMap<String, String>,
}

impl PromptBuilder {
    /// Create a new prompt builder.
    pub fn new() -> Self {
        Self::default()
    }

    /// Add a section to the prompt.
    pub fn section(mut self, name: impl Into<String>, content: impl Into<String>) -> Self {
        self.sections.push((name.into(), content.into()));
        self
    }

    /// Add a variable value.
    pub fn var(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.variables.insert(key.into(), value.into());
        self
    }

    /// Add multiple variable values.
    pub fn vars(
        mut self,
        vars: impl IntoIterator<Item = (impl Into<String>, impl Into<String>)>,
    ) -> Self {
        for (k, v) in vars {
            self.variables.insert(k.into(), v.into());
        }
        self
    }

    /// Build the final prompt string.
    pub fn build(self) -> String {
        let mut result = String::new();

        for (name, content) in self.sections {
            if !result.is_empty() {
                result.push_str("\n\n");
            }

            // Apply variable substitution
            let mut processed = content;
            for (key, value) in &self.variables {
                let placeholder = format!("{{{{{}}}}}", key);
                processed = processed.replace(&placeholder, value);
                // Also support simple {key} format
                let simple_placeholder = format!("{{{}}}", key);
                processed = processed.replace(&simple_placeholder, value);
            }

            // Add section header if name is not empty
            if !name.is_empty() {
                result.push_str(&format!("## {}\n\n", name));
            }
            result.push_str(&processed);
        }

        result
    }
}

/// Common prompt templates for typical use cases.
pub struct CommonPrompts;

impl CommonPrompts {
    /// Get a summarization prompt template.
    pub fn summarize() -> PromptTemplate {
        PromptTemplate::new(
            "Summarize the following text in {{style}} style:\n\n{{text}}\n\nSummary:",
        )
        .with_default("style", "concise")
    }

    /// Get a translation prompt template.
    pub fn translate() -> PromptTemplate {
        PromptTemplate::new(
            "Translate the following text to {{target_language}}:\n\n{{text}}\n\nTranslation:",
        )
    }

    /// Get a question answering prompt template.
    pub fn qa() -> PromptTemplate {
        PromptTemplate::new(
            "Context:\n{{context}}\n\nQuestion: {{question}}\n\nAnswer based only on the context provided:"
        )
    }

    /// Get a code review prompt template.
    pub fn code_review() -> PromptTemplate {
        PromptTemplate::new(
            "Review the following {{language}} code for issues, improvements, and best practices:\n\n```{{language}}\n{{code}}\n```\n\nProvide a detailed review:"
        )
        .with_default("language", "")
    }

    /// Get a text classification prompt template.
    pub fn classify() -> PromptTemplate {
        PromptTemplate::new(
            "Classify the following text into one of these categories: {{categories}}\n\nText: {{text}}\n\nCategory:"
        )
    }

    /// Get an entity extraction prompt template.
    pub fn extract_entities() -> PromptTemplate {
        PromptTemplate::new(
            "Extract all {{entity_type}} entities from the following text:\n\n{{text}}\n\nEntities (as JSON array):"
        )
        .with_default("entity_type", "named")
    }

    /// Get a rewriting prompt template.
    pub fn rewrite() -> PromptTemplate {
        PromptTemplate::new(
            "Rewrite the following text in {{tone}} tone:\n\n{{text}}\n\nRewritten:",
        )
        .with_default("tone", "professional")
    }

    /// Get a chain-of-thought reasoning prompt template.
    pub fn chain_of_thought() -> PromptTemplate {
        PromptTemplate::new("{{question}}\n\nLet's think step by step:")
    }

    /// Create a library with all common prompts.
    pub fn library() -> PromptLibrary {
        let mut lib = PromptLibrary::new();

        lib.add_with_category("summarize", Self::summarize(), "text");
        lib.add_with_category("translate", Self::translate(), "text");
        lib.add_with_category("qa", Self::qa(), "qa");
        lib.add_with_category("code_review", Self::code_review(), "code");
        lib.add_with_category("classify", Self::classify(), "classification");
        lib.add_with_category("extract_entities", Self::extract_entities(), "extraction");
        lib.add_with_category("rewrite", Self::rewrite(), "text");
        lib.add_with_category("chain_of_thought", Self::chain_of_thought(), "reasoning");

        lib
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_prompt_library() {
        let mut lib = PromptLibrary::new();

        lib.add("test", PromptTemplate::new("Hello {{name}}"));

        assert!(lib.has("test"));
        assert!(!lib.has("nonexistent"));
        assert_eq!(lib.len(), 1);
    }

    #[test]
    fn test_prompt_library_categories() {
        let mut lib = PromptLibrary::new();

        lib.add_with_category("t1", PromptTemplate::new("Template 1"), "cat1");
        lib.add_with_category("t2", PromptTemplate::new("Template 2"), "cat1");
        lib.add_with_category("t3", PromptTemplate::new("Template 3"), "cat2");

        assert_eq!(lib.list_by_category("cat1").len(), 2);
        assert_eq!(lib.list_by_category("cat2").len(), 1);
        assert_eq!(lib.categories().len(), 2);
    }

    #[test]
    fn test_prompt_library_render() {
        let mut lib = PromptLibrary::new();
        lib.add("greet", PromptTemplate::new("Hello, {{name}}!"));

        let mut vars = HashMap::new();
        vars.insert("name".to_string(), "World".to_string());

        let result = lib.render("greet", &vars).unwrap();
        assert_eq!(result, "Hello, World!");
    }

    #[test]
    fn test_prompt_config() {
        let config = PromptConfig::new("test", "Hello {{name}}")
            .with_description("A test template")
            .with_default("name", "World")
            .with_category("greetings")
            .with_tag("simple");

        assert_eq!(config.name, "test");
        assert_eq!(config.description, "A test template");
        assert_eq!(config.defaults.get("name"), Some(&"World".to_string()));
        assert_eq!(config.category, Some("greetings".to_string()));
        assert_eq!(config.tags, vec!["simple"]);
    }

    #[test]
    fn test_prompt_config_to_template() {
        let config = PromptConfig::new("test", "Hello {{name}}").with_default("name", "World");

        let template = config.into_template();
        let result = template.render(&HashMap::new()).unwrap();
        assert_eq!(result, "Hello World");
    }

    #[test]
    fn test_prompt_builder() {
        let prompt = PromptBuilder::new()
            .section("Context", "You are a helpful assistant.")
            .section("Task", "Answer the question: {{question}}")
            .var("question", "What is 2+2?")
            .build();

        assert!(prompt.contains("## Context"));
        assert!(prompt.contains("You are a helpful assistant."));
        assert!(prompt.contains("## Task"));
        assert!(prompt.contains("What is 2+2?"));
    }

    #[test]
    fn test_prompt_builder_no_header() {
        let prompt = PromptBuilder::new()
            .section("", "Just some content")
            .build();

        assert!(!prompt.contains("##"));
        assert!(prompt.contains("Just some content"));
    }

    #[test]
    fn test_common_prompts() {
        let summarize = CommonPrompts::summarize();
        let mut vars = HashMap::new();
        vars.insert("text".to_string(), "Long text here".to_string());

        let result = summarize.render(&vars).unwrap();
        assert!(result.contains("Long text here"));
        assert!(result.contains("concise")); // Default value

        let translate = CommonPrompts::translate();
        vars.insert("target_language".to_string(), "Spanish".to_string());
        let result = translate.render(&vars).unwrap();
        assert!(result.contains("Spanish"));
    }

    #[test]
    fn test_common_prompts_library() {
        let lib = CommonPrompts::library();

        assert!(lib.has("summarize"));
        assert!(lib.has("translate"));
        assert!(lib.has("qa"));
        assert!(lib.has("code_review"));
        assert!(lib.len() >= 8);
    }

    #[test]
    fn test_library_remove() {
        let mut lib = PromptLibrary::new();
        lib.add_with_category("test", PromptTemplate::new("Hello"), "cat1");

        assert!(lib.has("test"));
        assert_eq!(lib.list_by_category("cat1").len(), 1);

        lib.remove("test");

        assert!(!lib.has("test"));
        assert_eq!(lib.list_by_category("cat1").len(), 0);
    }
}