Skip to content

Split the model store into multiple files #1640

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 2 commits into from
Jul 1, 2025

Conversation

engelmi
Copy link
Member

@engelmi engelmi commented Jul 1, 2025

The source code for the model store is getting bigger, so splitting it into multiple source files under a directory helps keeping it easier to read.

Summary by Sourcery

Split the monolithic model store into multiple focused modules and update references accordingly

Enhancements:

  • Extract SnapshotFile, LocalSnapshotFile, and validate_snapshot_files into ramalama/model_store/snapshot_file.py
  • Move RefFile logic to ramalama/model_store/reffile.py
  • Move GlobalModelStore and ModelFile dataclass to ramalama/model_store/global_store.py
  • Define store directory constants in ramalama/model_store/constants.py
  • Relocate top-level store logic to ramalama/model_store/store.py
  • Update import paths throughout the codebase and tests to use the new module structure

The source code for the model store is getting bigger, so splitting it
into multiple source files under a directory helps keeping it easier
to read.

Signed-off-by: Michael Engel <[email protected]>
Copy link
Contributor

sourcery-ai bot commented Jul 1, 2025

Reviewer's Guide

Restructure the monolithic model_store.py into a modular package under ramalama/model_store, distributing core classes and constants into dedicated files and updating import paths accordingly.

Class diagram for the new model store package structure

classDiagram
    class ModelStore
    class GlobalModelStore
    class ModelFile
    class RefFile
    class SnapshotFile
    class LocalSnapshotFile
    class SnapshotFileType

    ModelStore --> GlobalModelStore : uses
    GlobalModelStore o-- ModelFile : contains
    GlobalModelStore o-- RefFile : uses
    RefFile <|-- RefFile : from_path (factory)
    SnapshotFile <|-- LocalSnapshotFile
    SnapshotFile --> SnapshotFileType : uses
    LocalSnapshotFile --> SnapshotFileType : uses
    GlobalModelStore --> SnapshotFileType : uses

    %% File locations
    class GlobalModelStore {
      +__init__(base_path)
      +list_models(engine, show_container)
      +verify_snapshot()
      +cleanup()
      +path
    }
    class ModelFile {
      +name: str
      +modified: float
      +size: int
      +is_partial: bool
    }
    class RefFile {
      +hash: str
      +filenames: list[str]
      +model_name: str
      +chat_template_name: str
      +mmproj_name: str
      +path
      +from_path(path)
      +remove_file(name)
      +serialize()
      +write_to_file()
    }
    class SnapshotFile {
      +url: str
      +header: Dict
      +hash: str
      +name: str
      +type: SnapshotFileType
      +should_show_progress: bool
      +should_verify_checksum: bool
      +required: bool
      +download(blob_file_path, snapshot_dir)
    }
    class LocalSnapshotFile {
      +content: str
      +download(blob_file_path, snapshot_dir)
    }
    class SnapshotFileType {
      <<enum>>
      Model
      ChatTemplate
      Other
      Mmproj
    }
Loading

File-Level Changes

Change Details Files
Modularized model store implementation
  • Extracted SnapshotFileType, SnapshotFile, LocalSnapshotFile, and validate_snapshot_files into snapshot_file.py
  • Moved GlobalModelStore class and ModelFile dataclass into global_store.py
  • Relocated RefFile class and serialization logic into reffile.py
  • Centralized DIRECTORY_NAME_BLOBS/REFS/SNAPSHOTS constants in constants.py
  • Renamed original model_store.py to store.py and removed the now-extracted definitions
ramalama/model_store/store.py
ramalama/model_store/snapshot_file.py
ramalama/model_store/global_store.py
ramalama/model_store/reffile.py
ramalama/model_store/constants.py
Updated import paths to match new module layout
  • Adjusted imports in hf_style_repo_base.py, huggingface.py, modelscope.py, and test_model_store.py to reference snapshot_file.py
  • Updated store.py imports for go2jinja, GlobalModelStore, RefFile, and constants from new submodules
  • Removed obsolete imports (dataclasses, enum, typing, etc.) from the refactored store.py
ramalama/hf_style_repo_base.py
ramalama/huggingface.py
ramalama/modelscope.py
test/unit/test_model_store.py
ramalama/model_store/store.py

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!

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

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary of Changes

Hello @engelmi, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly improves the maintainability and readability of the model_store component by breaking down a large, single file into a well-organized directory structure with dedicated modules for different functionalities. This refactoring makes the codebase easier to navigate and understand for current and future developers.

Highlights

  • Code Organization: The monolithic ramalama/model_store.py file has been refactored and split into a new ramalama/model_store/ directory containing several specialized modules.
  • New Modules: Introduced constants.py for shared directory names, global_store.py for the GlobalModelStore class, reffile.py for the RefFile class, and snapshot_file.py for SnapshotFile related classes and functions.
  • Import Path Updates: Updated import statements across various files (cli.py, hf_style_repo_base.py, huggingface.py, model.py, modelscope.py, ollama.py, url.py, and unit tests) to reflect the new modular structure of the model_store.
  • Core ModelStore: The main ModelStore class now resides in ramalama/model_store/store.py and imports its dependencies from the newly created, more granular modules.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

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 and they look great!

Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments

### Comment 1
<location> `ramalama/model_store/global_store.py:49` </location>
<code_context>
+                    for snapshot_file in ref_file.filenames:
</code_context>

<issue_to_address>
Possible silent skipping of missing files.

Consider adding a warning or error log when both files are missing to aid debugging and clarify incomplete models.
</issue_to_address>

### Comment 2
<location> `ramalama/model_store/reffile.py:19` </location>
<code_context>
+    def path(self) -> str:
+        return self._path
+
+    def from_path(path: str) -> "RefFile":
+        ref_file = RefFile()
+        ref_file._path = path
</code_context>

<issue_to_address>
from_path should be a staticmethod or classmethod.

Since from_path doesn't use self and creates a new instance, it should be marked as @staticmethod or @classmethod for clarity.
</issue_to_address>

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.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

The model store has been refactored into multiple files, which improves code maintainability. The changes include moving snapshot file logic, ref file logic, global store logic, store directory constants, and top-level store logic into separate modules. Import paths have been updated to reflect the new module structure.

Signed-off-by: Michael Engel <[email protected]>
@rhatdan
Copy link
Member

rhatdan commented Jul 1, 2025

LGTM

@rhatdan rhatdan merged commit fe756cc into containers:main Jul 1, 2025
23 of 25 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