test_apps.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. import os
  2. import pytest
  3. import yaml
  4. from embedchain import App, CustomApp, Llama2App, OpenSourceApp
  5. from embedchain.config import (AddConfig, AppConfig, BaseEmbedderConfig,
  6. BaseLlmConfig, ChromaDbConfig)
  7. from embedchain.embedder.base import BaseEmbedder
  8. from embedchain.llm.base import BaseLlm
  9. from embedchain.vectordb.base import BaseVectorDB, BaseVectorDbConfig
  10. from embedchain.vectordb.chroma import ChromaDB
  11. @pytest.fixture
  12. def app():
  13. os.environ["OPENAI_API_KEY"] = "test_api_key"
  14. return App()
  15. @pytest.fixture
  16. def custom_app():
  17. os.environ["OPENAI_API_KEY"] = "test_api_key"
  18. return CustomApp()
  19. @pytest.fixture
  20. def opensource_app():
  21. os.environ["OPENAI_API_KEY"] = "test_api_key"
  22. return OpenSourceApp()
  23. @pytest.fixture
  24. def llama2_app():
  25. os.environ["OPENAI_API_KEY"] = "test_api_key"
  26. os.environ["REPLICATE_API_TOKEN"] = "-"
  27. return Llama2App()
  28. def test_app(app):
  29. assert isinstance(app.llm, BaseLlm)
  30. assert isinstance(app.db, BaseVectorDB)
  31. assert isinstance(app.embedder, BaseEmbedder)
  32. def test_custom_app(custom_app):
  33. assert isinstance(custom_app.llm, BaseLlm)
  34. assert isinstance(custom_app.db, BaseVectorDB)
  35. assert isinstance(custom_app.embedder, BaseEmbedder)
  36. def test_opensource_app(opensource_app):
  37. assert isinstance(opensource_app.llm, BaseLlm)
  38. assert isinstance(opensource_app.db, BaseVectorDB)
  39. assert isinstance(opensource_app.embedder, BaseEmbedder)
  40. def test_llama2_app(llama2_app):
  41. assert isinstance(llama2_app.llm, BaseLlm)
  42. assert isinstance(llama2_app.db, BaseVectorDB)
  43. assert isinstance(llama2_app.embedder, BaseEmbedder)
  44. class TestConfigForAppComponents:
  45. def test_constructor_config(self):
  46. collection_name = "my-test-collection"
  47. app = App(db_config=ChromaDbConfig(collection_name=collection_name))
  48. assert app.db.config.collection_name == collection_name
  49. def test_component_config(self):
  50. collection_name = "my-test-collection"
  51. database = ChromaDB(config=ChromaDbConfig(collection_name=collection_name))
  52. app = App(db=database)
  53. assert app.db.config.collection_name == collection_name
  54. def test_different_configs_are_proper_instances(self):
  55. app_config = AppConfig()
  56. wrong_config = AddConfig()
  57. with pytest.raises(TypeError):
  58. App(config=wrong_config)
  59. assert isinstance(app_config, AppConfig)
  60. llm_config = BaseLlmConfig()
  61. wrong_llm_config = "wrong_llm_config"
  62. with pytest.raises(TypeError):
  63. App(llm_config=wrong_llm_config)
  64. assert isinstance(llm_config, BaseLlmConfig)
  65. db_config = BaseVectorDbConfig()
  66. wrong_db_config = "wrong_db_config"
  67. with pytest.raises(TypeError):
  68. App(db_config=wrong_db_config)
  69. assert isinstance(db_config, BaseVectorDbConfig)
  70. embedder_config = BaseEmbedderConfig()
  71. wrong_embedder_config = "wrong_embedder_config"
  72. with pytest.raises(TypeError):
  73. App(embedder_config=wrong_embedder_config)
  74. assert isinstance(embedder_config, BaseEmbedderConfig)
  75. def test_components_raises_type_error_if_not_proper_instances(self):
  76. wrong_llm = "wrong_llm"
  77. with pytest.raises(TypeError):
  78. App(llm=wrong_llm)
  79. wrong_db = "wrong_db"
  80. with pytest.raises(TypeError):
  81. App(db=wrong_db)
  82. wrong_embedder = "wrong_embedder"
  83. with pytest.raises(TypeError):
  84. App(embedder=wrong_embedder)
  85. class TestAppFromConfig:
  86. def load_config_data(self, yaml_path):
  87. with open(yaml_path, "r") as file:
  88. return yaml.safe_load(file)
  89. def test_from_chroma_config(self):
  90. yaml_path = "configs/chroma.yaml"
  91. config_data = self.load_config_data(yaml_path)
  92. app = App.from_config(yaml_path)
  93. # Check if the App instance and its components were created correctly
  94. assert isinstance(app, App)
  95. # Validate the AppConfig values
  96. assert app.config.id == config_data["app"]["config"]["id"]
  97. assert app.config.collection_name == config_data["app"]["config"]["collection_name"]
  98. # Even though not present in the config, the default value is used
  99. assert app.config.collect_metrics is True
  100. # Validate the LLM config values
  101. llm_config = config_data["llm"]["config"]
  102. assert app.llm.config.temperature == llm_config["temperature"]
  103. assert app.llm.config.max_tokens == llm_config["max_tokens"]
  104. assert app.llm.config.top_p == llm_config["top_p"]
  105. assert app.llm.config.stream == llm_config["stream"]
  106. # Validate the VectorDB config values
  107. db_config = config_data["vectordb"]["config"]
  108. assert app.db.config.collection_name == db_config["collection_name"]
  109. assert app.db.config.dir == db_config["dir"]
  110. assert app.db.config.allow_reset == db_config["allow_reset"]
  111. # Validate the Embedder config values
  112. embedder_config = config_data["embedder"]["config"]
  113. assert app.embedder.config.model == embedder_config["model"]
  114. assert app.embedder.config.deployment_name == embedder_config["deployment_name"]
  115. def test_from_opensource_config(self):
  116. yaml_path = "configs/opensource.yaml"
  117. config_data = self.load_config_data(yaml_path)
  118. app = App.from_config(yaml_path)
  119. # Check if the App instance and its components were created correctly
  120. assert isinstance(app, App)
  121. # Validate the AppConfig values
  122. assert app.config.id == config_data["app"]["config"]["id"]
  123. assert app.config.collection_name == config_data["app"]["config"]["collection_name"]
  124. assert app.config.collect_metrics == config_data["app"]["config"]["collect_metrics"]
  125. # Validate the LLM config values
  126. llm_config = config_data["llm"]["config"]
  127. assert app.llm.config.temperature == llm_config["temperature"]
  128. assert app.llm.config.max_tokens == llm_config["max_tokens"]
  129. assert app.llm.config.top_p == llm_config["top_p"]
  130. assert app.llm.config.stream == llm_config["stream"]
  131. # Validate the VectorDB config values
  132. db_config = config_data["vectordb"]["config"]
  133. assert app.db.config.collection_name == db_config["collection_name"]
  134. assert app.db.config.dir == db_config["dir"]
  135. assert app.db.config.allow_reset == db_config["allow_reset"]
  136. # Validate the Embedder config values
  137. embedder_config = config_data["embedder"]["config"]
  138. assert app.embedder.config.model == embedder_config["model"]
  139. assert app.embedder.config.deployment_name == embedder_config["deployment_name"]