-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add spec and parser for ilab_model_list (#4311)
Signed-off-by: jiazhang <[email protected]> rh-pre-commit.version: 2.3.1 rh-pre-commit.check-secrets: ENABLED
- Loading branch information
1 parent
644d314
commit 1a2b3f7
Showing
5 changed files
with
129 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.. automodule:: insights.parsers.ilab | ||
:members: | ||
:show-inheritance: |
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,60 @@ | ||
""" | ||
Ilab parsers | ||
============ | ||
Parsers provided by this module include: | ||
IlabModuleList - command ``/usr/bin/ilab model list`` | ||
----------------------------------------------------- | ||
""" | ||
from insights.core import CommandParser | ||
from insights.core.plugins import parser | ||
from insights.core.exceptions import SkipComponent, ParseException | ||
from insights.specs import Specs | ||
|
||
|
||
@parser(Specs.ilab_model_list) | ||
class IlabModuleList(CommandParser, list): | ||
""" | ||
This parser will parse the output of "/usr/bin/ilab model list". | ||
Sample output from ``/usr/bin/ilab model list``:: | ||
+-----------------------------------+---------------------+---------+ | ||
| Model Name | Last Modified | Size | | ||
+-----------------------------------+---------------------+---------+ | ||
| models/prometheus-8x7b-v2-0 | 2024-08-09 13:28:50 | 87.0 GB| | ||
| models/mixtral-8x7b-instruct-v0-1 | 2024-08-09 13:28:24 | 87.0 GB| | ||
| models/granite-7b-redhat-lab | 2024-08-09 14:28:40 | 12.6 GB| | ||
| models/granite-7b-starter | 2024-08-09 14:40:35 | 12.6 GB| | ||
+-----------------------------------+---------------------+---------+ | ||
Examples: | ||
>>> type(ilab_model_list_info) | ||
<class 'insights.parsers.ilab.IlabModuleList'> | ||
>>> ilab_model_list_info[0]["model_name"] | ||
'models/prometheus-8x7b-v2-0' | ||
Attributes: | ||
models (list): list of model names | ||
""" | ||
def parse_content(self, content): | ||
if not content: | ||
raise SkipComponent("Empty") | ||
if not content[0].startswith("+-"): | ||
raise ParseException("Unexpected format: %s" % content[0]) | ||
for line in content: | ||
if line.startswith("|") and "Model Name" not in line: | ||
split_items = line.split("|") | ||
if len(split_items) == 5: | ||
self.append( | ||
{ | ||
"model_name": split_items[1].strip(), | ||
"last_modified": split_items[2].strip(), | ||
"size": split_items[3].strip(), | ||
} | ||
) | ||
else: | ||
raise ParseException("Unexpected format: %s" % line) | ||
self.models = [m['model_name'] for m in self] |
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,64 @@ | ||
import doctest | ||
import pytest | ||
|
||
from insights.core.exceptions import SkipComponent, ParseException | ||
from insights.parsers import ilab | ||
from insights.parsers.ilab import IlabModuleList | ||
from insights.tests import context_wrap | ||
|
||
ILAB_MODULE_LIST = """ | ||
+-----------------------------------+---------------------+---------+ | ||
| Model Name | Last Modified | Size | | ||
+-----------------------------------+---------------------+---------+ | ||
| models/prometheus-8x7b-v2-0 | 2024-08-09 13:28:50 | 87.0 GB| | ||
| models/mixtral-8x7b-instruct-v0-1 | 2024-08-09 13:28:24 | 87.0 GB| | ||
| models/granite-7b-redhat-lab | 2024-08-09 14:28:40 | 12.6 GB| | ||
| models/granite-7b-starter | 2024-08-09 14:40:35 | 12.6 GB| | ||
+-----------------------------------+---------------------+---------+ | ||
""".strip() | ||
|
||
ILAB_MODULE_LIST_ISSUE_FORMAT = """ | ||
+-----------------------------------+---------------------+ | ||
| Model Name | Last Modified | | ||
+-----------------------------------+---------------------+ | ||
| models/mixtral-8x7b-instruct-v0-1 | 2024-08-09 13:28:24 | | ||
| models/granite-7b-redhat-lab | 2024-08-09 14:28:40 | | ||
| models/granite-7b-starter | 2024-08-09 14:40:35 | | ||
+-----------------------------------+---------------------+ | ||
""".strip() | ||
|
||
ILAB_MODULE_LIST_ERROR = """ | ||
Usage: ilab model [OPTIONS] COMMAND [ARGS]... | ||
Try 'ilab model --help' for help. | ||
Error: `/root/.config/instructlab/config.yaml` does not exist or is not a readable file. | ||
Please run `ilab config init` or point to a valid configuration file using `--config=<path>`. | ||
""".strip() | ||
|
||
ILAB_MODULE_LIST_EMPTY = "" | ||
|
||
|
||
def test_ilab_model_list(): | ||
ilab_module_list_info = IlabModuleList(context_wrap(ILAB_MODULE_LIST)) | ||
assert len(ilab_module_list_info) == 4 | ||
assert ilab_module_list_info[0]["model_name"] == "models/prometheus-8x7b-v2-0" | ||
assert ilab_module_list_info[1]["last_modified"] == "2024-08-09 13:28:24" | ||
assert ilab_module_list_info[2]["size"] == "12.6 GB" | ||
assert ilab_module_list_info.models == ["models/prometheus-8x7b-v2-0", "models/mixtral-8x7b-instruct-v0-1", "models/granite-7b-redhat-lab", "models/granite-7b-starter"] | ||
|
||
with pytest.raises(SkipComponent): | ||
IlabModuleList(context_wrap(ILAB_MODULE_LIST_EMPTY)) | ||
|
||
with pytest.raises(ParseException): | ||
IlabModuleList(context_wrap(ILAB_MODULE_LIST_ERROR)) | ||
|
||
with pytest.raises(ParseException): | ||
IlabModuleList(context_wrap(ILAB_MODULE_LIST_ISSUE_FORMAT)) | ||
|
||
|
||
def test_ilab_doc_examples(): | ||
env = { | ||
'ilab_model_list_info': IlabModuleList(context_wrap(ILAB_MODULE_LIST)) | ||
} | ||
failed, total = doctest.testmod(ilab, globs=env) | ||
assert failed == 0 |