test_apps.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. class TestAppFromConfig:
  76. def load_config_data(self, yaml_path):
  77. with open(yaml_path, "r") as file:
  78. return yaml.safe_load(file)
  79. def test_from_chroma_config(self):
  80. yaml_path = "configs/chroma.yaml"
  81. config_data = self.load_config_data(yaml_path)
  82. app = App.from_config(yaml_path)
  83. # Check if the App instance and its components were created correctly
  84. assert isinstance(app, App)
  85. # Validate the AppConfig values
  86. assert app.config.id == config_data["app"]["config"]["id"]
  87. assert app.config.collection_name == config_data["app"]["config"]["collection_name"]
  88. # Even though not present in the config, the default value is used
  89. assert app.config.collect_metrics is True
  90. # Validate the LLM config values
  91. llm_config = config_data["llm"]["config"]
  92. assert app.llm.config.temperature == llm_config["temperature"]
  93. assert app.llm.config.max_tokens == llm_config["max_tokens"]
  94. assert app.llm.config.top_p == llm_config["top_p"]
  95. assert app.llm.config.stream == llm_config["stream"]
  96. # Validate the VectorDB config values
  97. db_config = config_data["vectordb"]["config"]
  98. assert app.db.config.collection_name == db_config["collection_name"]
  99. assert app.db.config.dir == db_config["dir"]
  100. assert app.db.config.allow_reset == db_config["allow_reset"]
  101. # Validate the Embedder config values
  102. embedder_config = config_data["embedder"]["config"]
  103. assert app.embedder.config.model == embedder_config["model"]
  104. assert app.embedder.config.deployment_name == embedder_config["deployment_name"]
  105. def test_from_opensource_config(self):
  106. yaml_path = "configs/opensource.yaml"
  107. config_data = self.load_config_data(yaml_path)
  108. app = App.from_config(yaml_path)
  109. # Check if the App instance and its components were created correctly
  110. assert isinstance(app, App)
  111. # Validate the AppConfig values
  112. assert app.config.id == config_data["app"]["config"]["id"]
  113. assert app.config.collection_name == config_data["app"]["config"]["collection_name"]
  114. assert app.config.collect_metrics == config_data["app"]["config"]["collect_metrics"]
  115. # Validate the LLM config values
  116. llm_config = config_data["llm"]["config"]
  117. assert app.llm.config.temperature == llm_config["temperature"]
  118. assert app.llm.config.max_tokens == llm_config["max_tokens"]
  119. assert app.llm.config.top_p == llm_config["top_p"]
  120. assert app.llm.config.stream == llm_config["stream"]
  121. # Validate the VectorDB config values
  122. db_config = config_data["vectordb"]["config"]
  123. assert app.db.config.collection_name == db_config["collection_name"]
  124. assert app.db.config.dir == db_config["dir"]
  125. assert app.db.config.allow_reset == db_config["allow_reset"]
  126. # Validate the Embedder config values
  127. embedder_config = config_data["embedder"]["config"]
  128. assert app.embedder.config.model == embedder_config["model"]
  129. assert app.embedder.config.deployment_name == embedder_config["deployment_name"]