From a8d20cf5cab98a8211fb7008cba8fb3f7b2f8af1 Mon Sep 17 00:00:00 2001 From: lluviaoscuradeldoce-design Date: Thu, 29 Jan 2026 18:01:57 -0500 Subject: [PATCH] feat(memory): support custom embeddings (baseUrl, dimensions) for Qwen compatibility --- extensions/memory-lancedb/config.ts | 11 +++++++++-- extensions/memory-lancedb/index.ts | 9 +++++---- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/extensions/memory-lancedb/config.ts b/extensions/memory-lancedb/config.ts index c0382392f..5f74345c3 100644 --- a/extensions/memory-lancedb/config.ts +++ b/extensions/memory-lancedb/config.ts @@ -7,6 +7,8 @@ export type MemoryConfig = { provider: "openai"; model?: string; apiKey: string; + baseUrl?: string; + dimensions?: number; }; dbPath?: string; autoCapture?: boolean; @@ -54,7 +56,10 @@ function resolveEnvVars(value: string): string { function resolveEmbeddingModel(embedding: Record): string { const model = typeof embedding.model === "string" ? embedding.model : DEFAULT_MODEL; - vectorDimsForModel(model); + // Only validate model if dimensions not explicitly provided + if (typeof embedding.dimensions !== "number") { + vectorDimsForModel(model); + } return model; } @@ -70,7 +75,7 @@ export const memoryConfigSchema = { if (!embedding || typeof embedding.apiKey !== "string") { throw new Error("embedding.apiKey is required"); } - assertAllowedKeys(embedding, ["apiKey", "model"], "embedding config"); + assertAllowedKeys(embedding, ["apiKey", "model", "baseUrl", "dimensions"], "embedding config"); const model = resolveEmbeddingModel(embedding); @@ -79,6 +84,8 @@ export const memoryConfigSchema = { provider: "openai", model, apiKey: resolveEnvVars(embedding.apiKey), + baseUrl: typeof embedding.baseUrl === "string" ? embedding.baseUrl : undefined, + dimensions: typeof embedding.dimensions === "number" ? embedding.dimensions : undefined, }, dbPath: typeof cfg.dbPath === "string" ? cfg.dbPath : DEFAULT_DB_PATH, autoCapture: cfg.autoCapture !== false, diff --git a/extensions/memory-lancedb/index.ts b/extensions/memory-lancedb/index.ts index e7daab6f5..ebe0574e3 100644 --- a/extensions/memory-lancedb/index.ts +++ b/extensions/memory-lancedb/index.ts @@ -52,7 +52,7 @@ class MemoryDB { constructor( private readonly dbPath: string, private readonly vectorDim: number, - ) {} + ) { } private async ensureInitialized(): Promise { if (this.table) return; @@ -156,8 +156,9 @@ class Embeddings { constructor( apiKey: string, private model: string, + baseURL?: string, ) { - this.client = new OpenAI({ apiKey }); + this.client = new OpenAI({ apiKey, baseURL }); } async embed(text: string): Promise { @@ -223,9 +224,9 @@ const memoryPlugin = { register(api: MoltbotPluginApi) { const cfg = memoryConfigSchema.parse(api.pluginConfig); const resolvedDbPath = api.resolvePath(cfg.dbPath!); - const vectorDim = vectorDimsForModel(cfg.embedding.model ?? "text-embedding-3-small"); + const vectorDim = cfg.embedding.dimensions ?? vectorDimsForModel(cfg.embedding.model ?? "text-embedding-3-small"); const db = new MemoryDB(resolvedDbPath, vectorDim); - const embeddings = new Embeddings(cfg.embedding.apiKey, cfg.embedding.model!); + const embeddings = new Embeddings(cfg.embedding.apiKey, cfg.embedding.model!, cfg.embedding.baseUrl); api.logger.info( `memory-lancedb: plugin registered (db: ${resolvedDbPath}, lazy init)`,