Skip to main content

llm_tool/types/
prompt.rs

1use alloc::string::String;
2
3use serde::{Deserialize, Serialize};
4
5/// Describes a prompt template available in the registry.
6#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
7pub struct PromptDefinition {
8    /// Prompt name.
9    pub name: String,
10    /// Human-readable description.
11    #[serde(default, skip_serializing_if = "String::is_empty")]
12    pub description: String,
13    /// Arguments accepted by this prompt.
14    #[serde(default, skip_serializing_if = "alloc::vec::Vec::is_empty")]
15    pub arguments: alloc::vec::Vec<PromptArgumentDefinition>,
16}
17
18/// An argument accepted by a prompt template.
19#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
20pub struct PromptArgumentDefinition {
21    /// Argument name.
22    pub name: String,
23    /// Argument description.
24    #[serde(default, skip_serializing_if = "String::is_empty")]
25    pub description: String,
26    /// Whether this argument is required.
27    #[serde(default)]
28    pub required: bool,
29}
30
31/// The role of a message in a prompt output (`user`, `assistant`, or `system`).
32#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
33#[serde(rename_all = "lowercase")]
34pub enum PromptRole {
35    /// User message.
36    User,
37    /// Assistant message.
38    Assistant,
39    /// System message.
40    System,
41}
42
43impl PromptRole {
44    /// The string slice representation of the role (`"user"`, `"assistant"`, or `"system"`).
45    #[must_use]
46    pub const fn as_str(self) -> &'static str {
47        match self {
48            Self::User => "user",
49            Self::Assistant => "assistant",
50            Self::System => "system",
51        }
52    }
53}
54
55impl core::fmt::Display for PromptRole {
56    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
57        f.write_str(self.as_str())
58    }
59}
60
61/// A rendered message inside a prompt output.
62#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
63pub struct PromptOutputMessage {
64    /// Message role.
65    pub role: PromptRole,
66    /// Text content.
67    pub content: String,
68}
69
70impl PromptOutputMessage {
71    /// Create a user role message.
72    pub fn user(content: impl Into<String>) -> Self {
73        Self {
74            role: PromptRole::User,
75            content: content.into(),
76        }
77    }
78
79    /// Create an assistant role message.
80    pub fn assistant(content: impl Into<String>) -> Self {
81        Self {
82            role: PromptRole::Assistant,
83            content: content.into(),
84        }
85    }
86
87    /// Create a system role message.
88    pub fn system(content: impl Into<String>) -> Self {
89        Self {
90            role: PromptRole::System,
91            content: content.into(),
92        }
93    }
94}
95
96/// The output returned by rendering a prompt template.
97#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
98pub struct PromptOutput {
99    /// Rendered messages.
100    pub messages: alloc::vec::Vec<PromptOutputMessage>,
101}
102
103impl PromptOutput {
104    /// Create a new prompt output with a single user message.
105    pub fn user(content: impl Into<String>) -> Self {
106        Self {
107            messages: alloc::vec![PromptOutputMessage::user(content)],
108        }
109    }
110
111    /// Create a new prompt output with a single assistant message.
112    pub fn assistant(content: impl Into<String>) -> Self {
113        Self {
114            messages: alloc::vec![PromptOutputMessage::assistant(content)],
115        }
116    }
117
118    /// Create a new prompt output with a single system message.
119    pub fn system(content: impl Into<String>) -> Self {
120        Self {
121            messages: alloc::vec![PromptOutputMessage::system(content)],
122        }
123    }
124}
125
126impl From<String> for PromptOutput {
127    fn from(content: String) -> Self {
128        Self::user(content)
129    }
130}
131
132impl From<&str> for PromptOutput {
133    fn from(content: &str) -> Self {
134        Self::user(content)
135    }
136}