test_app.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import os
  2. import pytest
  3. import yaml
  4. from embedchain import App
  5. from embedchain.config import ChromaDbConfig
  6. from embedchain.embedder.base import BaseEmbedder
  7. from embedchain.llm.base import BaseLlm
  8. from embedchain.vectordb.base import BaseVectorDB
  9. from embedchain.vectordb.chroma import ChromaDB
  10. @pytest.fixture
  11. def app():
  12. os.environ["OPENAI_API_KEY"] = "test_api_key"
  13. return App()
  14. def test_app(app):
  15. assert isinstance(app.llm, BaseLlm)
  16. assert isinstance(app.db, BaseVectorDB)
  17. assert isinstance(app.embedding_model, BaseEmbedder)
  18. class TestConfigForAppComponents:
  19. def test_constructor_config(self):
  20. collection_name = "my-test-collection"
  21. db = ChromaDB(config=ChromaDbConfig(collection_name=collection_name))
  22. app = App(db=db)
  23. assert app.db.config.collection_name == collection_name
  24. def test_component_config(self):
  25. collection_name = "my-test-collection"
  26. database = ChromaDB(config=ChromaDbConfig(collection_name=collection_name))
  27. app = App(db=database)
  28. assert app.db.config.collection_name == collection_name
  29. class TestAppFromConfig:
  30. def load_config_data(self, yaml_path):
  31. with open(yaml_path, "r") as file:
  32. return yaml.safe_load(file)
  33. def test_from_chroma_config(self, mocker):
  34. mocker.patch("embedchain.vectordb.chroma.chromadb.Client")
  35. yaml_path = "configs/chroma.yaml"
  36. config_data = self.load_config_data(yaml_path)
  37. app = App.from_config(config_path=yaml_path)
  38. # Check if the App instance and its components were created correctly
  39. assert isinstance(app, App)
  40. # Validate the AppConfig values
  41. assert app.config.id == config_data["app"]["config"]["id"]
  42. # Even though not present in the config, the default value is used
  43. assert app.config.collect_metrics is True
  44. # Validate the LLM config values
  45. llm_config = config_data["llm"]["config"]
  46. assert app.llm.config.temperature == llm_config["temperature"]
  47. assert app.llm.config.max_tokens == llm_config["max_tokens"]
  48. assert app.llm.config.top_p == llm_config["top_p"]
  49. assert app.llm.config.stream == llm_config["stream"]
  50. # Validate the VectorDB config values
  51. db_config = config_data["vectordb"]["config"]
  52. assert app.db.config.collection_name == db_config["collection_name"]
  53. assert app.db.config.dir == db_config["dir"]
  54. assert app.db.config.allow_reset == db_config["allow_reset"]
  55. # Validate the Embedder config values
  56. embedder_config = config_data["embedder"]["config"]
  57. assert app.embedding_model.config.model == embedder_config["model"]
  58. assert app.embedding_model.config.deployment_name == embedder_config.get("deployment_name")
  59. def test_from_opensource_config(self, mocker):
  60. mocker.patch("embedchain.vectordb.chroma.chromadb.Client")
  61. yaml_path = "configs/opensource.yaml"
  62. config_data = self.load_config_data(yaml_path)
  63. app = App.from_config(yaml_path)
  64. # Check if the App instance and its components were created correctly
  65. assert isinstance(app, App)
  66. # Validate the AppConfig values
  67. assert app.config.id == config_data["app"]["config"]["id"]
  68. assert app.config.collect_metrics == config_data["app"]["config"]["collect_metrics"]
  69. # Validate the LLM config values
  70. llm_config = config_data["llm"]["config"]
  71. assert app.llm.config.model == llm_config["model"]
  72. assert app.llm.config.temperature == llm_config["temperature"]
  73. assert app.llm.config.max_tokens == llm_config["max_tokens"]
  74. assert app.llm.config.top_p == llm_config["top_p"]
  75. assert app.llm.config.stream == llm_config["stream"]
  76. # Validate the VectorDB config values
  77. db_config = config_data["vectordb"]["config"]
  78. assert app.db.config.collection_name == db_config["collection_name"]
  79. assert app.db.config.dir == db_config["dir"]
  80. assert app.db.config.allow_reset == db_config["allow_reset"]
  81. # Validate the Embedder config values
  82. embedder_config = config_data["embedder"]["config"]
  83. assert app.embedding_model.config.deployment_name == embedder_config["deployment_name"]