#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Prompt<'a> {
Document(Option<&'a str>),
SearchQuery,
QuestionAnswering,
FactChecking,
CodeRetrieval,
Classification,
Clustering,
SentenceSimilarity,
Bare,
Custom(&'a str),
}
impl<'a> Prompt<'a> {
pub const NAMED: [(&'static str, &'static str); 8] = [
("document", "title: none | text: "),
("search_query", "task: search result | query: "),
("question_answering", "task: question answering | query: "),
("fact_checking", "task: fact checking | query: "),
("code_retrieval", "task: code retrieval | query: "),
("classification", "task: classification | query: "),
("clustering", "task: clustering | query: "),
("sentence_similarity", "task: sentence similarity | query: "),
];
#[inline]
pub const fn config_key(&self) -> Option<&'static str> {
match self.named_index() {
Some(index) => Some(Self::NAMED[index].0),
None => None,
}
}
pub(crate) const fn pieces(&self) -> [&'a str; 3] {
match (self, self.named_index()) {
(_, Some(index)) => [Self::NAMED[index].1, "", ""],
(Self::Document(Some(title)), None) => ["title: ", title, " | text: "],
(Self::Custom(prefix), None) => [prefix, "", ""],
_ => ["", "", ""],
}
}
const fn named_index(&self) -> Option<usize> {
match self {
Self::Document(None) => Some(0),
Self::SearchQuery => Some(1),
Self::QuestionAnswering => Some(2),
Self::FactChecking => Some(3),
Self::CodeRetrieval => Some(4),
Self::Classification => Some(5),
Self::Clustering => Some(6),
Self::SentenceSimilarity => Some(7),
Self::Document(Some(_)) | Self::Bare | Self::Custom(_) => None,
}
}
}
impl core::fmt::Display for Prompt<'_> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match (self, self.config_key()) {
(_, Some(key)) => f.write_str(key),
(Self::Document(Some(title)), None) => write!(f, "document(title={title:?})"),
(Self::Custom(prefix), None) => write!(f, "custom({prefix:?})"),
_ => f.write_str("bare"),
}
}
}
#[cfg(test)]
mod tests;