1use smart_default::SmartDefault as Default;
2
3use crate::Message;
4
5use crate::prelude::*;
6
7#[derive(Debug, Default)]
9pub struct ChatPrompt
10{
11 pub messages: Vec<Message>,
13 #[default(Format::Text)]
15 pub format: Format,
16}
17
18impl ChatPrompt
19{
20 pub fn new() -> Self
22 {
23 Self {
24 messages: Default::default(),
25 format: Format::Text,
26 }
27 }
28 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 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 pub fn user(self, content: impl Into<String>) -> Self
48 {
49 self.message(Role::User, content)
50 }
51 pub fn system(self, content: impl Into<String>) -> Self
53 {
54 self.message(Role::System, content)
55 }
56 pub fn system_opt(self, content: Option<String>) -> Self
58 {
59 self.message_opt(Role::System, content)
60 }
61 pub fn assistant(self, content: impl Into<String>) -> Self
63 {
64 self.message(Role::Assistant, content)
65 }
66 pub fn assistant_opt(self, content: Option<String>) -> Self
68 {
69 self.message_opt(Role::Assistant, content)
70 }
71 pub fn format(mut self, format: impl Into<Format>) -> Self
73 {
74 self.format = format.into();
75 self
76 }
77}
78
79#[derive(Debug)]
81pub struct GenerationPrompt
82{
83 pub user: String,
85 pub system: Option<String>,
87 pub assistant: Option<String>,
89 pub format: Format,
91 #[cfg(feature = "image")]
93 pub image: Option<kproc_values::Image>,
94}
95
96impl GenerationPrompt
97{
98 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 pub fn system(mut self, content: impl Into<String>) -> Self
112 {
113 self.system = Some(content.into());
114 self
115 }
116 pub fn assistant(mut self, content: impl Into<String>) -> Self
118 {
119 self.assistant = Some(content.into());
120 self
121 }
122 pub fn format(mut self, format: impl Into<Format>) -> Self
124 {
125 self.format = format.into();
126 self
127 }
128 #[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}