test_embedchain.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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("chromadb.api.models.Collection.Collection.get")
  10. @patch("embedchain.embedchain.EmbedChain.retrieve_from_database")
  11. @patch("embedchain.llm.base_llm.BaseLlm.get_answer_from_llm")
  12. @patch("embedchain.llm.base_llm.BaseLlm.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. config = AppConfig(log_level="DEBUG", collect_metrics=False)
  36. app = App(config=config, chromadb_config=ChromaDbConfig(chroma_settings={"allow_reset": True}))
  37. app.reset()
  38. # Make sure the client is still healthy
  39. app.db.client.heartbeat()
  40. # Make sure the collection exists, and can be added to
  41. app.db.collection.add(
  42. embeddings=[[1.1, 2.3, 3.2], [4.5, 6.9, 4.4], [1.1, 2.3, 3.2]],
  43. metadatas=[
  44. {"chapter": "3", "verse": "16"},
  45. {"chapter": "3", "verse": "5"},
  46. {"chapter": "29", "verse": "11"},
  47. ],
  48. ids=["id1", "id2", "id3"],
  49. )
  50. app.reset()