From 6406d2f5b5daf35a8cfe550290dabf8196653aba Mon Sep 17 00:00:00 2001 From: juk0de Date: Fri, 11 Aug 2023 18:12:49 +0200 Subject: [PATCH 01/17] started to implement sub-commands --- chatmastermind/api_client.py | 2 +- chatmastermind/main.py | 242 ++++++++++++++++++++++------------- chatmastermind/storage.py | 16 +-- chatmastermind/utils.py | 4 +- tests/test_main.py | 28 ++-- 5 files changed, 176 insertions(+), 116 deletions(-) diff --git a/chatmastermind/api_client.py b/chatmastermind/api_client.py index b9b0d05..8eaf695 100644 --- a/chatmastermind/api_client.py +++ b/chatmastermind/api_client.py @@ -5,7 +5,7 @@ def openai_api_key(api_key: str) -> None: openai.api_key = api_key -def display_models() -> None: +def print_models() -> None: not_ready = [] for engine in sorted(openai.Engine.list()['data'], key=lambda x: x['id']): if engine['ready']: diff --git a/chatmastermind/main.py b/chatmastermind/main.py index 68fe906..1b512e4 100755 --- a/chatmastermind/main.py +++ b/chatmastermind/main.py @@ -7,32 +7,33 @@ import sys import argcomplete import argparse import pathlib -from .utils import terminal_width, process_tags, display_chat, display_source_code, display_tags_frequency -from .storage import save_answers, create_chat, get_tags, get_tags_unique, read_file, dump_data -from .api_client import ai, openai_api_key, display_models +from .utils import terminal_width, process_tags, print_chat_hist, display_source_code, print_tags_frequency +from .storage import save_answers, create_chat_hist, get_tags, get_tags_unique, read_file, dump_data +from .api_client import ai, openai_api_key, print_models from itertools import zip_longest - -def run_print_command(args: argparse.Namespace, config: dict) -> None: - fname = pathlib.Path(args.print) - if fname.suffix == '.yaml': - with open(args.print, 'r') as f: - data = yaml.load(f, Loader=yaml.FullLoader) - elif fname.suffix == '.txt': - data = read_file(fname) - else: - print(f"Unknown file type: {args.print}") - sys.exit(1) - if args.only_source_code: - display_source_code(data['answer']) - else: - print(dump_data(data).strip()) +default_config = '.config.yaml' -def process_and_display_chat(args: argparse.Namespace, +def tags_completer(prefix, parsed_args, **kwargs): + with open(parsed_args.config, 'r') as f: + config = yaml.load(f, Loader=yaml.FullLoader) + return get_tags_unique(config, prefix) + + +def read_config(path: str): + with open(path, 'r') as f: + config = yaml.load(f, Loader=yaml.FullLoader) + return config + + +def create_question_and_chat(args: argparse.Namespace, config: dict, - dump: bool = False ) -> tuple[list[dict[str, str]], str, list[str]]: + """ + Creates the "SI request", including the question and chat history as determined + by the specified tags. + """ tags = args.tags or [] extags = args.extags or [] otags = args.output_tags or [] @@ -55,25 +56,42 @@ def process_and_display_chat(args: argparse.Namespace, question_parts.append(f"```\n{r.read().strip()}\n```") full_question = '\n\n'.join(question_parts) - chat = create_chat(full_question, tags, extags, config, - args.match_all_tags, args.with_tags, - args.with_file) - display_chat(chat, dump, args.only_source_code) + chat = create_chat_hist(full_question, tags, extags, config, + args.match_all_tags, args.with_tags, + args.with_file) return chat, full_question, tags -def process_and_display_tags(args: argparse.Namespace, - config: dict, - dump: bool = False - ) -> None: - display_tags_frequency(get_tags(config, None), dump) +def tag_cmd(args: argparse.Namespace) -> None: + """ + Handler for the 'tag' command. + """ + config = read_config(args.config) + if args.list: + print_tags_frequency(get_tags(config, None), args.dump) -def handle_question(args: argparse.Namespace, - config: dict, - dump: bool = False - ) -> None: - chat, question, tags = process_and_display_chat(args, config, dump) +def model_cmd(args: argparse.Namespace) -> None: + """ + Handler for the 'model' command. + """ + if args.list: + print_models() + + +def ask_cmd(args: argparse.Namespace) -> None: + """ + Handler for the 'ask' command. + """ + config = read_config(args.config) + if args.max_tokens: + config['openai']['max_tokens'] = args.max_tokens + if args.temperature: + config['openai']['temperature'] = args.temperature + if args.model: + config['openai']['model'] = args.model + chat, question, tags = create_question_and_chat(args, config) + print_chat_hist(chat, args.dump, args.only_source_code) otags = args.output_tags or [] answers, usage = ai(chat, config, args.number) save_answers(question, answers, tags, otags, config) @@ -81,77 +99,119 @@ def handle_question(args: argparse.Namespace, print(f"Usage: {usage}") -def tags_completer(prefix, parsed_args, **kwargs): - with open(parsed_args.config, 'r') as f: - config = yaml.load(f, Loader=yaml.FullLoader) - return get_tags_unique(config, prefix) +def hist_cmd(args: argparse.Namespace) -> None: + """ + Handler for the 'hist' command. + """ + config = read_config(args.config) + chat, q, t = create_question_and_chat(args, config) + print_chat_hist(chat, args.dump, args.only_source_code) + + +def print_cmd(args: argparse.Namespace) -> None: + """ + Handler for the 'print' command. + """ + fname = pathlib.Path(args.print) + if fname.suffix == '.yaml': + with open(args.print, 'r') as f: + data = yaml.load(f, Loader=yaml.FullLoader) + elif fname.suffix == '.txt': + data = read_file(fname) + else: + print(f"Unknown file type: {args.print}") + sys.exit(1) + if args.only_source_code: + display_source_code(data['answer']) + else: + print(dump_data(data).strip()) def create_parser() -> argparse.ArgumentParser: - default_config = '.config.yaml' parser = argparse.ArgumentParser( description="ChatMastermind is a Python application that automates conversation with AI") - group = parser.add_mutually_exclusive_group(required=True) - group.add_argument('-p', '--print', help='File to print') - group.add_argument('-q', '--question', nargs='*', help='Question to ask') - group.add_argument('-D', '--chat-dump', help="Print chat history as Python structure", action='store_true') - group.add_argument('-d', '--chat', help="Print chat history as readable text", action='store_true') - group.add_argument('-l', '--list-tags', help="List all tags and their frequency", action='store_true') - group.add_argument('-L', '--list-models', help="List all available models", action='store_true') parser.add_argument('-c', '--config', help='Config file name.', default=default_config) - parser.add_argument('-m', '--max-tokens', help='Max tokens to use', type=int) - parser.add_argument('-T', '--temperature', help='Temperature to use', type=float) - parser.add_argument('-M', '--model', help='Model to use') - parser.add_argument('-n', '--number', help='Number of answers to produce', type=int, default=1) - parser.add_argument('-s', '--source', nargs='*', help='Source add content of a file to the query') - parser.add_argument('-S', '--only-source-code', help='Print only source code', action='store_true') - parser.add_argument('-w', '--with-tags', help="Print chat history with tags.", action='store_true') - parser.add_argument('-W', '--with-file', - help="Print chat history with filename.", - action='store_true') - parser.add_argument('-a', '--match-all-tags', - help="All given tags must match when selecting chat history entries.", - action='store_true') - tags_arg = parser.add_argument('-t', '--tags', nargs='*', help='List of tag names', metavar='TAGS') - tags_arg.completer = tags_completer # type: ignore - extags_arg = parser.add_argument('-e', '--extags', nargs='*', help='List of tag names to exclude', metavar='EXTAGS') - extags_arg.completer = tags_completer # type: ignore - otags_arg = parser.add_argument('-o', '--output-tags', nargs='*', help='List of output tag names, default is input', metavar='OTAGS') - otags_arg.completer = tags_completer # type: ignore - argcomplete.autocomplete(parser) + + # subcommand-parser + cmdparser = parser.add_subparsers(dest='command', + title='commands', + description='supported commands') + cmdparser.required = True + + # a parent parser for all commands that support tag selection + tag_parser = argparse.ArgumentParser(add_help=False) + tag_arg = tag_parser.add_argument('-t', '--tags', nargs='*', + help='List of tag names', metavar='TAGS') + tag_arg.completer = tags_completer # type: ignore + extag_arg = tag_parser.add_argument('-e', '--extags', nargs='*', + help='List of tag names to exclude', metavar='EXTAGS') + extag_arg.completer = tags_completer # type: ignore + otag_arg = tag_parser.add_argument('-o', '--output-tags', nargs='*', + help='List of output tag names, default is input', metavar='OTAGS') + otag_arg.completer = tags_completer # type: ignore + tag_parser.add_argument('-a', '--match-all-tags', + help="All given tags must match when selecting chat history entries", + action='store_true') + # enable autocompletion for tags + argcomplete.autocomplete(tag_parser) + + # 'ask' command parser + ask_cmd_parser = cmdparser.add_parser('ask', parents=[tag_parser], + help="Ask a question.") + ask_cmd_parser.set_defaults(func=ask_cmd) + ask_cmd_parser.add_argument('-q', '--question', nargs='+', help='Question to ask', required=True) + ask_cmd_parser.add_argument('-m', '--max-tokens', help='Max tokens to use', type=int) + ask_cmd_parser.add_argument('-T', '--temperature', help='Temperature to use', type=float) + ask_cmd_parser.add_argument('-M', '--model', help='Model to use') + ask_cmd_parser.add_argument('-n', '--number', help='Number of answers to produce', type=int, default=1) + ask_cmd_parser.add_argument('-s', '--source', nargs='*', help='Source add content of a file to the query') + + # 'hist' command parser + hist_cmd_parser = cmdparser.add_parser('hist', parents=[tag_parser], + help="Print chat history.") + hist_cmd_parser.set_defaults(func=hist_cmd) + hist_cmd_parser.add_argument('-d', '--dump', help="Print chat history as Python structure", + action='store_true') + hist_cmd_parser.add_argument('-w', '--with-tags', help="Print chat history with tags.", + action='store_true') + hist_cmd_parser.add_argument('-W', '--with-files', help="Print chat history with filenames.", + action='store_true') + hist_cmd_parser.add_argument('-S', '--only-source-code', help='Print only source code', + action='store_true') + + # 'tag' command parser + tag_cmd_parser = cmdparser.add_parser('tag', + help="Manage tags.") + tag_cmd_parser.set_defaults(func=tag_cmd) + tag_cmd_parser.add_argument('-l', '--list', help="List all tags and their frequency", + action='store_true') + + # 'model' command parser + model_cmd_parser = cmdparser.add_parser('model', + help="Manage models.") + model_cmd_parser.set_defaults(func=model_cmd) + model_cmd_parser.add_argument('-l', '--list', help="List all available models", + action='store_true') + + # 'print' command parser + print_cmd_parser = cmdparser.add_parser('print', + help="Print files.") + print_cmd_parser.set_defaults(func=print_cmd) + print_cmd_parser.add_argument('-f', '--file', help='File to print', required=True) + print_cmd_parser.add_argument('-S', '--only-source-code', help='Print only source code', + action='store_true') + return parser def main() -> int: parser = create_parser() args = parser.parse_args() + command = parser.parse_args() - with open(args.config, 'r') as f: - config = yaml.load(f, Loader=yaml.FullLoader) + openai_api_key(read_config(args.config)['openai']['api_key']) - openai_api_key(config['openai']['api_key']) - - if args.max_tokens: - config['openai']['max_tokens'] = args.max_tokens - - if args.temperature: - config['openai']['temperature'] = args.temperature - - if args.model: - config['openai']['model'] = args.model - - if args.print: - run_print_command(args, config) - elif args.question: - handle_question(args, config) - elif args.chat_dump: - process_and_display_chat(args, config, dump=True) - elif args.chat: - process_and_display_chat(args, config) - elif args.list_tags: - process_and_display_tags(args, config) - elif args.list_models: - display_models() + command.func(command) return 0 diff --git a/chatmastermind/storage.py b/chatmastermind/storage.py index ac59eb5..4705893 100644 --- a/chatmastermind/storage.py +++ b/chatmastermind/storage.py @@ -63,14 +63,14 @@ def save_answers(question: str, f.write(f'{num}') -def create_chat(question: Optional[str], - tags: Optional[List[str]], - extags: Optional[List[str]], - config: Dict[str, Any], - match_all_tags: bool = False, - with_tags: bool = False, - with_file: bool = False - ) -> List[Dict[str, str]]: +def create_chat_hist(question: Optional[str], + tags: Optional[List[str]], + extags: Optional[List[str]], + config: Dict[str, Any], + match_all_tags: bool = False, + with_tags: bool = False, + with_file: bool = False + ) -> List[Dict[str, str]]: chat: List[Dict[str, str]] = [] append_message(chat, 'system', config['system'].strip()) for file in sorted(pathlib.Path(config['db']).iterdir()): diff --git a/chatmastermind/utils.py b/chatmastermind/utils.py index bc1dcd2..ca92d25 100644 --- a/chatmastermind/utils.py +++ b/chatmastermind/utils.py @@ -57,7 +57,7 @@ def display_source_code(content: str) -> None: pass -def display_chat(chat, dump=False, source_code=False) -> None: +def print_chat_hist(chat, dump=False, source_code=False) -> None: if dump: pp(chat) return @@ -75,7 +75,7 @@ def display_chat(chat, dump=False, source_code=False) -> None: print(f"{message['role'].upper()}: {message['content']}") -def display_tags_frequency(tags: List[str], dump=False) -> None: +def print_tags_frequency(tags: List[str], dump=False) -> None: if dump: pp(tags) return diff --git a/tests/test_main.py b/tests/test_main.py index 48d9ea8..c0aa32c 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -3,9 +3,9 @@ import io import pathlib import argparse from chatmastermind.utils import terminal_width -from chatmastermind.main import create_parser, handle_question +from chatmastermind.main import create_parser, ask_cmd from chatmastermind.api_client import ai -from chatmastermind.storage import create_chat, save_answers, dump_data +from chatmastermind.storage import create_chat_hist, save_answers, dump_data from unittest import mock from unittest.mock import patch, MagicMock, Mock @@ -30,7 +30,7 @@ class TestCreateChat(unittest.TestCase): {'question': 'test_content', 'answer': 'some answer', 'tags': ['test_tag']})) - test_chat = create_chat(self.question, self.tags, None, self.config) + test_chat = create_chat_hist(self.question, self.tags, None, self.config) self.assertEqual(len(test_chat), 4) self.assertEqual(test_chat[0], @@ -52,7 +52,7 @@ class TestCreateChat(unittest.TestCase): {'question': 'test_content', 'answer': 'some answer', 'tags': ['other_tag']})) - test_chat = create_chat(self.question, self.tags, None, self.config) + test_chat = create_chat_hist(self.question, self.tags, None, self.config) self.assertEqual(len(test_chat), 2) self.assertEqual(test_chat[0], @@ -75,7 +75,7 @@ class TestCreateChat(unittest.TestCase): 'tags': ['test_tag2']})), ) - test_chat = create_chat(self.question, [], None, self.config) + test_chat = create_chat_hist(self.question, [], None, self.config) self.assertEqual(len(test_chat), 6) self.assertEqual(test_chat[0], @@ -112,24 +112,24 @@ class TestHandleQuestion(unittest.TestCase): 'setting2': 'value2' } - @patch("chatmastermind.main.create_chat", return_value="test_chat") + @patch("chatmastermind.main.create_chat_hist", return_value="test_chat") @patch("chatmastermind.main.process_tags") @patch("chatmastermind.main.ai", return_value=(["answer1", "answer2", "answer3"], "test_usage")) @patch("chatmastermind.utils.pp") @patch("builtins.print") - def test_handle_question(self, mock_print, mock_pp, mock_ai, - mock_process_tags, mock_create_chat): + def test_ask_cmd(self, mock_print, mock_pp, mock_ai, + mock_process_tags, mock_create_chat_hist): open_mock = MagicMock() with patch("chatmastermind.storage.open", open_mock): - handle_question(self.args, self.config, True) + ask_cmd(self.args, self.config, True) mock_process_tags.assert_called_once_with(self.args.tags, self.args.extags, []) - mock_create_chat.assert_called_once_with(self.question, - self.args.tags, - self.args.extags, - self.config, - False, False, False) + mock_create_chat_hist.assert_called_once_with(self.question, + self.args.tags, + self.args.extags, + self.config, + False, False, False) mock_pp.assert_called_once_with("test_chat") mock_ai.assert_called_with("test_chat", self.config, From f90e7bcd47971c7d0d64406c5210641774df3f40 Mon Sep 17 00:00:00 2001 From: juk0de Date: Sat, 12 Aug 2023 08:13:31 +0200 Subject: [PATCH 02/17] fixed 'hist' command and simplified reading the config file --- chatmastermind/main.py | 38 +++++++++++++++++++++----------------- chatmastermind/utils.py | 5 ++++- 2 files changed, 25 insertions(+), 18 deletions(-) diff --git a/chatmastermind/main.py b/chatmastermind/main.py index 1b512e4..32622da 100755 --- a/chatmastermind/main.py +++ b/chatmastermind/main.py @@ -7,7 +7,7 @@ import sys import argcomplete import argparse import pathlib -from .utils import terminal_width, process_tags, print_chat_hist, display_source_code, print_tags_frequency +from .utils import terminal_width, print_tag_args, print_chat_hist, display_source_code, print_tags_frequency from .storage import save_answers, create_chat_hist, get_tags, get_tags_unique, read_file, dump_data from .api_client import ai, openai_api_key, print_models from itertools import zip_longest @@ -27,9 +27,9 @@ def read_config(path: str): return config -def create_question_and_chat(args: argparse.Namespace, - config: dict, - ) -> tuple[list[dict[str, str]], str, list[str]]: +def create_question_with_hist(args: argparse.Namespace, + config: dict, + ) -> tuple[list[dict[str, str]], str, list[str]]: """ Creates the "SI request", including the question and chat history as determined by the specified tags. @@ -39,7 +39,7 @@ def create_question_and_chat(args: argparse.Namespace, otags = args.output_tags or [] if not args.only_source_code: - process_tags(tags, extags, otags) + print_tag_args(tags, extags, otags) question_parts = [] question_list = args.question if args.question is not None else [] @@ -62,16 +62,15 @@ def create_question_and_chat(args: argparse.Namespace, return chat, full_question, tags -def tag_cmd(args: argparse.Namespace) -> None: +def tag_cmd(args: argparse.Namespace, config: dict) -> None: """ Handler for the 'tag' command. """ - config = read_config(args.config) if args.list: print_tags_frequency(get_tags(config, None), args.dump) -def model_cmd(args: argparse.Namespace) -> None: +def model_cmd(args: argparse.Namespace, config: dict) -> None: """ Handler for the 'model' command. """ @@ -79,18 +78,17 @@ def model_cmd(args: argparse.Namespace) -> None: print_models() -def ask_cmd(args: argparse.Namespace) -> None: +def ask_cmd(args: argparse.Namespace, config: dict) -> None: """ Handler for the 'ask' command. """ - config = read_config(args.config) if args.max_tokens: config['openai']['max_tokens'] = args.max_tokens if args.temperature: config['openai']['temperature'] = args.temperature if args.model: config['openai']['model'] = args.model - chat, question, tags = create_question_and_chat(args, config) + chat, question, tags = create_question_with_hist(args, config) print_chat_hist(chat, args.dump, args.only_source_code) otags = args.output_tags or [] answers, usage = ai(chat, config, args.number) @@ -99,16 +97,21 @@ def ask_cmd(args: argparse.Namespace) -> None: print(f"Usage: {usage}") -def hist_cmd(args: argparse.Namespace) -> None: +def hist_cmd(args: argparse.Namespace, config: dict) -> None: """ Handler for the 'hist' command. """ - config = read_config(args.config) - chat, q, t = create_question_and_chat(args, config) + tags = args.tags or [] + extags = args.extags or [] + + chat = create_chat_hist(None, tags, extags, config, + args.match_all_tags, + args.with_tags, + args.with_files) print_chat_hist(chat, args.dump, args.only_source_code) -def print_cmd(args: argparse.Namespace) -> None: +def print_cmd(args: argparse.Namespace, config: dict) -> None: """ Handler for the 'print' command. """ @@ -209,9 +212,10 @@ def main() -> int: args = parser.parse_args() command = parser.parse_args() - openai_api_key(read_config(args.config)['openai']['api_key']) + config = read_config(args.config) + openai_api_key(config['openai']['api_key']) - command.func(command) + command.func(command, config) return 0 diff --git a/chatmastermind/utils.py b/chatmastermind/utils.py index ca92d25..cdd0e60 100644 --- a/chatmastermind/utils.py +++ b/chatmastermind/utils.py @@ -11,7 +11,10 @@ def pp(*args, **kwargs) -> None: return PrettyPrinter(width=terminal_width()).pprint(*args, **kwargs) -def process_tags(tags: list[str], extags: list[str], otags: list[str]) -> None: +def print_tag_args(tags: list[str], extags: list[str], otags: list[str]) -> None: + """ + Prints the tags specified in the given args. + """ printed_messages = [] if tags: From 5a435c5f8f701e14a7a82b675b581aaa34fe10fc Mon Sep 17 00:00:00 2001 From: juk0de Date: Sat, 12 Aug 2023 08:20:00 +0200 Subject: [PATCH 03/17] fixed 'tag' and 'hist' commands --- chatmastermind/main.py | 8 ++++---- chatmastermind/utils.py | 5 +---- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/chatmastermind/main.py b/chatmastermind/main.py index 32622da..f80c26c 100755 --- a/chatmastermind/main.py +++ b/chatmastermind/main.py @@ -67,7 +67,7 @@ def tag_cmd(args: argparse.Namespace, config: dict) -> None: Handler for the 'tag' command. """ if args.list: - print_tags_frequency(get_tags(config, None), args.dump) + print_tags_frequency(get_tags(config, None)) def model_cmd(args: argparse.Namespace, config: dict) -> None: @@ -115,14 +115,14 @@ def print_cmd(args: argparse.Namespace, config: dict) -> None: """ Handler for the 'print' command. """ - fname = pathlib.Path(args.print) + fname = pathlib.Path(args.file) if fname.suffix == '.yaml': - with open(args.print, 'r') as f: + with open(args.file, 'r') as f: data = yaml.load(f, Loader=yaml.FullLoader) elif fname.suffix == '.txt': data = read_file(fname) else: - print(f"Unknown file type: {args.print}") + print(f"Unknown file type: {args.file}") sys.exit(1) if args.only_source_code: display_source_code(data['answer']) diff --git a/chatmastermind/utils.py b/chatmastermind/utils.py index cdd0e60..78440fa 100644 --- a/chatmastermind/utils.py +++ b/chatmastermind/utils.py @@ -78,9 +78,6 @@ def print_chat_hist(chat, dump=False, source_code=False) -> None: print(f"{message['role'].upper()}: {message['content']}") -def print_tags_frequency(tags: List[str], dump=False) -> None: - if dump: - pp(tags) - return +def print_tags_frequency(tags: List[str]) -> None: for tag in sorted(set(tags)): print(f"- {tag}: {tags.count(tag)}") From 5119b3a8743db64fb4f865a19ebab7b4fbf47e16 Mon Sep 17 00:00:00 2001 From: juk0de Date: Sat, 12 Aug 2023 08:28:07 +0200 Subject: [PATCH 04/17] fixed 'ask' command --- chatmastermind/main.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/chatmastermind/main.py b/chatmastermind/main.py index f80c26c..9c8c3c5 100755 --- a/chatmastermind/main.py +++ b/chatmastermind/main.py @@ -57,8 +57,7 @@ def create_question_with_hist(args: argparse.Namespace, full_question = '\n\n'.join(question_parts) chat = create_chat_hist(full_question, tags, extags, config, - args.match_all_tags, args.with_tags, - args.with_file) + args.match_all_tags, False, False) return chat, full_question, tags @@ -89,7 +88,7 @@ def ask_cmd(args: argparse.Namespace, config: dict) -> None: if args.model: config['openai']['model'] = args.model chat, question, tags = create_question_with_hist(args, config) - print_chat_hist(chat, args.dump, args.only_source_code) + print_chat_hist(chat, False, args.only_source_code) otags = args.output_tags or [] answers, usage = ai(chat, config, args.number) save_answers(question, answers, tags, otags, config) @@ -162,12 +161,16 @@ def create_parser() -> argparse.ArgumentParser: ask_cmd_parser = cmdparser.add_parser('ask', parents=[tag_parser], help="Ask a question.") ask_cmd_parser.set_defaults(func=ask_cmd) - ask_cmd_parser.add_argument('-q', '--question', nargs='+', help='Question to ask', required=True) + ask_cmd_parser.add_argument('-q', '--question', nargs='+', help='Question to ask', + required=True) ask_cmd_parser.add_argument('-m', '--max-tokens', help='Max tokens to use', type=int) ask_cmd_parser.add_argument('-T', '--temperature', help='Temperature to use', type=float) ask_cmd_parser.add_argument('-M', '--model', help='Model to use') - ask_cmd_parser.add_argument('-n', '--number', help='Number of answers to produce', type=int, default=1) + ask_cmd_parser.add_argument('-n', '--number', help='Number of answers to produce', type=int, + default=1) ask_cmd_parser.add_argument('-s', '--source', nargs='*', help='Source add content of a file to the query') + ask_cmd_parser.add_argument('-S', '--only-source-code', help='Add pure source code to the chat history', + action='store_true') # 'hist' command parser hist_cmd_parser = cmdparser.add_parser('hist', parents=[tag_parser], From 93a8b0081af71f5b0ea05fc71b0393150326a052 Mon Sep 17 00:00:00 2001 From: juk0de Date: Sat, 12 Aug 2023 09:50:54 +0200 Subject: [PATCH 05/17] main: cleanup --- chatmastermind/main.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/chatmastermind/main.py b/chatmastermind/main.py index 9c8c3c5..b1db2d6 100755 --- a/chatmastermind/main.py +++ b/chatmastermind/main.py @@ -81,12 +81,6 @@ def ask_cmd(args: argparse.Namespace, config: dict) -> None: """ Handler for the 'ask' command. """ - if args.max_tokens: - config['openai']['max_tokens'] = args.max_tokens - if args.temperature: - config['openai']['temperature'] = args.temperature - if args.model: - config['openai']['model'] = args.model chat, question, tags = create_question_with_hist(args, config) print_chat_hist(chat, False, args.only_source_code) otags = args.output_tags or [] @@ -137,8 +131,8 @@ def create_parser() -> argparse.ArgumentParser: # subcommand-parser cmdparser = parser.add_subparsers(dest='command', title='commands', - description='supported commands') - cmdparser.required = True + description='supported commands', + required=True) # a parent parser for all commands that support tag selection tag_parser = argparse.ArgumentParser(add_help=False) @@ -214,9 +208,16 @@ def main() -> int: parser = create_parser() args = parser.parse_args() command = parser.parse_args() - config = read_config(args.config) + + # modify config according to args openai_api_key(config['openai']['api_key']) + if args.max_tokens: + config['openai']['max_tokens'] = args.max_tokens + if args.temperature: + config['openai']['temperature'] = args.temperature + if args.model: + config['openai']['model'] = args.model command.func(command, config) From 056bf4c6b574177c13e8260765f1508c7b220018 Mon Sep 17 00:00:00 2001 From: juk0de Date: Sat, 12 Aug 2023 09:51:13 +0200 Subject: [PATCH 06/17] fixed almost all tests --- tests/test_main.py | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/tests/test_main.py b/tests/test_main.py index c0aa32c..4434757 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -7,7 +7,7 @@ from chatmastermind.main import create_parser, ask_cmd from chatmastermind.api_client import ai from chatmastermind.storage import create_chat_hist, save_answers, dump_data from unittest import mock -from unittest.mock import patch, MagicMock, Mock +from unittest.mock import patch, MagicMock, Mock, ANY class TestCreateChat(unittest.TestCase): @@ -113,23 +113,28 @@ class TestHandleQuestion(unittest.TestCase): } @patch("chatmastermind.main.create_chat_hist", return_value="test_chat") - @patch("chatmastermind.main.process_tags") + @patch("chatmastermind.main.print_tag_args") + @patch("chatmastermind.utils.print_chat_hist") @patch("chatmastermind.main.ai", return_value=(["answer1", "answer2", "answer3"], "test_usage")) @patch("chatmastermind.utils.pp") @patch("builtins.print") def test_ask_cmd(self, mock_print, mock_pp, mock_ai, - mock_process_tags, mock_create_chat_hist): + mock_print_tag_args, mock_create_chat_hist, + mock_print_chat_hist): open_mock = MagicMock() with patch("chatmastermind.storage.open", open_mock): - ask_cmd(self.args, self.config, True) - mock_process_tags.assert_called_once_with(self.args.tags, - self.args.extags, - []) + ask_cmd(self.args, self.config) + mock_print_tag_args.assert_called_once_with(self.args.tags, + self.args.extags, + []) mock_create_chat_hist.assert_called_once_with(self.question, self.args.tags, self.args.extags, self.config, False, False, False) + mock_print_chat_hist.assert_called_once_with('test_chat', + False, + self.args.only_source_code) mock_pp.assert_called_once_with("test_chat") mock_ai.assert_called_with("test_chat", self.config, @@ -205,15 +210,15 @@ class TestAI(unittest.TestCase): class TestCreateParser(unittest.TestCase): def test_create_parser(self): - with patch('argparse.ArgumentParser.add_mutually_exclusive_group') as mock_add_mutually_exclusive_group: - mock_group = Mock() - mock_add_mutually_exclusive_group.return_value = mock_group + with patch('argparse.ArgumentParser.add_subparsers') as mock_add_subparsers: + mock_cmdparser = Mock() + mock_add_subparsers.return_value = mock_cmdparser parser = create_parser() self.assertIsInstance(parser, argparse.ArgumentParser) - mock_add_mutually_exclusive_group.assert_called_once_with(required=True) - mock_group.add_argument.assert_any_call('-p', '--print', help='File to print') - mock_group.add_argument.assert_any_call('-q', '--question', nargs='*', help='Question to ask') - mock_group.add_argument.assert_any_call('-D', '--chat-dump', help="Print chat history as Python structure", action='store_true') - mock_group.add_argument.assert_any_call('-d', '--chat', help="Print chat history as readable text", action='store_true') + mock_add_subparsers.assert_called_once_with(dest='command', title='commands', description='supported commands', required=True) + mock_cmdparser.add_parser.assert_any_call('ask', parents=ANY, help=ANY) + mock_cmdparser.add_parser.assert_any_call('hist', parents=ANY, help=ANY) + mock_cmdparser.add_parser.assert_any_call('tag', help=ANY) + mock_cmdparser.add_parser.assert_any_call('model', help=ANY) + mock_cmdparser.add_parser.assert_any_call('print', help=ANY) self.assertTrue('.config.yaml' in parser.get_default('config')) - self.assertEqual(parser.get_default('number'), 1) From bc5e6228a63e8eb657e7f1ee2f23de5a56721465 Mon Sep 17 00:00:00 2001 From: juk0de Date: Sat, 12 Aug 2023 10:21:09 +0200 Subject: [PATCH 07/17] defined 'ConfigType' for configuration file type hinting --- chatmastermind/main.py | 14 +++++++------- chatmastermind/storage.py | 18 +++++++++--------- chatmastermind/utils.py | 4 +++- 3 files changed, 19 insertions(+), 17 deletions(-) diff --git a/chatmastermind/main.py b/chatmastermind/main.py index b1db2d6..843885c 100755 --- a/chatmastermind/main.py +++ b/chatmastermind/main.py @@ -7,7 +7,7 @@ import sys import argcomplete import argparse import pathlib -from .utils import terminal_width, print_tag_args, print_chat_hist, display_source_code, print_tags_frequency +from .utils import terminal_width, print_tag_args, print_chat_hist, display_source_code, print_tags_frequency, ConfigType from .storage import save_answers, create_chat_hist, get_tags, get_tags_unique, read_file, dump_data from .api_client import ai, openai_api_key, print_models from itertools import zip_longest @@ -28,7 +28,7 @@ def read_config(path: str): def create_question_with_hist(args: argparse.Namespace, - config: dict, + config: ConfigType, ) -> tuple[list[dict[str, str]], str, list[str]]: """ Creates the "SI request", including the question and chat history as determined @@ -61,7 +61,7 @@ def create_question_with_hist(args: argparse.Namespace, return chat, full_question, tags -def tag_cmd(args: argparse.Namespace, config: dict) -> None: +def tag_cmd(args: argparse.Namespace, config: ConfigType) -> None: """ Handler for the 'tag' command. """ @@ -69,7 +69,7 @@ def tag_cmd(args: argparse.Namespace, config: dict) -> None: print_tags_frequency(get_tags(config, None)) -def model_cmd(args: argparse.Namespace, config: dict) -> None: +def model_cmd(args: argparse.Namespace, config: ConfigType) -> None: """ Handler for the 'model' command. """ @@ -77,7 +77,7 @@ def model_cmd(args: argparse.Namespace, config: dict) -> None: print_models() -def ask_cmd(args: argparse.Namespace, config: dict) -> None: +def ask_cmd(args: argparse.Namespace, config: ConfigType) -> None: """ Handler for the 'ask' command. """ @@ -90,7 +90,7 @@ def ask_cmd(args: argparse.Namespace, config: dict) -> None: print(f"Usage: {usage}") -def hist_cmd(args: argparse.Namespace, config: dict) -> None: +def hist_cmd(args: argparse.Namespace, config: ConfigType) -> None: """ Handler for the 'hist' command. """ @@ -104,7 +104,7 @@ def hist_cmd(args: argparse.Namespace, config: dict) -> None: print_chat_hist(chat, args.dump, args.only_source_code) -def print_cmd(args: argparse.Namespace, config: dict) -> None: +def print_cmd(args: argparse.Namespace, config: ConfigType) -> None: """ Handler for the 'print' command. """ diff --git a/chatmastermind/storage.py b/chatmastermind/storage.py index 4705893..afd1e8d 100644 --- a/chatmastermind/storage.py +++ b/chatmastermind/storage.py @@ -1,7 +1,7 @@ import yaml import io import pathlib -from .utils import terminal_width, append_message, message_to_chat +from .utils import terminal_width, append_message, message_to_chat, ConfigType from typing import List, Dict, Any, Optional @@ -41,11 +41,11 @@ def save_answers(question: str, answers: list[str], tags: list[str], otags: Optional[list[str]], - config: Dict[str, Any] + config: ConfigType ) -> None: wtags = otags or tags num, inum = 0, 0 - next_fname = pathlib.Path(config['db']) / '.next' + next_fname = pathlib.Path(str(config['db'])) / '.next' try: with open(next_fname, 'r') as f: num = int(f.read()) @@ -66,14 +66,14 @@ def save_answers(question: str, def create_chat_hist(question: Optional[str], tags: Optional[List[str]], extags: Optional[List[str]], - config: Dict[str, Any], + config: ConfigType, match_all_tags: bool = False, with_tags: bool = False, with_file: bool = False ) -> List[Dict[str, str]]: chat: List[Dict[str, str]] = [] - append_message(chat, 'system', config['system'].strip()) - for file in sorted(pathlib.Path(config['db']).iterdir()): + append_message(chat, 'system', str(config['system']).strip()) + for file in sorted(pathlib.Path(str(config['db'])).iterdir()): if file.suffix == '.yaml': with open(file, 'r') as f: data = yaml.load(f, Loader=yaml.FullLoader) @@ -97,9 +97,9 @@ def create_chat_hist(question: Optional[str], return chat -def get_tags(config: Dict[str, Any], prefix: Optional[str]) -> List[str]: +def get_tags(config: ConfigType, prefix: Optional[str]) -> List[str]: result = [] - for file in sorted(pathlib.Path(config['db']).iterdir()): + for file in sorted(pathlib.Path(str(config['db'])).iterdir()): if file.suffix == '.yaml': with open(file, 'r') as f: data = yaml.load(f, Loader=yaml.FullLoader) @@ -116,5 +116,5 @@ def get_tags(config: Dict[str, Any], prefix: Optional[str]) -> List[str]: return result -def get_tags_unique(config: Dict[str, Any], prefix: Optional[str]) -> List[str]: +def get_tags_unique(config: ConfigType, prefix: Optional[str]) -> List[str]: return list(set(get_tags(config, prefix))) diff --git a/chatmastermind/utils.py b/chatmastermind/utils.py index 78440fa..2a58dae 100644 --- a/chatmastermind/utils.py +++ b/chatmastermind/utils.py @@ -1,6 +1,8 @@ import shutil from pprint import PrettyPrinter -from typing import List, Dict +from typing import List, Dict, Union + +ConfigType = Dict[str, Union[str, Dict[str, Union[str, int]]]] def terminal_width() -> int: From e4d055b90033773bba712f8ab49cf9339d7d83ed Mon Sep 17 00:00:00 2001 From: Oleksandr Kozachuk Date: Sat, 12 Aug 2023 12:20:49 +0200 Subject: [PATCH 08/17] Fix the max_tokens, temperature, and model setup. --- chatmastermind/main.py | 26 ++++++++++++++++++-------- chatmastermind/utils.py | 11 +++++------ 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/chatmastermind/main.py b/chatmastermind/main.py index 843885c..3ed387c 100755 --- a/chatmastermind/main.py +++ b/chatmastermind/main.py @@ -21,7 +21,7 @@ def tags_completer(prefix, parsed_args, **kwargs): return get_tags_unique(config, prefix) -def read_config(path: str): +def read_config(path: str) -> ConfigType: with open(path, 'r') as f: config = yaml.load(f, Loader=yaml.FullLoader) return config @@ -81,6 +81,15 @@ def ask_cmd(args: argparse.Namespace, config: ConfigType) -> None: """ Handler for the 'ask' command. """ + if type(config['openai']) is not dict: + raise RuntimeError('Configuration openai is not a dict.') + config_openai = config['openai'] + if args.max_tokens: + config_openai['max_tokens'] = args.max_tokens + if args.temperature: + config_openai['temperature'] = args.temperature + if args.model: + config_openai['model'] = args.model chat, question, tags = create_question_with_hist(args, config) print_chat_hist(chat, False, args.only_source_code) otags = args.output_tags or [] @@ -211,13 +220,14 @@ def main() -> int: config = read_config(args.config) # modify config according to args - openai_api_key(config['openai']['api_key']) - if args.max_tokens: - config['openai']['max_tokens'] = args.max_tokens - if args.temperature: - config['openai']['temperature'] = args.temperature - if args.model: - config['openai']['model'] = args.model + if type(config['openai']) is dict: + config_openai = config['openai'] + else: + RuntimeError("Configuration openai is not a dict.") + if type(config_openai['api_key']) is str: + openai_api_key(config_openai['api_key']) + else: + raise RuntimeError("Configuration openai.api_key is not a string.") command.func(command, config) diff --git a/chatmastermind/utils.py b/chatmastermind/utils.py index 2a58dae..fba8296 100644 --- a/chatmastermind/utils.py +++ b/chatmastermind/utils.py @@ -1,8 +1,7 @@ import shutil from pprint import PrettyPrinter -from typing import List, Dict, Union -ConfigType = Dict[str, Union[str, Dict[str, Union[str, int]]]] +ConfigType = dict[str, str | dict[str, str | int | float]] def terminal_width() -> int: @@ -31,15 +30,15 @@ def print_tag_args(tags: list[str], extags: list[str], otags: list[str]) -> None print() -def append_message(chat: List[Dict[str, str]], +def append_message(chat: list[dict[str, str]], role: str, content: str ) -> None: chat.append({'role': role, 'content': content.replace("''", "'")}) -def message_to_chat(message: Dict[str, str], - chat: List[Dict[str, str]], +def message_to_chat(message: dict[str, str], + chat: list[dict[str, str]], with_tags: bool = False, with_file: bool = False ) -> None: @@ -80,6 +79,6 @@ def print_chat_hist(chat, dump=False, source_code=False) -> None: print(f"{message['role'].upper()}: {message['content']}") -def print_tags_frequency(tags: List[str]) -> None: +def print_tags_frequency(tags: list[str]) -> None: for tag in sorted(set(tags)): print(f"- {tag}: {tags.count(tag)}") From 4b2f634b79ea491ab0bddec3c8fcd56c7ae26934 Mon Sep 17 00:00:00 2001 From: Oleksandr Kozachuk Date: Sat, 12 Aug 2023 12:30:07 +0200 Subject: [PATCH 09/17] Remove wrong comment and make it more readable. --- chatmastermind/main.py | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/chatmastermind/main.py b/chatmastermind/main.py index 3ed387c..3150931 100755 --- a/chatmastermind/main.py +++ b/chatmastermind/main.py @@ -219,15 +219,10 @@ def main() -> int: command = parser.parse_args() config = read_config(args.config) - # modify config according to args - if type(config['openai']) is dict: - config_openai = config['openai'] + if type(config['openai']) is dict and type(config['openai']['api_key']) is str: + openai_api_key(config['openai']['api_key']) else: - RuntimeError("Configuration openai is not a dict.") - if type(config_openai['api_key']) is str: - openai_api_key(config_openai['api_key']) - else: - raise RuntimeError("Configuration openai.api_key is not a string.") + raise RuntimeError("Configuration openai.api_key is wrong.") command.func(command, config) From 1fb9144192b8839839a0e6e29b285a783bbd044d Mon Sep 17 00:00:00 2001 From: Oleksandr Kozachuk Date: Sat, 12 Aug 2023 12:44:13 +0200 Subject: [PATCH 10/17] Change REDAME.md with the new call semantics. --- README.md | 78 +++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 56 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 4ff5d97..95d60a9 100644 --- a/README.md +++ b/README.md @@ -29,65 +29,99 @@ pip install . ## Usage +The `cmm` script has global options, a list of commands, and options per command: + ```bash -cmm [-h] [-p PRINT | -q QUESTION | -D | -d | -l] [-c CONFIG] [-m MAX_TOKENS] [-T TEMPERATURE] [-M MODEL] [-n NUMBER] [-t [TAGS [TAGS ...]]] [-e [EXTAGS [EXTAGS ...]]] [-o [OTAGS [OTAGS ...]]] [-a] [-w] [-W] +cmm [global options] command [command options] ``` -### Arguments +### Global Options -- `-p`, `--print`: YAML file to print. -- `-q`, `--question`: Question to ask. -- `-D`, `--chat-dump`: Print chat history as a Python structure. -- `-d`, `--chat`: Print chat history as readable text. -- `-a`, `--match-all-tags`: All given tags must match when selecting chat history entries. -- `-w`, `--with-tags`: Print chat history with tags. -- `-W`, `--with-tags`: Print chat history with filenames. -- `-l`, `--list-tags`: List all tags and their frequency. - `-c`, `--config`: Config file name (defaults to `.config.yaml`). + +### Commands + +- `ask`: Ask a question. +- `hist`: Print chat history. +- `tag`: Manage tags. +- `model`: Manage models. +- `print`: Print files. + +### Command Options + +#### `ask` Command Options + +- `-q`, `--question`: Question to ask (required). - `-m`, `--max-tokens`: Max tokens to use. - `-T`, `--temperature`: Temperature to use. - `-M`, `--model`: Model to use. - `-n`, `--number`: Number of answers to produce (default is 3). +- `-s`, `--source`: Add content of a file to the query. +- `-S`, `--only-source-code`: Add pure source code to the chat history. - `-t`, `--tags`: List of tag names. - `-e`, `--extags`: List of tag names to exclude. - `-o`, `--output-tags`: List of output tag names (default is the input tags). +- `-a`, `--match-all-tags`: All given tags must match when selecting chat history entries. + +#### `hist` Command Options + +- `-d`, `--dump`: Print chat history as Python structure. +- `-w`, `--with-tags`: Print chat history with tags. +- `-W`, `--with-files`: Print chat history with filenames. +- `-S`, `--only-source-code`: Print only source code. +- `-t`, `--tags`: List of tag names. +- `-e`, `--extags`: List of tag names to exclude. +- `-a`, `--match-all-tags`: All given tags must match when selecting chat history entries. + +#### `tag` Command Options + +- `-l`, `--list`: List all tags and their frequency. + +#### `model` Command Options + +- `-l`, `--list`: List all available models. + +#### `print` Command Options + +- `-f`, `--file`: File to print (required). +- `-S`, `--only-source-code`: Print only source code. ### Examples -1. Print the contents of a YAML file: +1. Ask a question: ```bash -cmm -p example.yaml +cmm ask -q "What is the meaning of life?" -t philosophy -e religion ``` -2. Ask a question: +2. Display the chat history: ```bash -cmm -q "What is the meaning of life?" -t philosophy -e religion +cmm hist ``` -3. Display the chat history as a Python structure: +3. Filter chat history by tags: ```bash -cmm -D +cmm hist -t tag1 tag2 ``` -4. Display the chat history as readable text: +4. Exclude chat history by tags: ```bash -cmm -d +cmm hist -e tag3 tag4 ``` -5. Filter chat history by tags: +5. List all tags and their frequency: ```bash -cmm -d -t tag1 tag2 +cmm tag -l ``` -6. Exclude chat history by tags: +6. Print the contents of a file: ```bash -cmm -d -e tag3 tag4 +cmm print -f example.yaml ``` ## Configuration From 6ed459be6fe5fa76b631cb6b972b31c26d5cd634 Mon Sep 17 00:00:00 2001 From: Oleksandr Kozachuk Date: Sat, 12 Aug 2023 13:17:10 +0200 Subject: [PATCH 11/17] Fix tests. --- tests/test_main.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/tests/test_main.py b/tests/test_main.py index 4434757..632124a 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -102,6 +102,9 @@ class TestHandleQuestion(unittest.TestCase): source=None, only_source_code=False, number=3, + max_tokens=None, + temperature=None, + model=None, match_all_tags=False, with_tags=False, with_file=False, @@ -109,18 +112,19 @@ class TestHandleQuestion(unittest.TestCase): self.config = { 'db': 'test_files', 'setting1': 'value1', - 'setting2': 'value2' + 'setting2': 'value2', + 'openai': {}, } @patch("chatmastermind.main.create_chat_hist", return_value="test_chat") @patch("chatmastermind.main.print_tag_args") - @patch("chatmastermind.utils.print_chat_hist") + @patch("chatmastermind.main.print_chat_hist") @patch("chatmastermind.main.ai", return_value=(["answer1", "answer2", "answer3"], "test_usage")) @patch("chatmastermind.utils.pp") @patch("builtins.print") def test_ask_cmd(self, mock_print, mock_pp, mock_ai, - mock_print_tag_args, mock_create_chat_hist, - mock_print_chat_hist): + mock_print_chat_hist, mock_print_tag_args, + mock_create_chat_hist): open_mock = MagicMock() with patch("chatmastermind.storage.open", open_mock): ask_cmd(self.args, self.config) @@ -135,7 +139,6 @@ class TestHandleQuestion(unittest.TestCase): mock_print_chat_hist.assert_called_once_with('test_chat', False, self.args.only_source_code) - mock_pp.assert_called_once_with("test_chat") mock_ai.assert_called_with("test_chat", self.config, self.args.number) From f371a6146e00b41a1eb005f94da0842a1772f8ff Mon Sep 17 00:00:00 2001 From: juk0de Date: Sat, 12 Aug 2023 13:55:39 +0200 Subject: [PATCH 12/17] moved 'read_config' to storage.py and added 'write_config' --- chatmastermind/main.py | 8 +------- chatmastermind/storage.py | 11 +++++++++++ 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/chatmastermind/main.py b/chatmastermind/main.py index 3150931..0486ae6 100755 --- a/chatmastermind/main.py +++ b/chatmastermind/main.py @@ -8,7 +8,7 @@ import argcomplete import argparse import pathlib from .utils import terminal_width, print_tag_args, print_chat_hist, display_source_code, print_tags_frequency, ConfigType -from .storage import save_answers, create_chat_hist, get_tags, get_tags_unique, read_file, dump_data +from .storage import save_answers, create_chat_hist, get_tags, get_tags_unique, read_file, read_config, dump_data from .api_client import ai, openai_api_key, print_models from itertools import zip_longest @@ -21,12 +21,6 @@ def tags_completer(prefix, parsed_args, **kwargs): return get_tags_unique(config, prefix) -def read_config(path: str) -> ConfigType: - with open(path, 'r') as f: - config = yaml.load(f, Loader=yaml.FullLoader) - return config - - def create_question_with_hist(args: argparse.Namespace, config: ConfigType, ) -> tuple[list[dict[str, str]], str, list[str]]: diff --git a/chatmastermind/storage.py b/chatmastermind/storage.py index afd1e8d..d90598b 100644 --- a/chatmastermind/storage.py +++ b/chatmastermind/storage.py @@ -22,6 +22,17 @@ def read_file(fname: pathlib.Path, tags_only: bool = False) -> Dict[str, Any]: "file": fname.name} +def read_config(path: str) -> ConfigType: + with open(path, 'r') as f: + config = yaml.load(f, Loader=yaml.FullLoader) + return config + + +def write_config(path: str, config: ConfigType) -> None: + with open(path, 'w') as f: + yaml.dump(config, f) + + def dump_data(data: Dict[str, Any]) -> str: with io.StringIO() as fd: fd.write(f'TAGS: {" ".join(data["tags"])}\n') From b6eb7d9af8e2a50a9bf20db7a84c4660adfb04a9 Mon Sep 17 00:00:00 2001 From: Oleksandr Kozachuk Date: Sat, 12 Aug 2023 13:57:52 +0200 Subject: [PATCH 13/17] Fix autocompletion. --- chatmastermind/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chatmastermind/main.py b/chatmastermind/main.py index 0486ae6..2e0cce1 100755 --- a/chatmastermind/main.py +++ b/chatmastermind/main.py @@ -152,7 +152,6 @@ def create_parser() -> argparse.ArgumentParser: help="All given tags must match when selecting chat history entries", action='store_true') # enable autocompletion for tags - argcomplete.autocomplete(tag_parser) # 'ask' command parser ask_cmd_parser = cmdparser.add_parser('ask', parents=[tag_parser], @@ -204,6 +203,7 @@ def create_parser() -> argparse.ArgumentParser: print_cmd_parser.add_argument('-S', '--only-source-code', help='Print only source code', action='store_true') + argcomplete.autocomplete(parser) return parser From f7ba0c000f4dae95380d3fa8e8cc8af996972df1 Mon Sep 17 00:00:00 2001 From: juk0de Date: Sat, 12 Aug 2023 14:12:35 +0200 Subject: [PATCH 14/17] renamed 'model' command to 'config' --- chatmastermind/main.py | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/chatmastermind/main.py b/chatmastermind/main.py index 2e0cce1..cc634fc 100755 --- a/chatmastermind/main.py +++ b/chatmastermind/main.py @@ -8,7 +8,7 @@ import argcomplete import argparse import pathlib from .utils import terminal_width, print_tag_args, print_chat_hist, display_source_code, print_tags_frequency, ConfigType -from .storage import save_answers, create_chat_hist, get_tags, get_tags_unique, read_file, read_config, dump_data +from .storage import save_answers, create_chat_hist, get_tags, get_tags_unique, read_file, read_config, write_config, dump_data from .api_client import ai, openai_api_key, print_models from itertools import zip_longest @@ -63,12 +63,20 @@ def tag_cmd(args: argparse.Namespace, config: ConfigType) -> None: print_tags_frequency(get_tags(config, None)) -def model_cmd(args: argparse.Namespace, config: ConfigType) -> None: +def config_cmd(args: argparse.Namespace, config: ConfigType) -> None: """ - Handler for the 'model' command. + Handler for the 'config' command. """ - if args.list: + if type(config['openai']) is not dict: + raise RuntimeError('Configuration openai is not a dict.') + + if args.list_models: print_models() + elif args.show_model: + print(config['openai']['model']) + elif args.model: + config['openai']['model'] = args.model + write_config(args.config, config) def ask_cmd(args: argparse.Namespace, config: ConfigType) -> None: @@ -188,12 +196,16 @@ def create_parser() -> argparse.ArgumentParser: tag_cmd_parser.add_argument('-l', '--list', help="List all tags and their frequency", action='store_true') - # 'model' command parser - model_cmd_parser = cmdparser.add_parser('model', - help="Manage models.") - model_cmd_parser.set_defaults(func=model_cmd) - model_cmd_parser.add_argument('-l', '--list', help="List all available models", - action='store_true') + # 'config' command parser + config_cmd_parser = cmdparser.add_parser('config', + help="Manage configuration") + config_cmd_parser.set_defaults(func=config_cmd) + config_group = config_cmd_parser.add_mutually_exclusive_group(required=True) + config_group.add_argument('-L', '--list-models', help="List all available models", + action='store_true') + config_group.add_argument('-m', '--show-model', help="Show current model", + action='store_true') + config_group.add_argument('-M', '--model', help="Set model in the config file") # 'print' command parser print_cmd_parser = cmdparser.add_parser('print', From 22bebc16ed1300fb1226794e1d1e80b58d72c085 Mon Sep 17 00:00:00 2001 From: juk0de Date: Sat, 12 Aug 2023 14:14:06 +0200 Subject: [PATCH 15/17] fixed min nr of expected arguments --- chatmastermind/main.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/chatmastermind/main.py b/chatmastermind/main.py index cc634fc..623b83a 100755 --- a/chatmastermind/main.py +++ b/chatmastermind/main.py @@ -147,13 +147,13 @@ def create_parser() -> argparse.ArgumentParser: # a parent parser for all commands that support tag selection tag_parser = argparse.ArgumentParser(add_help=False) - tag_arg = tag_parser.add_argument('-t', '--tags', nargs='*', + tag_arg = tag_parser.add_argument('-t', '--tags', nargs='+', help='List of tag names', metavar='TAGS') tag_arg.completer = tags_completer # type: ignore - extag_arg = tag_parser.add_argument('-e', '--extags', nargs='*', + extag_arg = tag_parser.add_argument('-e', '--extags', nargs='+', help='List of tag names to exclude', metavar='EXTAGS') extag_arg.completer = tags_completer # type: ignore - otag_arg = tag_parser.add_argument('-o', '--output-tags', nargs='*', + otag_arg = tag_parser.add_argument('-o', '--output-tags', nargs='+', help='List of output tag names, default is input', metavar='OTAGS') otag_arg.completer = tags_completer # type: ignore tag_parser.add_argument('-a', '--match-all-tags', @@ -172,7 +172,7 @@ def create_parser() -> argparse.ArgumentParser: ask_cmd_parser.add_argument('-M', '--model', help='Model to use') ask_cmd_parser.add_argument('-n', '--number', help='Number of answers to produce', type=int, default=1) - ask_cmd_parser.add_argument('-s', '--source', nargs='*', help='Source add content of a file to the query') + ask_cmd_parser.add_argument('-s', '--source', nargs='+', help='Source add content of a file to the query') ask_cmd_parser.add_argument('-S', '--only-source-code', help='Add pure source code to the chat history', action='store_true') From c4a7c07a0c8d2875bc87579e1fab3a042dd3ebe6 Mon Sep 17 00:00:00 2001 From: juk0de Date: Sat, 12 Aug 2023 14:14:51 +0200 Subject: [PATCH 16/17] fixed tests --- tests/test_main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_main.py b/tests/test_main.py index 632124a..0cfd1fd 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -222,6 +222,6 @@ class TestCreateParser(unittest.TestCase): mock_cmdparser.add_parser.assert_any_call('ask', parents=ANY, help=ANY) mock_cmdparser.add_parser.assert_any_call('hist', parents=ANY, help=ANY) mock_cmdparser.add_parser.assert_any_call('tag', help=ANY) - mock_cmdparser.add_parser.assert_any_call('model', help=ANY) + mock_cmdparser.add_parser.assert_any_call('config', help=ANY) mock_cmdparser.add_parser.assert_any_call('print', help=ANY) self.assertTrue('.config.yaml' in parser.get_default('config')) From 1e15a52e269f3bf44e472f84402958fe1e7b27e0 Mon Sep 17 00:00:00 2001 From: juk0de Date: Sat, 12 Aug 2023 18:34:19 +0200 Subject: [PATCH 17/17] updated README and some minor renaming --- README.md | 8 +++++--- chatmastermind/main.py | 8 ++++---- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 95d60a9..d55102a 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,7 @@ cmm [global options] command [command options] - `ask`: Ask a question. - `hist`: Print chat history. - `tag`: Manage tags. -- `model`: Manage models. +- `config`: Manage configuration. - `print`: Print files. ### Command Options @@ -77,9 +77,11 @@ cmm [global options] command [command options] - `-l`, `--list`: List all tags and their frequency. -#### `model` Command Options +#### `config` Command Options -- `-l`, `--list`: List all available models. +- `-l`, `--list-models`: List all available models. +- `-m`, `--print-model`: Print the currently configured model. +- `-M`, `--model`: Set model in the config file. #### `print` Command Options diff --git a/chatmastermind/main.py b/chatmastermind/main.py index 623b83a..0d68779 100755 --- a/chatmastermind/main.py +++ b/chatmastermind/main.py @@ -25,7 +25,7 @@ def create_question_with_hist(args: argparse.Namespace, config: ConfigType, ) -> tuple[list[dict[str, str]], str, list[str]]: """ - Creates the "SI request", including the question and chat history as determined + Creates the "AI request", including the question and chat history as determined by the specified tags. """ tags = args.tags or [] @@ -72,7 +72,7 @@ def config_cmd(args: argparse.Namespace, config: ConfigType) -> None: if args.list_models: print_models() - elif args.show_model: + elif args.print_model: print(config['openai']['model']) elif args.model: config['openai']['model'] = args.model @@ -201,9 +201,9 @@ def create_parser() -> argparse.ArgumentParser: help="Manage configuration") config_cmd_parser.set_defaults(func=config_cmd) config_group = config_cmd_parser.add_mutually_exclusive_group(required=True) - config_group.add_argument('-L', '--list-models', help="List all available models", + config_group.add_argument('-l', '--list-models', help="List all available models", action='store_true') - config_group.add_argument('-m', '--show-model', help="Show current model", + config_group.add_argument('-m', '--print-model', help="Print the currently configured model", action='store_true') config_group.add_argument('-M', '--model', help="Set model in the config file")