test_embedchain.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import os
  2. import unittest
  3. from unittest.mock import patch
  4. from embedchain import App
  5. from embedchain.config import AppConfig
  6. class TestChromaDbHostsLoglevel(unittest.TestCase):
  7. os.environ["OPENAI_API_KEY"] = "test_key"
  8. @patch("chromadb.api.models.Collection.Collection.add")
  9. @patch("chromadb.api.models.Collection.Collection.get")
  10. @patch("embedchain.embedchain.EmbedChain.retrieve_from_database")
  11. @patch("embedchain.embedchain.EmbedChain.get_answer_from_llm")
  12. @patch("embedchain.embedchain.EmbedChain.get_llm_model_answer")
  13. def test_whole_app(
  14. self,
  15. _mock_get,
  16. _mock_add,
  17. _mock_ec_retrieve_from_database,
  18. _mock_get_answer_from_llm,
  19. mock_ec_get_llm_model_answer,
  20. ):
  21. """
  22. Test if the `App` instance is initialized without a config that does not contain default hosts and ports.
  23. """
  24. config = AppConfig(log_level="DEBUG", collect_metrics=False)
  25. app = App(config)
  26. knowledge = "lorem ipsum dolor sit amet, consectetur adipiscing"
  27. app.add(knowledge, data_type="text")
  28. app.query("What text did I give you?")
  29. app.chat("What text did I give you?")
  30. self.assertEqual(mock_ec_get_llm_model_answer.call_args[1]["documents"], [knowledge])
  31. def test_add_after_reset(self):
  32. """
  33. Test if the `App` instance is correctly reconstructed after a reset.
  34. """
  35. app = App()
  36. app.reset()
  37. # Make sure the client is still healthy
  38. app.db.client.heartbeat()
  39. # Make sure the collection exists, and can be added to
  40. app.collection.add(
  41. embeddings=[[1.1, 2.3, 3.2], [4.5, 6.9, 4.4], [1.1, 2.3, 3.2]],
  42. metadatas=[
  43. {"chapter": "3", "verse": "16"},
  44. {"chapter": "3", "verse": "5"},
  45. {"chapter": "29", "verse": "11"},
  46. ],
  47. ids=["id1", "id2", "id3"],
  48. )
  49. app.reset()