Files
wehub-resource-sync 555e282cc4
pi-agent-plugin checks / lint (push) Has been cancelled
pi-agent-plugin checks / test (20) (push) Has been cancelled
pi-agent-plugin checks / test (22) (push) Has been cancelled
pi-agent-plugin checks / build (push) Has been cancelled
TypeScript SDK CI / check_changes (push) Has been cancelled
TypeScript SDK CI / changelog_check (push) Has been cancelled
ci / changelog_check (push) Has been cancelled
ci / check_changes (push) Has been cancelled
ci / build_mem0 (3.10) (push) Has been cancelled
ci / build_mem0 (3.11) (push) Has been cancelled
ci / build_mem0 (3.12) (push) Has been cancelled
CLI Node CI / lint (push) Has been cancelled
CLI Node CI / test (20) (push) Has been cancelled
CLI Node CI / test (22) (push) Has been cancelled
CLI Node CI / build (push) Has been cancelled
CLI Python CI / lint (push) Has been cancelled
CLI Python CI / test (3.10) (push) Has been cancelled
CLI Python CI / test (3.11) (push) Has been cancelled
CLI Python CI / test (3.12) (push) Has been cancelled
CLI Python CI / build (push) Has been cancelled
openclaw checks / lint (push) Has been cancelled
openclaw checks / test (20) (push) Has been cancelled
openclaw checks / test (22) (push) Has been cancelled
openclaw checks / build (push) Has been cancelled
opencode-plugin checks / build (push) Has been cancelled
TypeScript SDK CI / build_ts_sdk (20) (push) Has been cancelled
TypeScript SDK CI / build_ts_sdk (22) (push) Has been cancelled
TypeScript SDK CI / integration_ts_sdk (20) (push) Has been cancelled
TypeScript SDK CI / integration_ts_sdk (22) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:03:45 +08:00

185 lines
7.1 KiB
Python

from unittest.mock import Mock, patch
import pytest
from mem0.configs.llms.base import BaseLlmConfig
from mem0.llms.groq import GroqLLM
@pytest.fixture
def mock_groq_client():
with patch("mem0.llms.groq.Groq") as mock_groq:
mock_client = Mock()
mock_groq.return_value = mock_client
yield mock_client
def test_generate_response_without_tools(mock_groq_client):
config = BaseLlmConfig(model="llama3-70b-8192", temperature=0.7, max_tokens=100, top_p=1.0)
llm = GroqLLM(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_groq_client.chat.completions.create.return_value = mock_response
response = llm.generate_response(messages)
mock_groq_client.chat.completions.create.assert_called_once_with(
model="llama3-70b-8192", 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_groq_client):
config = BaseLlmConfig(model="llama3-70b-8192", temperature=0.7, max_tokens=100, top_p=1.0)
llm = GroqLLM(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_groq_client.chat.completions.create.return_value = mock_response
response = llm.generate_response(messages, tools=tools)
mock_groq_client.chat.completions.create.assert_called_once_with(
model="llama3-70b-8192",
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."}
@pytest.mark.parametrize("model", ["groq/compound", "groq/compound-mini"])
def test_generate_response_skips_json_mode_for_compound_models(mock_groq_client, model):
config = BaseLlmConfig(model=model, temperature=0.7, max_tokens=100, top_p=1.0)
llm = GroqLLM(config)
messages = [{"role": "user", "content": "Hi, I'm Alice and I love hiking."}]
# Compound models answer JSON-mode requests with empty or non-JSON content;
# the mock mirrors that plain-text reply. These tests pin request
# construction (response_format omitted), not end-to-end extraction.
mock_response = Mock()
mock_response.choices = [Mock(message=Mock(content="Alice introduced herself and mentioned she loves hiking."))]
mock_groq_client.chat.completions.create.return_value = mock_response
llm.generate_response(messages, response_format={"type": "json_object"})
_, kwargs = mock_groq_client.chat.completions.create.call_args
assert "response_format" not in kwargs
def test_generate_response_keeps_json_mode_for_standard_model(mock_groq_client):
config = BaseLlmConfig(model="llama-3.3-70b-versatile", temperature=0.7, max_tokens=100, top_p=1.0)
llm = GroqLLM(config)
messages = [{"role": "user", "content": "Hi, I'm Alice and I love hiking."}]
mock_response = Mock()
mock_response.choices = [Mock(message=Mock(content='{"memory": ["Name is Alice", "Loves hiking"]}'))]
mock_groq_client.chat.completions.create.return_value = mock_response
llm.generate_response(messages, response_format={"type": "json_object"})
_, kwargs = mock_groq_client.chat.completions.create.call_args
assert kwargs["response_format"] == {"type": "json_object"}
def test_generate_response_keeps_non_json_response_format_for_compound_model(mock_groq_client):
config = BaseLlmConfig(model="groq/compound", temperature=0.7, max_tokens=100, top_p=1.0)
llm = GroqLLM(config)
messages = [{"role": "user", "content": "Hi, I'm Alice and I love hiking."}]
mock_response = Mock()
mock_response.choices = [Mock(message=Mock(content="Alice loves hiking."))]
mock_groq_client.chat.completions.create.return_value = mock_response
llm.generate_response(messages, response_format={"type": "text"})
_, kwargs = mock_groq_client.chat.completions.create.call_args
assert kwargs["response_format"] == {"type": "text"}
def test_generate_response_keeps_tools_when_skipping_json_mode(mock_groq_client):
config = BaseLlmConfig(model="groq/compound", temperature=0.7, max_tokens=100, top_p=1.0)
llm = GroqLLM(config)
messages = [{"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 = "Done."
mock_message.tool_calls = None
mock_response.choices = [Mock(message=mock_message)]
mock_groq_client.chat.completions.create.return_value = mock_response
llm.generate_response(messages, response_format={"type": "json_object"}, tools=tools)
_, kwargs = mock_groq_client.chat.completions.create.call_args
assert "response_format" not in kwargs
assert kwargs["tools"] == tools
assert kwargs["tool_choice"] == "auto"
def test_generate_response_handles_non_string_model(mock_groq_client):
config = BaseLlmConfig(model={"name": "custom-model"}, temperature=0.7, max_tokens=100, top_p=1.0)
llm = GroqLLM(config)
messages = [{"role": "user", "content": "Hi, I'm Alice and I love hiking."}]
mock_response = Mock()
mock_response.choices = [Mock(message=Mock(content='{"memory": []}'))]
mock_groq_client.chat.completions.create.return_value = mock_response
llm.generate_response(messages, response_format={"type": "json_object"})
_, kwargs = mock_groq_client.chat.completions.create.call_args
assert kwargs["response_format"] == {"type": "json_object"}