Skip to content

shanemeister/genai-agent-core

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

64 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🧠 GenAI Agent Core β€” RAG Pipeline with Mixtral, LLaMA3, and OpenAI

CI

CI: see .github/workflows/ci.yml. Currently a stub workflow (real pytest job blocked on NOES-54). Branch protection on main requires CI pass before merge β€” admin (rsync deploy) can still push directly.

This project implements a production-ready Retrieval-Augmented Generation (RAG) assistant using LangChain, FAISS, and three model options: Mixtral (GGUF), LLaMA3 (Hugging Face Transformers), and GPT-4o (OpenAI). It supports both CLI and FastAPI interfaces for real-time question answering over embedded documents.

⚠️ Note: This is a continuous work in progress as time allows. Please check back frequently for update.

βœ… Designed for technical interviews, enterprise search, or AI assistant prototyping β€” fully offline-capable and extensible.

πŸš‰ Platform: This system is built on a local machine to avoid cloud compute costs. However, it can be easily transported to any cloud such as AWS, GCP, or Azure. To run locally requires a rather beefy workstation/server. I developed it using a system with the following specs:

  • CPU: Threadripper 64 Core, 128 Threads
  • Memory: 512 GB DDR5
  • Disk: 20 TB NVMe SSD
  • GPU: Two Nvidia A6000 GPUs

βœ… Key Features

  • πŸ” RAG architecture: combines retrieval + generation for grounded, explainable answers.
  • πŸ€– Local LLM inference: runs via llama.cpp or Hugging Face Transformers.
  • 🧠 Semantic vector search: uses sentence-transformers and FAISS.
  • πŸ“† FastAPI backend: exposes /ask and /rebuild endpoints with JSON responses.
  • πŸ’ͺ Session memory: persists chat history in PostgreSQL.
  • πŸ“ Document ingestion: supports .pdf, .docx, and .txt files via directory-based loader.
  • πŸ§ͺ Swagger docs: self-documenting API.
  • πŸ” Offline mode: models can run fully locally with no external API dependencies.
  • πŸ”’ Token estimation: optional for local models like LLaMA3 and Mixtral.
  • 🧾 Model logging: API metadata includes model used and latency.

πŸ“ Project Structure

β”œβ”€β”€ app
β”‚   β”œβ”€β”€ agents
β”‚   β”‚   β”œβ”€β”€ doc_agent.py
β”‚   β”‚   └── orchestrator.py
β”‚   β”œβ”€β”€ chains
β”‚   β”‚   └── qa_chain.py
β”‚   β”œβ”€β”€ configs
β”‚   β”‚   β”œβ”€β”€ llm_config.yaml
β”‚   β”‚   β”œβ”€β”€ pdf_tags.yml
β”‚   β”‚   └── prompts
β”‚   β”œβ”€β”€ embeddings
β”‚   β”‚   └── embedder.py
β”‚   β”œβ”€β”€ interface
β”‚   β”‚   β”œβ”€β”€ __pycache__
β”‚   β”‚   β”‚   β”œβ”€β”€ query_eval.cpython-310.pyc
β”‚   β”‚   β”‚   └── query_plus.cpython-310.pyc
β”‚   β”‚   β”œβ”€β”€ query_eval.py
β”‚   β”‚   β”œβ”€β”€ query_plus.py
β”‚   β”‚   └── query.py
β”‚   β”œβ”€β”€ llm_core.py
β”‚   β”œβ”€β”€ loaders
β”‚   β”‚   └── fhir_loaders.py
β”‚   β”œβ”€β”€ retrievers
β”‚   β”‚   └── vector_retriever.py
β”‚   β”œβ”€β”€ tagging
β”‚   β”‚   └── semantic_tagger.py
β”‚   β”œβ”€β”€ tools
β”‚   β”‚   β”œβ”€β”€ __pycache__
β”‚   β”‚   β”‚   └── summarize.cpython-310.pyc
β”‚   β”‚   └── summarize.py
β”‚   └── vectorstore
β”‚       β”œβ”€β”€ chunk_db.json
β”‚       β”œβ”€β”€ index.faiss
β”‚       └── index.pkl
β”œβ”€β”€ chat
β”‚   β”œβ”€β”€ postgres_history.py
β”‚   β”œβ”€β”€ __pycache__
β”‚   β”‚   β”œβ”€β”€ postgres_history.cpython-310.pyc
β”‚   β”‚   └── vectorstore_memory.cpython-310.pyc
β”‚   └── vectorstore_memory.py
β”œβ”€β”€ config.py
β”œβ”€β”€ data
β”‚   β”œβ”€β”€ csv
β”‚   β”œβ”€β”€ docs
β”‚   β”‚   β”œβ”€β”€ rag_explained.txt
β”‚   β”œβ”€β”€ json
β”‚   β”œβ”€β”€ kindle
β”‚   β”œβ”€β”€ pdfs
β”‚   β”‚   β”œβ”€β”€ 🎬
β”‚   └── text
β”œβ”€β”€ eval
β”‚   β”œβ”€β”€ eval_queries.json 
β”‚   └── eval_queries.jsonl
β”œβ”€β”€ LICENSE
β”œβ”€β”€ main.py
β”œβ”€β”€ models
β”‚   β”œβ”€β”€ download_mixtral.py
β”‚   β”œβ”€β”€ llama3-8b
β”‚   β”‚   β”œβ”€β”€  🎬
β”‚   β”œβ”€β”€ mistral-7b
β”‚   β”‚   └── 🎬
β”‚   β”œβ”€β”€ mixtral-8x7b
β”‚   β”‚   β”œβ”€β”€ 🎬
β”‚   β”œβ”€β”€ mixtral-gguf
β”‚   β”‚   β”œβ”€β”€ 🎬
β”‚   └── model_test.py
β”œβ”€β”€ rag_api
β”œβ”€β”€ rag_api_service.py
β”œβ”€β”€ README.md
β”œβ”€β”€ rebuild_vs.py
β”œβ”€β”€ requirements.txt
β”œβ”€β”€ search_vectorstore.py
β”œβ”€β”€ test.py
└── vectorstore
    β”œβ”€β”€ index.faiss
    └── index.pkl

πŸš€ Quickstart

1. Install dependencies

conda activate genai-core
pip install -r requirements.txt
pip install sentencepiece

2. Add your documents

Drop your .pdf, .docx, or .txt files into data/ (or subfolders).

Then run:

python rebuild_vs.py

This rebuilds the FAISS vector index with semantic embeddings using sentence-transformers.


3. Query via CLI

python app/interface/query_plus.py "What is RAG?" --model mixtral --chat --session-id demo1

Options:

  • --model mixtral|llama3|gpt4o
  • --filter-tag or --filter-file for scoped retrieval
  • --chat enables persistent memory
  • --session-id groups chats by user

4. Query via FastAPI

Start the API:

uvicorn rag_api_service:app --reload

Visit Swagger docs:

http://127.0.0.1:8000/docs

Example POST to /ask:

{
  "query": "What is retrieval-augmented generation?",
  "session_id": "test1",
  "model": "llama3",
  "token_estimate": true
}

Response includes:

  • answer

  • sources[]: source text chunks

  • meta:

    • elapsed_seconds
    • tokens_estimated
    • model

πŸ’‘ Prompt Engineering

Prompts are dynamically constructed using:

  • Retrieved chunks (similarity_search_with_score)
  • Optional session memory
  • Custom headers: "You are an AI document analyst..."

You can customize this in generate_prompt() inside query_plus.py.


🧠 Model Options

  • Mixtral: GGUF model run via llama.cpp (fast, parallelized on GPU)
  • LLaMA 3 8B: Hugging Face Transformers model running in FP16 on GPU
  • GPT-4o: OpenAI fallback using API + YAML config

To use OpenAI:

export OPENAI_API_KEY=sk-...

πŸ’ƒ PostgreSQL Chat History

Chat logs are stored in chathist.chat_history:

  • session_id, role, content, created_at
  • Used for session memory and context carryover

Uses .pgpass for secure auth (no credentials in code).


πŸ“Š Metadata & Monitoring

API responses include:

  • elapsed_seconds: total inference + retrieval time
  • tokens: OpenAI usage count if applicable
  • tokens_estimated: estimated using tokenizer for local models
  • model: which LLM handled the request

πŸ§ͺ Performance Comparison

Model Latency (s) Local Token Usage Notes
Mixtral ~2.7 βœ… Yes estimated Fast + multi-GPU (llama.cpp)
LLaMA3-8B ~11.2 βœ… Yes estimated HuggingFace, accurate, slower
GPT-4o ~1.5–2.5 ❌ API exact via API Fastest, but external dependency

πŸ”’ Testing / Debugging Tools

Tool Command
Search vectorstore python search_vectorstore.py "your query"
Rebuild index python rebuild_vs.py
API query curl -X POST http://127.0.0.1:8000/ask ...

πŸ” Offline-First Capabilities

Set local_files_only=True in AutoTokenizer and AutoModelForCausalLM to enforce offline mode.


🌐 Roadmap

  • βœ… Swagger-enabled API
  • βœ… Chat memory with PostgreSQL
  • βœ… LLaMA 3 support via Transformers
  • βœ… Token estimation for local models
  • βœ… Model logging in metadata
  • ⏳ User auth or API key guardrails
  • ⏳ Vectorstore auto-refresh with S3 uploads
  • ⏳ Web frontend (Streamlit or React)
  • ⏳ GCP Cloud Run deployment config

πŸ‘€ Author

Randall Shane β€” LinkedIn

Senior AI/ML Systems Architect with 20+ years of experience designing scalable, production-grade platforms across healthcare, finance, and enterprise analytics. Most recently led end-to-end GenAI and retrieval-augmented generation (RAG) development using LangChain, FAISS, and OpenAI/GCP models.

This repository reflects my current focus: building secure, modular, and extensible GenAI pipelines that combine local and cloud-hosted LLMs with vector search, prompt engineering, and human-in-the-loop evaluation. I lead senior-level roles in AI/ML engineering or GenAI systems design, particularly where intelligent assistants and clinical/niche use cases are a focus.

About

Framework for GenAI work for general public access

Resources

License

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors