8000 GitHub - cmcconnell1/rust-llm-tool: self-hosted LLM API powered by Rust, for local PDF document indexing for Retrieval-Augmented Generation (RAG). Built with Axum, SQLite, and Ollama
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

self-hosted LLM API powered by Rust, for local PDF document indexing for Retrieval-Augmented Generation (RAG). Built with Axum, SQLite, and Ollama

Notifications You must be signed in to change notification settings

cmcconnell1/rust-llm-tool

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

1 Commit
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Rust LLM Tool with Local PDF Knowledge Base

A self-hosted LLM API powered by Rust, providing local PDF document indexing for Retrieval-Augmented Generation (RAG). Built with Axum, SQLite, and Ollama.

⚠️ Note: Learning Project Notice This my learning project to explore Rust web development, as 8000 ync programming, and LLM integration. For production use, consider established solutions like:

🎯 Purpose & Learning Goals

This project demonstrates:

  • Building async web services with Rust and Axum
  • Implementing basic RAG (Retrieval-Augmented Generation)
  • Integrating with local LLMs via Ollama
  • Working with SQLite in Rust
  • PDF processing and text extraction
  • Error handling and logging in Rust

πŸ—οΈ Architecture

Key Components

  • Web Server: Axum for routing and request handling
  • Database: SQLite with async support via SQLx
  • LLM Integration: Ollama for local model inference
  • PDF Processing: pdf-extract for text extraction
  • Error Handling: Custom error types with thiserror
  • Logging: env_logger for structured logging

Data Flow

  1. PDFs are processed and stored in SQLite
  2. User submits query via REST API
  3. Relevant context is retrieved from stored PDF content
  4. Query + context sent to LLM via Ollama
  5. Response returned to user

πŸš€ Getting Started

Prerequisites

# Install Rust
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

# Install SQLx CLI
cargo install sqlx-cli

# Install Ollama
curl -fsSL https://ollama.ai/install.sh | sh

# Install pdftotext (poppler-utils)
# For Ubuntu/Debian:
sudo apt-get install poppler-utils
# For macOS:
brew install poppler
# For Fedora:
sudo dnf install poppler-utils

Setup

# Clone repository
git clone https://github.com/cmcconnell1/rust-llm-tool.git
cd rust-llm-tool

# Run setup script
bash setup.sh

# Start server
RUST_LOG=info cargo run --release

πŸ”§ Configuration

Edit config.yaml to customize:

default_model: "gemma"  # Ollama model to use
database_url: "sqlite://./data/database.db"
api_port: 8000
pdf_directory: "./pdfs"

πŸ“‘ API Endpoints

Query LLM

curl -X POST "http://127.0.0.1:8000/query" \
     -H "Content-Type: application/json" \
     -d '{"prompt": "What is Rust?"}'

Batch Query

curl -X POST "http://127.0.0.1:8000/batch-query" \
     -H "Content-Type: application/json" \
     -d '[
         {"prompt": "What is Rust?"},
         {"prompt": "Explain async/await"}
     ]'

Validate PDF Knowledge

curl http://127.0.0.1:8000/validate-pdfs

πŸ“š Code Structure

Key Files

  • src/main.rs: Server setup and routing
  • src/api.rs: API endpoint handlers
  • src/db.rs: Database initialization and queries
  • src/pdf.rs: PDF processing logic
  • src/ollama.rs: LLM integration
  • src/config.rs: Configuration management

Error Handling

  • Custom error types for each module
  • Proper error propagation using Result
  • Structured logging with different levels

Async/Await

  • Uses Tokio runtime for async operations
  • Proper connection pooling with SQLx
  • Async HTTP clients for Ollama integration

πŸ”œ Future Enhancements

  • Implement vector embeddings for better retrieval
  • Add response streaming support
  • Improve PDF chunking strategy
  • Add caching layer
  • Create web dashboard

πŸ“– Learning Resources

πŸ“š PDF Processing

Directory Structure

PDFs are stored in the pdfs/ directory with subdirectories for different categories:

pdfs/
β”œβ”€β”€ cloud/
β”œβ”€β”€ programming/
β”œβ”€β”€ k8s/
β”œβ”€β”€ SQL/
└── security/

Processing PDFs

The system recursively processes PDFs from all subdirectories:

# Process all PDFs (with detailed logging)
RUST_LOG=info cargo run --bin process_pdfs

# Or use the validation endpoint
curl http://127.0.0.1:8000/validate-pdfs

PDF Processing Details

System Requirements

  • pdftotext utility (part of poppler-utils) must be installed on your system
  • Maximum PDF file size: 100MB
  • Minimum content length: 100 characters

Why pdftotext?

The project uses the system's pdftotext utility instead of pure Rust PDF processing because:

  1. PDF format complexity: The PDF specification is extensive and complex, making pure Rust implementations either incomplete or unstable for production use
  2. Reliability: Popular system utilities like pdftotext have been battle-tested for years and handle various PDF variants and edge cases
  3. Performance: Native system utilities often provide better performance than pure Rust alternatives
  4. Security: System PDF utilities typically include security patches and handle malformed PDFs safely

While pure Rust alternatives exist (like pdf-extract crate), they often:

  • Lack support for complex PDF features
  • Have issues with certain PDF encodings
  • May crash on malformed PDFs
  • Don't handle all PDF security features

The tradeoff is the external dependency, but the benefits in reliability and feature support outweigh this drawback for this learning project.

Known PDF Issues

  • Some PDFs may contain corrupt data streams
  • Unicode mismatches with certain ligatures (fi, fl, ff)
  • Large PDFs (>100MB) are skipped
  • PDFs with insufficient text content are skipped

PDF Processing Rules

  • Maximum file size: 100MB
  • Minimum content length: 100 characters
  • Supported formats: Standard PDF files
  • Unicode normalization is applied
  • Common ligatures are converted to standard characters
  • Relative paths are preserved in database

Troubleshooting PDF Processing

If PDFs fail to process:

  1. Check file permissions
  2. Verify PDF is not corrupted
  3. Ensure file size is under 100MB
  4. Check logs for specific error messages
  5. Try processing individual files for debugging

πŸ“ License

MIT License

About

self-hosted LLM API powered by Rust, for local PDF document indexing for Retrieval-Augmented Generation (RAG). Built with Axum, SQLite, and Ollama

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published
0