Skip to content

vnknowledge2014/ai-financial-reader

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

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

Repository files navigation

AI Financial Report Reader

An AI-powered platform for reading and analyzing financial reports using RAG/CAG/KAG architecture with Rust.

πŸ—οΈ Architecture

Functional Core - Imperative Shell + Railway Oriented Programming

β”œβ”€β”€ core/               # Business logic (Functional Core)
β”‚   β”œβ”€β”€ domain/        # Domain models
β”‚   β”œβ”€β”€ ports/         # Trait interfaces
β”‚   β”œβ”€β”€ adapters/      # I/O implementations (Imperative Shell)
β”‚   β”œβ”€β”€ services/      # RAG/CAG/KAG services
β”‚   └── agent/         # Agent orchestration
β”œβ”€β”€ api/               # Axum REST API
β”œβ”€β”€ cli/               # Ratatui terminal UI
β”œβ”€β”€ financial_service/ # Python yfinance microservice
β”œβ”€β”€ prompts.json       # Externalized System Prompts
β”œβ”€β”€ llm_config.json    # LLM provider configuration
└── docker-compose.yml # Infrastructure orchestration

πŸ› οΈ Tech Stack

  • Language: Rust
  • File Storage: MinIO (S3-compatible)
  • Database: SurrealDB (Vector + Graph + Memory Store)
  • LLM: Multi-provider support (Ollama, Gemini, OpenAI, Anthropic)
  • Embeddings: Ollama mxbai-embed-large
  • PDF Extraction: Extractous
  • Backend: Axum with streaming support
  • CLI: Ratatui
  • Financial Data: Python/yfinance microservice
  • Web Search: Brave Search API
  • Container: Docker Compose

πŸš€ Quick Start

Prerequisites

  • Docker & Docker Compose
  • Rust toolchain (1.75+)
  • (Optional) Local Ollama models cached at ~/.ollama

Option 1: Full Docker Mode (Recommended)

Run everything in Docker, including the API server:

./start.sh --docker

This will:

  1. Start all infrastructure (SurrealDB, Ollama, MinIO, Financial Service)
  2. Build and run the API server in Docker
  3. Copy local Ollama models if available (faster startup)
  4. Wait for all services to be healthy

Once ready, run the CLI:

cargo run --bin cli

Option 2: Local Development Mode

Run the API locally with Docker infrastructure:

# Start infrastructure only
./start.sh

# In terminal 1: Run API server
cargo run --bin api

# In terminal 2: Run CLI
cargo run --bin cli

Quick Rebuild

Rebuild only the API container without restarting infrastructure:

./start.sh --rebuild

πŸ“Š Services

Service Port Description
SurrealDB 8000 Vector, Graph, and Memory Database
Ollama 11434 LLM & embedding service
MinIO 9000 S3-compatible file storage
MinIO Console 9001 Web UI for file management
API Server 3000 REST API endpoints
Financial Service 8001 Python/yfinance for stock data

βš™οΈ Configuration

Environment Variables (.env)

Copy from example and customize:

cp .env.example .env

Key variables:

  • GEMINI_API_KEY - Google Gemini API key
  • OPENAI_API_KEY - OpenAI API key
  • ANTHROPIC_API_KEY - Anthropic API key
  • BRAVE_SEARCH_API_KEY - Brave Search API key
  • OLLAMA_BASE_URL - Ollama endpoint (default: http://localhost:11434)

LLM Configuration (llm_config.json)

Configure which LLM provider and model to use for each task:

  • Main LLM for generation
  • Intent detection
  • Entity extraction
  • Embeddings

Prompts (prompts.json)

System prompts for the AI agents:

  • general_system_prompt: Used for standard RAG/CAG queries
  • financial_analysis_prompt: Used for financial analysis with Chain of Thought (CoT)

πŸ“– Usage

Upload Document via API

curl -X POST http://localhost:3000/api/documents \
  -F "file=@NAB_2024_Annual_Report.pdf"

Query via API (Streaming)

curl -X POST http://localhost:3000/api/query/stream \
  -H "Content-Type: application/json" \
  -d '{"text": "What was the total revenue?", "max_results": 50}'

Query via API (Non-Streaming)

curl -X POST http://localhost:3000/api/query \
  -H "Content-Type: application/json" \
  -d '{"text": "What are the key financial highlights?", "max_results": 50}'

Using CLI

cargo run --bin cli

Features:

  • Interactive document upload
  • Real-time streaming responses
  • Conversation history with CAG
  • Multi-document support

πŸ§ͺ Architecture Details

RAG/CAG/KAG Implementation

  1. RAG (Retrieval Augmented Generation)

    • Vector similarity search on embeddings
    • Retrieve top-k relevant chunks
    • LLM generates answer with context
  2. CAG (Context Augmented Generation)

    • Memory Manager: Short-term (Sliding Window) and Long-term (Vector Search)
    • SurrealDB Memory Store: Persists conversation history
    • Context-aware follow-up questions
  3. KAG (Knowledge Augmented Generation)

    • Graph traversal to find related documents
    • Cross-document knowledge synthesis
    • Relationship-aware answers
  4. Agent Orchestrator

    • Intent Detection: Routes queries to RAG, Financial Analysis, or Web Search
    • Web Search: Integrates real-time data via Brave Search API
    • Financial Service: Fetches live stock data via yfinance
    • Multi-entity Support: Handles queries about multiple companies

Data Flow

PDF Upload β†’ MinIO Storage
           β†’ PDF Extraction (Extractous)
           β†’ Text Chunking (Semantic)
           β†’ Embedding Generation (Ollama)
           β†’ Vector Storage (SurrealDB)
           β†’ Graph Relationships (SurrealDB)

Query β†’ Intent Detection (Orchestrator)
      β†’ [Web Search Strategy] β†’ Brave Search β†’ Answer
      β†’ [Financial Data Strategy] β†’ yfinance Service β†’ Answer
      β†’ [RAG/CAG Strategy]
          β†’ Memory Retrieval (Short-term + Long-term)
          β†’ Vector Similarity Search (Documents)
          β†’ LLM Generation (Streaming)
          β†’ Answer with Citations

🧩 Project Structure

AI_Financial_Report_Reader_v5/
β”œβ”€β”€ api/                       # REST API Server (Axum)
β”‚   └── src/
β”‚       β”œβ”€β”€ main.rs           # Server setup, routes, AppState
β”‚       β”œβ”€β”€ dto.rs            # Request/Response DTOs
β”‚       └── handlers/         # Route handlers
β”‚           β”œβ”€β”€ documents.rs  # Upload, list, delete documents
β”‚           β”œβ”€β”€ query.rs      # Query & streaming endpoints
β”‚           └── health.rs     # Health check
β”‚
β”œβ”€β”€ cli/                       # Terminal UI Client (Ratatui)
β”‚   └── src/
β”‚       β”œβ”€β”€ main.rs           # TUI application
β”‚       β”œβ”€β”€ ui.rs             # UI components & layout
β”‚       β”œβ”€β”€ client.rs         # API client with streaming
β”‚       β”œβ”€β”€ markdown.rs       # Markdown rendering
β”‚       └── utils.rs          # Helper utilities
β”‚
β”œβ”€β”€ core/                      # Business Logic (Functional Core)
β”‚   └── src/
β”‚       β”œβ”€β”€ config.rs         # Configuration management
β”‚       β”œβ”€β”€ domain/           # Pure domain models
β”‚       β”‚   β”œβ”€β”€ mod.rs        # Document, Chunk, Query, Answer
β”‚       β”‚   β”œβ”€β”€ entity.rs     # Named entities (Company, Ticker)
β”‚       β”‚   β”œβ”€β”€ agent.rs      # Agent types & tools
β”‚       β”‚   β”œβ”€β”€ memory.rs     # MemoryEntry, conversation history
β”‚       β”‚   └── error.rs      # Domain errors (Railway pattern)
β”‚       β”‚
β”‚       β”œβ”€β”€ ports/            # Trait interfaces (Dependency Inversion)
β”‚       β”‚   β”œβ”€β”€ llm.rs        # LLM trait (generate, stream)
β”‚       β”‚   β”œβ”€β”€ embedding.rs  # Embedding trait
β”‚       β”‚   β”œβ”€β”€ vector_db.rs  # VectorDatabase trait
β”‚       β”‚   β”œβ”€β”€ graph_db.rs   # GraphDatabase trait
β”‚       β”‚   β”œβ”€β”€ memory_store.rs # MemoryStore trait
β”‚       β”‚   β”œβ”€β”€ file_storage.rs # FileStorage trait
β”‚       β”‚   β”œβ”€β”€ pdf_extractor.rs # PdfExtractor trait
β”‚       β”‚   β”œβ”€β”€ web_search.rs # WebSearch trait
β”‚       β”‚   β”œβ”€β”€ financial.rs  # FinancialData trait
β”‚       β”‚   └── entity_extraction.rs # NER trait
β”‚       β”‚
β”‚       β”œβ”€β”€ adapters/         # Concrete Implementations
β”‚       β”‚   β”œβ”€β”€ ollama.rs     # Ollama LLM & embeddings
β”‚       β”‚   β”œβ”€β”€ gemini_llm.rs # Google Gemini LLM
β”‚       β”‚   β”œβ”€β”€ gemini_custom.rs # Custom Gemini client
β”‚       β”‚   β”œβ”€β”€ llm_factory.rs # Multi-provider factory
β”‚       β”‚   β”œβ”€β”€ surrealdb_vector.rs # Vector storage
β”‚       β”‚   β”œβ”€β”€ surrealdb_graph.rs  # Graph relationships
β”‚       β”‚   β”œβ”€β”€ surrealdb_memory.rs # Conversation memory
β”‚       β”‚   β”œβ”€β”€ rustfs.rs     # MinIO S3 storage
β”‚       β”‚   β”œβ”€β”€ pdf_extractous.rs # PDF extraction (Extractous)
β”‚       β”‚   β”œβ”€β”€ brave_search.rs # Brave Search API
β”‚       β”‚   β”œβ”€β”€ financial_service.rs # yfinance client
β”‚       β”‚   └── ollama_ner.rs # Named Entity Recognition
β”‚       β”‚
β”‚       β”œβ”€β”€ services/         # Business Logic Services
β”‚       β”‚   β”œβ”€β”€ document_ingestion.rs # Upload & indexing
β”‚       β”‚   β”œβ”€β”€ embedding.rs  # Embedding generation
β”‚       β”‚   β”œβ”€β”€ memory_manager.rs # Short/long-term memory
β”‚       β”‚   β”œβ”€β”€ query_processing.rs # Query preprocessing
β”‚       β”‚   β”œβ”€β”€ re_ranker.rs  # Result re-ranking
β”‚       β”‚   β”œβ”€β”€ router_agent.rs # Intent detection & routing
β”‚       β”‚   β”œβ”€β”€ agent_orchestrator.rs # Main orchestrator
β”‚       β”‚   β”œβ”€β”€ cag.rs        # Context Augmented Generation
β”‚       β”‚   └── kag.rs        # Knowledge Augmented Generation
β”‚       β”‚
β”‚       └── agent/            # Agent Framework
β”‚           β”œβ”€β”€ agent.rs      # Agent execution loop
β”‚           β”œβ”€β”€ builder.rs    # Agent builder pattern
β”‚           β”œβ”€β”€ tool.rs       # Tool definitions
β”‚           └── prompts.rs    # Agent system prompts
β”‚
β”œβ”€β”€ financial_service/         # Python Microservice (yfinance)
β”‚   β”œβ”€β”€ main.py               # FastAPI server
β”‚   β”œβ”€β”€ Dockerfile
β”‚   └── requirements.txt
β”‚
β”œβ”€β”€ scripts/                   # Utility Scripts
β”‚   β”œβ”€β”€ download_models.sh    # Ollama model setup
β”‚   β”œβ”€β”€ run_benchmark.sh      # Performance benchmarks
β”‚   └── test_*.sh             # Test scripts
β”‚
β”œβ”€β”€ docker-compose.yml         # Infrastructure orchestration
β”œβ”€β”€ Dockerfile                 # API container build
β”œβ”€β”€ start.sh                   # One-command startup
β”œβ”€β”€ llm_config.json           # LLM provider configuration
β”œβ”€β”€ prompts.json              # System prompts
└── .env.example              # Environment template

πŸ”§ Development

Run Tests

cargo test --workspace

Run Benchmark

./scripts/run_benchmark.sh

Check Code

cargo check
cargo clippy

Format Code

cargo fmt

πŸ› Troubleshooting

Port 3000 Already in Use

lsof -ti:3000 | xargs kill -9

Ollama Models Not Loading

# Models are auto-copied from ~/.ollama if available
# Otherwise, pull manually:
docker exec -it ollama ollama pull qwen3:8b
docker exec -it ollama ollama pull mxbai-embed-large

SurrealDB Connection Issues

# Check container health
docker exec surrealdb /surreal isready --conn http://localhost:8000

# Restart if needed
docker-compose restart surrealdb

MinIO Access

Default credentials: minioadmin / minioadmin

Console: http://localhost:9001

Docker Daemon Issues

If using both Podman Desktop and OrbStack, ensure only one is running:

# Check current Docker context
docker context ls

# Switch to OrbStack
docker context use orbstack

πŸ“„ License

MIT License

πŸ‘₯ Contributors

Mike Graham

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors