Compare commits

..
Author SHA1 Message Date
kartik-mem0 65f42bbf43 Merge branch 'main' into fix/lmstudio-embeddings-base-url 2026-08-14 17:13:37 +05:30
kartik-mem0 2b8e4ea059 fix(llms/lmstudio): honor LMSTUDIO_BASE_URL in the LLM too
The embedder fix in this branch left the sibling LLM still hardcoded, so
a self-hosted LM Studio behind a non-default host worked for embeddings
and silently fell back to localhost for generation.

LMStudioConfig baked "http://localhost:1234/v1" into __init__, which
makes any env-var lookup downstream dead code. The field now defaults to
None and LMStudioLLM resolves config value, then LMSTUDIO_BASE_URL, then
the localhost default, matching the embedder and DeepSeek.
2026-08-13 19:45:57 +05:30
kartik-mem0 a5dab4279b fix(ts-oss): honor LMSTUDIO_BASE_URL env var in LM Studio LLM and embedder
Both the TS OSS LM Studio LLM and embedder hardcoded the base URL default
without checking the LMSTUDIO_BASE_URL environment variable, unlike the
Together provider which already supports TOGETHER_API_BASE. Precedence is
now explicit config value > LMSTUDIO_BASE_URL env var > existing default.
2026-08-13 19:42:03 +05:30
kartik-mem0 08e9cc476c fix(embeddings/lmstudio): honor LMSTUDIO_BASE_URL env var
BaseEmbedderConfig baked "http://localhost:1234/v1" into the
lmstudio_base_url default, so the attribute was never falsy and the
embedder had no way to tell "unset" from "explicitly localhost". Setting
LMSTUDIO_BASE_URL had no effect.

The default moves to the provider, which now resolves explicit config,
then LMSTUDIO_BASE_URL, then the localhost default, matching how the
OpenAI embedder already reads OPENAI_BASE_URL. The resolved value is
written back to config.lmstudio_base_url, so the attribute still reads
the same after construction.

Closes #6692
2026-08-13 17:36:59 +05:30
12 changed files with 190 additions and 55 deletions
+5 -1
View File
@@ -12,7 +12,11 @@ export class LMStudioEmbedder implements Embedder {
private model: string;
constructor(config: EmbeddingConfig) {
const baseURL = config.baseURL ?? config.url ?? DEFAULT_BASE_URL;
const baseURL =
config.baseURL ??
config.url ??
process.env.LMSTUDIO_BASE_URL ??
DEFAULT_BASE_URL;
const apiKey = config.apiKey || DEFAULT_LMSTUDIO_API_KEY;
this.openai = new OpenAI({ apiKey, baseURL: String(baseURL) });
this.model = config.model || DEFAULT_MODEL;
+2 -1
View File
@@ -12,7 +12,8 @@ export class LMStudioLLM extends OpenAILLM {
super({
...config,
apiKey: config.apiKey || DEFAULT_LMSTUDIO_API_KEY,
baseURL: config.baseURL ?? DEFAULT_BASE_URL,
baseURL:
config.baseURL ?? process.env.LMSTUDIO_BASE_URL ?? DEFAULT_BASE_URL,
model: config.model || DEFAULT_MODEL,
});
}
@@ -10,14 +10,64 @@ const mockCreate = jest.fn().mockResolvedValue({
data: [{ embedding: mockEmbedding }],
});
let capturedConstructorArgs: any;
jest.mock("openai", () => {
return jest.fn().mockImplementation(() => ({
embeddings: { create: mockCreate },
}));
return jest.fn().mockImplementation((args: any) => {
capturedConstructorArgs = args;
return {
embeddings: { create: mockCreate },
};
});
});
describe("LMStudioEmbedder (unit)", () => {
beforeEach(() => mockCreate.mockClear());
beforeEach(() => {
mockCreate.mockClear();
capturedConstructorArgs = undefined;
delete process.env.LMSTUDIO_BASE_URL;
});
afterEach(() => {
delete process.env.LMSTUDIO_BASE_URL;
});
it("uses the default base URL when neither config nor env var is set", async () => {
const embedder = new LMStudioEmbedder({ model: "test-model" });
await embedder.embed("hello");
expect(capturedConstructorArgs).toMatchObject({
baseURL: "http://localhost:1234/v1",
});
});
it("uses LMSTUDIO_BASE_URL when config does not provide a base URL", async () => {
process.env.LMSTUDIO_BASE_URL = "http://example.test:5678/v1";
const embedder = new LMStudioEmbedder({ model: "test-model" });
await embedder.embed("hello");
expect(capturedConstructorArgs).toMatchObject({
baseURL: "http://example.test:5678/v1",
});
});
it("prefers an explicit config baseURL over LMSTUDIO_BASE_URL", async () => {
process.env.LMSTUDIO_BASE_URL = "http://example.test:5678/v1";
const embedder = new LMStudioEmbedder({
model: "test-model",
baseURL: "http://config.test:9999/v1",
});
await embedder.embed("hello");
expect(capturedConstructorArgs).toMatchObject({
baseURL: "http://config.test:9999/v1",
});
});
it("embed() calls OpenAI with encoding_format float and returns vector", async () => {
const embedder = new LMStudioEmbedder({
+44 -4
View File
@@ -5,16 +5,56 @@
import { LMStudioLLM } from "../src/llms/lmstudio";
let capturedConstructorArgs: any;
const mockCreate = jest.fn();
jest.mock("openai", () => {
return jest.fn().mockImplementation(() => ({
chat: { completions: { create: mockCreate } },
}));
return jest.fn().mockImplementation((args: any) => {
capturedConstructorArgs = args;
return {
chat: { completions: { create: mockCreate } },
};
});
});
describe("LMStudioLLM (unit)", () => {
beforeEach(() => mockCreate.mockClear());
beforeEach(() => {
mockCreate.mockClear();
capturedConstructorArgs = undefined;
delete process.env.LMSTUDIO_BASE_URL;
});
afterEach(() => {
delete process.env.LMSTUDIO_BASE_URL;
});
it("uses the default base URL when neither config nor env var is set", () => {
new LMStudioLLM({});
expect(capturedConstructorArgs).toMatchObject({
baseURL: "http://localhost:1234/v1",
});
});
it("uses LMSTUDIO_BASE_URL when config does not provide a base URL", () => {
process.env.LMSTUDIO_BASE_URL = "http://example.test:5678/v1";
new LMStudioLLM({});
expect(capturedConstructorArgs).toMatchObject({
baseURL: "http://example.test:5678/v1",
});
});
it("prefers an explicit config baseURL over LMSTUDIO_BASE_URL", () => {
process.env.LMSTUDIO_BASE_URL = "http://example.test:5678/v1";
new LMStudioLLM({ baseURL: "http://config.test:9999/v1" });
expect(capturedConstructorArgs).toMatchObject({
baseURL: "http://config.test:9999/v1",
});
});
it("generateResponse() returns a text response", async () => {
mockCreate.mockResolvedValueOnce({
+3 -2
View File
@@ -34,7 +34,7 @@ class BaseEmbedderConfig(ABC):
# Gemini specific
output_dimensionality: Optional[str] = None,
# LM Studio specific
lmstudio_base_url: Optional[str] = "http://localhost:1234/v1",
lmstudio_base_url: Optional[str] = None,
# AWS Bedrock specific
aws_access_key_id: Optional[str] = None,
aws_secret_access_key: Optional[str] = None,
@@ -70,7 +70,8 @@ class BaseEmbedderConfig(ABC):
:type memory_update_embedding_type: Optional[str], optional
:param memory_search_embedding_type: The type of embedding to use for the search memory action, defaults to None
:type memory_search_embedding_type: Optional[str], optional
:param lmstudio_base_url: LM Studio base URL to be use, defaults to "http://localhost:1234/v1"
:param lmstudio_base_url: LM Studio base URL to be use, defaults to None
(resolved by the provider from LMSTUDIO_BASE_URL, then "http://localhost:1234/v1")
:type lmstudio_base_url: Optional[str], optional
"""
+2 -1
View File
@@ -39,6 +39,7 @@ class LMStudioConfig(BaseLlmConfig):
vision_details: Vision detail level, defaults to "auto"
http_client_proxies: HTTP client proxy settings, defaults to None
lmstudio_base_url: LM Studio base URL, defaults to None
(resolved by the provider from LMSTUDIO_BASE_URL, then "http://localhost:1234/v1")
lmstudio_response_format: LM Studio response format, defaults to None
"""
# Initialize base parameters
@@ -55,5 +56,5 @@ class LMStudioConfig(BaseLlmConfig):
)
# LM Studio-specific parameters
self.lmstudio_base_url = lmstudio_base_url or "http://localhost:1234/v1"
self.lmstudio_base_url = lmstudio_base_url
self.lmstudio_response_format = lmstudio_response_format
+4
View File
@@ -1,3 +1,4 @@
import os
from typing import Literal, Optional
from openai import OpenAI
@@ -13,6 +14,9 @@ class LMStudioEmbedding(EmbeddingBase):
self.config.model = self.config.model or "nomic-ai/nomic-embed-text-v1.5-GGUF/nomic-embed-text-v1.5.f16.gguf"
self.config.embedding_dims = self.config.embedding_dims or 1536
self.config.api_key = self.config.api_key or "lm-studio"
self.config.lmstudio_base_url = (
self.config.lmstudio_base_url or os.getenv("LMSTUDIO_BASE_URL") or "http://localhost:1234/v1"
)
self.client = OpenAI(base_url=self.config.lmstudio_base_url, api_key=self.config.api_key)
+2 -11
View File
@@ -8,14 +8,6 @@ from mem0.embeddings.base import EmbeddingBase
from mem0.utils.gcp_auth import GCPAuthenticator
def _max_instances_per_request(model: str) -> int:
# Vertex AI's per-request input cap depends on the model family: gemini-embedding-*
# accepts exactly one input text per predict() call, while text-embedding-004/005 and
# text-multilingual-embedding-002 accept up to 250.
# https://docs.cloud.google.com/vertex-ai/docs/samples/aiplatform-sdk-embedding
return 1 if model.startswith("gemini-embedding") else 250
class VertexAIEmbedding(EmbeddingBase):
def __init__(self, config: Optional[BaseEmbedderConfig] = None):
super().__init__(config)
@@ -80,9 +72,8 @@ class VertexAIEmbedding(EmbeddingBase):
raise ValueError(f"Invalid memory action: {memory_action}")
embedding_type = self.embedding_types[memory_action]
all_embeddings = []
chunk_size = _max_instances_per_request(self.config.model)
for i in range(0, len(texts), chunk_size):
chunk = texts[i : i + chunk_size]
for i in range(0, len(texts), 250):
chunk = texts[i : i + 250]
inputs = [TextEmbeddingInput(text=t, task_type=embedding_type) for t in chunk]
results = self.model.get_embeddings(texts=inputs, output_dimensionality=self.config.embedding_dims)
all_embeddings.extend(r.values for r in results)
+4
View File
@@ -1,4 +1,5 @@
import json
import os
from typing import Dict, List, Optional, Union
from openai import OpenAI
@@ -37,6 +38,9 @@ class LMStudioLLM(LLMBase):
or "lmstudio-community/Meta-Llama-3.1-70B-Instruct-GGUF/Meta-Llama-3.1-70B-Instruct-IQ2_M.gguf"
)
self.config.api_key = self.config.api_key or "lm-studio"
self.config.lmstudio_base_url = (
self.config.lmstudio_base_url or os.getenv("LMSTUDIO_BASE_URL") or "http://localhost:1234/v1"
)
self.client = OpenAI(base_url=self.config.lmstudio_base_url, api_key=self.config.api_key)
+28 -1
View File
@@ -6,7 +6,6 @@ from mem0.configs.embeddings.base import BaseEmbedderConfig
from mem0.embeddings.lmstudio import LMStudioEmbedding
@pytest.fixture
def mock_lm_studio_client():
with patch("mem0.embeddings.lmstudio.OpenAI") as mock_openai:
@@ -80,3 +79,31 @@ def test_embed_batch_count_mismatch_raises(mock_lm_studio_client):
with pytest.raises(ValueError, match="returned 1 embeddings for 2 texts"):
embedder.embed_batch(["first text", "second text"])
def test_base_url_honors_lmstudio_base_url_env(mock_lm_studio_client, monkeypatch):
monkeypatch.setenv("LMSTUDIO_BASE_URL", "http://lmstudio.internal:9000/v1")
with patch("mem0.embeddings.lmstudio.OpenAI") as mock_openai:
LMStudioEmbedding(BaseEmbedderConfig())
assert mock_openai.call_args.kwargs["base_url"] == "http://lmstudio.internal:9000/v1"
def test_base_url_prefers_explicit_config_over_env(mock_lm_studio_client, monkeypatch):
monkeypatch.setenv("LMSTUDIO_BASE_URL", "http://lmstudio.internal:9000/v1")
with patch("mem0.embeddings.lmstudio.OpenAI") as mock_openai:
LMStudioEmbedding(BaseEmbedderConfig(lmstudio_base_url="http://explicit:1234/v1"))
assert mock_openai.call_args.kwargs["base_url"] == "http://explicit:1234/v1"
def test_base_url_falls_back_to_localhost_default(mock_lm_studio_client, monkeypatch):
monkeypatch.delenv("LMSTUDIO_BASE_URL", raising=False)
with patch("mem0.embeddings.lmstudio.OpenAI") as mock_openai:
embedder = LMStudioEmbedding(BaseEmbedderConfig())
assert mock_openai.call_args.kwargs["base_url"] == "http://localhost:1234/v1"
assert embedder.config.lmstudio_base_url == "http://localhost:1234/v1"
+4 -30
View File
@@ -163,9 +163,7 @@ def test_invalid_memory_action(mock_text_embedding_model, mock_config):
@patch("mem0.embeddings.vertexai.TextEmbeddingModel")
def test_embed_batch_single_call(mock_text_embedding_model, mock_os_environ, mock_config):
# text-embedding-005 accepts up to 250 inputs per request, so 2 texts fit in one call.
# (gemini-embedding-* only accepts 1 input per request; see test_embed_batch_default_model_sends_one_text_per_call.)
mock_config.return_value.model = "text-embedding-005"
mock_config.return_value.model = "gemini-embedding-001"
mock_config.return_value.embedding_dims = 256
config = mock_config()
@@ -200,9 +198,7 @@ def test_embed_batch_empty_list(mock_text_embedding_model, mock_os_environ, mock
@patch("mem0.embeddings.vertexai.TextEmbeddingModel")
def test_embed_batch_count_mismatch_raises(mock_text_embedding_model, mock_os_environ, mock_config):
# Use a 250-cap model so both texts land in a single chunk/call, keeping the mismatch
# (1 embedding returned for that one call) triggered by that single call.
mock_config.return_value.model = "text-embedding-005"
mock_config.return_value.model = "gemini-embedding-001"
mock_config.return_value.embedding_dims = 256
config = mock_config()
@@ -265,8 +261,8 @@ def test_embed_batch_invalid_memory_action_raises(mock_text_embedding_model, moc
@patch("mem0.embeddings.vertexai.TextEmbeddingModel")
def test_embed_batch_chunking_triggers_two_api_calls(mock_text_embedding_model, mock_os_environ, mock_config):
"""text-embedding-005 accepts up to 250 inputs/request: 300 texts -> 2 calls (chunks of 250 and 50)."""
mock_config.return_value.model = "text-embedding-005"
"""300 texts must produce exactly 2 get_embeddings calls (chunks of 250 and 50)."""
mock_config.return_value.model = "gemini-embedding-001"
mock_config.return_value.embedding_dims = 256
config = mock_config()
@@ -282,25 +278,3 @@ def test_embed_batch_chunking_triggers_two_api_calls(mock_text_embedding_model,
assert mock_text_embedding_model.from_pretrained.return_value.get_embeddings.call_count == 2
assert len(result) == 300
@patch("mem0.embeddings.vertexai.TextEmbeddingModel")
def test_embed_batch_default_model_sends_one_text_per_call(mock_text_embedding_model, mock_os_environ, mock_config):
"""gemini-embedding-001 (default) accepts 1 input/request: 300 texts -> 300 calls of 1 text each."""
mock_config.return_value.model = "gemini-embedding-001"
mock_config.return_value.embedding_dims = 256
config = mock_config()
embedder = VertexAIEmbedding(config)
def make_single_response(texts, output_dimensionality):
assert len(texts) == 1
return [Mock(values=[0.1, 0.2]) for _ in texts]
mock_text_embedding_model.from_pretrained.return_value.get_embeddings.side_effect = make_single_response
texts = [f"text {i}" for i in range(300)]
result = embedder.embed_batch(texts)
assert mock_text_embedding_model.from_pretrained.return_value.get_embeddings.call_count == 300
assert len(result) == 300
+38
View File
@@ -2,6 +2,7 @@ from unittest.mock import Mock, patch
import pytest
from mem0.configs.llms.base import BaseLlmConfig
from mem0.configs.llms.lmstudio import LMStudioConfig
from mem0.llms.lmstudio import LMStudioLLM
@@ -70,3 +71,40 @@ def test_generate_response_specifying_response_format(mock_lm_studio_client):
)
assert response == "I'm doing well, thank you for asking!"
def test_base_url_honors_lmstudio_base_url_env(monkeypatch):
monkeypatch.setenv("LMSTUDIO_BASE_URL", "http://lmstudio.internal:9000/v1")
with patch("mem0.llms.lmstudio.OpenAI") as mock_openai:
LMStudioLLM(LMStudioConfig())
assert mock_openai.call_args.kwargs["base_url"] == "http://lmstudio.internal:9000/v1"
def test_base_url_prefers_explicit_config_over_env(monkeypatch):
monkeypatch.setenv("LMSTUDIO_BASE_URL", "http://lmstudio.internal:9000/v1")
with patch("mem0.llms.lmstudio.OpenAI") as mock_openai:
LMStudioLLM(LMStudioConfig(lmstudio_base_url="http://explicit:1234/v1"))
assert mock_openai.call_args.kwargs["base_url"] == "http://explicit:1234/v1"
def test_base_url_falls_back_to_localhost_default(monkeypatch):
monkeypatch.delenv("LMSTUDIO_BASE_URL", raising=False)
with patch("mem0.llms.lmstudio.OpenAI") as mock_openai:
llm = LMStudioLLM(LMStudioConfig())
assert mock_openai.call_args.kwargs["base_url"] == "http://localhost:1234/v1"
assert llm.config.lmstudio_base_url == "http://localhost:1234/v1"
def test_base_url_honors_env_via_base_llm_config(monkeypatch):
monkeypatch.setenv("LMSTUDIO_BASE_URL", "http://lmstudio.internal:9000/v1")
with patch("mem0.llms.lmstudio.OpenAI") as mock_openai:
LMStudioLLM(BaseLlmConfig(model="some-model"))
assert mock_openai.call_args.kwargs["base_url"] == "http://lmstudio.internal:9000/v1"