Added mapping of https to specific transport#2117
Conversation
Reviewer's GuideThis PR enhances the CLI argument setup by introducing automatic mapping of HTTPS model URLs to the appropriate transport schemes (hf:// or ollama://) while preserving direct file references, tracking the original and unresolved model inputs, and extending shortname resolution to both single and list inputs. Supporting unit tests were added to validate the end-to-end parsing behavior. File-Level Changes
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Summary of ChangesHello @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 introduces a significant quality-of-life improvement for Highlights
Using Gemini Code AssistThe 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 by creating a comment using either
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 Limitations & Feedback Gemini Code Assist 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. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. 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
|
There was a problem hiding this comment.
Hey there - 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/cli.py:331-340` </location>
<code_context>
+ return None
+
+ if getattr(args, "MODEL", None):
+ if isinstance(args.MODEL, str):
+ args.INITIAL_MODEL = args.MODEL
+ translated = map_https_to_transport(args.MODEL)
+ if translated:
+ args.MODEL = translated
+ if isinstance(args.MODEL, list):
+ args.INITIAL_MODEL = [m for m in args.MODEL]
+ for i in range(len(args.MODEL)):
+ translated = map_https_to_transport(args.MODEL[i])
+ if translated:
+ args.MODEL[i] = translated
+
+ # Resolve shortnames of model input
if getattr(args, "MODEL", None):
- if args.subcommand != "rm":
+ if isinstance(args.MODEL, str):
+ args.UNRESOLVED_MODEL = args.MODEL
resolved_model = shortnames.resolve(args.MODEL)
if resolved_model:
</code_context>
<issue_to_address>
**suggestion:** Duplicated logic for str and list types could be refactored.
Refactor the repeated str and list handling for MODEL into a shared helper to simplify both transport mapping and shortname resolution.
</issue_to_address>
### Comment 2
<location> `test/unit/test_cli.py:181-190` </location>
<code_context>
+@pytest.mark.parametrize(
</code_context>
<issue_to_address>
**suggestion (testing):** Consider adding tests for list-type MODEL inputs and error conditions.
Add test cases for list-type MODEL inputs to verify correct handling, and include tests for error conditions like malformed URLs, unsupported domains, and missing MODEL attributes.
Suggested implementation:
```python
@pytest.mark.parametrize(
"input_args, expected_initial, expected_unresolved, expected_resolved",
[
# Single MODEL string
(
Namespace(MODEL="https://huggingface.co/TinyLlama/TinyLlama-1.1B-Chat-v0.6/blob/main/ggml-model-q4_0.gguf"),
"https://huggingface.co/TinyLlama/TinyLlama-1.1B-Chat-v0.6/blob/main/ggml-model-q4_0.gguf",
"https://huggingface.co/TinyLlama/TinyLlama-1.1B-Chat-v0.6/blob/main/ggml-model-q4_0.gguf",
"https://huggingface.co/TinyLlama/TinyLlama-1.1B-Chat-v0.6/blob/main/ggml-model-q4_0.gguf",
),
# List-type MODEL input
(
Namespace(MODEL=[
"https://huggingface.co/TinyLlama/TinyLlama-1.1B-Chat-v0.6/blob/main/ggml-model-q4_0.gguf",
"https://hf.co/mlx-community/Kimi-Linear-48B-A3B-Instruct-4bit"
]),
[
"https://huggingface.co/TinyLlama/TinyLlama-1.1B-Chat-v0.6/blob/main/ggml-model-q4_0.gguf",
"https://hf.co/mlx-community/Kimi-Linear-48B-A3B-Instruct-4bit"
],
[
"https://huggingface.co/TinyLlama/TinyLlama-1.1B-Chat-v0.6/blob/main/ggml-model-q4_0.gguf",
"https://hf.co/mlx-community/Kimi-Linear-48B-A3B-Instruct-4bit"
],
[
"https://huggingface.co/TinyLlama/TinyLlama-1.1B-Chat-v0.6/blob/main/ggml-model-q4_0.gguf",
"https://hf.co/mlx-community/Kimi-Linear-48B-A3B-Instruct-4bit"
],
),
# Malformed URL
(
Namespace(MODEL="not_a_url"),
"not_a_url",
"not_a_url",
pytest.raises(ValueError), # Expecting error handling
),
# Unsupported domain
(
Namespace(MODEL="https://unsupported-domain.com/model.gguf"),
"https://unsupported-domain.com/model.gguf",
"https://unsupported-domain.com/model.gguf",
pytest.raises(NoGGUFModelFileFound), # Expecting error handling
),
# Missing MODEL attribute
(
Namespace(),
None,
None,
pytest.raises(AttributeError), # Expecting error handling
),
(
Namespace(MODEL="https://hf.co/mlx-community/Kimi-Linear-48B-A3B-Instruct-4bit"),
```
You may need to adjust the test function to handle cases where `pytest.raises` is used as the expected output, i.e., wrap the call in a `with` block for those cases. Also, ensure that your code under test can handle list-type MODEL inputs and error conditions as expected.
</issue_to_address>
### Comment 3
<location> `test/unit/test_cli.py:202-204` </location>
<code_context>
+ "ollama://library/smollm:135m",
+ "ollama://library/smollm:135m",
+ ),
+ (Namespace(MODEL="tinyllama"), "tinyllama", "tinyllama", "hf://TheBloke/TinyLlama-1.1B-Chat-v1.0-GGUF"),
+ ],
+)
+def test_post_parse_setup_model_input(
+ input_args: Namespace, expected_initial: str, expected_unresolved: str, expected_resolved: str
+):
+ input_args.debug = False
+ post_parse_setup(input_args)
+
+ assert hasattr(input_args, "INITIAL_MODEL"), "parsed arguments should always have INITIAL_MODEL field"
+ assert hasattr(input_args, "UNRESOLVED_MODEL"), "parsed arguments should always have RESOLVED_MODEL field"
+
+ assert input_args.INITIAL_MODEL == expected_initial
+ assert input_args.UNRESOLVED_MODEL == expected_unresolved
+ assert input_args.MODEL == expected_resolved
+ assert input_args.model == input_args.MODEL
</code_context>
<issue_to_address>
**suggestion (testing):** Consider adding a test for unsupported or unknown shortnames.
Please add a test case with an unknown shortname to confirm the code's behavior in this scenario, such as leaving MODEL unchanged or raising an error.
```suggestion
(Namespace(MODEL="tinyllama"), "tinyllama", "tinyllama", "hf://TheBloke/TinyLlama-1.1B-Chat-v1.0-GGUF"),
# Test for unknown/unsupported shortname
(Namespace(MODEL="unknownmodel"), "unknownmodel", "unknownmodel", "unknownmodel"),
],
)
```
</issue_to_address>
### Comment 4
<location> `test/unit/test_cli.py:217` </location>
<code_context>
+ (Namespace(MODEL="https://huggingface.co/TinyLlama/TinyLlama-1.1B-Chat-v0.6/blob/main/ggml-model-q4_0.gguf"),
</code_context>
<issue_to_address>
**suggestion (testing):** Consider testing URLs with http (not https) and with additional path segments.
Please add tests for http URLs and for cases with varying numbers of path segments to fully cover the mapping logic.
</issue_to_address>
### Comment 5
<location> `ramalama/cli.py:314` </location>
<code_context>
def post_parse_setup(args):
"""Perform additional setup after parsing arguments."""
+
</code_context>
<issue_to_address>
**issue (complexity):** Consider refactoring the model transformation logic to normalize input and apply both mapping and resolution in a single pass.
Consider normalizing `args.MODEL` to a list once, then running both transforms in one pass. For example:
```python
def _normalize_to_list(x):
return x if isinstance(x, list) else [x]
def _apply_transforms(models: list[str]) -> list[str]:
# 1) map https:// → hf:/ollama:
# 2) resolve shortnames
out = []
for m in models:
m2 = map_https_to_transport(m) or m
out.append(shortnames.resolve(m2) or m2)
return out
def post_parse_setup(args):
# ...
if not getattr(args, "MODEL", None):
return
raw = args.MODEL
models = _normalize_to_list(raw)
args.UNRESOLVED_MODEL = models.copy()
transformed = _apply_transforms(models)
# restore original shape
args.MODEL = transformed if isinstance(raw, list) else transformed[0]
if not hasattr(args, "model"):
args.model = args.MODEL
# ...
```
Benefits:
- removes duplicate str/list branches
- flattens nesting
- keeps `INITIAL_MODEL` or other tracking trivial if needed (`args.INITIAL_MODEL = raw` or `.copy()`)
</issue_to_address>
### Comment 6
<location> `ramalama/cli.py:337` </location>
<code_context>
def post_parse_setup(args):
"""Perform additional setup after parsing arguments."""
# Resolve https:// model inputs to ollama or huggingface based on url domain
def map_https_to_transport(input: str) -> str | None:
if input.startswith("https://") or input.startswith("http://"):
url = urlparse(input)
# detect if the whole repo is defined or a specific file
if url.path.count("/") != 2:
return None
if url.hostname in ["hf.co", "huggingface.co"]:
return f"hf:/{url.path}"
elif url.hostname in ["ollama.com"]:
return f"ollama:/{url.path}"
return None
if getattr(args, "MODEL", None):
if isinstance(args.MODEL, str):
args.INITIAL_MODEL = args.MODEL
translated = map_https_to_transport(args.MODEL)
if translated:
args.MODEL = translated
if isinstance(args.MODEL, list):
args.INITIAL_MODEL = [m for m in args.MODEL]
for i in range(len(args.MODEL)):
translated = map_https_to_transport(args.MODEL[i])
if translated:
args.MODEL[i] = translated
# Resolve shortnames of model input
if getattr(args, "MODEL", None):
if isinstance(args.MODEL, str):
args.UNRESOLVED_MODEL = args.MODEL
resolved_model = shortnames.resolve(args.MODEL)
if resolved_model:
args.MODEL = resolved_model
if isinstance(args.MODEL, list):
args.UNRESOLVED_MODEL = [m for m in args.MODEL]
for i in range(len(args.MODEL)):
resolved_model = shortnames.resolve(args.MODEL[i])
if resolved_model:
args.MODEL[i] = resolved_model
# TODO: This is a hack. Once we have more typing in place these special cases
# _should_ be removed.
if not hasattr(args, "model"):
args.model = args.MODEL
# Validate that --add-to-unit is only used with --generate and its format is <section>:<key>:<value>
if hasattr(args, "add_to_unit") and (add_to_units := args.add_to_unit):
if getattr(args, "generate", None) is None:
parser = get_parser()
parser.error("--add-to-unit can only be used with --generate")
if not (all(len([value for value in unit_to_add.split(":", 2) if value]) == 3 for unit_to_add in add_to_units)):
parser = get_parser()
parser.error("--add-to-unit parameters must be of the form <section>:<key>:<value>")
if hasattr(args, "runtime_args"):
args.runtime_args = shlex.split(args.runtime_args)
# MLX runtime automatically requires --nocontainer
if getattr(args, "runtime", None) == "mlx":
if getattr(args, "container", None) is True:
logger.info("MLX runtime automatically uses --nocontainer mode")
args.container = False
if hasattr(args, 'pull'):
args.pull = normalize_pull_arg(args.pull, getattr(args, 'engine', None))
configure_logger("DEBUG" if args.debug else "WARNING")
</code_context>
<issue_to_address>
**issue (code-quality):** We've found these issues:
- Replace identity comprehension with call to collection constructor [×2] ([`identity-comprehension`](https://docs.sourcery.ai/Reference/Default-Rules/refactorings/identity-comprehension/))
- Invert any/all to simplify comparisons ([`invert-any-all`](https://docs.sourcery.ai/Reference/Default-Rules/refactorings/invert-any-all/))
<br/><details><summary>Explanation</summary>Convert list/set/tuple comprehensions that do not change the input elements into.
#### Before
```python
# List comprehensions
[item for item in coll]
[item for item in friends.names()]
# Dict comprehensions
{k: v for k, v in coll}
{k: v for k, v in coll.items()} # Only if we know coll is a `dict`
# Unneeded call to `.items()`
dict(coll.items()) # Only if we know coll is a `dict`
# Set comprehensions
{item for item in coll}
```
#### After
```python
# List comprehensions
list(iter(coll))
list(iter(friends.names()))
# Dict comprehensions
dict(coll)
dict(coll)
# Unneeded call to `.items()`
dict(coll)
# Set comprehensions
set(coll)
```
All these comprehensions are just creating a copy of the original collection.
They can all be simplified by simply constructing a new collection directly. The
resulting code is easier to read and shows the intent more clearly.
Convert list/set/tuple comprehensions that do not change the input elements into.
#### Before
```python
# List comprehensions
[item for item in coll]
[item for item in friends.names()]
# Dict comprehensions
{k: v for k, v in coll}
{k: v for k, v in coll.items()} # Only if we know coll is a `dict`
# Unneeded call to `.items()`
dict(coll.items()) # Only if we know coll is a `dict`
# Set comprehensions
{item for item in coll}
```
#### After
```python
# List comprehensions
list(iter(coll))
list(iter(friends.names()))
# Dict comprehensions
dict(coll)
dict(coll)
# Unneeded call to `.items()`
dict(coll)
# Set comprehensions
set(coll)
```
All these comprehensions are just creating a copy of the original collection.
They can all be simplified by simply constructing a new collection directly. The
resulting code is easier to read and shows the intent more clearly.
</details>
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
There was a problem hiding this comment.
Code Review
The pull request successfully introduces the functionality to map HTTPS URLs from Hugging Face and Ollama to their respective internal transport schemes, which is a valuable enhancement for user convenience. The new unit tests effectively validate this new behavior across various scenarios. My main feedback focuses on improving the maintainability of the post_parse_setup function by refactoring duplicated logic for model processing.
e5dcf32 to
992afa0
Compare
Relates to: containers#2104 In order to enable simply copying the huggingface and ollama URL from the website and pull the repository, the scheme is detected and then the prefix replaced by one of the specific transports. This also detects if a specific file, e.g. .gguf, should be pulled and doesn't perform the mapping in that case. Signed-off-by: Michael Engel <mengel@redhat.com>
Signed-off-by: Michael Engel <mengel@redhat.com>
b1509bf to
3135c70
Compare
|
LGTM |
Relates to: #2104
In order to enable simply copying the huggingface and ollama URL from the website and pull the repository, the scheme is detected and then the prefix replaced by one of the specific transports. This also detects if a specific file, e.g. .gguf, should be pulled and doesn't perform the mapping in that case.
Example usage can be seen in the unit tests.
In essence, this enables to go to the huggingface and/or ollama website and simply copy the URL from the browser, e.g. https://hf.co/mlx-community/Kimi-Linear-48B-A3B-Instruct-4bit, and have all files in the repository being pulled instead of the .html file.
Note: This requires the hf cli at the moment for hf repos.
Summary by Sourcery
Add transport mapping for HTTPS model URLs and enhance CLI argument post-parsing to preserve and resolve model inputs consistently for both single and list cases.
New Features:
Enhancements:
Tests: