Skip to content

feat(samples): add Dozer + LLM + Vector DB + LangChain real-time RAG sample (#1690) - #2563

Open
andreikharytonenka-lab wants to merge 1 commit into
getdozer:mainfrom
andreikharytonenka-lab:feat/dozer-llm-vector-langchain-sample-1690
Open

andreikharytonenka-lab wants to merge 1 commit into
getdozer:mainfrom
andreikharytonenka-lab:feat/dozer-llm-vector-langchain-sample-1690

Conversation

@andreikharytonenka-lab

Copy link
Copy Markdown

Resolves #1690
/claim #1690

Summary

Adds a complete, self-contained Dozer + LLM + Vector Database + LangChain real-time Retrieval-Augmented Generation (RAG) banking & credit-card advisory sample in examples/llm-vector-langchain/, implementing the architecture from the Dozer engineering article Hyper-personalization with LLMs, Vector Databases, and Dozer.


Architecture & Component Breakdown

PostgreSQL 15 (wal_level=logical, REPLICA IDENTITY FULL)
  ├── customers              (credit_score, annual_income, account_tier, preferred_reward_type)
  ├── transactions           (merchant_name, category, amount, txn_timestamp)
  ├── credit_card_products   (reward_rate_pct, annual_fee, apr_pct, min_credit_score, min_annual_income)
  └── documents              (card disclosures, lounge guides, fee waiver & credit-builder policies)
         │
         ▼  PostgreSQL Logical Replication (CDC)
Dozer Real-Time Data Engine (dozer-config.yaml — REST :8080 / gRPC :50051)
  ├── Streaming SQL View 1: `customer_spending_profiles` (/customer_profiles)
  │     LEFT JOIN customers + transactions -> real-time category spend aggregations
  │     (travel_spend, dining_spend, groceries_spend, tech_spend, total_spend, transaction_count)
  ├── Streaming SQL View 2: `eligible_card_recommendations` (/eligible_cards)
  │     INNER JOIN customers + credit_card_products
  │     ON c.credit_score >= p.min_credit_score AND c.annual_income >= p.min_annual_income
  └── Direct Indexed Endpoints: /documents, /credit_card_products, /transactions, /customers
         │                                           │
         │ Structured Real-Time Context              │ Incremental CDC Events (INSERT / UPDATE / DELETE)
         ▼                                           ▼
  DozerClient (/query, /count)            DozerCDCSynchronizer -> DozerLangChainVectorStore
         │                                (Qdrant / In-Memory Vector Index + Metadata Filtering)
         └───────────────────────┬───────────────────┘
                                 ▼
              DozerBankingRAGPipeline (LangChain LCEL)
              `ChatPromptTemplate | ChatOpenAI / RuleBasedBankingLLM | StrOutputParser`
              (Computes annualized net reward value ($) from live Dozer spend + `[DOC-ID]` citations)

Key Highlights

  1. Canonical dozer-config.yaml Schema (examples/llm-vector-langchain/dozer-config.yaml):

    • Uses canonical config: !Postgres tag connection (banking_postgres) and enables push_events: true, dynamic: true, grpc_web: true.
    • Defines 4 PostgreSQL CDC source tables (customers, transactions, credit_card_products, documents).
    • Uses top-level streaming sql: with SELECT ... INTO customer_spending_profiles and SELECT ... INTO eligible_card_recommendations.
    • Exposes 6 indexed endpoints (/customer_profiles, /eligible_cards, /credit_card_products, /documents, /transactions, /customers) with explicit index.primary_key definitions.
  2. PostgreSQL Logical Replication Seed (examples/llm-vector-langchain/seed.sql):

    • Configures ALTER TABLE ... REPLICA IDENTITY FULL; on all 4 source tables so PostgreSQL emits full before/after row payloads for Dozer CDC.
    • Seeds 4 banking customer personas (CUST-001 Ava Sharma, CUST-002 Marcus Vance, CUST-003 Elena Rostova, CUST-004 David Chen), 5 credit card tiers, 14 categorized transactions, and 8 detailed policy & product disclosure documents (DOC-001 through DOC-008).
  3. Dozer REST & Incremental CDC Synchronizer (examples/llm-vector-langchain/dozer_client.py):

    • load_and_validate_dozer_config(): PyYAML !Postgres tag loader and schema validator ensuring all sources, SELECT ... INTO views, and endpoint primary keys are valid.
    • DozerClient: queries Dozer REST endpoints (POST /<endpoint>/query and POST /<endpoint>/count with $filter, $order_by, $limit, $skip) and includes an InMemoryDozerBackend fallback that parses seed.sql and evaluates the exact streaming SQL views for zero-dependency CI/offline testing.
    • DozerCDCSynchronizer: tracks primary-key state snapshots and dispatches incremental CDCEvent mutations (CDCOperation.INSERT, CDCOperation.UPDATE, CDCOperation.DELETE) to downstream vector store listeners.
  4. LangChain VectorStore with Real-Time CDC Index Mutations (examples/llm-vector-langchain/vector_store.py):

    • DozerLangChainVectorStore(VectorStore): full langchain_core.vectorstores.VectorStore implementation supporting real-time apply_cdc_event() (INSERT, UPDATE, DELETE) without full corpus re-indexing, plus credit-score eligibility metadata filtering (max_min_credit_score and card_ids).
    • DeterministicBankingEmbeddings(Embeddings): 128-dimensional langchain_core.embeddings.Embeddings implementation for deterministic local & CI semantic retrieval (while supporting OpenAIEmbeddings / ChatOpenAI when OPENAI_API_KEY is provided).
  5. LangChain LCEL Hyper-Personalized RAG Pipeline (examples/llm-vector-langchain/rag_pipeline.py):

    • Fuses live Dozer /customer_profiles and /eligible_cards with vector-retrieved policy documents.
    • Ranks eligible credit cards by estimated annualized net reward benefit ($) computed directly from the customer's live Dozer category spend (travel_spend, dining_spend, groceries_spend, tech_spend) minus annual_fee.
    • Immediately reacts to streaming CDC events across /customers, /transactions, and /documents.
  6. Docker Compose & CLI (docker-compose.yml, Dockerfile, app.py):

    • docker-compose.yml orchestrates PostgreSQL 15 (wal_level=logical), Qdrant (v1.9.0), Dozer (getdozer/dozer:latest), and the Python RAG service.
    • app.py supports --mode demo, --mode ask, and --mode sync (both against live Dozer :8080 and with --offline).

Verification & Automated Test Suite (pytest)

Added 17 comprehensive unit and integration tests in examples/llm-vector-langchain/tests/test_dozer_rag.py:

$ pytest -v examples/llm-vector-langchain/tests/test_dozer_rag.py
============================= test session starts =============================
collected 17 items

examples/llm-vector-langchain/tests/test_dozer_rag.py::test_dozer_config_yaml_valid_schema PASSED [  5%]
examples/llm-vector-langchain/tests/test_dozer_rag.py::test_dozer_config_rejects_invalid_inline_endpoint_sql PASSED [ 11%]
examples/llm-vector-langchain/tests/test_dozer_rag.py::test_seed_sql_tables_and_records PASSED [ 17%]
examples/llm-vector-langchain/tests/test_dozer_client_query_and_count PASSED [ 23%]
examples/llm-vector-langchain/tests/test_dozer_rag.py::test_dozer_sql_spending_aggregation_accuracy PASSED [ 29%]
examples/llm-vector-langchain/tests/test_dozer_rag.py::test_dozer_sql_card_eligibility_filtering PASSED [ 35%]
examples/llm-vector-langchain/tests/test_dozer_rag.py::test_cdc_synchronizer_initial_snapshot_and_incremental_diff PASSED [ 41%]
examples/llm-vector-langchain/tests/test_dozer_rag.py::test_vector_store_semantic_search_and_metadata_filter PASSED [ 47%]
examples/llm-vector-langchain/tests/test_dozer_rag.py::test_vector_store_cdc_insert_event PASSED [ 52%]
examples/llm-vector-langchain/tests/test_dozer_rag.py::test_vector_store_cdc_update_event PASSED [ 58%]
examples/llm-vector-langchain/tests/test_dozer_rag.py::test_vector_store_cdc_delete_event PASSED [ 64%]
examples/llm-vector-langchain/tests/test_dozer_rag.py::test_rag_pipeline_hyper_personalization_travel_customer PASSED [ 70%]
examples/llm-vector-langchain/tests/test_dozer_rag.py::test_rag_pipeline_hyper_personalization_groceries_customer PASSED [ 76%]
examples/llm-vector-langchain/tests/test_dozer_rag.py::test_rag_pipeline_hyper_personalization_credit_builder PASSED [ 82%]
examples/llm-vector-langchain/tests/test_dozer_rag.py::test_rag_pipeline_hyper_personalization_tech_executive_customer PASSED [ 88%]
examples/llm-vector-langchain/tests/test_dozer_rag.py::test_rag_pipeline_realtime_cdc_transaction_and_credit_score_upgrade PASSED [ 94%]
examples/llm-vector-langchain/tests/test_dozer_rag.py::test_cli_demo_and_ask_execution PASSED [100%]

============================= 17 passed in 0.60s ==============================

@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.


Caliber Foundry seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Sample: Dozer + LLM + Vector database + Langchain sample

2 participants