Skip to content

Commit

Permalink
feat: Add spec and parser for ilab_model_list (#4311)
Browse files Browse the repository at this point in the history
Signed-off-by: jiazhang <[email protected]>

rh-pre-commit.version: 2.3.1
rh-pre-commit.check-secrets: ENABLED
  • Loading branch information
wushiqinlou authored Dec 20, 2024
1 parent 644d314 commit 1a2b3f7
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 0 deletions.
3 changes: 3 additions & 0 deletions docs/shared_parsers_catalog/ilab.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.. automodule:: insights.parsers.ilab
:members:
:show-inheritance:
60 changes: 60 additions & 0 deletions insights/parsers/ilab.py
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]
1 change: 1 addition & 0 deletions insights/specs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ class Specs(SpecSet):
ibm_lparcfg = RegistryPoint(filterable=True, no_obfuscate=['hostname', 'ip'])
ifcfg = RegistryPoint(multi_output=True)
ifcfg_static_route = RegistryPoint(filterable=True, multi_output=True)
ilab_model_list = RegistryPoint(no_obfuscate=['hostname', 'ip'])
imagemagick_policy = RegistryPoint(multi_output=True, filterable=True, no_obfuscate=['ip'])
image_builder_facts = RegistryPoint()
init_ora = RegistryPoint()
Expand Down
1 change: 1 addition & 0 deletions insights/specs/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,7 @@ class DefaultSpecs(Specs):
ibm_lparcfg = simple_file("/proc/powerpc/lparcfg")
ifcfg = glob_file("/etc/sysconfig/network-scripts/ifcfg-*")
ifcfg_static_route = glob_file("/etc/sysconfig/network-scripts/route-*")
ilab_model_list = simple_command("/usr/bin/ilab model list")
imagemagick_policy = glob_file(
["/etc/ImageMagick/policy.xml", "/usr/lib*/ImageMagick-6.5.4/config/policy.xml"]
)
Expand Down
64 changes: 64 additions & 0 deletions insights/tests/parsers/test_ilab.py
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

0 comments on commit 1a2b3f7

Please sign in to comment.