mirror of
https://github.com/giancarloerra/socraticode.git
synced 2026-07-03 14:05:21 +02:00
feat(embeddings): add LM Studio as a first-class embedding provider (#42)
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.
This commit is contained in:
@@ -22,6 +22,8 @@ describe("embedding-config", () => {
|
||||
delete process.env.OLLAMA_API_KEY;
|
||||
delete process.env.OPENAI_API_KEY;
|
||||
delete process.env.GOOGLE_API_KEY;
|
||||
delete process.env.LMSTUDIO_URL;
|
||||
delete process.env.LMSTUDIO_API_KEY;
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
@@ -178,6 +180,7 @@ describe("embedding-config", () => {
|
||||
embeddingProvider: "ollama",
|
||||
ollamaMode: "external",
|
||||
ollamaUrl: "http://remote-gpu:11434",
|
||||
lmstudioUrl: "http://localhost:1234/v1",
|
||||
embeddingModel: "mxbai-embed-large",
|
||||
embeddingDimensions: 1024,
|
||||
embeddingContextLength: 512,
|
||||
@@ -215,7 +218,7 @@ describe("embedding-config", () => {
|
||||
it("throws for invalid EMBEDDING_PROVIDER", () => {
|
||||
process.env.EMBEDDING_PROVIDER = "anthropic";
|
||||
expect(() => loadEmbeddingConfig()).toThrow(
|
||||
'Invalid EMBEDDING_PROVIDER: "anthropic". Must be "ollama", "openai", or "google".',
|
||||
'Invalid EMBEDDING_PROVIDER: "anthropic". Must be "ollama", "openai", "google", or "lmstudio".',
|
||||
);
|
||||
});
|
||||
|
||||
@@ -248,4 +251,82 @@ describe("embedding-config", () => {
|
||||
expect(config.embeddingContextLength).toBe(4096);
|
||||
});
|
||||
});
|
||||
|
||||
describe("lmstudio provider", () => {
|
||||
it("loads when EMBEDDING_MODEL and EMBEDDING_DIMENSIONS are set", () => {
|
||||
process.env.EMBEDDING_PROVIDER = "lmstudio";
|
||||
process.env.EMBEDDING_MODEL = "nomic-embed-text-v1.5";
|
||||
process.env.EMBEDDING_DIMENSIONS = "768";
|
||||
|
||||
const config = loadEmbeddingConfig();
|
||||
expect(config.embeddingProvider).toBe("lmstudio");
|
||||
expect(config.embeddingModel).toBe("nomic-embed-text-v1.5");
|
||||
expect(config.embeddingDimensions).toBe(768);
|
||||
});
|
||||
|
||||
it("defaults LMSTUDIO_URL to http://localhost:1234/v1", () => {
|
||||
process.env.EMBEDDING_PROVIDER = "lmstudio";
|
||||
process.env.EMBEDDING_MODEL = "nomic-embed-text-v1.5";
|
||||
process.env.EMBEDDING_DIMENSIONS = "768";
|
||||
|
||||
const config = loadEmbeddingConfig();
|
||||
expect(config.lmstudioUrl).toBe("http://localhost:1234/v1");
|
||||
});
|
||||
|
||||
it("respects LMSTUDIO_URL override", () => {
|
||||
process.env.EMBEDDING_PROVIDER = "lmstudio";
|
||||
process.env.EMBEDDING_MODEL = "nomic-embed-text-v1.5";
|
||||
process.env.EMBEDDING_DIMENSIONS = "768";
|
||||
process.env.LMSTUDIO_URL = "http://gpu-rig.local:5678/v1";
|
||||
|
||||
const config = loadEmbeddingConfig();
|
||||
expect(config.lmstudioUrl).toBe("http://gpu-rig.local:5678/v1");
|
||||
});
|
||||
|
||||
it("throws when EMBEDDING_MODEL is missing", () => {
|
||||
process.env.EMBEDDING_PROVIDER = "lmstudio";
|
||||
process.env.EMBEDDING_DIMENSIONS = "768";
|
||||
|
||||
expect(() => loadEmbeddingConfig()).toThrow(
|
||||
/EMBEDDING_MODEL is required when EMBEDDING_PROVIDER=lmstudio/,
|
||||
);
|
||||
});
|
||||
|
||||
it("throws when EMBEDDING_DIMENSIONS is missing", () => {
|
||||
process.env.EMBEDDING_PROVIDER = "lmstudio";
|
||||
process.env.EMBEDDING_MODEL = "nomic-embed-text-v1.5";
|
||||
|
||||
expect(() => loadEmbeddingConfig()).toThrow(
|
||||
/EMBEDDING_DIMENSIONS is required when EMBEDDING_PROVIDER=lmstudio/,
|
||||
);
|
||||
});
|
||||
|
||||
it("includes example dimensions in the error message for discoverability", () => {
|
||||
process.env.EMBEDDING_PROVIDER = "lmstudio";
|
||||
process.env.EMBEDDING_MODEL = "nomic-embed-text-v1.5";
|
||||
|
||||
expect(() => loadEmbeddingConfig()).toThrow(
|
||||
/768 for nomic-embed-text-v1\.5/,
|
||||
);
|
||||
});
|
||||
|
||||
it("does not require LMSTUDIO_API_KEY", () => {
|
||||
process.env.EMBEDDING_PROVIDER = "lmstudio";
|
||||
process.env.EMBEDDING_MODEL = "nomic-embed-text-v1.5";
|
||||
process.env.EMBEDDING_DIMENSIONS = "768";
|
||||
// Intentionally no LMSTUDIO_API_KEY — LM Studio's Local Server has no auth by default.
|
||||
|
||||
expect(() => loadEmbeddingConfig()).not.toThrow();
|
||||
});
|
||||
|
||||
it("respects EMBEDDING_CONTEXT_LENGTH override", () => {
|
||||
process.env.EMBEDDING_PROVIDER = "lmstudio";
|
||||
process.env.EMBEDDING_MODEL = "qwen3-embedding-8b";
|
||||
process.env.EMBEDDING_DIMENSIONS = "4096";
|
||||
process.env.EMBEDDING_CONTEXT_LENGTH = "32768";
|
||||
|
||||
const config = loadEmbeddingConfig();
|
||||
expect(config.embeddingContextLength).toBe(32768);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -19,6 +19,8 @@ describe("embedding-provider", () => {
|
||||
delete process.env.OLLAMA_API_KEY;
|
||||
delete process.env.OPENAI_API_KEY;
|
||||
delete process.env.GOOGLE_API_KEY;
|
||||
delete process.env.LMSTUDIO_URL;
|
||||
delete process.env.LMSTUDIO_API_KEY;
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
@@ -45,6 +47,14 @@ describe("embedding-provider", () => {
|
||||
expect(provider.name).toBe("google");
|
||||
});
|
||||
|
||||
it("creates LMStudioEmbeddingProvider when configured", async () => {
|
||||
process.env.EMBEDDING_PROVIDER = "lmstudio";
|
||||
process.env.EMBEDDING_MODEL = "nomic-embed-text-v1.5";
|
||||
process.env.EMBEDDING_DIMENSIONS = "768";
|
||||
const provider = await getEmbeddingProvider();
|
||||
expect(provider.name).toBe("lmstudio");
|
||||
});
|
||||
|
||||
it("caches provider instance", async () => {
|
||||
const p1 = await getEmbeddingProvider();
|
||||
const p2 = await getEmbeddingProvider();
|
||||
@@ -133,3 +143,61 @@ describe("GoogleEmbeddingProvider", () => {
|
||||
expect(health.statusLines.some((l) => l.includes("Missing"))).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("LMStudioEmbeddingProvider", () => {
|
||||
const originalEnv = { ...process.env };
|
||||
|
||||
beforeEach(() => {
|
||||
resetEmbeddingConfig();
|
||||
resetEmbeddingProvider();
|
||||
delete process.env.EMBEDDING_PROVIDER;
|
||||
delete process.env.EMBEDDING_MODEL;
|
||||
delete process.env.EMBEDDING_DIMENSIONS;
|
||||
delete process.env.EMBEDDING_CONTEXT_LENGTH;
|
||||
delete process.env.LMSTUDIO_URL;
|
||||
delete process.env.LMSTUDIO_API_KEY;
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
resetEmbeddingConfig();
|
||||
resetEmbeddingProvider();
|
||||
process.env = { ...originalEnv };
|
||||
});
|
||||
|
||||
it("ensureReady throws an actionable error when LM Studio is unreachable", async () => {
|
||||
process.env.EMBEDDING_PROVIDER = "lmstudio";
|
||||
process.env.EMBEDDING_MODEL = "nomic-embed-text-v1.5";
|
||||
process.env.EMBEDDING_DIMENSIONS = "768";
|
||||
// Point at a deliberately closed port so the request fails fast.
|
||||
process.env.LMSTUDIO_URL = "http://127.0.0.1:1/v1";
|
||||
|
||||
const provider = await getEmbeddingProvider();
|
||||
await expect(provider.ensureReady()).rejects.toThrow(
|
||||
/LM Studio is not reachable at http:\/\/127\.0\.0\.1:1\/v1/,
|
||||
);
|
||||
});
|
||||
|
||||
it("healthCheck reports unreachable LM Studio without throwing", async () => {
|
||||
process.env.EMBEDDING_PROVIDER = "lmstudio";
|
||||
process.env.EMBEDDING_MODEL = "nomic-embed-text-v1.5";
|
||||
process.env.EMBEDDING_DIMENSIONS = "768";
|
||||
process.env.LMSTUDIO_URL = "http://127.0.0.1:1/v1";
|
||||
|
||||
const provider = await getEmbeddingProvider();
|
||||
const health = await provider.healthCheck();
|
||||
|
||||
expect(health.available).toBe(false);
|
||||
expect(health.modelReady).toBe(false);
|
||||
expect(health.statusLines.some((l) => l.includes("LM Studio") && l.includes("Not reachable"))).toBe(true);
|
||||
});
|
||||
|
||||
it("does not require LMSTUDIO_API_KEY to construct the provider", async () => {
|
||||
process.env.EMBEDDING_PROVIDER = "lmstudio";
|
||||
process.env.EMBEDDING_MODEL = "nomic-embed-text-v1.5";
|
||||
process.env.EMBEDDING_DIMENSIONS = "768";
|
||||
// Intentionally no LMSTUDIO_API_KEY.
|
||||
|
||||
const provider = await getEmbeddingProvider();
|
||||
expect(provider.name).toBe("lmstudio");
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user