The Python Together LLM and embedder ignored TOGETHER_API_BASE, unlike their TypeScript siblings, so users behind a gateway or proxy could not redirect Together traffic. Add together_base_url to a new TogetherConfig (LLM side) and to BaseEmbedderConfig (embedder side, matching the existing openai_base_url/huggingface_base_url pattern), and resolve base_url with explicit config value > TOGETHER_API_BASE env var > hardcoded default in both mem0/llms/together.py and mem0/embeddings/together.py. Also register TogetherConfig in LlmFactory.provider_to_class so config dicts routed through Memory pick it up. Closes #6587
138 lines
5.1 KiB
Python
138 lines
5.1 KiB
Python
from unittest.mock import Mock, patch
|
|
|
|
import pytest
|
|
|
|
from mem0.configs.llms.base import BaseLlmConfig
|
|
from mem0.configs.llms.together import TogetherConfig
|
|
from mem0.llms.together import TogetherLLM
|
|
|
|
|
|
@pytest.fixture
|
|
def mock_together_client():
|
|
with patch("mem0.llms.together.Together") as mock_together:
|
|
mock_client = Mock()
|
|
mock_together.return_value = 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):
|
|
config = BaseLlmConfig(model="mistralai/Mixtral-8x7B-Instruct-v0.1", temperature=0.7, max_tokens=100, top_p=1.0)
|
|
llm = TogetherLLM(config)
|
|
messages = [
|
|
{"role": "system", "content": "You are a helpful assistant."},
|
|
{"role": "user", "content": "Hello, how are you?"},
|
|
]
|
|
|
|
mock_response = Mock()
|
|
mock_response.choices = [Mock(message=Mock(content="I'm doing well, thank you for asking!"))]
|
|
mock_together_client.chat.completions.create.return_value = mock_response
|
|
|
|
response = llm.generate_response(messages)
|
|
|
|
mock_together_client.chat.completions.create.assert_called_once_with(
|
|
model="mistralai/Mixtral-8x7B-Instruct-v0.1", messages=messages, temperature=0.7, max_tokens=100, top_p=1.0
|
|
)
|
|
assert response == "I'm doing well, thank you for asking!"
|
|
|
|
|
|
def test_generate_response_with_tools(mock_together_client):
|
|
config = BaseLlmConfig(model="mistralai/Mixtral-8x7B-Instruct-v0.1", temperature=0.7, max_tokens=100, top_p=1.0)
|
|
llm = TogetherLLM(config)
|
|
messages = [
|
|
{"role": "system", "content": "You are a helpful assistant."},
|
|
{"role": "user", "content": "Add a new memory: Today is a sunny day."},
|
|
]
|
|
tools = [
|
|
{
|
|
"type": "function",
|
|
"function": {
|
|
"name": "add_memory",
|
|
"description": "Add a memory",
|
|
"parameters": {
|
|
"type": "object",
|
|
"properties": {"data": {"type": "string", "description": "Data to add to memory"}},
|
|
"required": ["data"],
|
|
},
|
|
},
|
|
}
|
|
]
|
|
|
|
mock_response = Mock()
|
|
mock_message = Mock()
|
|
mock_message.content = "I've added the memory for you."
|
|
|
|
mock_tool_call = Mock()
|
|
mock_tool_call.function.name = "add_memory"
|
|
mock_tool_call.function.arguments = '{"data": "Today is a sunny day."}'
|
|
|
|
mock_message.tool_calls = [mock_tool_call]
|
|
mock_response.choices = [Mock(message=mock_message)]
|
|
mock_together_client.chat.completions.create.return_value = mock_response
|
|
|
|
response = llm.generate_response(messages, tools=tools)
|
|
|
|
mock_together_client.chat.completions.create.assert_called_once_with(
|
|
model="mistralai/Mixtral-8x7B-Instruct-v0.1",
|
|
messages=messages,
|
|
temperature=0.7,
|
|
max_tokens=100,
|
|
top_p=1.0,
|
|
tools=tools,
|
|
tool_choice="auto",
|
|
)
|
|
|
|
assert response["content"] == "I've added the memory for you."
|
|
assert len(response["tool_calls"]) == 1
|
|
assert response["tool_calls"][0]["name"] == "add_memory"
|
|
assert response["tool_calls"][0]["arguments"] == {"data": "Today is a sunny day."}
|
|
|
|
|
|
def test_generate_response_forwards_extra_kwargs(mock_together_client):
|
|
"""Per the LLMBase contract, extra provider-specific kwargs must be accepted and
|
|
forwarded to the Together client (matching openai/deepseek/vllm/xai behavior)."""
|
|
config = BaseLlmConfig(model="mistralai/Mixtral-8x7B-Instruct-v0.1", temperature=0.7, max_tokens=100, top_p=1.0)
|
|
llm = TogetherLLM(config)
|
|
messages = [{"role": "user", "content": "Hello"}]
|
|
|
|
mock_response = Mock()
|
|
mock_response.choices = [Mock(message=Mock(content="Hi"))]
|
|
mock_together_client.chat.completions.create.return_value = mock_response
|
|
|
|
response = llm.generate_response(messages, frequency_penalty=0.5)
|
|
|
|
assert response == "Hi"
|
|
call_kwargs = mock_together_client.chat.completions.create.call_args.kwargs
|
|
assert call_kwargs["frequency_penalty"] == 0.5
|