-
Notifications
You must be signed in to change notification settings - Fork 124
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
Moved pruning protocol from model to factory #882
Merged
ericcurtin
merged 1 commit into
containers:main
from
engelmi:move-model-input-prune-to-factory
Feb 25, 2025
Merged
Changes from all commits
Commits
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 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
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
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,33 +1,69 @@ | ||
from typing import Union | ||
from urllib.parse import urlparse | ||
|
||
from ramalama.common import rm_until_substring | ||
from ramalama.huggingface import Huggingface | ||
from ramalama.model import MODEL_TYPES | ||
from ramalama.oci import OCI | ||
from ramalama.ollama import Ollama | ||
from ramalama.url import URL | ||
|
||
|
||
class ModelFactory: | ||
|
||
def __init__(self, model: str, transport: str = "ollama", engine: str = "podman"): | ||
def __init__(self, model: str, transport: str = "ollama", engine: str = "podman", ignore_stderr: bool = False): | ||
self.model = model | ||
self.transport = transport | ||
self.engine = engine | ||
self.ignore_stderr = ignore_stderr | ||
|
||
def prune_model_input(self, cls: type[Union[Huggingface, Ollama, OCI, URL]]) -> str: | ||
engelmi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# remove protocol from model input | ||
pruned_model_input = rm_until_substring(self.model, "://") | ||
|
||
if cls == Huggingface: | ||
pruned_model_input = rm_until_substring(pruned_model_input, "hf.co/") | ||
elif cls == Ollama: | ||
pruned_model_input = rm_until_substring(pruned_model_input, "ollama.com/library/") | ||
|
||
return pruned_model_input | ||
|
||
def validate_oci_model_input(self): | ||
if self.model.startswith("oci://") or self.model.startswith("docker://"): | ||
return | ||
|
||
for t in MODEL_TYPES: | ||
if self.model.startswith(t + "://"): | ||
raise ValueError(f"{self.model} invalid: Only OCI Model types supported") | ||
|
||
def create_huggingface(self) -> Huggingface: | ||
return Huggingface(self.prune_model_input(Huggingface)) | ||
|
||
def create_ollama(self) -> Ollama: | ||
return Ollama(self.prune_model_input(Ollama)) | ||
|
||
def create_oci(self) -> OCI: | ||
self.validate_oci_model_input() | ||
return OCI(self.prune_model_input(OCI), self.engine, self.ignore_stderr) | ||
|
||
def create_url(self) -> URL: | ||
return URL(self.prune_model_input(URL), urlparse(self.model).scheme) | ||
|
||
def create(self) -> Union[Huggingface, Ollama, OCI, URL]: | ||
if self.model.startswith("huggingface://") or self.model.startswith("hf://") or self.model.startswith("hf.co/"): | ||
return Huggingface(self.model) | ||
return self.create_huggingface() | ||
if self.model.startswith("ollama://") or "ollama.com/library/" in self.model: | ||
return Ollama(self.model) | ||
return self.create_ollama() | ||
if self.model.startswith("oci://") or self.model.startswith("docker://"): | ||
return OCI(self.model, self.engine) | ||
return self.create_oci() | ||
if self.model.startswith("http://") or self.model.startswith("https://") or self.model.startswith("file://"): | ||
return URL(self.model) | ||
return self.create_url() | ||
|
||
if self.transport == "huggingface": | ||
return Huggingface(self.model) | ||
return self.create_huggingface() | ||
if self.transport == "ollama": | ||
return Ollama(self.model) | ||
return self.create_ollama() | ||
if self.transport == "oci": | ||
return OCI(self.model, self.engine) | ||
return self.create_oci() | ||
|
||
raise KeyError(f'transport "{self.transport}" not supported. Must be oci, huggingface, or ollama.') |
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
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import pytest | ||
|
||
from ramalama.common import rm_until_substring | ||
|
||
@pytest.mark.parametrize( | ||
"input,rm_until,expected", | ||
[ | ||
("", "", ""), | ||
("huggingface://granite-code", "://", "granite-code"), | ||
("hf://granite-code", "://", "granite-code"), | ||
("hf.co/granite-code", "hf.co/", "granite-code"), | ||
("http://huggingface.co/ibm-granite/granite-3b-code-base-2k-GGUF/blob/main/granite-3b-code-base.Q4_K_M.gguf", ".co/", "ibm-granite/granite-3b-code-base-2k-GGUF/blob/main/granite-3b-code-base.Q4_K_M.gguf"), | ||
("file:///tmp/models/granite-3b-code-base.Q4_K_M.gguf", "", "file:///tmp/models/granite-3b-code-base.Q4_K_M.gguf"), | ||
] | ||
) | ||
def test_rm_until_substring(input: str, rm_until: str, expected: str): | ||
actual = rm_until_substring(input, rm_until) | ||
assert actual == expected |
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.
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.
It's probably not for this PR, but it would be better if we resolved all environment variables, configuration files, CLI args, once early in execution of ramalama. I started that here at one point, but it drifted:
load_and_merge_config
we tend to do things like, use the "engine" value or container_manager() over and over again in this codebase which isn't great.
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.
I agree and good to know. I'll keep that in mind and will try to refactor occurrences like this.
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.
config
here is already the result ofload_and_merge_config
where we do exactly the same thing. Maybe a dataclass or something similar might help. Although its less flexible, the fields are stated explicitly and don't encourage specifying defaults when the field isn't there.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.
Yeah we should probably be doing something like:
here. The code is misleading, it suggests we might not have resolved this value correctly up to this point.