provider.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /**
  2. * provider.ts - Embedding provider abstraction
  3. *
  4. * Defines the EmbeddingProvider interface that allows qmd to use either:
  5. * - LocalLlamaCppProvider (legacy, GGUF via node-llama-cpp)
  6. * - OpenAIEmbeddingsProvider (HTTP, OpenAI-compatible endpoint like ai.mm.mk)
  7. *
  8. * The factory in `./factory.ts` selects an implementation based on env vars,
  9. * a CLI flag, or `~/.config/qmd/config.json`.
  10. */
  11. /**
  12. * Error thrown when the provider's reported model id does not match the
  13. * model id baked into existing `content_vectors` rows. Forces user to
  14. * re-embed (`qmd embed -f`) or pin the matching model id.
  15. */
  16. export class ModelMismatchError extends Error {
  17. providerModel;
  18. existingModels;
  19. constructor(providerModel, existingModels) {
  20. const list = existingModels.join(", ");
  21. super(`Embedding model mismatch: existing vectors use model(s) [${list}] ` +
  22. `but the configured provider reports "${providerModel}". ` +
  23. `Run \`qmd embed -f\` (or \`--rebuild\`) to re-embed everything with ` +
  24. `the new model, or set QMD_EMBED_MODEL_ID="${existingModels[0] ?? ""}" ` +
  25. `to keep the existing vectors.`);
  26. this.name = "ModelMismatchError";
  27. this.providerModel = providerModel;
  28. this.existingModels = existingModels;
  29. }
  30. }
  31. /**
  32. * Verify that the provider's model id is compatible with the existing
  33. * `content_vectors` entries. Pass-through (no-op) if the table is empty
  34. * (fresh DB) or if the model id appears in the distinct set.
  35. *
  36. * Caller passes `existingModels` (typically result of
  37. * `SELECT DISTINCT model FROM content_vectors`).
  38. */
  39. export function assertModelCompatible(providerModel, existingModels) {
  40. // Empty DB — nothing to compare against, anything goes.
  41. if (existingModels.length === 0)
  42. return;
  43. if (existingModels.includes(providerModel))
  44. return;
  45. throw new ModelMismatchError(providerModel, existingModels);
  46. }