test_litellm.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import pytest
  2. from unittest.mock import Mock, patch
  3. from mem0.llms import litellm
  4. @pytest.fixture
  5. def mock_litellm():
  6. with patch('mem0.llms.litellm.litellm') as mock_litellm:
  7. yield mock_litellm
  8. def test_generate_response_with_unsupported_model(mock_litellm):
  9. llm = litellm.LiteLLM(model="unsupported-model")
  10. messages = [{"role": "user", "content": "Hello"}]
  11. mock_litellm.supports_function_calling.return_value = False
  12. with pytest.raises(ValueError, match="Model 'unsupported-model' in litellm does not support function calling."):
  13. llm.generate_response(messages)
  14. def test_generate_response_without_tools(mock_litellm):
  15. llm = litellm.LiteLLM()
  16. messages = [
  17. {"role": "system", "content": "You are a helpful assistant."},
  18. {"role": "user", "content": "Hello, how are you?"}
  19. ]
  20. mock_response = Mock()
  21. mock_response.choices = [Mock(message=Mock(content="I'm doing well, thank you for asking!"))]
  22. mock_litellm.completion.return_value = mock_response
  23. mock_litellm.supports_function_calling.return_value = True
  24. response = llm.generate_response(messages)
  25. mock_litellm.completion.assert_called_once_with(
  26. model="gpt-4o",
  27. messages=messages
  28. )
  29. assert response == "I'm doing well, thank you for asking!"
  30. def test_generate_response_with_tools(mock_litellm):
  31. llm = litellm.LiteLLM()
  32. messages = [
  33. {"role": "system", "content": "You are a helpful assistant."},
  34. {"role": "user", "content": "Add a new memory: Today is a sunny day."}
  35. ]
  36. tools = [
  37. {
  38. "type": "function",
  39. "function": {
  40. "name": "add_memory",
  41. "description": "Add a memory",
  42. "parameters": {
  43. "type": "object",
  44. "properties": {
  45. "data": {"type": "string", "description": "Data to add to memory"}
  46. },
  47. "required": ["data"],
  48. },
  49. },
  50. }
  51. ]
  52. mock_response = Mock()
  53. mock_message = Mock()
  54. mock_message.content = "I've added the memory for you."
  55. mock_tool_call = Mock()
  56. mock_tool_call.function.name = "add_memory"
  57. mock_tool_call.function.arguments = '{"data": "Today is a sunny day."}'
  58. mock_message.tool_calls = [mock_tool_call]
  59. mock_response.choices = [Mock(message=mock_message)]
  60. mock_litellm.completion.return_value = mock_response
  61. mock_litellm.supports_function_calling.return_value = True
  62. response = llm.generate_response(messages, tools=tools)
  63. mock_litellm.completion.assert_called_once_with(
  64. model="gpt-4o",
  65. messages=messages,
  66. tools=tools,
  67. tool_choice="auto"
  68. )
  69. assert response["content"] == "I've added the memory for you."
  70. assert len(response["tool_calls"]) == 1
  71. assert response["tool_calls"][0]["name"] == "add_memory"
  72. assert response["tool_calls"][0]["arguments"] == {'data': 'Today is a sunny day.'}