Compare commits
4
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
65f42bbf43 | ||
|
|
2b8e4ea059 | ||
|
|
a5dab4279b | ||
|
|
08e9cc476c |
@@ -12,7 +12,11 @@ export class LMStudioEmbedder implements Embedder {
|
|||||||
private model: string;
|
private model: string;
|
||||||
|
|
||||||
constructor(config: EmbeddingConfig) {
|
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;
|
const apiKey = config.apiKey || DEFAULT_LMSTUDIO_API_KEY;
|
||||||
this.openai = new OpenAI({ apiKey, baseURL: String(baseURL) });
|
this.openai = new OpenAI({ apiKey, baseURL: String(baseURL) });
|
||||||
this.model = config.model || DEFAULT_MODEL;
|
this.model = config.model || DEFAULT_MODEL;
|
||||||
|
|||||||
@@ -12,7 +12,8 @@ export class LMStudioLLM extends OpenAILLM {
|
|||||||
super({
|
super({
|
||||||
...config,
|
...config,
|
||||||
apiKey: config.apiKey || DEFAULT_LMSTUDIO_API_KEY,
|
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,
|
model: config.model || DEFAULT_MODEL,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,14 +10,64 @@ const mockCreate = jest.fn().mockResolvedValue({
|
|||||||
data: [{ embedding: mockEmbedding }],
|
data: [{ embedding: mockEmbedding }],
|
||||||
});
|
});
|
||||||
|
|
||||||
|
let capturedConstructorArgs: any;
|
||||||
|
|
||||||
jest.mock("openai", () => {
|
jest.mock("openai", () => {
|
||||||
return jest.fn().mockImplementation(() => ({
|
return jest.fn().mockImplementation((args: any) => {
|
||||||
embeddings: { create: mockCreate },
|
capturedConstructorArgs = args;
|
||||||
}));
|
return {
|
||||||
|
embeddings: { create: mockCreate },
|
||||||
|
};
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("LMStudioEmbedder (unit)", () => {
|
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 () => {
|
it("embed() calls OpenAI with encoding_format float and returns vector", async () => {
|
||||||
const embedder = new LMStudioEmbedder({
|
const embedder = new LMStudioEmbedder({
|
||||||
|
|||||||
@@ -5,16 +5,56 @@
|
|||||||
|
|
||||||
import { LMStudioLLM } from "../src/llms/lmstudio";
|
import { LMStudioLLM } from "../src/llms/lmstudio";
|
||||||
|
|
||||||
|
let capturedConstructorArgs: any;
|
||||||
const mockCreate = jest.fn();
|
const mockCreate = jest.fn();
|
||||||
|
|
||||||
jest.mock("openai", () => {
|
jest.mock("openai", () => {
|
||||||
return jest.fn().mockImplementation(() => ({
|
return jest.fn().mockImplementation((args: any) => {
|
||||||
chat: { completions: { create: mockCreate } },
|
capturedConstructorArgs = args;
|
||||||
}));
|
return {
|
||||||
|
chat: { completions: { create: mockCreate } },
|
||||||
|
};
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("LMStudioLLM (unit)", () => {
|
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 () => {
|
it("generateResponse() returns a text response", async () => {
|
||||||
mockCreate.mockResolvedValueOnce({
|
mockCreate.mockResolvedValueOnce({
|
||||||
|
|||||||
@@ -20,8 +20,6 @@ class BaseEmbedderConfig(ABC):
|
|||||||
ollama_base_url: Optional[str] = None,
|
ollama_base_url: Optional[str] = None,
|
||||||
# Openai specific
|
# Openai specific
|
||||||
openai_base_url: Optional[str] = None,
|
openai_base_url: Optional[str] = None,
|
||||||
# Together specific
|
|
||||||
together_base_url: Optional[str] = None,
|
|
||||||
# Huggingface specific
|
# Huggingface specific
|
||||||
model_kwargs: Optional[dict] = None,
|
model_kwargs: Optional[dict] = None,
|
||||||
huggingface_base_url: Optional[str] = None,
|
huggingface_base_url: Optional[str] = None,
|
||||||
@@ -36,7 +34,7 @@ class BaseEmbedderConfig(ABC):
|
|||||||
# Gemini specific
|
# Gemini specific
|
||||||
output_dimensionality: Optional[str] = None,
|
output_dimensionality: Optional[str] = None,
|
||||||
# LM Studio specific
|
# LM Studio specific
|
||||||
lmstudio_base_url: Optional[str] = "http://localhost:1234/v1",
|
lmstudio_base_url: Optional[str] = None,
|
||||||
# AWS Bedrock specific
|
# AWS Bedrock specific
|
||||||
aws_access_key_id: Optional[str] = None,
|
aws_access_key_id: Optional[str] = None,
|
||||||
aws_secret_access_key: Optional[str] = None,
|
aws_secret_access_key: Optional[str] = None,
|
||||||
@@ -60,8 +58,6 @@ class BaseEmbedderConfig(ABC):
|
|||||||
:type huggingface_base_url: Optional[str], optional
|
:type huggingface_base_url: Optional[str], optional
|
||||||
:param openai_base_url: Openai base URL to be use, defaults to "https://api.openai.com/v1"
|
:param openai_base_url: Openai base URL to be use, defaults to "https://api.openai.com/v1"
|
||||||
:type openai_base_url: Optional[str], optional
|
:type openai_base_url: Optional[str], optional
|
||||||
:param together_base_url: Together base URL to be use, defaults to None
|
|
||||||
:type together_base_url: Optional[str], optional
|
|
||||||
:param azure_kwargs: key-value arguments for the AzureOpenAI embedding model, defaults a dict inside init
|
:param azure_kwargs: key-value arguments for the AzureOpenAI embedding model, defaults a dict inside init
|
||||||
:type azure_kwargs: Optional[Dict[str, Any]], defaults a dict inside init
|
:type azure_kwargs: Optional[Dict[str, Any]], defaults a dict inside init
|
||||||
:param http_client_proxies: The proxy server settings used to create self.http_client, defaults to None
|
:param http_client_proxies: The proxy server settings used to create self.http_client, defaults to None
|
||||||
@@ -74,14 +70,14 @@ class BaseEmbedderConfig(ABC):
|
|||||||
:type memory_update_embedding_type: Optional[str], optional
|
: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
|
: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
|
: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
|
:type lmstudio_base_url: Optional[str], optional
|
||||||
"""
|
"""
|
||||||
|
|
||||||
self.model = model
|
self.model = model
|
||||||
self.api_key = api_key
|
self.api_key = api_key
|
||||||
self.openai_base_url = openai_base_url
|
self.openai_base_url = openai_base_url
|
||||||
self.together_base_url = together_base_url
|
|
||||||
self.embedding_dims = embedding_dims
|
self.embedding_dims = embedding_dims
|
||||||
|
|
||||||
# AzureOpenAI specific
|
# AzureOpenAI specific
|
||||||
|
|||||||
@@ -39,6 +39,7 @@ class LMStudioConfig(BaseLlmConfig):
|
|||||||
vision_details: Vision detail level, defaults to "auto"
|
vision_details: Vision detail level, defaults to "auto"
|
||||||
http_client_proxies: HTTP client proxy settings, defaults to None
|
http_client_proxies: HTTP client proxy settings, defaults to None
|
||||||
lmstudio_base_url: LM Studio base URL, 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
|
lmstudio_response_format: LM Studio response format, defaults to None
|
||||||
"""
|
"""
|
||||||
# Initialize base parameters
|
# Initialize base parameters
|
||||||
@@ -55,5 +56,5 @@ class LMStudioConfig(BaseLlmConfig):
|
|||||||
)
|
)
|
||||||
|
|
||||||
# LM Studio-specific parameters
|
# 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
|
self.lmstudio_response_format = lmstudio_response_format
|
||||||
|
|||||||
@@ -1,56 +0,0 @@
|
|||||||
from typing import Optional
|
|
||||||
|
|
||||||
from mem0.configs.llms.base import BaseLlmConfig
|
|
||||||
|
|
||||||
|
|
||||||
class TogetherConfig(BaseLlmConfig):
|
|
||||||
"""
|
|
||||||
Configuration class for Together-specific parameters.
|
|
||||||
Inherits from BaseLlmConfig and adds Together-specific settings.
|
|
||||||
"""
|
|
||||||
|
|
||||||
def __init__(
|
|
||||||
self,
|
|
||||||
# Base parameters
|
|
||||||
model: Optional[str] = None,
|
|
||||||
temperature: float = 0.1,
|
|
||||||
api_key: Optional[str] = None,
|
|
||||||
max_tokens: int = 2000,
|
|
||||||
top_p: float = 0.1,
|
|
||||||
top_k: int = 1,
|
|
||||||
enable_vision: bool = False,
|
|
||||||
vision_details: Optional[str] = "auto",
|
|
||||||
http_client_proxies: Optional[dict] = None,
|
|
||||||
# Together-specific parameters
|
|
||||||
together_base_url: Optional[str] = None,
|
|
||||||
):
|
|
||||||
"""
|
|
||||||
Initialize Together configuration.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
model: Together model to use, defaults to None
|
|
||||||
temperature: Controls randomness, defaults to 0.1
|
|
||||||
api_key: Together API key, defaults to None
|
|
||||||
max_tokens: Maximum tokens to generate, defaults to 2000
|
|
||||||
top_p: Nucleus sampling parameter, defaults to 0.1
|
|
||||||
top_k: Top-k sampling parameter, defaults to 1
|
|
||||||
enable_vision: Enable vision capabilities, defaults to False
|
|
||||||
vision_details: Vision detail level, defaults to "auto"
|
|
||||||
http_client_proxies: HTTP client proxy settings, defaults to None
|
|
||||||
together_base_url: Together API base URL, defaults to None
|
|
||||||
"""
|
|
||||||
# Initialize base parameters
|
|
||||||
super().__init__(
|
|
||||||
model=model,
|
|
||||||
temperature=temperature,
|
|
||||||
api_key=api_key,
|
|
||||||
max_tokens=max_tokens,
|
|
||||||
top_p=top_p,
|
|
||||||
top_k=top_k,
|
|
||||||
enable_vision=enable_vision,
|
|
||||||
vision_details=vision_details,
|
|
||||||
http_client_proxies=http_client_proxies,
|
|
||||||
)
|
|
||||||
|
|
||||||
# Together-specific parameters
|
|
||||||
self.together_base_url = together_base_url
|
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import os
|
||||||
from typing import Literal, Optional
|
from typing import Literal, Optional
|
||||||
|
|
||||||
from openai import OpenAI
|
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.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.embedding_dims = self.config.embedding_dims or 1536
|
||||||
self.config.api_key = self.config.api_key or "lm-studio"
|
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)
|
self.client = OpenAI(base_url=self.config.lmstudio_base_url, api_key=self.config.api_key)
|
||||||
|
|
||||||
|
|||||||
@@ -13,9 +13,8 @@ class TogetherEmbedding(EmbeddingBase):
|
|||||||
|
|
||||||
self.config.model = self.config.model or "intfloat/multilingual-e5-large-instruct"
|
self.config.model = self.config.model or "intfloat/multilingual-e5-large-instruct"
|
||||||
api_key = self.config.api_key or os.getenv("TOGETHER_API_KEY")
|
api_key = self.config.api_key or os.getenv("TOGETHER_API_KEY")
|
||||||
base_url = self.config.together_base_url or os.getenv("TOGETHER_API_BASE") or "https://api.together.ai/v1"
|
|
||||||
self.config.embedding_dims = self.config.embedding_dims or 1024
|
self.config.embedding_dims = self.config.embedding_dims or 1024
|
||||||
self.client = Together(api_key=api_key, base_url=base_url)
|
self.client = Together(api_key=api_key)
|
||||||
|
|
||||||
def embed(self, text, memory_action: Optional[Literal["add", "search", "update"]] = None):
|
def embed(self, text, memory_action: Optional[Literal["add", "search", "update"]] = None):
|
||||||
"""
|
"""
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import json
|
import json
|
||||||
|
import os
|
||||||
from typing import Dict, List, Optional, Union
|
from typing import Dict, List, Optional, Union
|
||||||
|
|
||||||
from openai import OpenAI
|
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"
|
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.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)
|
self.client = OpenAI(base_url=self.config.lmstudio_base_url, api_key=self.config.api_key)
|
||||||
|
|
||||||
|
|||||||
+3
-24
@@ -1,6 +1,6 @@
|
|||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
from typing import Dict, List, Optional, Union
|
from typing import Dict, List, Optional
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from together import Together
|
from together import Together
|
||||||
@@ -8,40 +8,19 @@ except ImportError:
|
|||||||
raise ImportError("The 'together' library is required. Please install it using 'pip install together'.")
|
raise ImportError("The 'together' library is required. Please install it using 'pip install together'.")
|
||||||
|
|
||||||
from mem0.configs.llms.base import BaseLlmConfig
|
from mem0.configs.llms.base import BaseLlmConfig
|
||||||
from mem0.configs.llms.together import TogetherConfig
|
|
||||||
from mem0.llms.base import LLMBase
|
from mem0.llms.base import LLMBase
|
||||||
from mem0.memory.utils import extract_json
|
from mem0.memory.utils import extract_json
|
||||||
|
|
||||||
|
|
||||||
class TogetherLLM(LLMBase):
|
class TogetherLLM(LLMBase):
|
||||||
def __init__(self, config: Optional[Union[BaseLlmConfig, TogetherConfig, Dict]] = None):
|
def __init__(self, config: Optional[BaseLlmConfig] = None):
|
||||||
# Convert to TogetherConfig if needed
|
|
||||||
if config is None:
|
|
||||||
config = TogetherConfig()
|
|
||||||
elif isinstance(config, dict):
|
|
||||||
config = TogetherConfig(**config)
|
|
||||||
elif isinstance(config, BaseLlmConfig) and not isinstance(config, TogetherConfig):
|
|
||||||
# Convert BaseLlmConfig to TogetherConfig
|
|
||||||
config = TogetherConfig(
|
|
||||||
model=config.model,
|
|
||||||
temperature=config.temperature,
|
|
||||||
api_key=config.api_key,
|
|
||||||
max_tokens=config.max_tokens,
|
|
||||||
top_p=config.top_p,
|
|
||||||
top_k=config.top_k,
|
|
||||||
enable_vision=config.enable_vision,
|
|
||||||
vision_details=config.vision_details,
|
|
||||||
http_client_proxies=config.http_client_proxies,
|
|
||||||
)
|
|
||||||
|
|
||||||
super().__init__(config)
|
super().__init__(config)
|
||||||
|
|
||||||
if not self.config.model:
|
if not self.config.model:
|
||||||
self.config.model = "MiniMaxAI/MiniMax-M3"
|
self.config.model = "MiniMaxAI/MiniMax-M3"
|
||||||
|
|
||||||
api_key = self.config.api_key or os.getenv("TOGETHER_API_KEY")
|
api_key = self.config.api_key or os.getenv("TOGETHER_API_KEY")
|
||||||
base_url = self.config.together_base_url or os.getenv("TOGETHER_API_BASE") or "https://api.together.ai/v1"
|
self.client = Together(api_key=api_key)
|
||||||
self.client = Together(api_key=api_key, base_url=base_url)
|
|
||||||
|
|
||||||
def _parse_response(self, response, tools):
|
def _parse_response(self, response, tools):
|
||||||
"""
|
"""
|
||||||
|
|||||||
@@ -13,7 +13,6 @@ from mem0.configs.llms.lmstudio import LMStudioConfig
|
|||||||
from mem0.configs.llms.minimax import MinimaxConfig
|
from mem0.configs.llms.minimax import MinimaxConfig
|
||||||
from mem0.configs.llms.ollama import OllamaConfig
|
from mem0.configs.llms.ollama import OllamaConfig
|
||||||
from mem0.configs.llms.openai import OpenAIConfig
|
from mem0.configs.llms.openai import OpenAIConfig
|
||||||
from mem0.configs.llms.together import TogetherConfig
|
|
||||||
from mem0.configs.llms.vllm import VllmConfig
|
from mem0.configs.llms.vllm import VllmConfig
|
||||||
from mem0.configs.llms.xai import XAIConfig
|
from mem0.configs.llms.xai import XAIConfig
|
||||||
from mem0.configs.rerankers.base import BaseRerankerConfig
|
from mem0.configs.rerankers.base import BaseRerankerConfig
|
||||||
@@ -44,7 +43,7 @@ class LlmFactory:
|
|||||||
"ollama": ("mem0.llms.ollama.OllamaLLM", OllamaConfig),
|
"ollama": ("mem0.llms.ollama.OllamaLLM", OllamaConfig),
|
||||||
"openai": ("mem0.llms.openai.OpenAILLM", OpenAIConfig),
|
"openai": ("mem0.llms.openai.OpenAILLM", OpenAIConfig),
|
||||||
"groq": ("mem0.llms.groq.GroqLLM", BaseLlmConfig),
|
"groq": ("mem0.llms.groq.GroqLLM", BaseLlmConfig),
|
||||||
"together": ("mem0.llms.together.TogetherLLM", TogetherConfig),
|
"together": ("mem0.llms.together.TogetherLLM", BaseLlmConfig),
|
||||||
"aws_bedrock": ("mem0.llms.aws_bedrock.AWSBedrockLLM", AWSBedrockConfig),
|
"aws_bedrock": ("mem0.llms.aws_bedrock.AWSBedrockLLM", AWSBedrockConfig),
|
||||||
"litellm": ("mem0.llms.litellm.LiteLLM", BaseLlmConfig),
|
"litellm": ("mem0.llms.litellm.LiteLLM", BaseLlmConfig),
|
||||||
"azure_openai": ("mem0.llms.azure_openai.AzureOpenAILLM", AzureOpenAIConfig),
|
"azure_openai": ("mem0.llms.azure_openai.AzureOpenAILLM", AzureOpenAIConfig),
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ from mem0.configs.embeddings.base import BaseEmbedderConfig
|
|||||||
from mem0.embeddings.lmstudio import LMStudioEmbedding
|
from mem0.embeddings.lmstudio import LMStudioEmbedding
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def mock_lm_studio_client():
|
def mock_lm_studio_client():
|
||||||
with patch("mem0.embeddings.lmstudio.OpenAI") as mock_openai:
|
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"):
|
with pytest.raises(ValueError, match="returned 1 embeddings for 2 texts"):
|
||||||
embedder.embed_batch(["first text", "second text"])
|
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"
|
||||||
|
|||||||
@@ -85,29 +85,3 @@ def test_explicit_config_overrides_defaults(mock_together_client):
|
|||||||
mock_together_client.embeddings.create.return_value = Mock(data=[Mock(embedding=[0.0] * 768)])
|
mock_together_client.embeddings.create.return_value = Mock(data=[Mock(embedding=[0.0] * 768)])
|
||||||
embedder.embed("hello")
|
embedder.embed("hello")
|
||||||
mock_together_client.embeddings.create.assert_called_once_with(model="BAAI/bge-base-en-v1.5", input="hello")
|
mock_together_client.embeddings.create.assert_called_once_with(model="BAAI/bge-base-en-v1.5", input="hello")
|
||||||
|
|
||||||
|
|
||||||
def test_together_embedding_base_url(monkeypatch):
|
|
||||||
# case1: default config uses Together's official base url
|
|
||||||
monkeypatch.delenv("TOGETHER_API_BASE", raising=False)
|
|
||||||
config = BaseEmbedderConfig(model=DEFAULT_MODEL, embedding_dims=DEFAULT_EMBEDDING_DIMS, api_key="api_key")
|
|
||||||
embedder = TogetherEmbedding(config)
|
|
||||||
assert str(embedder.client.base_url) == "https://api.together.ai/v1/"
|
|
||||||
|
|
||||||
# case2: with env variable TOGETHER_API_BASE
|
|
||||||
provider_base_url = "https://api.provider.com/v1/"
|
|
||||||
monkeypatch.setenv("TOGETHER_API_BASE", provider_base_url)
|
|
||||||
config = BaseEmbedderConfig(model=DEFAULT_MODEL, embedding_dims=DEFAULT_EMBEDDING_DIMS, api_key="api_key")
|
|
||||||
embedder = TogetherEmbedding(config)
|
|
||||||
assert str(embedder.client.base_url) == provider_base_url
|
|
||||||
|
|
||||||
# case3: with config.together_base_url (explicit config beats env var)
|
|
||||||
config_base_url = "https://api.config.com/v1/"
|
|
||||||
config = BaseEmbedderConfig(
|
|
||||||
model=DEFAULT_MODEL,
|
|
||||||
embedding_dims=DEFAULT_EMBEDDING_DIMS,
|
|
||||||
api_key="api_key",
|
|
||||||
together_base_url=config_base_url,
|
|
||||||
)
|
|
||||||
embedder = TogetherEmbedding(config)
|
|
||||||
assert str(embedder.client.base_url) == config_base_url
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ from unittest.mock import Mock, patch
|
|||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
|
from mem0.configs.llms.base import BaseLlmConfig
|
||||||
from mem0.configs.llms.lmstudio import LMStudioConfig
|
from mem0.configs.llms.lmstudio import LMStudioConfig
|
||||||
from mem0.llms.lmstudio import LMStudioLLM
|
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!"
|
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"
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ from unittest.mock import Mock, patch
|
|||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from mem0.configs.llms.base import BaseLlmConfig
|
from mem0.configs.llms.base import BaseLlmConfig
|
||||||
from mem0.configs.llms.together import TogetherConfig
|
|
||||||
from mem0.llms.together import TogetherLLM
|
from mem0.llms.together import TogetherLLM
|
||||||
|
|
||||||
|
|
||||||
@@ -15,38 +14,6 @@ def mock_together_client():
|
|||||||
yield mock_client
|
yield mock_client
|
||||||
|
|
||||||
|
|
||||||
def test_together_llm_base_url(monkeypatch):
|
|
||||||
# case1: default config uses Together's official base url
|
|
||||||
monkeypatch.delenv("TOGETHER_API_BASE", raising=False)
|
|
||||||
config = BaseLlmConfig(
|
|
||||||
model="mistralai/Mixtral-8x7B-Instruct-v0.1", temperature=0.7, max_tokens=100, top_p=1.0, api_key="api_key"
|
|
||||||
)
|
|
||||||
llm = TogetherLLM(config)
|
|
||||||
assert str(llm.client.base_url) == "https://api.together.ai/v1/"
|
|
||||||
|
|
||||||
# case2: with env variable TOGETHER_API_BASE
|
|
||||||
provider_base_url = "https://api.provider.com/v1/"
|
|
||||||
monkeypatch.setenv("TOGETHER_API_BASE", provider_base_url)
|
|
||||||
config = TogetherConfig(
|
|
||||||
model="mistralai/Mixtral-8x7B-Instruct-v0.1", temperature=0.7, max_tokens=100, top_p=1.0, api_key="api_key"
|
|
||||||
)
|
|
||||||
llm = TogetherLLM(config)
|
|
||||||
assert str(llm.client.base_url) == provider_base_url
|
|
||||||
|
|
||||||
# case3: with config.together_base_url (explicit config beats env var)
|
|
||||||
config_base_url = "https://api.config.com/v1/"
|
|
||||||
config = TogetherConfig(
|
|
||||||
model="mistralai/Mixtral-8x7B-Instruct-v0.1",
|
|
||||||
temperature=0.7,
|
|
||||||
max_tokens=100,
|
|
||||||
top_p=1.0,
|
|
||||||
api_key="api_key",
|
|
||||||
together_base_url=config_base_url,
|
|
||||||
)
|
|
||||||
llm = TogetherLLM(config)
|
|
||||||
assert str(llm.client.base_url) == config_base_url
|
|
||||||
|
|
||||||
|
|
||||||
def test_generate_response_without_tools(mock_together_client):
|
def test_generate_response_without_tools(mock_together_client):
|
||||||
config = BaseLlmConfig(model="mistralai/Mixtral-8x7B-Instruct-v0.1", temperature=0.7, max_tokens=100, top_p=1.0)
|
config = BaseLlmConfig(model="mistralai/Mixtral-8x7B-Instruct-v0.1", temperature=0.7, max_tokens=100, top_p=1.0)
|
||||||
llm = TogetherLLM(config)
|
llm = TogetherLLM(config)
|
||||||
|
|||||||
Reference in New Issue
Block a user