Skip to main content

lc_rag/
adapter.rs

1// lc-rag/src/adapter.rs
2//! RagRunnable adapter - bridges RAGPipeline to the Runnable trait.
3//!
4//! This allows RAG pipelines to participate in LCEL pipelines via `pipe()`.
5
6use async_trait::async_trait;
7use lc_core::runnables::{LcelError, Runnable, RunnableConfig};
8use std::sync::Arc;
9
10use crate::pipeline::RAGPipeline;
11
12/// Adapter that wraps a `RAGPipeline` as a `Runnable<String, String>`.
13///
14/// This enables RAG pipelines to participate in LCEL pipelines:
15///
16/// ```rust,ignore
17/// let rag_runnable = RagRunnable::new(Arc::new(pipeline));
18/// let pipeline = rag_runnable.pipe(parser);
19/// ```
20pub struct RagRunnable {
21    pipeline: Arc<RAGPipeline>,
22}
23
24impl RagRunnable {
25    /// Create a new adapter wrapping the given RAG pipeline.
26    pub fn new(pipeline: Arc<RAGPipeline>) -> Self {
27        Self { pipeline }
28    }
29}
30
31#[async_trait]
32impl Runnable<String, String> for RagRunnable {
33    type Error = LcelError;
34
35    async fn invoke(
36        &self,
37        input: String,
38        _config: Option<RunnableConfig>,
39    ) -> Result<String, LcelError> {
40        self.pipeline
41            .query(&input)
42            .await
43            .map_err(|e| LcelError::Other(format!("RAG query error: {}", e)))
44    }
45
46    // stream, batch, transform use default implementations
47}
48
49#[cfg(test)]
50mod tests {
51
52    #[test]
53    fn rag_runnable_creation() {
54        // Just verify the type exists and compiles
55        // Actual integration tests would require a live LLM
56    }
57}