pub trait BaseDocumentCompressor: Send + Sync {
// Required method
fn compress_documents<'life0, 'life1, 'async_trait>(
&'life0 self,
documents: Vec<Document>,
query: &'life1 str,
callbacks: Option<Callbacks>,
) -> Pin<Box<dyn Future<Output = Result<Vec<Document>, Box<dyn Error + Send + Sync>>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait;
// Provided method
fn compress_documents_sync(
&self,
documents: Vec<Document>,
query: &str,
callbacks: Option<Callbacks>,
) -> Result<Vec<Document>, Box<dyn Error + Send + Sync>>
where Self: Sized { ... }
}Expand description
Base trait for document compressors.
This abstraction is primarily used for post-processing of retrieved documents.
Document objects matching a given query are first retrieved.
Then the list of documents can be further processed.
For example, one could re-rank the retrieved documents using an LLM.
Users should favor using a RunnableLambda instead of implementing this
trait directly when possible.
§Example
ⓘ
use agent_chain_core::documents::{BaseDocumentCompressor, Document};
use agent_chain_core::callbacks::Callbacks;
use async_trait::async_trait;
struct MyCompressor {
threshold: f64,
}
#[async_trait]
impl BaseDocumentCompressor for MyCompressor {
async fn compress_documents(
&self,
documents: Vec<Document>,
query: &str,
_callbacks: Option<Callbacks>,
) -> Result<Vec<Document>, Box<dyn std::error::Error + Send + Sync>> {
// Filter documents based on some criteria
Ok(documents
.into_iter()
.filter(|doc| doc.page_content.contains(query))
.collect())
}
}Required Methods§
Sourcefn compress_documents<'life0, 'life1, 'async_trait>(
&'life0 self,
documents: Vec<Document>,
query: &'life1 str,
callbacks: Option<Callbacks>,
) -> Pin<Box<dyn Future<Output = Result<Vec<Document>, Box<dyn Error + Send + Sync>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn compress_documents<'life0, 'life1, 'async_trait>(
&'life0 self,
documents: Vec<Document>,
query: &'life1 str,
callbacks: Option<Callbacks>,
) -> Pin<Box<dyn Future<Output = Result<Vec<Document>, Box<dyn Error + Send + Sync>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Provided Methods§
Sourcefn compress_documents_sync(
&self,
documents: Vec<Document>,
query: &str,
callbacks: Option<Callbacks>,
) -> Result<Vec<Document>, Box<dyn Error + Send + Sync>>where
Self: Sized,
fn compress_documents_sync(
&self,
documents: Vec<Document>,
query: &str,
callbacks: Option<Callbacks>,
) -> Result<Vec<Document>, Box<dyn Error + Send + Sync>>where
Self: Sized,
Synchronously compress retrieved documents given the query context.
This is a blocking version of compress_documents.
The default implementation runs the async version using a blocking runtime.
§Arguments
documents- The retrievedDocumentobjects.query- The query context.callbacks- OptionalCallbacksto run during compression.
§Returns
The compressed documents, or an error if compression fails.