test_embedchain.py 2.0 KB

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