-
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
Moved pruning protocol from model to factory #882
Conversation
Reviewer's Guide by SourceryThis pull request refactors the model input parsing logic by moving the protocol pruning functionality from the model classes to the Sequence diagram for Model Creation with Protocol PruningsequenceDiagram
participant Client
participant ModelFactory
participant Huggingface
participant Ollama
participant OCI
participant URL
Client->>ModelFactory: create(model, transport, engine)
ModelFactory->>ModelFactory: prune_model_input(model, cls)
alt model.startswith("huggingface://") or model.startswith("hf://") or model.startswith("hf.co/")
ModelFactory->>Huggingface: Huggingface(pruned_model)
ModelFactory-->>Client: Huggingface
else model.startswith("ollama://") or "ollama.com/library/" in model
ModelFactory->>Ollama: Ollama(pruned_model)
ModelFactory-->>Client: Ollama
else model.startswith("oci://") or model.startswith("docker://")
ModelFactory->>ModelFactory: validate_oci_model_input()
ModelFactory->>OCI: OCI(pruned_model, engine)
ModelFactory-->>Client: OCI
else model.startswith("http://") or model.startswith("https://") or model.startswith("file://")
ModelFactory->>URL: URL(pruned_model)
ModelFactory-->>Client: URL
else transport == "huggingface"
ModelFactory->>Huggingface: Huggingface(pruned_model)
ModelFactory-->>Client: Huggingface
else transport == "ollama"
ModelFactory->>Ollama: Ollama(pruned_model)
ModelFactory-->>Client: Ollama
else transport == "oci"
ModelFactory->>ModelFactory: validate_oci_model_input()
ModelFactory->>OCI: OCI(pruned_model, engine)
ModelFactory-->>Client: OCI
end
Updated class diagram for ModelFactoryclassDiagram
class ModelFactory {
- model: str
- transport: str
- engine: str
+ __init__(model: str, transport: str, engine: str)
+ create(): Union[Huggingface, Ollama, OCI, URL]
+ prune_model_input(cls: type[Union[Huggingface, Ollama, OCI, URL]]) : str
+ validate_oci_model_input()
}
class Huggingface {
}
class Ollama {
}
class OCI {
}
class URL {
}
ModelFactory --|> Huggingface : creates
ModelFactory --|> Ollama : creates
ModelFactory --|> OCI : creates
ModelFactory --|> URL : creates
note for ModelFactory "The ModelFactory class now includes a prune_model_input method to remove the protocol from the model input string."
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
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.
Hey @engelmi - I've reviewed your changes - here's some feedback:
Overall Comments:
- Consider adding a helper function to construct the
Input
dataclass in your tests to reduce duplication. - The
validate_oci_model_input
method could be a standalone function instead of a method on theModelFactory
class.
Here's what I looked at during the review
- 🟢 General issues: all looks good
- 🟢 Security: all looks good
- 🟢 Testing: all looks good
- 🟡 Complexity: 1 issue found
- 🟢 Documentation: all looks good
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
8a3437a
to
2778ff6
Compare
The [07:52:37.127614592] $ /home/runner/work/ramalama/ramalama/bin/ramalama convert /tmp/ramalama_bats.yyeHxU/aimodel foobar
# [07:52:37.962614926] Error: invalid repository name (1b572995d33b4edde32e4b0112342f32bbd43dcbe5df317d452d962e3b15c691), cannot specify 64-byte hexadecimal strings
# Failed to create manifest for OCI foobar : Command '['podman', 'manifest', 'create', 'foobar', '1b572995d33b4edde32e4b0112342f32bbd43dcbe5df317d452d962e3b15c691']' returned non-zero exit status 125.
# Converting /tmp/ramalama_bats.yyeHxU/aimodel to foobar...
# Building foobar... @ericcurtin WDYT? Update: |
2778ff6
to
2840eb8
Compare
By moving the pruning of the protocol from the model input to the model_factory and encapsulating it in a dedicated function, unit tests can be written more easily. Signed-off-by: Michael Engel <mengel@redhat.com>
2840eb8
to
fc75d9f
Compare
@@ -775,7 +774,7 @@ def push_cli(args): | |||
raise e | |||
try: | |||
# attempt to push as a container image | |||
m = OCI(tgt, config.get('engine', container_manager())) | |||
m = ModelFactory(tgt, engine=config.get('engine', container_manager())).create_oci() |
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 of load_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:
config['engine']
here. The code is misleading, it suggests we might not have resolved this value correctly up to this point.
By moving the pruning of the protocol from the model input to the model_factory and encapsulating it in a dedicated function, unit tests can be written more easily.
Summary by Sourcery
Moves the protocol parsing logic from the model classes to the model factory to improve testability and code organization. It introduces a
prune_model_input
method in theModelFactory
class to remove the protocol from the model input string. It also adds avalidate_oci_model_input
method to validate OCI model inputs. Additionally, it includes new unit tests to validate the new logic.Enhancements:
Tests: