mirror of
https://github.com/giancarloerra/socraticode.git
synced 2026-07-03 14:05:21 +02:00
332ee800a8
LM Studio's Local Server speaks the OpenAI-compatible /v1/embeddings protocol, so users running it as their model host (chat plus embedding in one desktop app, GGUF model management) had no clean integration path. Changes: - src/services/provider-lmstudio.ts: new LMStudioEmbeddingProvider wrapping the OpenAI SDK with a custom baseURL (default http://localhost:1234/v1). Sends a placeholder API key to satisfy the OpenAI SDK while LM Studio's Local Server runs without auth by default. Skips the dimensions parameter because LM Studio models have no Matryoshka projection. Forces encoding_format=float to defeat the OpenAI SDK 6.x base64 default, which would otherwise mangle LM Studio's plain-array responses into 1024 zeros. - src/services/embedding-config.ts: extends the EmbeddingProvider union, reads LMSTUDIO_URL and LMSTUDIO_API_KEY, fail-fast validation when EMBEDDING_PROVIDER=lmstudio without EMBEDDING_MODEL or EMBEDDING_DIMENSIONS. - src/services/embedding-provider.ts: factory case for lmstudio with a dynamic import to avoid loading the OpenAI SDK at startup for ollama users. - ensureReady distinguishes "LM Studio unreachable" from "reachable but embedding model not loaded" so the operator knows whether to start the Local Server or load the configured model. - src/services/qdrant.ts: minor refactor to extract the hybrid-search query payload to a local const for readability. - README.md: dedicated LM Studio section, MCP host config example, env-var table entries. - tests/unit/embedding-config.test.ts: 8 new cases (required-env validation, URL default and override, optional API key, context-length override). - tests/unit/embedding-provider.test.ts: 3 new cases (factory wiring, ensureReady error format against a closed port, healthCheck unreachable output). Backward compatible. The lmstudio provider is opt-in via EMBEDDING_PROVIDER=lmstudio. Existing ollama, openai, and google paths are untouched.
85 lines
3.2 KiB
TypeScript
85 lines
3.2 KiB
TypeScript
// SPDX-License-Identifier: AGPL-3.0-only
|
|
// Copyright (C) 2026 Giancarlo Erra - Altaire Limited
|
|
/**
|
|
* Embedding provider factory.
|
|
*
|
|
* All embedding providers implement the EmbeddingProvider interface (defined
|
|
* in embedding-types.ts), allowing the rest of the codebase to be agnostic
|
|
* about which backend generates the vectors.
|
|
*
|
|
* Providers:
|
|
* - ollama (default) — local Ollama (Docker or external)
|
|
* - openai — OpenAI Embeddings API (text-embedding-3-small, etc.)
|
|
* - google — Google Generative AI Embedding API (gemini-embedding-001, etc.)
|
|
* - lmstudio — local LM Studio server via OpenAI-compatible API
|
|
*/
|
|
|
|
import type { InfraProgressCallback } from "./docker.js";
|
|
import { getEmbeddingConfig, type EmbeddingProvider as ProviderName } from "./embedding-config.js";
|
|
import { logger } from "./logger.js";
|
|
|
|
// Re-export types so existing consumers don't need to change their imports.
|
|
export type { EmbeddingHealthStatus, EmbeddingProvider, EmbeddingReadinessResult } from "./embedding-types.js";
|
|
|
|
import type { EmbeddingProvider } from "./embedding-types.js";
|
|
|
|
// ── Factory ──────────────────────────────────────────────────────────────
|
|
|
|
let _provider: EmbeddingProvider | null = null;
|
|
let _providerName: ProviderName | null = null;
|
|
|
|
/**
|
|
* Get (or create) the active embedding provider singleton.
|
|
* Provider is selected based on EMBEDDING_PROVIDER env var.
|
|
* @param onProgress Optional callback for reporting infrastructure setup progress
|
|
*/
|
|
export async function getEmbeddingProvider(onProgress?: InfraProgressCallback): Promise<EmbeddingProvider> {
|
|
const config = getEmbeddingConfig();
|
|
|
|
// Recreate if provider changed (e.g. after config reset in tests)
|
|
if (_provider && _providerName === config.embeddingProvider) {
|
|
return _provider;
|
|
}
|
|
|
|
const name = config.embeddingProvider;
|
|
logger.info("Initializing embedding provider", { provider: name });
|
|
|
|
switch (name) {
|
|
case "ollama": {
|
|
// Dynamic imports avoid loading all provider SDKs at startup.
|
|
const { OllamaEmbeddingProvider } = await import("./provider-ollama.js");
|
|
_provider = new OllamaEmbeddingProvider(onProgress);
|
|
break;
|
|
}
|
|
case "openai": {
|
|
const { OpenAIEmbeddingProvider } = await import("./provider-openai.js");
|
|
_provider = new OpenAIEmbeddingProvider();
|
|
break;
|
|
}
|
|
case "google": {
|
|
const { GoogleEmbeddingProvider } = await import("./provider-google.js");
|
|
_provider = new GoogleEmbeddingProvider();
|
|
break;
|
|
}
|
|
case "lmstudio": {
|
|
const { LMStudioEmbeddingProvider } = await import("./provider-lmstudio.js");
|
|
_provider = new LMStudioEmbeddingProvider();
|
|
break;
|
|
}
|
|
default:
|
|
throw new Error(
|
|
`Unknown embedding provider: "${name}". Must be "ollama", "openai", "google", or "lmstudio".`,
|
|
);
|
|
}
|
|
|
|
_providerName = name;
|
|
// biome-ignore lint/style/noNonNullAssertion: _provider is guaranteed set by the switch above
|
|
return _provider!;
|
|
}
|
|
|
|
/** Reset the provider singleton (for testing). */
|
|
export function resetEmbeddingProvider(): void {
|
|
_provider = null;
|
|
_providerName = null;
|
|
}
|