From 32bd17594b4bd9587537b225d957a1ec0859756f Mon Sep 17 00:00:00 2001 From: juk0de Date: Sat, 5 Aug 2023 08:09:35 +0200 Subject: [PATCH] Added option '-W' to print chat history with filenames --- README.md | 9 +++++---- chatmastermind/main.py | 10 ++++++---- chatmastermind/storage.py | 8 +++++--- chatmastermind/utils.py | 5 ++++- 4 files changed, 20 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index e42d1df..74aeb9b 100644 --- a/README.md +++ b/README.md @@ -28,16 +28,17 @@ pip install . ## Usage ```bash -cmm [-h] [-p PRINT | -q QUESTION | -D | -d] [-c CONFIG] [-m MAX_TOKENS] [-T TEMPERATURE] [-M MODEL] [-n NUMBER] [-t [TAGS [TAGS ...]]] [-e [EXTAGS [EXTAGS ...]]] [-o [OTAGS [OTAGS ...]]] [-w] +cmm [-h] [-p PRINT | -q QUESTION | -D | -d] [-c CONFIG] [-m MAX_TOKENS] [-T TEMPERATURE] [-M MODEL] [-n NUMBER] [-t [TAGS [TAGS ...]]] [-e [EXTAGS [EXTAGS ...]]] [-o [OTAGS [OTAGS ...]]] [-w] [-W] ``` ### Arguments - `-p`, `--print`: YAML file to print. - `-q`, `--question`: Question to ask. -- `-D`, `--chat-dump`: Print chat as a Python structure. -- `-d`, `--chat`: Print chat as readable text. -- `-w`, `--with-tags`: Print chat with tags. +- `-D`, `--chat-dump`: Print chat history as a Python structure. +- `-d`, `--chat`: Print chat history as readable text. +- `-w`, `--with-tags`: Print chat history with tags. +- `-W`, `--with-tags`: Print chat history with filenames. - `-c`, `--config`: Config file name (defaults to `.config.yaml`). - `-m`, `--max-tokens`: Max tokens to use. - `-T`, `--temperature`: Temperature to use. diff --git a/chatmastermind/main.py b/chatmastermind/main.py index 011577d..9828399 100755 --- a/chatmastermind/main.py +++ b/chatmastermind/main.py @@ -55,7 +55,8 @@ 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.with_tags) + chat = create_chat(full_question, tags, extags, config, + args.with_tags, args.with_file) display_chat(chat, dump, args.only_source_code) return chat, full_question, tags @@ -85,8 +86,8 @@ def create_parser() -> argparse.ArgumentParser: 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 as Python structure", action='store_true') - group.add_argument('-d', '--chat', help="Print chat as readable text", action='store_true') + 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') 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) @@ -94,7 +95,8 @@ def create_parser() -> argparse.ArgumentParser: 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 with tags.", 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') 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') diff --git a/chatmastermind/storage.py b/chatmastermind/storage.py index 9fa3a84..7418edf 100644 --- a/chatmastermind/storage.py +++ b/chatmastermind/storage.py @@ -15,7 +15,8 @@ def read_file(fname: str, tags_only: bool = False) -> Dict[str, Any]: answer_idx = text.index("==== ANSWER ====") question = "\n".join(text[question_idx:answer_idx]).strip() answer = "\n".join(text[answer_idx + 1:]).strip() - return {"question": question, "answer": answer, "tags": tags} + return {"question": question, "answer": answer, "tags": tags, + "file": pathlib.Path(fname).name} def dump_data(data: Dict[str, Any]) -> str: @@ -63,7 +64,8 @@ def create_chat(question: Optional[str], tags: Optional[List[str]], extags: Optional[List[str]], config: Dict[str, Any], - with_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()) @@ -81,7 +83,7 @@ def create_chat(question: Optional[str], extags_do_not_match = \ not extags or not data_tags.intersection(extags) if tags_match and extags_do_not_match: - message_to_chat(data, chat, with_tags) + message_to_chat(data, chat, with_tags, with_file) if question: append_message(chat, 'user', question) return chat diff --git a/chatmastermind/utils.py b/chatmastermind/utils.py index c4270c7..ef876e8 100644 --- a/chatmastermind/utils.py +++ b/chatmastermind/utils.py @@ -35,13 +35,16 @@ def append_message(chat: List[Dict[str, str]], def message_to_chat(message: Dict[str, str], chat: List[Dict[str, str]], - with_tags: bool = False + with_tags: bool = False, + with_file: bool = False ) -> None: append_message(chat, 'user', message['question']) append_message(chat, 'assistant', message['answer']) if with_tags: tags = ", ".join(message['tags']) append_message(chat, 'tags', tags) + if with_file: + append_message(chat, 'file', message['file']) def display_source_code(content: str) -> None: