| Crates.io | airust |
| lib.rs | airust |
| version | 0.1.6 |
| created_at | 2025-03-29 13:32:48.202286+00 |
| updated_at | 2025-04-01 14:46:01.361497+00 |
| description | Trainable, modular AI engine in Rust with compile-time knowledge |
| homepage | https://github.com/LEVOGNE/airust |
| repository | https://github.com/LEVOGNE/airust |
| max_upload_size | |
| id | 1611277 |
| size | 161,400 |
๐ง airust is a modular, trainable AI library written in Rust.
It supports compile-time knowledge through JSON files and provides sophisticated prediction engines for natural language input.
train.json)airust CLI for:
airust as a Rust library in your own applications (Web, CLI, Desktop, IoT)๐งฉ Modular Architecture with Unified Traits:
Agent โ Base trait for all agents with enhanced prediction capabilitiesTrainableAgent โ For agents that can be trained with examplesContextualAgent โ For context-aware conversational agentsConfidenceAgent โ New trait for agents that can provide prediction confidence๐ง Intelligent Agent Implementations:
MatchAgent โ Advanced matching with configurable strategies
TfidfAgent โ Sophisticated similarity detection using BM25 algorithm
ContextAgent<A> โ Flexible context-aware wrapper
๐ Enhanced Response Handling:
ResponseFormat with support for:
๐พ Intelligent Knowledge Base:
train.json๐ PDF Processing and Knowledge Extraction:
PdfLoader with configurable extraction parameters:
๐ Advanced Text Processing:
๐ ๏ธ Unified CLI Tool:
[dependencies]
airust = "0.1.5"
use airust::{Agent, TrainableAgent, MatchAgent, ResponseFormat, KnowledgeBase};
fn main() {
// Load embedded knowledge base
let kb = KnowledgeBase::from_embedded();
// Create and train agent
let mut agent = MatchAgent::new_exact();
agent.train(kb.get_examples());
// Ask a question
let answer = agent.predict("What is airust?");
// Print the response (converted from ResponseFormat to String)
println!("Answer: {}", String::from(answer));
}
The file format knowledge/train.json has been extended to support both the old and new format:
[
{
"input": "What is airust?",
"output": {
"Text": "A modular AI library in Rust."
},
"weight": 2.0
},
{
"input": "What agents are available?",
"output": {
"Markdown": "- **MatchAgent** (exact & fuzzy)\n- **TfidfAgent** (BM25)\n- **ContextAgent** (context-aware)"
},
"weight": 1.0
}
]
Legacy format is still supported for backward compatibility.
# Simple query
airust query simple "What is airust?"
airust query fuzzy "What is airust?"
airust query tfidf "Explain airust"
# Interactive mode
airust interactive
# Knowledge base management
airust knowledge
AIRust includes powerful tools for converting PDF documents into structured knowledge bases:
# Convert a PDF file to a knowledge base with default settings
cargo run --bin pdf2kb path/to/document.pdf
# Specify custom output location
cargo run --bin pdf2kb path/to/document.pdf custom/output/path.json
# With custom chunk parameters
cargo run --bin pdf2kb path/to/document.pdf --min-chunk 100 --max-chunk 2000 --overlap 300
# Additional options
cargo run --bin pdf2kb path/to/document.pdf --weight 1.5 --no-metadata --no-sentence-split
# Import PDF directly through AIRust
cargo run --bin airust -- import-pdf path/to/document.pdf
After converting multiple PDFs to knowledge bases, merge them into a unified knowledge source:
# Merge all JSON files in the knowledge/ directory
cargo run --bin merge_kb
--min-chunk <size>: Minimum chunk size in characters (default: 50)--max-chunk <size>: Maximum chunk size in characters (default: 1000)--overlap <size>: Overlap between chunks in characters (default: 200)--weight <value>: Weight for generated training examples (default: 1.0)--no-metadata: Disable inclusion of metadata in training examples--no-sentence-split: Disable sentence boundary detection for chunkinguse airust::{Agent, TrainableAgent, ContextualAgent, TfidfAgent, ContextAgent, KnowledgeBase};
fn main() {
// Load embedded knowledge base
let kb = KnowledgeBase::from_embedded();
// Create and train base agent
let mut base_agent = TfidfAgent::new()
.with_bm25_params(1.5, 0.8); // Custom BM25 tuning
base_agent.train(kb.get_examples());
// Wrap in a context-aware agent (remembering 3 turns)
let mut agent = ContextAgent::new(base_agent, 3)
.with_context_format(ContextFormat::List);
// First question
let answer1 = agent.predict("What is airust?");
println!("A1: {}", String::from(answer1.clone()));
// Add to context history
agent.add_context("What is airust?".to_string(), answer1);
// Follow-up question
let answer2 = agent.predict("What features does it provide?");
println!("A2: {}", String::from(answer2));
}
use airust::{PdfLoader, PdfLoaderConfig, KnowledgeBase, TfidfAgent, Agent, TrainableAgent};
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create a custom PDF loader configuration
let config = PdfLoaderConfig {
min_chunk_size: 100,
max_chunk_size: 1500,
chunk_overlap: 250,
default_weight: 1.2,
include_metadata: true,
split_by_sentence: true,
};
// Initialize the loader with custom configuration
let loader = PdfLoader::with_config(config);
// Convert PDF to a knowledge base
let kb = loader.pdf_to_knowledge_base("documents/technical-paper.pdf")?;
println!("Extracted {} training examples", kb.get_examples().len());
// Create and train an agent with the extracted knowledge
let mut agent = TfidfAgent::new();
agent.train(kb.get_examples());
// Ask questions about the PDF content
let answer = agent.predict("What are the main findings in the paper?");
println!("Answer: {}", String::from(answer));
// Save the knowledge base for future use
kb.save(Some("knowledge/technical-paper.json".into()))?;
Ok(())
}
// Configurable fuzzy matching
let agent = MatchAgent::new(MatchingStrategy::Fuzzy(FuzzyOptions {
max_distance: Some(5), // Maximum Levenshtein distance
threshold_factor: Some(0.2) // Dynamic length-based threshold
}));
// Multiple context representation strategies
let context_agent = ContextAgent::new(base_agent, 3)
.with_context_format(ContextFormat::List);
// Other formats: QAPairs, Sentence, Custom
// Text processing capabilities
let tokens = text_utils::tokenize("Hello, world!");
let unique_terms = text_utils::unique_terms(text);
let ngrams = text_utils::create_ngrams(text, 2);
// Advanced PDF configuration
let config = PdfLoaderConfig {
min_chunk_size: 100,
max_chunk_size: 1500,
chunk_overlap: 250,
default_weight: 1.2,
include_metadata: true,
split_by_sentence: true,
};
let loader = PdfLoader::with_config(config);
// Convert PDF to knowledge base
let kb = loader.pdf_to_knowledge_base("path/to/document.pdf")?;
MIT
Built with โค๏ธ in Rust.
Contributions and extensions are welcome!
This guide helps you migrate from airust 0.1.x to 0.1.5.
trait Agent {
fn predict(&self, input: &str) -> ResponseFormat;
}
trait TrainableAgent: Agent {
fn train(&mut self, data: &[TrainingExample]);
}
trait ContextualAgent: Agent {
fn add_context(&mut self, question: String, answer: ResponseFormat);
}
let answer: ResponseFormat = agent.predict("Question");
let answer_string: String = String::from(answer);
struct TrainingExample {
input: String,
output: ResponseFormat,
weight: f32,
}
let mut agent = MatchAgent::new_exact();
let mut agent = MatchAgent::new_fuzzy();
With options:
let mut agent = MatchAgent::new(MatchingStrategy::Fuzzy(FuzzyOptions {
max_distance: Some(5),
threshold_factor: Some(0.2),
}));
let mut base_agent = TfidfAgent::new();
base_agent.train(&data);
let mut agent = ContextAgent::new(base_agent, 5);
let kb = KnowledgeBase::from_embedded();
let data = kb.get_examples();
let mut kb = KnowledgeBase::new();
kb.add_example("Question".to_string(), "Answer".to_string(), 1.0);
cargo run --bin airust -- query simple "What is airust?"
cargo run --bin airust -- interactive
cargo run --bin airust -- knowledge
# Convert PDFs to knowledge bases
cargo run --bin pdf2kb document.pdf
# Import PDF directly in AIRust
cargo run --bin airust -- import-pdf document.pdf
# Merge PDF-derived knowledge bases
cargo run --bin merge_kb
lib.rs re-exports