5153: Return docid in case of errors while rendering the document template r=Kerollmops a=dureuill

Improves error message:

Before: 

```
ERROR index_scheduler: Batch failed Index `mieli`: user error: missing field in document: liquid: Unknown index
  with:
    variable=doc
    requested index=title
    available indexes=by, id, kids, parent, text, time, type
```

After:

```
ERROR index_scheduler: Batch failed Index `mieli`: user error: missing field in document `11345147`: liquid: Unknown index
  with:
    variable=doc
    requested index=title
    available indexes=by, id, kids, parent, text, time, type
```

Co-authored-by: Louis Dureuil <louis@meilisearch.com>
This commit is contained in:
meili-bors[bot] 2024-12-11 15:01:40 +00:00 committed by GitHub
commit 5bc6391700
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 25 additions and 3 deletions

View File

@ -38,6 +38,16 @@ pub struct RenderPromptError {
pub fault: FaultSource, pub fault: FaultSource,
} }
impl RenderPromptError { impl RenderPromptError {
pub(crate) fn missing_context_with_external_docid(
external_docid: String,
inner: liquid::Error,
) -> RenderPromptError {
Self {
kind: RenderPromptErrorKind::MissingContextWithExternalDocid(external_docid, inner),
fault: FaultSource::User,
}
}
pub(crate) fn missing_context(inner: liquid::Error) -> RenderPromptError { pub(crate) fn missing_context(inner: liquid::Error) -> RenderPromptError {
Self { kind: RenderPromptErrorKind::MissingContext(inner), fault: FaultSource::User } Self { kind: RenderPromptErrorKind::MissingContext(inner), fault: FaultSource::User }
} }
@ -47,6 +57,8 @@ impl RenderPromptError {
pub enum RenderPromptErrorKind { pub enum RenderPromptErrorKind {
#[error("missing field in document: {0}")] #[error("missing field in document: {0}")]
MissingContext(liquid::Error), MissingContext(liquid::Error),
#[error("missing field in document `{0}`: {1}")]
MissingContextWithExternalDocid(String, liquid::Error),
} }
impl From<RenderPromptError> for crate::Error { impl From<RenderPromptError> for crate::Error {

View File

@ -119,6 +119,7 @@ impl Prompt {
'doc: 'a, // lifetime of the allocator, will live for an entire chunk of documents 'doc: 'a, // lifetime of the allocator, will live for an entire chunk of documents
>( >(
&self, &self,
external_docid: &str,
document: impl crate::update::new::document::Document<'a> + Debug, document: impl crate::update::new::document::Document<'a> + Debug,
field_id_map: &RefCell<GlobalFieldsIdsMap>, field_id_map: &RefCell<GlobalFieldsIdsMap>,
doc_alloc: &'doc Bump, doc_alloc: &'doc Bump,
@ -130,9 +131,12 @@ impl Prompt {
self.max_bytes.unwrap_or_else(default_max_bytes).get(), self.max_bytes.unwrap_or_else(default_max_bytes).get(),
doc_alloc, doc_alloc,
); );
self.template self.template.render_to(&mut rendered, &context).map_err(|liquid_error| {
.render_to(&mut rendered, &context) RenderPromptError::missing_context_with_external_docid(
.map_err(RenderPromptError::missing_context)?; external_docid.to_owned(),
liquid_error,
)
})?;
Ok(std::str::from_utf8(rendered.into_bump_slice()) Ok(std::str::from_utf8(rendered.into_bump_slice())
.expect("render can only write UTF-8 because all inputs and processing preserve utf-8")) .expect("render can only write UTF-8 because all inputs and processing preserve utf-8"))
} }

View File

@ -130,6 +130,7 @@ impl<'a, 'b, 'extractor> Extractor<'extractor> for EmbeddingExtractor<'a, 'b> {
); );
} else if new_vectors.regenerate { } else if new_vectors.regenerate {
let new_rendered = prompt.render_document( let new_rendered = prompt.render_document(
update.external_document_id(),
update.current( update.current(
&context.rtxn, &context.rtxn,
context.index, context.index,
@ -139,6 +140,7 @@ impl<'a, 'b, 'extractor> Extractor<'extractor> for EmbeddingExtractor<'a, 'b> {
&context.doc_alloc, &context.doc_alloc,
)?; )?;
let old_rendered = prompt.render_document( let old_rendered = prompt.render_document(
update.external_document_id(),
update.merged( update.merged(
&context.rtxn, &context.rtxn,
context.index, context.index,
@ -158,6 +160,7 @@ impl<'a, 'b, 'extractor> Extractor<'extractor> for EmbeddingExtractor<'a, 'b> {
} }
} else if old_vectors.regenerate { } else if old_vectors.regenerate {
let old_rendered = prompt.render_document( let old_rendered = prompt.render_document(
update.external_document_id(),
update.current( update.current(
&context.rtxn, &context.rtxn,
context.index, context.index,
@ -167,6 +170,7 @@ impl<'a, 'b, 'extractor> Extractor<'extractor> for EmbeddingExtractor<'a, 'b> {
&context.doc_alloc, &context.doc_alloc,
)?; )?;
let new_rendered = prompt.render_document( let new_rendered = prompt.render_document(
update.external_document_id(),
update.merged( update.merged(
&context.rtxn, &context.rtxn,
context.index, context.index,
@ -216,6 +220,7 @@ impl<'a, 'b, 'extractor> Extractor<'extractor> for EmbeddingExtractor<'a, 'b> {
); );
} else if new_vectors.regenerate { } else if new_vectors.regenerate {
let rendered = prompt.render_document( let rendered = prompt.render_document(
insertion.external_document_id(),
insertion.inserted(), insertion.inserted(),
context.new_fields_ids_map, context.new_fields_ids_map,
&context.doc_alloc, &context.doc_alloc,
@ -229,6 +234,7 @@ impl<'a, 'b, 'extractor> Extractor<'extractor> for EmbeddingExtractor<'a, 'b> {
} }
} else { } else {
let rendered = prompt.render_document( let rendered = prompt.render_document(
insertion.external_document_id(),
insertion.inserted(), insertion.inserted(),
context.new_fields_ids_map, context.new_fields_ids_map,
&context.doc_alloc, &context.doc_alloc,