test_chat.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import os
  2. import unittest
  3. from unittest.mock import MagicMock, patch
  4. from embedchain import App
  5. from embedchain.config import AppConfig, ChatConfig
  6. class TestApp(unittest.TestCase):
  7. def setUp(self):
  8. os.environ["OPENAI_API_KEY"] = "test_key"
  9. self.app = App(config=AppConfig(collect_metrics=False))
  10. @patch.object(App, "retrieve_from_database", return_value=["Test context"])
  11. @patch.object(App, "get_answer_from_llm", return_value="Test answer")
  12. def test_chat_with_memory(self, mock_get_answer, mock_retrieve):
  13. """
  14. This test checks the functionality of the 'chat' method in the App class with respect to the chat history
  15. memory.
  16. The 'chat' method is called twice. The first call initializes the chat history memory.
  17. The second call is expected to use the chat history from the first call.
  18. Key assumptions tested:
  19. called with correct arguments, adding the correct chat history.
  20. - After the first call, 'memory.chat_memory.add_user_message' and 'memory.chat_memory.add_ai_message' are
  21. - During the second call, the 'chat' method uses the chat history from the first call.
  22. The test isolates the 'chat' method behavior by mocking out 'retrieve_from_database', 'get_answer_from_llm' and
  23. 'memory' methods.
  24. """
  25. app = App()
  26. first_answer = app.chat("Test query 1")
  27. self.assertEqual(first_answer, "Test answer")
  28. self.assertEqual(len(app.memory.chat_memory.messages), 2)
  29. second_answer = app.chat("Test query 2")
  30. self.assertEqual(second_answer, "Test answer")
  31. self.assertEqual(len(app.memory.chat_memory.messages), 4)
  32. @patch("chromadb.api.models.Collection.Collection.add", MagicMock)
  33. def test_chat_with_where_in_params(self):
  34. """
  35. This test checks the functionality of the 'chat' method in the App class.
  36. It simulates a scenario where the 'retrieve_from_database' method returns a context list based on
  37. a where filter and 'get_llm_model_answer' returns an expected answer string.
  38. The 'chat' method is expected to call 'retrieve_from_database' with the where filter and
  39. 'get_llm_model_answer' methods appropriately and return the right answer.
  40. Key assumptions tested:
  41. - 'retrieve_from_database' method is called exactly once with arguments: "Test query" and an instance of
  42. QueryConfig.
  43. - 'get_llm_model_answer' is called exactly once. The specific arguments are not checked in this test.
  44. - 'chat' method returns the value it received from 'get_llm_model_answer'.
  45. The test isolates the 'chat' method behavior by mocking out 'retrieve_from_database' and
  46. 'get_llm_model_answer' methods.
  47. """
  48. with patch.object(self.app, "retrieve_from_database") as mock_retrieve:
  49. mock_retrieve.return_value = ["Test context"]
  50. with patch.object(self.app, "get_llm_model_answer") as mock_answer:
  51. mock_answer.return_value = "Test answer"
  52. answer = self.app.chat("Test chat", where={"attribute": "value"})
  53. self.assertEqual(answer, "Test answer")
  54. self.assertEqual(mock_retrieve.call_args[0][0], "Test chat")
  55. self.assertEqual(mock_retrieve.call_args[0][2], {"attribute": "value"})
  56. mock_answer.assert_called_once()
  57. @patch("chromadb.api.models.Collection.Collection.add", MagicMock)
  58. def test_chat_with_where_in_chat_config(self):
  59. """
  60. This test checks the functionality of the 'chat' method in the App class.
  61. It simulates a scenario where the 'retrieve_from_database' method returns a context list based on
  62. a where filter and 'get_llm_model_answer' returns an expected answer string.
  63. The 'chat' method is expected to call 'retrieve_from_database' with the where filter specified
  64. in the QueryConfig and 'get_llm_model_answer' methods appropriately and return the right answer.
  65. Key assumptions tested:
  66. - 'retrieve_from_database' method is called exactly once with arguments: "Test query" and an instance of
  67. QueryConfig.
  68. - 'get_llm_model_answer' is called exactly once. The specific arguments are not checked in this test.
  69. - 'chat' method returns the value it received from 'get_llm_model_answer'.
  70. The test isolates the 'chat' method behavior by mocking out 'retrieve_from_database' and
  71. 'get_llm_model_answer' methods.
  72. """
  73. with patch.object(self.app, "retrieve_from_database") as mock_retrieve:
  74. mock_retrieve.return_value = ["Test context"]
  75. with patch.object(self.app, "get_llm_model_answer") as mock_answer:
  76. mock_answer.return_value = "Test answer"
  77. chatConfig = ChatConfig(where={"attribute": "value"})
  78. answer = self.app.chat("Test chat", chatConfig)
  79. self.assertEqual(answer, "Test answer")
  80. self.assertEqual(mock_retrieve.call_args[0][0], "Test chat")
  81. self.assertEqual(mock_retrieve.call_args[0][1].where, {"attribute": "value"})
  82. self.assertIsInstance(mock_retrieve.call_args[0][1], ChatConfig)
  83. mock_answer.assert_called_once()