test_embedchain.py 2.2 KB

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