Skip to content

training

module wittgenstein_pgvector_qsql.training

TrainingService — registers Q→SQL pairs with dedup guard and optional chart config.

Classes

  • TrainResult — Result of a train_qsql call.

  • TrainingService — Trains Q→SQL pairs into the pgvector store, with SQL-level dedup.

Functions

  • build_training_service — Convenience factory: creates QSQLVectorStore + SqlDedupRepository and wires them.

wittgenstein_pgvector_qsql.training.TrainResult

class TrainResult()

Result of a train_qsql call.

existing_question is populated when status='skipped', showing which question originally indexed this SQL.

wittgenstein_pgvector_qsql.training.TrainingService

class TrainingService(store: QSQLVectorStore, repository: SqlDedupRepository)

Trains Q→SQL pairs into the pgvector store, with SQL-level dedup.

Accepts collaborators via constructor DI so tests can inject mocks. Use build_training_service(connection_string, embedder) for production.

Parameters

  • store : QSQLVectorStore — QSQLVectorStore that persists question→sql embeddings.

  • repository : SqlDedupRepository — SqlDedupRepository that tracks already-indexed SQL hashes.

Methods

  • train_qsql — Index a Q→SQL pair into the vector store, skipping duplicates.

  • list_dedup — Return the dedup table as a DataFrame for auditing.

  • find_chart_for_sql — Return the chart_config trained for a SQL, or None if not stored.

wittgenstein_pgvector_qsql.training.TrainingService.train_qsql

method TrainingService.train_qsql(question: str, sql: str, *, chart: dict[str, Any] | None = None, dry_run: bool = False) → TrainResult

Index a Q→SQL pair into the vector store, skipping duplicates.

Flow

  1. Hash the normalized SQL → check dedup table.
  2. If already present → return status='skipped'.
  3. If dry_run=True → return status='dry_run' without writing.
  4. Otherwise → embed question + SQL into the store, write dedup entry.

wittgenstein_pgvector_qsql.training.TrainingService.list_dedup

method TrainingService.list_dedup() → pd.DataFrame

Return the dedup table as a DataFrame for auditing.

wittgenstein_pgvector_qsql.training.TrainingService.find_chart_for_sql

method TrainingService.find_chart_for_sql(sql: str) → dict[str, Any] | None

Return the chart_config trained for a SQL, or None if not stored.

wittgenstein_pgvector_qsql.training.build_training_service

build_training_service(connection_string: str, pg_dsn: str, embedder: Any) → TrainingService

Convenience factory: creates QSQLVectorStore + SqlDedupRepository and wires them.

Parameters

  • connection_string : str — psycopg3 URL for PGVector (e.g. 'postgresql+psycopg://user:pass@host/db').

  • pg_dsn : str — psycopg2 DSN for dedup table (e.g. 'postgresql://user:pass@host:port/db').

  • embedder : Any — LangChain Embeddings instance.