summaryrefslogtreecommitdiffstats
path: root/src/config/role.rs
blob: fee989e0ae71bebf357871ff93bb74b4239a6c45 (plain) (blame)
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
use super::message::{Message, MessageRole};

use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};

const TEMP_ROLE_NAME: &str = "temp";
const INPUT_PLACEHOLDER: &str = "__INPUT__";

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Role {
    /// Role name
    pub name: String,
    /// Prompt text
    pub prompt: String,
    /// What sampling temperature to use, between 0 and 2
    pub temperature: Option<f64>,
}

impl Role {
    pub fn new(prompt: &str, temperature: Option<f64>) -> Self {
        Self {
            name: TEMP_ROLE_NAME.into(),
            prompt: prompt.into(),
            temperature,
        }
    }

    pub fn is_temp(&self) -> bool {
        self.name == TEMP_ROLE_NAME
    }

    pub fn info(&self) -> Result<String> {
        let output = serde_yaml::to_string(&self)
            .with_context(|| format!("Unable to show info about role {}", &self.name))?;
        Ok(output)
    }

    pub fn embeded(&self) -> bool {
        self.prompt.contains(INPUT_PLACEHOLDER)
    }

    pub fn complete_prompt_args(&mut self, name: &str) {
        self.name = name.to_string();
        self.prompt = complete_prompt_args(&self.prompt, &self.name);
    }

    pub fn match_name(&self, name: &str) -> bool {
        if self.name.contains(':') {
            let role_name_parts: Vec<&str> = self.name.split(':').collect();
            let name_parts: Vec<&str> = name.split(':').collect();
            role_name_parts[0] == name_parts[0] && role_name_parts.len() == name_parts.len()
        } else {
            self.name == name
        }
    }

    pub fn echo_messages(&self, content: &str) -> String {
        if self.embeded() {
            merge_prompt_content(&self.prompt, content)
        } else {
            format!("{}\n{content}", self.prompt)
        }
    }

    pub fn build_messages(&self, content: &str) -> Vec<Message> {
        if self.embeded() {
            let content = merge_prompt_content(&self.prompt, content);
            vec![Message {
                role: MessageRole::User,
                content,
            }]
        } else {
            vec![
                Message {
                    role: MessageRole::System,
                    content: self.prompt.clone(),
                },
                Message {
                    role: MessageRole::User,
                    content: content.to_string(),
                },
            ]
        }
    }
}

fn merge_prompt_content(prompt: &str, content: &str) -> String {
    prompt.replace(INPUT_PLACEHOLDER, content)
}

fn complete_prompt_args(prompt: &str, name: &str) -> String {
    let mut prompt = prompt.to_string();
    for (i, arg) in name.split(':').skip(1).enumerate() {
        prompt = prompt.replace(&format!("__ARG{}__", i + 1), arg);
    }
    prompt
}

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

    #[test]
    fn test_merge_prompt_name() {
        assert_eq!(
            complete_prompt_args("convert __ARG1__", "convert:foo"),
            "convert foo"
        );
        assert_eq!(
            complete_prompt_args("convert __ARG1__ to __ARG2__", "convert:foo:bar"),
            "convert foo to bar"
        );
    }
}