-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: generalize creating objects from config (#233)
- Loading branch information
1 parent
ba4a59d
commit 0b6e1e1
Showing
34 changed files
with
606 additions
and
372 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 0 additions & 24 deletions
24
packages/ragbits-core/src/ragbits/core/embeddings/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,5 @@ | ||
import sys | ||
|
||
from ragbits.core.utils.config_handling import get_cls_from_config | ||
|
||
from .base import Embeddings, EmbeddingType | ||
from .litellm import LiteLLMEmbeddings | ||
from .noop import NoopEmbeddings | ||
|
||
__all__ = ["EmbeddingType", "Embeddings", "LiteLLMEmbeddings", "NoopEmbeddings"] | ||
|
||
module = sys.modules[__name__] | ||
|
||
|
||
def get_embeddings(embedder_config: dict) -> Embeddings: | ||
""" | ||
Initializes and returns an Embeddings object based on the provided embedder configuration. | ||
Args: | ||
embedder_config : A dictionary containing configuration details for the embedder. | ||
Returns: | ||
An instance of the specified Embeddings class, initialized with the provided config | ||
(if any) or default arguments. | ||
""" | ||
embeddings_type = embedder_config["type"] | ||
config = embedder_config.get("config", {}) | ||
|
||
embbedings = get_cls_from_config(embeddings_type, module) | ||
return embbedings(**config) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,4 @@ | ||
import sys | ||
|
||
from ragbits.core.utils.config_handling import get_cls_from_config | ||
|
||
from .base import LLM | ||
from .litellm import LiteLLM | ||
|
||
__all__ = ["LLM", "LiteLLM"] | ||
|
||
module = sys.modules[__name__] | ||
|
||
|
||
def get_llm(config: dict) -> LLM: | ||
""" | ||
Initializes and returns an LLM object based on the provided configuration. | ||
Args: | ||
config : A dictionary containing configuration details for the LLM. | ||
Returns: | ||
An instance of the specified LLM class, initialized with the provided config | ||
(if any) or default arguments. | ||
Raises: | ||
KeyError: If the configuration dictionary does not contain a "type" key. | ||
ValueError: If the LLM class is not a subclass of LLM. | ||
""" | ||
llm_type = config["type"] | ||
llm_config = config.get("config", {}) | ||
default_options = llm_config.pop("default_options", None) | ||
llm_cls = get_cls_from_config(llm_type, module) | ||
|
||
if not issubclass(llm_cls, LLM): | ||
raise ValueError(f"Invalid LLM class: {llm_cls}") | ||
|
||
# We need to infer the options class from the LLM class. | ||
# pylint: disable=protected-access | ||
options = llm_cls._options_cls(**default_options) if default_options else None # type: ignore | ||
|
||
return llm_cls(**llm_config, default_options=options) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 0 additions & 26 deletions
26
packages/ragbits-core/src/ragbits/core/metadata_stores/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,4 @@ | ||
import sys | ||
|
||
from ragbits.core.utils.config_handling import get_cls_from_config | ||
|
||
from .base import MetadataStore | ||
from .in_memory import InMemoryMetadataStore | ||
|
||
__all__ = ["InMemoryMetadataStore", "MetadataStore"] | ||
|
||
module = sys.modules[__name__] | ||
|
||
|
||
def get_metadata_store(metadata_store_config: dict | None) -> MetadataStore | None: | ||
""" | ||
Initializes and returns a MetadataStore object based on the provided configuration. | ||
Args: | ||
metadata_store_config: A dictionary containing configuration details for the MetadataStore. | ||
Returns: | ||
An instance of the specified MetadataStore class, initialized with the provided config | ||
(if any) or default arguments. | ||
""" | ||
if metadata_store_config is None: | ||
return None | ||
|
||
metadata_store_class = get_cls_from_config(metadata_store_config["type"], module) | ||
config = metadata_store_config.get("config", {}) | ||
|
||
return metadata_store_class(**config) |
8 changes: 7 additions & 1 deletion
8
packages/ragbits-core/src/ragbits/core/metadata_stores/base.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 0 additions & 23 deletions
23
packages/ragbits-core/src/ragbits/core/vector_stores/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,4 @@ | ||
import sys | ||
|
||
from ragbits.core.utils.config_handling import get_cls_from_config | ||
from ragbits.core.vector_stores.base import VectorStore, VectorStoreEntry, VectorStoreOptions, WhereQuery | ||
from ragbits.core.vector_stores.in_memory import InMemoryVectorStore | ||
|
||
__all__ = ["InMemoryVectorStore", "VectorStore", "VectorStoreEntry", "VectorStoreOptions", "WhereQuery"] | ||
|
||
|
||
def get_vector_store(config: dict) -> VectorStore: | ||
""" | ||
Initializes and returns a VectorStore object based on the provided configuration. | ||
Args: | ||
config: A dictionary containing configuration details for the VectorStore. | ||
Returns: | ||
An instance of the specified VectorStore class, initialized with the provided config | ||
(if any) or default arguments. | ||
Raises: | ||
KeyError: If the provided configuration does not contain a valid "type" key. | ||
InvalidConfigurationError: If the provided configuration is invalid. | ||
NotImplementedError: If the specified VectorStore class cannot be created from the provided configuration. | ||
""" | ||
vector_store_cls = get_cls_from_config(config["type"], sys.modules[__name__]) | ||
return vector_store_cls.from_config(config.get("config", {})) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.