Elffuss BraidHub

kikocis/llama-stack public

bb9be718c121 · 4.0 KiBRawPermalink
1# Copyright (c) Meta Platforms, Inc. and affiliates.
2# All rights reserved.
3#
4# This source code is licensed under the terms described in the LICENSE file in
5# the root directory of this source tree.
6
7import argparse
8import subprocess
9import textwrap
10from io import StringIO
11
12from llama_models.datatypes import CoreModelId, is_multimodal, model_family, ModelFamily
13
14from llama_stack.cli.subcommand import Subcommand
15
16
17class ModelPromptFormat(Subcommand):
18 """Llama model cli for describe a model prompt format (message formats)"""
19
20 def __init__(self, subparsers: argparse._SubParsersAction):
21 super().__init__()
22 self.parser = subparsers.add_parser(
23 "prompt-format",
24 prog="llama model prompt-format",
25 description="Show llama model message formats",
26 epilog=textwrap.dedent(
27 """
28 Example:
29 llama model prompt-format <options>
30 """
31 ),
32 formatter_class=argparse.RawTextHelpFormatter,
33 )
34 self._add_arguments()
35 self.parser.set_defaults(func=self._run_model_template_cmd)
36
37 def _add_arguments(self):
38 self.parser.add_argument(
39 "-m",
40 "--model-name",
41 type=str,
42 default="llama3_1",
43 help="Model Family (llama3_1, llama3_X, etc.)",
44 )
45
46 def _run_model_template_cmd(self, args: argparse.Namespace) -> None:
47 import pkg_resources
48
49 # Only Llama 3.1 and 3.2 are supported
50 supported_model_ids = [
51 m
52 for m in CoreModelId
53 if model_family(m) in {ModelFamily.llama3_1, ModelFamily.llama3_2}
54 ]
55 model_str = "\n".join([m.value for m in supported_model_ids])
56 try:
57 model_id = CoreModelId(args.model_name)
58 except ValueError:
59 self.parser.error(
60 f"{args.model_name} is not a valid Model. Choose one from --\n{model_str}"
61 )
62
63 if model_id not in supported_model_ids:
64 self.parser.error(
65 f"{model_id} is not a valid Model. Choose one from --\n {model_str}"
66 )
67
68 llama_3_1_file = pkg_resources.resource_filename(
69 "llama_models", "llama3_1/prompt_format.md"
70 )
71 llama_3_2_text_file = pkg_resources.resource_filename(
72 "llama_models", "llama3_2/text_prompt_format.md"
73 )
74 llama_3_2_vision_file = pkg_resources.resource_filename(
75 "llama_models", "llama3_2/vision_prompt_format.md"
76 )
77 if model_family(model_id) == ModelFamily.llama3_1:
78 with open(llama_3_1_file, "r") as f:
79 content = f.read()
80 elif model_family(model_id) == ModelFamily.llama3_2:
81 if is_multimodal(model_id):
82 with open(llama_3_2_vision_file, "r") as f:
83 content = f.read()
84 else:
85 with open(llama_3_2_text_file, "r") as f:
86 content = f.read()
87
88 render_markdown_to_pager(content)
89
90
91def render_markdown_to_pager(markdown_content: str):
92 from rich.console import Console
93 from rich.markdown import Markdown
94 from rich.style import Style
95 from rich.text import Text
96
97 class LeftAlignedHeaderMarkdown(Markdown):
98 def parse_header(self, token):
99 level = token.type.count("h")
100 content = Text(token.content)
101 header_style = Style(color="bright_blue", bold=True)
102 header = Text(f"{'#' * level} ", style=header_style) + content
103 self.add_text(header)
104
105 # Render the Markdown
106 md = LeftAlignedHeaderMarkdown(markdown_content)
107
108 # Capture the rendered output
109 output = StringIO()
110 console = Console(file=output, force_terminal=True, width=100) # Set a fixed width
111 console.print(md)
112 rendered_content = output.getvalue()
113
114 # Pipe to pager
115 pager = subprocess.Popen(["less", "-R"], stdin=subprocess.PIPE)
116 pager.communicate(input=rendered_content.encode())