Skip to content
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

Added model factory #874

Merged
merged 3 commits into from
Feb 24, 2025
Merged

Added model factory #874

merged 3 commits into from
Feb 24, 2025

Conversation

engelmi
Copy link
Member

@engelmi engelmi commented Feb 24, 2025

This PR includes the following changes:

  • Moved creating model based on cli input to factory
  • Added unit tests for new model factory
  • Created an abstract base class for models

The factory encapsulates the model creation and enables writing unit tests more easily. A few unit tests have been added to verify the correct Model class has been created based on the input.
The abstract base class for models is intended to help implement new Model types (such as s3) - so its clear which methods a model needs to have and which might already be provided by the Model implementation.

Summary by Sourcery

Introduces a model factory to create model instances, and an abstract base class for models. Adds unit tests for the new model factory. Updates CI to run unit tests.

Enhancements:

  • Introduces a ModelFactory class to encapsulate the logic for creating model instances based on the provided model string and transport configuration.
  • Introduces an abstract ModelBase class to define the interface for model implementations, ensuring consistency across different model types.

CI:

  • Adds a new CI job to run unit tests on each pull request.

Tests:

  • Adds unit tests for the ModelFactory to verify the correct model class is instantiated based on the input.

Copy link
Contributor

sourcery-ai bot commented Feb 24, 2025

Reviewer's Guide by Sourcery

This PR introduces a ModelFactory to handle model creation, an abstract ModelBase class, and unit tests for the factory. The model creation logic was moved from the cli.py to the new ModelFactory class. The CI pipeline was updated to include unit tests.

Sequence diagram for Model creation using ModelFactory

sequenceDiagram
    participant CLI
    participant ModelFactory
    participant Huggingface
    participant Ollama
    participant OCI
    participant URL

    CLI->>ModelFactory: create(model, transport, engine)
    alt model is Huggingface
        ModelFactory->>Huggingface: Huggingface(model)
        ModelFactory-->>CLI: Huggingface
    else model is Ollama
        ModelFactory->>Ollama: Ollama(model)
        ModelFactory-->>CLI: Ollama
    else model is OCI
        ModelFactory->>OCI: OCI(model, engine)
        ModelFactory-->>CLI: OCI
    else model is URL
        ModelFactory->>URL: URL(model)
        ModelFactory-->>CLI: URL
    end
Loading

Updated class diagram for Model and ModelBase

classDiagram
    class ModelBase {
        <<abstract>>
        +login(args)
        +logout(args)
        +pull(args)
        +push(source, args)
        +remove(args)
        +bench(args)
        +run(args)
        +perplexity(args)
        +serve(args)
        +exists(args)
        +inspect(args)
    }
    class Model {
        -model: str
        -directory: str
        -filename: str
        +__init__(model)
        +is_symlink_to(file_path, target_path)
        +attempt_to_use_versioned(conman, image, vers, args)
        +_image(args)
    }

    Model --|> ModelBase : extends
Loading

Class diagram for ModelFactory

classDiagram
    class ModelFactory {
        -model: str
        -transport: str
        -engine: str
        +__init__(model: str, transport: str, engine: str)
        +create()
    }

    ModelFactory --|> Huggingface : creates
    ModelFactory --|> Ollama : creates
    ModelFactory --|> OCI : creates
    ModelFactory --|> URL : creates
Loading

File-Level Changes

Change Details Files
Introduced an abstract base class ModelBase for models, defining the interface for model operations.
  • Added ModelBase class with abstract methods for common model operations.
  • Implemented NotImplementedError exceptions for each abstract method in ModelBase.
ramalama/model.py
Refactored model creation logic into a ModelFactory class.
  • Created ModelFactory class to encapsulate model instantiation.
  • Moved conditional model instantiation logic from cli.py to ModelFactory.create().
  • The New function in cli.py now uses the ModelFactory to create model instances.
ramalama/cli.py
ramalama/model_factory.py
Added unit tests for the ModelFactory class.
  • Created test_model_factory.py with parameterized tests for ModelFactory.create().
  • Added test cases to verify the correct model class is instantiated based on different inputs.
test/unit/test_model_factory.py
Makefile
Updated the CI pipeline to include unit tests.
  • Added a new job to run unit tests in .github/workflows/ci.yml.
  • Modified the Makefile to include a unit-tests target that runs pytest.
.github/workflows/ci.yml
Makefile
Updated integration tests to be called integration-tests instead of tests.
  • Renamed test target to integration-tests in Makefile.
  • Updated github actions to call integration-tests instead of test.
Makefile
.github/workflows/latest.yml
.github/workflows/nightly.yml

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!
  • Generate a plan of action for an issue: Comment @sourcery-ai plan on
    an issue to generate a plan of action for it.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a 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 renaming integration-tests to end-to-end-tests to better reflect what they test.
  • The __not_implemented_error method in ModelBase could be a static method or defined outside the class since it doesn't use self.
Here's what I looked at during the review
  • 🟡 General issues: 1 issue found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@engelmi engelmi force-pushed the added-model-factory branch 7 times, most recently from 72d983a to 268fbf3 Compare February 24, 2025 08:51
@engelmi
Copy link
Member Author

engelmi commented Feb 24, 2025

Not sure why the help test for a bogus transport fails, expected and actual look the same to me:

 # #|     FAIL: Verify bogus transport throws error
# #| expected: 'Error: transport "e_t12-6btz32rk" not supported. Must be oci, huggingface, or ollama.'
# #|   actual: 'Error: Transport "e_t12-6btz32rk" not supported. Must be oci, huggingface, or ollama.'

Edit: Never mind, found it... kind of playing "finding waldo".

Signed-off-by: Michael Engel <mengel@redhat.com>
Signed-off-by: Michael Engel <mengel@redhat.com>
Signed-off-by: Michael Engel <mengel@redhat.com>
@engelmi engelmi force-pushed the added-model-factory branch from 268fbf3 to 996e6f5 Compare February 24, 2025 09:11
@rhatdan
Copy link
Member

rhatdan commented Feb 24, 2025

LGTM
Nice addition, thanks @engelmi

@rhatdan rhatdan merged commit e9c47dc into containers:main Feb 24, 2025
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants