mirror of
https://github.com/meilisearch/meilisearch.git
synced 2024-11-23 02:27:40 +08:00
Remove prompt strategy and fallback
This commit is contained in:
parent
806e5b6899
commit
b8e4709dfa
@ -16,20 +16,16 @@ use crate::FieldsIdsMap;
|
|||||||
pub struct Prompt {
|
pub struct Prompt {
|
||||||
template: liquid::Template,
|
template: liquid::Template,
|
||||||
template_text: String,
|
template_text: String,
|
||||||
strategy: PromptFallbackStrategy,
|
|
||||||
fallback: String,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
|
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
|
||||||
pub struct PromptData {
|
pub struct PromptData {
|
||||||
pub template: String,
|
pub template: String,
|
||||||
pub strategy: PromptFallbackStrategy,
|
|
||||||
pub fallback: String,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<Prompt> for PromptData {
|
impl From<Prompt> for PromptData {
|
||||||
fn from(value: Prompt) -> Self {
|
fn from(value: Prompt) -> Self {
|
||||||
Self { template: value.template_text, strategy: value.strategy, fallback: value.fallback }
|
Self { template: value.template_text }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -37,19 +33,14 @@ impl TryFrom<PromptData> for Prompt {
|
|||||||
type Error = NewPromptError;
|
type Error = NewPromptError;
|
||||||
|
|
||||||
fn try_from(value: PromptData) -> Result<Self, Self::Error> {
|
fn try_from(value: PromptData) -> Result<Self, Self::Error> {
|
||||||
Prompt::new(value.template, Some(value.strategy), Some(value.fallback))
|
Prompt::new(value.template)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Clone for Prompt {
|
impl Clone for Prompt {
|
||||||
fn clone(&self) -> Self {
|
fn clone(&self) -> Self {
|
||||||
let template_text = self.template_text.clone();
|
let template_text = self.template_text.clone();
|
||||||
Self {
|
Self { template: new_template(&template_text).unwrap(), template_text }
|
||||||
template: new_template(&template_text).unwrap(),
|
|
||||||
template_text,
|
|
||||||
strategy: self.strategy,
|
|
||||||
fallback: self.fallback.clone(),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -67,37 +58,20 @@ fn default_template_text() -> &'static str {
|
|||||||
{% endfor %}"
|
{% endfor %}"
|
||||||
}
|
}
|
||||||
|
|
||||||
fn default_fallback() -> &'static str {
|
|
||||||
"<MISSING>"
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Default for Prompt {
|
impl Default for Prompt {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self { template: default_template(), template_text: default_template_text().into() }
|
||||||
template: default_template(),
|
|
||||||
template_text: default_template_text().into(),
|
|
||||||
strategy: Default::default(),
|
|
||||||
fallback: default_fallback().into(),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for PromptData {
|
impl Default for PromptData {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self { template: default_template_text().into() }
|
||||||
template: default_template_text().into(),
|
|
||||||
strategy: Default::default(),
|
|
||||||
fallback: default_fallback().into(),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Prompt {
|
impl Prompt {
|
||||||
pub fn new(
|
pub fn new(template: String) -> Result<Self, NewPromptError> {
|
||||||
template: String,
|
|
||||||
strategy: Option<PromptFallbackStrategy>,
|
|
||||||
fallback: Option<String>,
|
|
||||||
) -> Result<Self, NewPromptError> {
|
|
||||||
let this = Self {
|
let this = Self {
|
||||||
template: liquid::ParserBuilder::with_stdlib()
|
template: liquid::ParserBuilder::with_stdlib()
|
||||||
.build()
|
.build()
|
||||||
@ -105,8 +79,6 @@ impl Prompt {
|
|||||||
.parse(&template)
|
.parse(&template)
|
||||||
.map_err(NewPromptError::cannot_parse_template)?,
|
.map_err(NewPromptError::cannot_parse_template)?,
|
||||||
template_text: template,
|
template_text: template,
|
||||||
strategy: strategy.unwrap_or_default(),
|
|
||||||
fallback: fallback.unwrap_or_default(),
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// render template with special object that's OK with `doc.*` and `fields.*`
|
// render template with special object that's OK with `doc.*` and `fields.*`
|
||||||
@ -130,18 +102,6 @@ impl Prompt {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(
|
|
||||||
Debug, Default, Clone, PartialEq, Eq, Copy, serde::Serialize, serde::Deserialize, deserr::Deserr,
|
|
||||||
)]
|
|
||||||
#[serde(deny_unknown_fields, rename_all = "camelCase")]
|
|
||||||
#[deserr(rename_all = camelCase, deny_unknown_fields)]
|
|
||||||
pub enum PromptFallbackStrategy {
|
|
||||||
Fallback,
|
|
||||||
Skip,
|
|
||||||
#[default]
|
|
||||||
Error,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use super::Prompt;
|
use super::Prompt;
|
||||||
@ -156,18 +116,18 @@ mod test {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn empty_template() {
|
fn empty_template() {
|
||||||
Prompt::new("".into(), None, None).unwrap();
|
Prompt::new("".into()).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn template_ok() {
|
fn template_ok() {
|
||||||
Prompt::new("{{doc.title}}: {{doc.overview}}".into(), None, None).unwrap();
|
Prompt::new("{{doc.title}}: {{doc.overview}}".into()).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn template_syntax() {
|
fn template_syntax() {
|
||||||
assert!(matches!(
|
assert!(matches!(
|
||||||
Prompt::new("{{doc.title: {{doc.overview}}".into(), None, None),
|
Prompt::new("{{doc.title: {{doc.overview}}".into()),
|
||||||
Err(NewPromptError {
|
Err(NewPromptError {
|
||||||
kind: NewPromptErrorKind::CannotParseTemplate(_),
|
kind: NewPromptErrorKind::CannotParseTemplate(_),
|
||||||
fault: FaultSource::User
|
fault: FaultSource::User
|
||||||
@ -178,7 +138,7 @@ mod test {
|
|||||||
#[test]
|
#[test]
|
||||||
fn template_missing_doc() {
|
fn template_missing_doc() {
|
||||||
assert!(matches!(
|
assert!(matches!(
|
||||||
Prompt::new("{{title}}: {{overview}}".into(), None, None),
|
Prompt::new("{{title}}: {{overview}}".into()),
|
||||||
Err(NewPromptError {
|
Err(NewPromptError {
|
||||||
kind: NewPromptErrorKind::InvalidFieldsInTemplate(_),
|
kind: NewPromptErrorKind::InvalidFieldsInTemplate(_),
|
||||||
fault: FaultSource::User
|
fault: FaultSource::User
|
||||||
@ -188,21 +148,17 @@ mod test {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn template_nested_doc() {
|
fn template_nested_doc() {
|
||||||
Prompt::new("{{doc.actor.firstName}}: {{doc.actor.lastName}}".into(), None, None).unwrap();
|
Prompt::new("{{doc.actor.firstName}}: {{doc.actor.lastName}}".into()).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn template_fields() {
|
fn template_fields() {
|
||||||
Prompt::new("{% for field in fields %}{{field}}{% endfor %}".into(), None, None).unwrap();
|
Prompt::new("{% for field in fields %}{{field}}{% endfor %}".into()).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn template_fields_ok() {
|
fn template_fields_ok() {
|
||||||
Prompt::new(
|
Prompt::new("{% for field in fields %}{{field.name}}: {{field.value}}{% endfor %}".into())
|
||||||
"{% for field in fields %}{{field.name}}: {{field.value}}{% endfor %}".into(),
|
|
||||||
None,
|
|
||||||
None,
|
|
||||||
)
|
|
||||||
.unwrap();
|
.unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -210,7 +166,7 @@ mod test {
|
|||||||
fn template_fields_invalid() {
|
fn template_fields_invalid() {
|
||||||
assert!(matches!(
|
assert!(matches!(
|
||||||
// intentionally garbled field
|
// intentionally garbled field
|
||||||
Prompt::new("{% for field in fields %}{{field.vaelu}} {% endfor %}".into(), None, None),
|
Prompt::new("{% for field in fields %}{{field.vaelu}} {% endfor %}".into()),
|
||||||
Err(NewPromptError {
|
Err(NewPromptError {
|
||||||
kind: NewPromptErrorKind::InvalidFieldsInTemplate(_),
|
kind: NewPromptErrorKind::InvalidFieldsInTemplate(_),
|
||||||
fault: FaultSource::User
|
fault: FaultSource::User
|
||||||
|
@ -1073,11 +1073,10 @@ fn validate_prompt(
|
|||||||
match new {
|
match new {
|
||||||
Setting::Set(EmbeddingSettings {
|
Setting::Set(EmbeddingSettings {
|
||||||
embedder_options,
|
embedder_options,
|
||||||
document_template:
|
document_template: Setting::Set(PromptSettings { template: Setting::Set(template) }),
|
||||||
Setting::Set(PromptSettings { template: Setting::Set(template), strategy, fallback }),
|
|
||||||
}) => {
|
}) => {
|
||||||
// validate
|
// validate
|
||||||
let template = crate::prompt::Prompt::new(template, None, None)
|
let template = crate::prompt::Prompt::new(template)
|
||||||
.map(|prompt| crate::prompt::PromptData::from(prompt).template)
|
.map(|prompt| crate::prompt::PromptData::from(prompt).template)
|
||||||
.map_err(|inner| UserError::InvalidPromptForEmbeddings(name.to_owned(), inner))?;
|
.map_err(|inner| UserError::InvalidPromptForEmbeddings(name.to_owned(), inner))?;
|
||||||
|
|
||||||
@ -1085,8 +1084,6 @@ fn validate_prompt(
|
|||||||
embedder_options,
|
embedder_options,
|
||||||
document_template: Setting::Set(PromptSettings {
|
document_template: Setting::Set(PromptSettings {
|
||||||
template: Setting::Set(template),
|
template: Setting::Set(template),
|
||||||
strategy,
|
|
||||||
fallback,
|
|
||||||
}),
|
}),
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
use deserr::Deserr;
|
use deserr::Deserr;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
use crate::prompt::{PromptData, PromptFallbackStrategy};
|
use crate::prompt::PromptData;
|
||||||
use crate::update::Setting;
|
use crate::update::Setting;
|
||||||
use crate::vector::hf::WeightSource;
|
use crate::vector::hf::WeightSource;
|
||||||
use crate::vector::EmbeddingConfig;
|
use crate::vector::EmbeddingConfig;
|
||||||
@ -56,46 +56,28 @@ pub struct PromptSettings {
|
|||||||
#[serde(default, skip_serializing_if = "Setting::is_not_set")]
|
#[serde(default, skip_serializing_if = "Setting::is_not_set")]
|
||||||
#[deserr(default)]
|
#[deserr(default)]
|
||||||
pub template: Setting<String>,
|
pub template: Setting<String>,
|
||||||
#[serde(default, skip_serializing_if = "Setting::is_not_set")]
|
|
||||||
#[deserr(default)]
|
|
||||||
pub strategy: Setting<PromptFallbackStrategy>,
|
|
||||||
#[serde(default, skip_serializing_if = "Setting::is_not_set")]
|
|
||||||
#[deserr(default)]
|
|
||||||
pub fallback: Setting<String>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PromptSettings {
|
impl PromptSettings {
|
||||||
pub fn apply(&mut self, new: Self) {
|
pub fn apply(&mut self, new: Self) {
|
||||||
let PromptSettings { template, strategy, fallback } = new;
|
let PromptSettings { template } = new;
|
||||||
self.template.apply(template);
|
self.template.apply(template);
|
||||||
self.strategy.apply(strategy);
|
|
||||||
self.fallback.apply(fallback);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<PromptData> for PromptSettings {
|
impl From<PromptData> for PromptSettings {
|
||||||
fn from(value: PromptData) -> Self {
|
fn from(value: PromptData) -> Self {
|
||||||
Self {
|
Self { template: Setting::Set(value.template) }
|
||||||
template: Setting::Set(value.template),
|
|
||||||
strategy: Setting::Set(value.strategy),
|
|
||||||
fallback: Setting::Set(value.fallback),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<PromptSettings> for PromptData {
|
impl From<PromptSettings> for PromptData {
|
||||||
fn from(value: PromptSettings) -> Self {
|
fn from(value: PromptSettings) -> Self {
|
||||||
let mut this = PromptData::default();
|
let mut this = PromptData::default();
|
||||||
let PromptSettings { template, strategy, fallback } = value;
|
let PromptSettings { template } = value;
|
||||||
if let Some(template) = template.set() {
|
if let Some(template) = template.set() {
|
||||||
this.template = template;
|
this.template = template;
|
||||||
}
|
}
|
||||||
if let Some(strategy) = strategy.set() {
|
|
||||||
this.strategy = strategy;
|
|
||||||
}
|
|
||||||
if let Some(fallback) = fallback.set() {
|
|
||||||
this.fallback = fallback;
|
|
||||||
}
|
|
||||||
this
|
this
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user