kproc_llm/
prompts.rs

1use smart_default::SmartDefault as Default;
2
3use crate::Message;
4
5use crate::prelude::*;
6
7/// Prompt for a chat session
8#[derive(Debug, Default)]
9pub struct ChatPrompt
10{
11  /// Messages
12  pub messages: Vec<Message>,
13  /// Requested output format
14  #[default(Format::Text)]
15  pub format: Format,
16}
17
18impl ChatPrompt
19{
20  /// Start an empty chat prompt.
21  pub fn new() -> Self
22  {
23    Self {
24      messages: Default::default(),
25      format: Format::Text,
26    }
27  }
28  /// Create a new message, from the given role and content.
29  pub fn message(mut self, role: Role, content: impl Into<String>) -> Self
30  {
31    self.messages.push(Message {
32      role,
33      content: content.into(),
34    });
35    self
36  }
37  /// Create a new message, from the given role and content. Ignore if content is null.
38  pub fn message_opt(mut self, role: Role, content: Option<String>) -> Self
39  {
40    if let Some(content) = content
41    {
42      self.messages.push(Message { role, content });
43    }
44    self
45  }
46  /// Create a new prompt, from the given string.
47  pub fn user(self, content: impl Into<String>) -> Self
48  {
49    self.message(Role::User, content)
50  }
51  /// Set the system hint.
52  pub fn system(self, content: impl Into<String>) -> Self
53  {
54    self.message(Role::System, content)
55  }
56  /// Set the system hint.
57  pub fn system_opt(self, content: Option<String>) -> Self
58  {
59    self.message_opt(Role::System, content)
60  }
61  /// Set the system hint.
62  pub fn assistant(self, content: impl Into<String>) -> Self
63  {
64    self.message(Role::Assistant, content)
65  }
66  /// Set the system hint.
67  pub fn assistant_opt(self, content: Option<String>) -> Self
68  {
69    self.message_opt(Role::Assistant, content)
70  }
71  /// Set the result format
72  pub fn format(mut self, format: impl Into<Format>) -> Self
73  {
74    self.format = format.into();
75    self
76  }
77}
78
79/// Prompt
80#[derive(Debug)]
81pub struct GenerationPrompt
82{
83  /// Messages
84  pub user: String,
85  /// Messages
86  pub system: Option<String>,
87  /// Messages
88  pub assistant: Option<String>,
89  /// Requested output format
90  pub format: Format,
91  /// Optional prompt image (for multi modal models)
92  #[cfg(feature = "image")]
93  pub image: Option<kproc_values::Image>,
94}
95
96impl GenerationPrompt
97{
98  /// Start creation of generation prompt, with user prompt
99  pub fn prompt(user: impl Into<String>) -> Self
100  {
101    Self {
102      user: user.into(),
103      system: Default::default(),
104      assistant: Default::default(),
105      format: Format::Text,
106      #[cfg(feature = "image")]
107      image: None,
108    }
109  }
110  /// Set the system hint.
111  pub fn system(mut self, content: impl Into<String>) -> Self
112  {
113    self.system = Some(content.into());
114    self
115  }
116  /// Set the assistant hint.
117  pub fn assistant(mut self, content: impl Into<String>) -> Self
118  {
119    self.assistant = Some(content.into());
120    self
121  }
122  /// Set the result format
123  pub fn format(mut self, format: impl Into<Format>) -> Self
124  {
125    self.format = format.into();
126    self
127  }
128  /// Set the image, for use in multi-modal models
129  #[cfg(feature = "image")]
130  pub fn image(mut self, image: impl Into<kproc_values::Image>) -> Self
131  {
132    self.image = Some(image.into());
133    self
134  }
135}
136
137impl From<GenerationPrompt> for Vec<Message>
138{
139  fn from(value: GenerationPrompt) -> Self
140  {
141    let mut vec = Self::default();
142    if let Some(system) = value.system
143    {
144      vec.push(Message {
145        role: Role::System,
146        content: system,
147      });
148    }
149    if let Some(assistant) = value.assistant
150    {
151      vec.push(Message {
152        role: Role::Assistant,
153        content: assistant,
154      });
155    }
156    vec.push(Message {
157      role: Role::User,
158      content: value.user,
159    });
160    vec
161  }
162}