[Misc] Lint code and fix code smells (#1871)

This commit is contained in:
Deshraj Yadav
2024-09-16 17:39:54 -07:00
committed by GitHub
parent 0a78cb9f7a
commit 55c54beeab
57 changed files with 1178 additions and 1357 deletions

View File

@@ -1,4 +1,3 @@
from unittest.mock import Mock, patch
import httpx
@@ -7,26 +6,28 @@ import pytest
from mem0.configs.llms.base import BaseLlmConfig
from mem0.llms.azure_openai import AzureOpenAILLM
MODEL = "gpt-4o" # or your custom deployment name
MODEL = "gpt-4o" # or your custom deployment name
TEMPERATURE = 0.7
MAX_TOKENS = 100
TOP_P = 1.0
@pytest.fixture
def mock_openai_client():
with patch('mem0.llms.azure_openai.AzureOpenAI') as mock_openai:
with patch("mem0.llms.azure_openai.AzureOpenAI") as mock_openai:
mock_client = Mock()
mock_openai.return_value = mock_client
yield mock_client
def test_generate_response_without_tools(mock_openai_client):
config = BaseLlmConfig(model=MODEL, temperature=TEMPERATURE, max_tokens=MAX_TOKENS, top_p=TOP_P)
llm = AzureOpenAILLM(config)
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hello, how are you?"}
{"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_openai_client.chat.completions.create.return_value = mock_response
@@ -34,11 +35,7 @@ def test_generate_response_without_tools(mock_openai_client):
response = llm.generate_response(messages)
mock_openai_client.chat.completions.create.assert_called_once_with(
model=MODEL,
messages=messages,
temperature=TEMPERATURE,
max_tokens=MAX_TOKENS,
top_p=TOP_P
model=MODEL, messages=messages, temperature=TEMPERATURE, max_tokens=MAX_TOKENS, top_p=TOP_P
)
assert response == "I'm doing well, thank you for asking!"
@@ -48,7 +45,7 @@ def test_generate_response_with_tools(mock_openai_client):
llm = AzureOpenAILLM(config)
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Add a new memory: Today is a sunny day."}
{"role": "user", "content": "Add a new memory: Today is a sunny day."},
]
tools = [
{
@@ -58,23 +55,21 @@ def test_generate_response_with_tools(mock_openai_client):
"description": "Add a memory",
"parameters": {
"type": "object",
"properties": {
"data": {"type": "string", "description": "Data to add to memory"}
},
"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_openai_client.chat.completions.create.return_value = mock_response
@@ -88,24 +83,33 @@ def test_generate_response_with_tools(mock_openai_client):
max_tokens=MAX_TOKENS,
top_p=TOP_P,
tools=tools,
tool_choice="auto"
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.'}
assert response["tool_calls"][0]["arguments"] == {"data": "Today is a sunny day."}
def test_generate_with_http_proxies():
mock_http_client = Mock(spec=httpx.Client)
mock_http_client_instance = Mock(spec=httpx.Client)
mock_http_client.return_value = mock_http_client_instance
with (patch("mem0.llms.azure_openai.AzureOpenAI") as mock_azure_openai,
patch("httpx.Client", new=mock_http_client) as mock_http_client):
config = BaseLlmConfig(model=MODEL, temperature=TEMPERATURE, max_tokens=MAX_TOKENS, top_p=TOP_P,
api_key="test", http_client_proxies="http://testproxy.mem0.net:8000",
azure_kwargs= {"api_key" : "test"})
with (
patch("mem0.llms.azure_openai.AzureOpenAI") as mock_azure_openai,
patch("httpx.Client", new=mock_http_client) as mock_http_client,
):
config = BaseLlmConfig(
model=MODEL,
temperature=TEMPERATURE,
max_tokens=MAX_TOKENS,
top_p=TOP_P,
api_key="test",
http_client_proxies="http://testproxy.mem0.net:8000",
azure_kwargs={"api_key": "test"},
)
_ = AzureOpenAILLM(config)
@@ -114,6 +118,6 @@ def test_generate_with_http_proxies():
http_client=mock_http_client_instance,
azure_deployment=None,
azure_endpoint=None,
api_version=None
api_version=None,
)
mock_http_client.assert_called_once_with(proxies="http://testproxy.mem0.net:8000")

View File

@@ -8,7 +8,7 @@ from mem0.llms.groq import GroqLLM
@pytest.fixture
def mock_groq_client():
with patch('mem0.llms.groq.Groq') as mock_groq:
with patch("mem0.llms.groq.Groq") as mock_groq:
mock_client = Mock()
mock_groq.return_value = mock_client
yield mock_client
@@ -19,9 +19,9 @@ def test_generate_response_without_tools(mock_groq_client):
llm = GroqLLM(config)
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hello, how are you?"}
{"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
@@ -29,11 +29,7 @@ def test_generate_response_without_tools(mock_groq_client):
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
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!"
@@ -43,7 +39,7 @@ def test_generate_response_with_tools(mock_groq_client):
llm = GroqLLM(config)
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Add a new memory: Today is a sunny day."}
{"role": "user", "content": "Add a new memory: Today is a sunny day."},
]
tools = [
{
@@ -53,23 +49,21 @@ def test_generate_response_with_tools(mock_groq_client):
"description": "Add a memory",
"parameters": {
"type": "object",
"properties": {
"data": {"type": "string", "description": "Data to add to memory"}
},
"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
@@ -83,11 +77,10 @@ def test_generate_response_with_tools(mock_groq_client):
max_tokens=100,
top_p=1.0,
tools=tools,
tool_choice="auto"
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.'}
assert response["tool_calls"][0]["arguments"] == {"data": "Today is a sunny day."}

View File

@@ -8,14 +8,15 @@ from mem0.llms import litellm
@pytest.fixture
def mock_litellm():
with patch('mem0.llms.litellm.litellm') as mock_litellm:
with patch("mem0.llms.litellm.litellm") as mock_litellm:
yield mock_litellm
def test_generate_response_with_unsupported_model(mock_litellm):
config = BaseLlmConfig(model="unsupported-model", temperature=0.7, max_tokens=100, top_p=1)
llm = litellm.LiteLLM(config)
messages = [{"role": "user", "content": "Hello"}]
mock_litellm.supports_function_calling.return_value = False
with pytest.raises(ValueError, match="Model 'unsupported-model' in litellm does not support function calling."):
@@ -27,9 +28,9 @@ def test_generate_response_without_tools(mock_litellm):
llm = litellm.LiteLLM(config)
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hello, how are you?"}
{"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_litellm.completion.return_value = mock_response
@@ -38,11 +39,7 @@ def test_generate_response_without_tools(mock_litellm):
response = llm.generate_response(messages)
mock_litellm.completion.assert_called_once_with(
model="gpt-4o",
messages=messages,
temperature=0.7,
max_tokens=100,
top_p=1.0
model="gpt-4o", messages=messages, temperature=0.7, max_tokens=100, top_p=1.0
)
assert response == "I'm doing well, thank you for asking!"
@@ -52,7 +49,7 @@ def test_generate_response_with_tools(mock_litellm):
llm = litellm.LiteLLM(config)
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Add a new memory: Today is a sunny day."}
{"role": "user", "content": "Add a new memory: Today is a sunny day."},
]
tools = [
{
@@ -62,23 +59,21 @@ def test_generate_response_with_tools(mock_litellm):
"description": "Add a memory",
"parameters": {
"type": "object",
"properties": {
"data": {"type": "string", "description": "Data to add to memory"}
},
"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_litellm.completion.return_value = mock_response
@@ -87,16 +82,10 @@ def test_generate_response_with_tools(mock_litellm):
response = llm.generate_response(messages, tools=tools)
mock_litellm.completion.assert_called_once_with(
model="gpt-4o",
messages=messages,
temperature=0.7,
max_tokens=100,
top_p=1,
tools=tools,
tool_choice="auto"
model="gpt-4o", messages=messages, temperature=0.7, max_tokens=100, top_p=1, 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.'}
assert response["tool_calls"][0]["arguments"] == {"data": "Today is a sunny day."}

View File

@@ -9,61 +9,48 @@ from mem0.llms.utils.tools import ADD_MEMORY_TOOL
@pytest.fixture
def mock_ollama_client():
with patch('mem0.llms.ollama.Client') as mock_ollama:
with patch("mem0.llms.ollama.Client") as mock_ollama:
mock_client = Mock()
mock_client.list.return_value = {"models": [{"name": "llama3.1:70b"}]}
mock_ollama.return_value = mock_client
yield mock_client
def test_generate_response_without_tools(mock_ollama_client):
config = BaseLlmConfig(model="llama3.1:70b", temperature=0.7, max_tokens=100, top_p=1.0)
llm = OllamaLLM(config)
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hello, how are you?"}
{"role": "user", "content": "Hello, how are you?"},
]
mock_response = {
'message': {"content": "I'm doing well, thank you for asking!"}
}
mock_response = {"message": {"content": "I'm doing well, thank you for asking!"}}
mock_ollama_client.chat.return_value = mock_response
response = llm.generate_response(messages)
mock_ollama_client.chat.assert_called_once_with(
model="llama3.1:70b",
messages=messages,
options={
"temperature": 0.7,
"num_predict": 100,
"top_p": 1.0
}
model="llama3.1:70b", messages=messages, options={"temperature": 0.7, "num_predict": 100, "top_p": 1.0}
)
assert response == "I'm doing well, thank you for asking!"
def test_generate_response_with_tools(mock_ollama_client):
config = BaseLlmConfig(model="llama3.1:70b", temperature=0.7, max_tokens=100, top_p=1.0)
llm = OllamaLLM(config)
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Add a new memory: Today is a sunny day."}
{"role": "user", "content": "Add a new memory: Today is a sunny day."},
]
tools = [ADD_MEMORY_TOOL]
mock_response = {
'message': {
"message": {
"content": "I've added the memory for you.",
"tool_calls": [
{
"function": {
"name": "add_memory",
"arguments": {"data": "Today is a sunny day."}
}
}
]
"tool_calls": [{"function": {"name": "add_memory", "arguments": {"data": "Today is a sunny day."}}}],
}
}
mock_ollama_client.chat.return_value = mock_response
response = llm.generate_response(messages, tools=tools)
@@ -71,16 +58,11 @@ def test_generate_response_with_tools(mock_ollama_client):
mock_ollama_client.chat.assert_called_once_with(
model="llama3.1:70b",
messages=messages,
options={
"temperature": 0.7,
"num_predict": 100,
"top_p": 1.0
},
tools=tools
options={"temperature": 0.7, "num_predict": 100, "top_p": 1.0},
tools=tools,
)
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.'}
assert response["tool_calls"][0]["arguments"] == {"data": "Today is a sunny day."}

View File

@@ -8,7 +8,7 @@ from mem0.llms.openai import OpenAILLM
@pytest.fixture
def mock_openai_client():
with patch('mem0.llms.openai.OpenAI') as mock_openai:
with patch("mem0.llms.openai.OpenAI") as mock_openai:
mock_client = Mock()
mock_openai.return_value = mock_client
yield mock_client
@@ -19,9 +19,9 @@ def test_generate_response_without_tools(mock_openai_client):
llm = OpenAILLM(config)
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hello, how are you?"}
{"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_openai_client.chat.completions.create.return_value = mock_response
@@ -29,11 +29,7 @@ def test_generate_response_without_tools(mock_openai_client):
response = llm.generate_response(messages)
mock_openai_client.chat.completions.create.assert_called_once_with(
model="gpt-4o",
messages=messages,
temperature=0.7,
max_tokens=100,
top_p=1.0
model="gpt-4o", messages=messages, temperature=0.7, max_tokens=100, top_p=1.0
)
assert response == "I'm doing well, thank you for asking!"
@@ -43,7 +39,7 @@ def test_generate_response_with_tools(mock_openai_client):
llm = OpenAILLM(config)
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Add a new memory: Today is a sunny day."}
{"role": "user", "content": "Add a new memory: Today is a sunny day."},
]
tools = [
{
@@ -53,23 +49,21 @@ def test_generate_response_with_tools(mock_openai_client):
"description": "Add a memory",
"parameters": {
"type": "object",
"properties": {
"data": {"type": "string", "description": "Data to add to memory"}
},
"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_openai_client.chat.completions.create.return_value = mock_response
@@ -77,17 +71,10 @@ def test_generate_response_with_tools(mock_openai_client):
response = llm.generate_response(messages, tools=tools)
mock_openai_client.chat.completions.create.assert_called_once_with(
model="gpt-4o",
messages=messages,
temperature=0.7,
max_tokens=100,
top_p=1.0,
tools=tools,
tool_choice="auto"
model="gpt-4o", 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.'}
assert response["tool_calls"][0]["arguments"] == {"data": "Today is a sunny day."}

View File

@@ -8,7 +8,7 @@ from mem0.llms.together import TogetherLLM
@pytest.fixture
def mock_together_client():
with patch('mem0.llms.together.Together') as mock_together:
with patch("mem0.llms.together.Together") as mock_together:
mock_client = Mock()
mock_together.return_value = mock_client
yield mock_client
@@ -19,9 +19,9 @@ def test_generate_response_without_tools(mock_together_client):
llm = TogetherLLM(config)
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hello, how are you?"}
{"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
@@ -29,11 +29,7 @@ def test_generate_response_without_tools(mock_together_client):
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
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!"
@@ -43,7 +39,7 @@ def test_generate_response_with_tools(mock_together_client):
llm = TogetherLLM(config)
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Add a new memory: Today is a sunny day."}
{"role": "user", "content": "Add a new memory: Today is a sunny day."},
]
tools = [
{
@@ -53,23 +49,21 @@ def test_generate_response_with_tools(mock_together_client):
"description": "Add a memory",
"parameters": {
"type": "object",
"properties": {
"data": {"type": "string", "description": "Data to add to memory"}
},
"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
@@ -83,11 +77,10 @@ def test_generate_response_with_tools(mock_together_client):
max_tokens=100,
top_p=1.0,
tools=tools,
tool_choice="auto"
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.'}
assert response["tool_calls"][0]["arguments"] == {"data": "Today is a sunny day."}