RAGTool

Struct RAGTool 

Source
pub struct RAGTool { /* private fields */ }
Expand description

RAG Tool with flexible backend support

Implementations§

Source§

impl RAGTool

Source

pub fn new_in_memory( embedding_api_url: impl Into<String>, embedding_api_key: impl Into<String>, ) -> Self

Create a new RAG tool with in-memory vector store

Examples found in repository?
examples/rag_qdrant_comparison.rs (line 30)
19async fn demonstrate_in_memory() -> helios_engine::Result<()> {
20    println!("=== IN-MEMORY RAG DEMONSTRATION ===\n");
21
22    let api_key = std::env::var("OPENAI_API_KEY").unwrap_or_else(|_| {
23        println!("⚠ Warning: OPENAI_API_KEY not set. Using placeholder.");
24        "your-api-key-here".to_string()
25    });
26
27    let config = Config::from_file("config.toml").unwrap_or_else(|_| Config::new_default());
28
29    // Create in-memory RAG tool
30    let rag_tool = RAGTool::new_in_memory("https://api.openai.com/v1/embeddings", &api_key);
31
32    let mut agent = Agent::builder("InMemoryAgent")
33        .config(config)
34        .system_prompt("You are a helpful assistant with in-memory RAG capabilities.")
35        .tool(Box::new(rag_tool))
36        .max_iterations(8)
37        .build()
38        .await?;
39
40    println!("✓ In-memory agent created\n");
41
42    // Add some documents
43    println!("Adding documents...");
44    agent.chat("Store: The capital of France is Paris.").await?;
45    agent
46        .chat("Store: The capital of Germany is Berlin.")
47        .await?;
48    agent.chat("Store: The capital of Italy is Rome.").await?;
49    println!("✓ Documents added\n");
50
51    // Search
52    println!("Searching...");
53    let response = agent.chat("What is the capital of Germany?").await?;
54    println!("Agent: {}\n", response);
55
56    println!("Advantages of in-memory:");
57    println!("  ✓ No external dependencies");
58    println!("  ✓ Fast and simple");
59    println!("  ✓ Perfect for development");
60    println!("  ✗ No persistence (data lost on restart)");
61    println!("  ✗ Limited scalability\n");
62
63    Ok(())
64}
More examples
Hide additional examples
examples/rag_in_memory.rs (line 36)
18async fn main() -> helios_engine::Result<()> {
19    println!("🚀 Helios Engine - Agent with In-Memory RAG Example");
20    println!("===================================================\n");
21
22    // Check for the required OpenAI API key
23    let embedding_api_key = std::env::var("OPENAI_API_KEY").unwrap_or_else(|_| {
24        println!("⚠ Warning: OPENAI_API_KEY not set. Using placeholder.");
25        "your-api-key-here".to_string()
26    });
27
28    // Load configuration
29    let config = Config::from_file("config.toml").unwrap_or_else(|_| {
30        println!("⚠ No config.toml found, using default configuration");
31        Config::new_default()
32    });
33
34    // Create a new RAG tool with in-memory vector store
35    let rag_tool =
36        RAGTool::new_in_memory("https://api.openai.com/v1/embeddings", embedding_api_key);
37
38    // Create an agent with RAG capabilities
39    let mut agent = Agent::builder("KnowledgeAgent")
40        .config(config)
41        .system_prompt(
42            "You are a helpful assistant with access to an in-memory RAG (Retrieval-Augmented Generation) system. \
43             You can store documents and retrieve relevant information to answer questions. \
44             When answering questions, first search for relevant documents, then provide informed answers based on the retrieved context."
45        )
46        .tool(Box::new(rag_tool))
47        .max_iterations(10)
48        .build()
49        .await?;
50
51    println!("✓ Agent created with in-memory RAG capabilities\n");
52
53    // --- Example 1: Add knowledge about programming languages ---
54    println!("Example 1: Building a Knowledge Base");
55    println!("=====================================\n");
56
57    let documents = [
58        "Rust is a systems programming language that runs blazingly fast, prevents segfaults, \
59         and guarantees thread safety. It was created by Mozilla Research and first released in 2010.",
60        "Python is a high-level, interpreted programming language known for its clear syntax \
61         and readability. It was created by Guido van Rossum and first released in 1991.",
62        "JavaScript is a programming language commonly used for web development. It allows \
63         developers to create interactive web pages and runs in web browsers. It was created in 1995.",
64        "Go is a statically typed, compiled programming language designed at Google. It is \
65         syntactically similar to C, but with memory safety and garbage collection.",
66        "TypeScript is a strongly typed programming language that builds on JavaScript. It was \
67         developed and maintained by Microsoft and first released in 2012.",
68    ];
69
70    for (i, doc) in documents.iter().enumerate() {
71        println!("Adding document {}...", i + 1);
72        let response = agent
73            .chat(&format!("Store this information: {}", doc))
74            .await?;
75        println!("Agent: {}\n", response);
76    }
77
78    // --- Example 2: Semantic search with different queries ---
79    println!("\nExample 2: Semantic Search");
80    println!("==========================\n");
81
82    let queries = vec![
83        "What programming language prevents segfaults?",
84        "Tell me about the language created by Guido van Rossum",
85        "Which language is used for web development in browsers?",
86        "What language was developed by Google?",
87    ];
88
89    for query in queries {
90        println!("Query: {}", query);
91        let response = agent.chat(query).await?;
92        println!("Agent: {}\n", response);
93    }
94
95    // --- Example 3: Check document count ---
96    println!("\nExample 3: Document Count");
97    println!("=========================\n");
98
99    let response = agent.chat("How many documents are stored?").await?;
100    println!("Agent: {}\n", response);
101
102    // --- Example 4: Complex query requiring multiple documents ---
103    println!("\nExample 4: Complex Query");
104    println!("========================\n");
105
106    let response = agent
107        .chat("Compare the programming languages that were created in the 1990s")
108        .await?;
109    println!("Agent: {}\n", response);
110
111    println!("\n✅ Example completed successfully!");
112    println!("\n💡 Key Features Demonstrated:");
113    println!("  • In-memory vector storage (no external dependencies)");
114    println!("  • Document embedding with OpenAI embeddings");
115    println!("  • Semantic search with cosine similarity");
116    println!("  • RAG workflow for context-aware answers");
117    println!("  • Fast performance for development and testing");
118
119    println!("\n📝 Use Cases for In-Memory RAG:");
120    println!("  • Development and testing");
121    println!("  • Short-lived sessions");
122    println!("  • When persistence is not required");
123    println!("  • Rapid prototyping");
124
125    println!("\n🔧 Advantages:");
126    println!("  • No external dependencies (no database needed)");
127    println!("  • Fast setup and execution");
128    println!("  • Simple deployment");
129    println!("  • Perfect for demos and examples");
130
131    Ok(())
132}
Source

pub fn new_qdrant( qdrant_url: impl Into<String>, collection_name: impl Into<String>, embedding_api_url: impl Into<String>, embedding_api_key: impl Into<String>, ) -> Self

Create a new RAG tool with Qdrant vector store

Examples found in repository?
examples/rag_qdrant_comparison.rs (lines 77-82)
66async fn demonstrate_qdrant() -> helios_engine::Result<()> {
67    println!("=== QDRANT RAG DEMONSTRATION ===\n");
68
69    let api_key = std::env::var("OPENAI_API_KEY").unwrap_or_else(|_| {
70        println!("⚠ Warning: OPENAI_API_KEY not set. Using placeholder.");
71        "your-api-key-here".to_string()
72    });
73
74    let config = Config::from_file("config.toml").unwrap_or_else(|_| Config::new_default());
75
76    // Create Qdrant RAG tool
77    let rag_tool = RAGTool::new_qdrant(
78        "http://localhost:6333",
79        "comparison_demo",
80        "https://api.openai.com/v1/embeddings",
81        &api_key,
82    );
83
84    let mut agent = Agent::builder("QdrantAgent")
85        .config(config)
86        .system_prompt("You are a helpful assistant with Qdrant RAG capabilities.")
87        .tool(Box::new(rag_tool))
88        .max_iterations(8)
89        .build()
90        .await?;
91
92    println!("✓ Qdrant agent created\n");
93
94    // Clear any existing data
95    println!("Clearing existing data...");
96    agent.chat("Clear all documents").await?;
97    println!("✓ Cleared\n");
98
99    // Add some documents
100    println!("Adding documents...");
101    agent
102        .chat("Store: The Eiffel Tower is located in Paris, France.")
103        .await?;
104    agent
105        .chat("Store: The Colosseum is located in Rome, Italy.")
106        .await?;
107    agent
108        .chat("Store: The Brandenburg Gate is located in Berlin, Germany.")
109        .await?;
110    println!("✓ Documents added\n");
111
112    // Search
113    println!("Searching...");
114    let response = agent.chat("What famous landmark is in Berlin?").await?;
115    println!("Agent: {}\n", response);
116
117    println!("Advantages of Qdrant:");
118    println!("  ✓ Persistent storage");
119    println!("  ✓ Highly scalable");
120    println!("  ✓ Production-ready");
121    println!("  ✓ Advanced features (filtering, etc.)");
122    println!("  ✗ Requires external service");
123    println!("  ✗ More complex setup\n");
124
125    Ok(())
126}
Source

pub fn with_rag_system( rag_system: RAGSystem, backend_type: impl Into<String>, ) -> Self

Create with a custom RAG system

Trait Implementations§

Source§

impl Clone for RAGTool

Source§

fn clone(&self) -> RAGTool

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Tool for RAGTool

Source§

fn name(&self) -> &str

The name of the tool.
Source§

fn description(&self) -> &str

A description of the tool.
Source§

fn parameters(&self) -> HashMap<String, ToolParameter>

The parameters for the tool.
Source§

fn execute<'life0, 'async_trait>( &'life0 self, args: Value, ) -> Pin<Box<dyn Future<Output = Result<ToolResult>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Executes the tool with the given arguments.
Source§

fn to_definition(&self) -> ToolDefinition

Converts the tool to a ToolDefinition.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more