Elffuss BraidHub

kikocis/llama-stack public

7ef69e0d6d29 · 3.8 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 textwrap
9
10from termcolor import colored
11
12from llama_stack.cli.subcommand import Subcommand
13
14
15class ModelTemplate(Subcommand):
16 """Llama model cli for describe a model template (message formats)"""
17
18 def __init__(self, subparsers: argparse._SubParsersAction):
19 super().__init__()
20 self.parser = subparsers.add_parser(
21 "template",
22 prog="llama model template",
23 description="Show llama model message formats",
24 epilog=textwrap.dedent(
25 """
26 Example:
27 llama model template <options>
28 """
29 ),
30 formatter_class=argparse.RawTextHelpFormatter,
31 )
32 self._add_arguments()
33 self.parser.set_defaults(func=self._run_model_template_cmd)
34
35 def _prompt_type(self, value):
36 from llama_models.llama3.api.datatypes import ToolPromptFormat
37
38 try:
39 return ToolPromptFormat(value.lower())
40 except ValueError:
41 raise argparse.ArgumentTypeError(
42 f"{value} is not a valid ToolPromptFormat. Choose from {', '.join(t.value for t in ToolPromptFormat)}"
43 ) from None
44
45 def _add_arguments(self):
46 self.parser.add_argument(
47 "-m",
48 "--model-family",
49 type=str,
50 default="llama3_1",
51 help="Model Family (llama3_1, llama3_X, etc.)",
52 )
53 self.parser.add_argument(
54 "--name",
55 type=str,
56 help="Usecase template name (system_message, user_message, assistant_message, tool_message)...",
57 required=False,
58 )
59 self.parser.add_argument(
60 "--format",
61 type=str,
62 help="ToolPromptFormat (json or function_tag). This flag is used to print the template in a specific formats.",
63 required=False,
64 default="json",
65 )
66 self.parser.add_argument(
67 "--raw",
68 action="store_true",
69 help="If set to true, don't pretty-print into a table. Useful to copy-paste.",
70 )
71
72 def _run_model_template_cmd(self, args: argparse.Namespace) -> None:
73 from llama_models.llama3.api.interface import (
74 list_jinja_templates,
75 render_jinja_template,
76 )
77
78 from llama_stack.cli.table import print_table
79
80 if args.name:
81 tool_prompt_format = self._prompt_type(args.format)
82 template, tokens_info = render_jinja_template(args.name, tool_prompt_format)
83 rendered = ""
84 for tok, is_special in tokens_info:
85 if is_special:
86 rendered += colored(tok, "yellow", attrs=["bold"])
87 else:
88 rendered += tok
89
90 if not args.raw:
91 rendered = rendered.replace("\n", "↵\n")
92 print_table(
93 [
94 (
95 "Name",
96 colored(template.template_name, "white", attrs=["bold"]),
97 ),
98 ("Template", rendered),
99 ("Notes", template.notes),
100 ],
101 separate_rows=True,
102 )
103 else:
104 print("Template: ", template.template_name)
105 print("=" * 40)
106 print(rendered)
107 else:
108 templates = list_jinja_templates()
109 headers = ["Role", "Template Name"]
110 print_table(
111 [(t.role, t.template_name) for t in templates],
112 headers,
113 )