Skip to content

Commit 62c330e

Browse files
authored
feat(LM Studio): Add response_format param for LM Studio to config (mem0ai#2502)
1 parent c70dc76 commit 62c330e

File tree

4 files changed

+34
-0
lines changed

4 files changed

+34
-0
lines changed

docs/components/llms/models/lmstudio.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ config = {
2323
"temperature": 0.2,
2424
"max_tokens": 2000,
2525
"lmstudio_base_url": "http://localhost:1234/v1", # default LM Studio API URL
26+
"lmstudio_response_format": {"type": "json_schema", "json_schema": {"type": "object", "schema": {}}},
2627
}
2728
}
2829
}

mem0/configs/llms/base.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ def __init__(
4343
sarvam_base_url: Optional[str] = "https://api.sarvam.ai/v1",
4444
# LM Studio specific
4545
lmstudio_base_url: Optional[str] = "http://localhost:1234/v1",
46+
lmstudio_response_format: dict = None,
4647
# AWS Bedrock specific
4748
aws_access_key_id: Optional[str] = None,
4849
aws_secret_access_key: Optional[str] = None,
@@ -95,6 +96,8 @@ def __init__(
9596
:type sarvam_base_url: Optional[str], optional
9697
:param lmstudio_base_url: LM Studio base URL to be use, defaults to "http://localhost:1234/v1"
9798
:type lmstudio_base_url: Optional[str], optional
99+
:param lmstudio_response_format: LM Studio response format to be use, defaults to None
100+
:type lmstudio_response_format: Optional[Dict], optional
98101
"""
99102

100103
self.model = model
@@ -134,6 +137,7 @@ def __init__(
134137

135138
# LM Studio specific
136139
self.lmstudio_base_url = lmstudio_base_url
140+
self.lmstudio_response_format = lmstudio_response_format
137141

138142
# AWS Bedrock specific
139143
self.aws_access_key_id = aws_access_key_id

mem0/llms/lmstudio.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ def generate_response(
4646
}
4747
if response_format:
4848
params["response_format"] = response_format
49+
if self.config.lmstudio_response_format is not None:
50+
params["response_format"] = self.config.lmstudio_response_format
4951

5052
response = self.client.chat.completions.create(**params)
5153
return response.choices[0].message.content

tests/llms/test_lm_studio.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,30 @@ def test_generate_response_without_tools(mock_lm_studio_client):
4242
)
4343

4444
assert response == "I'm doing well, thank you for asking!"
45+
46+
def test_generate_response_specifying_response_format(mock_lm_studio_client):
47+
config = BaseLlmConfig(
48+
model="lmstudio-community/Meta-Llama-3.1-8B-Instruct-GGUF/Meta-Llama-3.1-8B-Instruct-Q4_K_M.gguf",
49+
temperature=0.7,
50+
max_tokens=100,
51+
top_p=1.0,
52+
lmstudio_response_format={"type": "json_schema"}, # Specifying the response format in config
53+
)
54+
llm = LMStudioLLM(config)
55+
messages = [
56+
{"role": "system", "content": "You are a helpful assistant."},
57+
{"role": "user", "content": "Hello, how are you?"},
58+
]
59+
60+
response = llm.generate_response(messages)
61+
62+
mock_lm_studio_client.chat.completions.create.assert_called_once_with(
63+
model="lmstudio-community/Meta-Llama-3.1-8B-Instruct-GGUF/Meta-Llama-3.1-8B-Instruct-Q4_K_M.gguf",
64+
messages=messages,
65+
temperature=0.7,
66+
max_tokens=100,
67+
top_p=1.0,
68+
response_format={"type": "json_schema"},
69+
)
70+
71+
assert response == "I'm doing well, thank you for asking!"

0 commit comments

Comments
 (0)