fix(python): huggingface TEI auth, procedural-memory content handling, and proxy pip auto-install (#6947)

This commit is contained in:
Kartik
2026-08-20 15:56:55 +05:30
committed by GitHub
parent 530d802b55
commit ed38ddf873
11 changed files with 264 additions and 18 deletions
@@ -72,7 +72,8 @@ def test_embed_with_custom_embedding_dims(mock_sentence_transformer):
assert result == [1.0, 1.1, 1.2]
def test_embed_with_huggingface_base_url():
def test_embed_with_huggingface_base_url(monkeypatch):
monkeypatch.delenv("HUGGINGFACE_API_KEY", raising=False)
config = BaseEmbedderConfig(
huggingface_base_url="http://localhost:8080",
model="my-custom-model",
@@ -81,20 +82,20 @@ def test_embed_with_huggingface_base_url():
with patch("mem0.embeddings.huggingface.OpenAI") as mock_openai:
mock_client = Mock()
mock_openai.return_value = mock_client
# Create a mock for the response object and its attributes
mock_embedding_response = Mock()
mock_embedding_response.embedding = [0.1, 0.2, 0.3]
mock_create_response = Mock()
mock_create_response.data = [mock_embedding_response]
mock_client.embeddings.create.return_value = mock_create_response
embedder = HuggingFaceEmbedding(config)
result = embedder.embed("Hello from custom endpoint")
mock_openai.assert_called_once_with(base_url="http://localhost:8080", api_key=None)
mock_openai.assert_called_once_with(base_url="http://localhost:8080", api_key="hf")
mock_client.embeddings.create.assert_called_once_with(
input="Hello from custom endpoint",
model="my-custom-model",
@@ -115,6 +116,26 @@ def test_embed_with_huggingface_base_url_forwards_api_key():
mock_openai.assert_called_once_with(base_url="http://localhost:8080", api_key="tei-token")
def test_embed_with_huggingface_base_url_falls_back_to_env_var(monkeypatch):
monkeypatch.setenv("HUGGINGFACE_API_KEY", "env-token")
config = BaseEmbedderConfig(huggingface_base_url="http://localhost:8080", model="my-custom-model")
with patch("mem0.embeddings.huggingface.OpenAI") as mock_openai:
HuggingFaceEmbedding(config)
mock_openai.assert_called_once_with(base_url="http://localhost:8080", api_key="env-token")
def test_embed_with_huggingface_base_url_config_key_beats_env_var(monkeypatch):
monkeypatch.setenv("HUGGINGFACE_API_KEY", "env-token")
config = BaseEmbedderConfig(
huggingface_base_url="http://localhost:8080", model="my-custom-model", api_key="config-token"
)
with patch("mem0.embeddings.huggingface.OpenAI") as mock_openai:
HuggingFaceEmbedding(config)
mock_openai.assert_called_once_with(base_url="http://localhost:8080", api_key="config-token")
def test_embed_batch_sentence_transformer(mock_sentence_transformer):
config = BaseEmbedderConfig()
embedder = HuggingFaceEmbedding(config)
@@ -0,0 +1,61 @@
import pytest
pytest.importorskip("langchain", reason="langchain is an optional extra")
from langchain.embeddings.base import Embeddings # noqa: E402
from mem0.configs.embeddings.base import BaseEmbedderConfig # noqa: E402
from mem0.embeddings.langchain import LangchainEmbedding # noqa: E402
class DummyEmbeddings(Embeddings):
def __init__(self):
self.queries = []
def embed_documents(self, texts):
return [[0.1, 0.2, 0.3] for _ in texts]
def embed_query(self, text):
self.queries.append(text)
return [0.1, 0.2, 0.3]
def test_missing_model_raises():
with pytest.raises(ValueError, match="`model` parameter is required"):
LangchainEmbedding(BaseEmbedderConfig())
def test_model_that_is_not_an_embeddings_instance_raises():
with pytest.raises(ValueError, match="`model` must be an instance of Embeddings"):
LangchainEmbedding(BaseEmbedderConfig(model="text-embedding-3-small"))
def test_configured_model_instance_is_kept():
model = DummyEmbeddings()
assert LangchainEmbedding(BaseEmbedderConfig(model=model)).langchain_model is model
def test_embed_delegates_to_embed_query():
model = DummyEmbeddings()
embedder = LangchainEmbedding(BaseEmbedderConfig(model=model))
assert embedder.embed("hello", "add") == [0.1, 0.2, 0.3]
assert model.queries == ["hello"]
def test_memory_action_never_reaches_the_model():
model = DummyEmbeddings()
embedder = LangchainEmbedding(BaseEmbedderConfig(model=model))
embedder.embed("hello", memory_action="add")
assert model.queries == ["hello"]
def test_embed_batch_falls_back_to_sequential_embed():
model = DummyEmbeddings()
embedder = LangchainEmbedding(BaseEmbedderConfig(model=model))
assert embedder.embed_batch(["a", "b"]) == [[0.1, 0.2, 0.3], [0.1, 0.2, 0.3]]
assert model.queries == ["a", "b"]
@@ -1,3 +1,6 @@
import builtins
import importlib
import sys
from unittest.mock import Mock, patch
import pytest
@@ -94,3 +97,25 @@ def test_embed_batch_count_mismatch_raises(mock_ollama_client):
with pytest.raises(ValueError, match="returned 1 embeddings for 2 texts"):
embedder.embed_batch(["first text", "second text"])
def test_missing_ollama_raises_actionable_import_error(monkeypatch):
"""Missing ollama raises a catchable ImportError naming the install command, never input()/sys.exit()."""
monkeypatch.delitem(sys.modules, "mem0.embeddings.ollama", raising=False)
monkeypatch.delitem(sys.modules, "ollama", raising=False)
real_import = builtins.__import__
def fake_import(name, *args, **kwargs):
if name == "ollama":
raise ImportError("No module named 'ollama'")
return real_import(name, *args, **kwargs)
monkeypatch.setattr(builtins, "__import__", fake_import)
with patch("builtins.input") as mock_input, patch("sys.exit") as mock_exit:
with pytest.raises(ImportError, match="pip install ollama"):
importlib.import_module("mem0.embeddings.ollama")
mock_input.assert_not_called()
mock_exit.assert_not_called()