RAG
A Rust library and CLI tool for Retrieval-Augmented Generation (RAG) with support for multiple embedding models and vector stores.
Features
- 🦀 Pure Rust implementation with async/await support
- 🤖 Multiple embedding model support (OpenAI, Ollama)
- 📊 Pluggable vector search indexes with multiple distance metrics (Cosine, Euclidean, Dot Product, Manhattan)
- 📊 In-memory vector stores with parallel batch search (
InMemoryVectorStore,MinimalVectorDB) - 📝 Multiple text chunking strategies (fixed-size, paragraph, sentence)
- 🎯 Configurable top-k retrieval
- 🔍 Metadata filtering support
- 💻 CLI tool for quick operations
- 📚 Easy-to-use library API
Installation
From source
As a library
Add to your Cargo.toml:
[]
= { = "https://github.com/yingkitw/rag" }
Quick Start
CLI Usage
# Set your API key (OpenAI or use Ollama)
# Add a document
# Query the vector store
# List documents
# Count documents
Library Usage
use ;
async
Examples
See the examples/ directory for more usage examples:
Configuration
Environment Variables
OPENAI_API_KEY: Your OpenAI API key (optional, will use Ollama if not set)OLLAMA_URL: Ollama server URL (default:http://localhost:11434)
Chunking Strategies
FixedSizeChunker: Splits text into chunks of fixed size with overlapParagraphChunker: Splits text by paragraphs (double newlines)SentenceChunker: Splits text by sentences
Embedding Models
OpenAI
let model = new;
let model = with_model;
Ollama
let model = new;
let model = new
.with_base_url;
API Reference
Core Types
EmbeddingModel: Trait for embedding modelsVectorStore: Trait for vector storage backendsRetriever: Main interface for RAG operationsDocument: Represents a stored document with content, metadata, and optional embeddingTextChunker: Trait for text chunking strategies
Retriever Methods
add_document(content): Add a single documentadd_document_with_metadata(content, metadata): Add a document with metadataretrieve(query): Retrieve relevant chunksretrieve_with_scores(query): Retrieve chunks with similarity scoresretrieve_filtered(query, metadata_filter): Retrieve with metadata filtering
Development
Run tests:
Run examples:
License
Apache-2.0
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.