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.
This commit is contained in:
kartik-mem0
2026-08-13 19:45:57 +05:30
parent a5dab4279b
commit 2b8e4ea059
3 changed files with 44 additions and 1 deletions
+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,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)
+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"