-
Notifications
You must be signed in to change notification settings - Fork 25
feat: add support for custom embedding model #56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d7fd99e
feat: add support for custom embedding model
VladimirKadlec f07f7bf
test: add basic test
VladimirKadlec bbc8e22
merge: main
VladimirKadlec 3b2baa4
fix: address PR comments
VladimirKadlec 0a3f7f6
Merge branch 'main' into embedding-config
VladimirKadlec File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| """Embedding Manager - Generic embedding configuration, validation, and parameter provider.""" | ||
|
|
||
| from ..llm import validate_openai_env | ||
| from ..models import EmbeddingConfig, SystemConfig | ||
|
|
||
|
|
||
| class EmbeddingError(Exception): | ||
| """Embedding config errors.""" | ||
|
|
||
|
|
||
| class EmbeddingManager: # pylint: disable=too-few-public-methods | ||
| """Generic Embedding Manager.""" | ||
|
|
||
| def __init__(self, config: EmbeddingConfig): | ||
| """Initialize with validated environment and constructed model name.""" | ||
| self.config = config | ||
| self._validate_config() | ||
| print( | ||
| f""" | ||
| ✅ Embedding Manager: {self.config.provider} -- {self.config.model} {self.config.provider_kwargs}""" | ||
| ) | ||
|
|
||
| def _validate_config(self) -> None: | ||
| """Validate config and env variables.""" | ||
|
|
||
| def empty_check() -> None: | ||
| pass | ||
|
|
||
| env_validator = { | ||
| "openai": validate_openai_env, | ||
| # "google": _validate_gemini_env, # Google embeddings are not supported at the moment | ||
| "huggingface": empty_check, | ||
| }.get(self.config.provider) | ||
|
|
||
| if env_validator is None: | ||
| raise EmbeddingError( | ||
| f"Unsupported embedding provider {self.config.provider}" | ||
| ) | ||
|
|
||
| env_validator() | ||
|
|
||
| @classmethod | ||
| def from_system_config(cls, system_config: SystemConfig) -> "EmbeddingManager": | ||
| """Create Embedding Manager from system configuration.""" | ||
| return cls(system_config.embedding) |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| """Ragas Embedding Manager - Ragas specific embedding wrapper.""" | ||
|
|
||
| from langchain_huggingface import HuggingFaceEmbeddings | ||
| from langchain_openai import OpenAIEmbeddings | ||
| from ragas.embeddings import LangchainEmbeddingsWrapper | ||
|
|
||
| from ..embedding.manager import EmbeddingManager | ||
|
|
||
|
|
||
| class RagasEmbeddingManager: # pylint: disable=too-few-public-methods | ||
| """Ragas Embedding Manager, modifies global ragas settings.""" | ||
|
|
||
| def __init__(self, embedding_manager: EmbeddingManager): | ||
| """Init RagasEmbeddingManager.""" | ||
| config = embedding_manager.config | ||
| self.config = config | ||
|
|
||
| embedding_class = { | ||
| "openai": OpenAIEmbeddings, | ||
| "huggingface": HuggingFaceEmbeddings, | ||
| }.get(config.provider) | ||
| if not embedding_class: | ||
| raise RuntimeError(f"Unknown embedding provider {config.provider}") | ||
|
|
||
| kwargs = config.provider_kwargs | ||
| if kwargs is None: | ||
| kwargs = {} | ||
|
|
||
| self.embeddings = LangchainEmbeddingsWrapper( | ||
| embedding_class(model=config.model, **kwargs) | ||
| ) |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is just for reference, still I would suggest to keep an opensource model as default.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that choosing some huggingface model requires downloading and running it locally, IMHO a big change from the current setup.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fo default, I would personally prefer a free model..
with huggingface most model gets downloaded easily, if it is not present in the system.
For openai, team needs to have the access/key..
I know we have openai as default LLM, but majorly people use gemini, so they won't have embedding model.