pub struct AdaptiveRAG<M: BaseChatModel, R: RetrieverTrait> { /* private fields */ }Expand description
Adaptive RAG that routes queries to the most appropriate strategy.
§Overview
- The LLM classifies the query into one of three buckets:
no_retrieval,single_search, ormulti_query. - Based on the decision:
- NoRetrieval: call the LLM directly.
- SingleSearch: retrieve documents, then generate.
- MultiQuery: generate multiple query variants, retrieve for each, merge results, then generate.
§Example
ⓘ
use langchainrust::agents::adaptive_rag::{AdaptiveRAG, RagDecision};
use langchainrust::OpenAIChat;
use langchainrust::retrieval::SimilarityRetriever;
let rag = AdaptiveRAG::new(llm, retriever);
let result = rag.invoke("What is the capital of France?").await?;
assert_eq!(result.decision, RagDecision::NoRetrieval);Implementations§
Source§impl<M: BaseChatModel, R: RetrieverTrait> AdaptiveRAG<M, R>
impl<M: BaseChatModel, R: RetrieverTrait> AdaptiveRAG<M, R>
Sourcepub fn new(llm: M, retriever: R) -> Self
pub fn new(llm: M, retriever: R) -> Self
Creates a new AdaptiveRAG with the given LLM and retriever.
Sourcepub fn with_retrieve_k(self, k: usize) -> Self
pub fn with_retrieve_k(self, k: usize) -> Self
Sets the number of documents to retrieve per query.
Sourcepub fn with_multi_query_count(self, count: usize) -> Self
pub fn with_multi_query_count(self, count: usize) -> Self
Sets the number of alternative queries for multi-query mode.
Sourcepub async fn invoke(
&self,
query: &str,
) -> Result<AdaptiveRAGResult, AdaptiveRAGError>
pub async fn invoke( &self, query: &str, ) -> Result<AdaptiveRAGResult, AdaptiveRAGError>
Invokes the adaptive RAG pipeline for the given query.
Sourcepub async fn stream(
&self,
query: &str,
) -> Result<Pin<Box<dyn Stream<Item = AgentStreamEvent> + Send>>, AdaptiveRAGError>
pub async fn stream( &self, query: &str, ) -> Result<Pin<Box<dyn Stream<Item = AgentStreamEvent> + Send>>, AdaptiveRAGError>
Streams the AdaptiveRAG execution, emitting pipeline step events.
Emits AgentStreamEvent::PipelineStep events for routing and generation,
and AgentStreamEvent::FinalAnswer when the answer is ready.
Trait Implementations§
Source§impl<M, R> Orchestrator for AdaptiveRAG<M, R>
impl<M, R> Orchestrator for AdaptiveRAG<M, R>
Source§type Output = AdaptiveRAGResult
type Output = AdaptiveRAGResult
Output type.
Source§fn run_with_context<'life0, 'life1, 'async_trait>(
&'life0 self,
input: Self::Input,
ctx: &'life1 RunContext,
) -> Pin<Box<dyn Future<Output = Result<Self::Output, AgentError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn run_with_context<'life0, 'life1, 'async_trait>(
&'life0 self,
input: Self::Input,
ctx: &'life1 RunContext,
) -> Pin<Box<dyn Future<Output = Result<Self::Output, AgentError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Execution entry point carrying the run context.
Auto Trait Implementations§
impl<M, R> Freeze for AdaptiveRAG<M, R>
impl<M, R> RefUnwindSafe for AdaptiveRAG<M, R>where
M: RefUnwindSafe,
R: RefUnwindSafe,
impl<M, R> Send for AdaptiveRAG<M, R>
impl<M, R> Sync for AdaptiveRAG<M, R>
impl<M, R> Unpin for AdaptiveRAG<M, R>
impl<M, R> UnsafeUnpin for AdaptiveRAG<M, R>where
M: UnsafeUnpin,
R: UnsafeUnpin,
impl<M, R> UnwindSafe for AdaptiveRAG<M, R>where
M: UnwindSafe,
R: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more