added option '-w' to print chat history with tags

This commit is contained in:
juk0de 2023-08-04 18:15:04 +02:00
parent bcfb41917a
commit b57d78a875
4 changed files with 13 additions and 6 deletions

View File

@ -28,7 +28,7 @@ pip install .
## Usage ## Usage
```bash ```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 ...]]] 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]
``` ```
### Arguments ### Arguments
@ -37,6 +37,7 @@ cmm [-h] [-p PRINT | -q QUESTION | -D | -d] [-c CONFIG] [-m MAX_TOKENS] [-T TEMP
- `-q`, `--question`: Question to ask. - `-q`, `--question`: Question to ask.
- `-D`, `--chat-dump`: Print chat as a Python structure. - `-D`, `--chat-dump`: Print chat as a Python structure.
- `-d`, `--chat`: Print chat as readable text. - `-d`, `--chat`: Print chat as readable text.
- `-w`, `--with-tags`: Print chat with tags.
- `-c`, `--config`: Config file name (defaults to `.config.yaml`). - `-c`, `--config`: Config file name (defaults to `.config.yaml`).
- `-m`, `--max-tokens`: Max tokens to use. - `-m`, `--max-tokens`: Max tokens to use.
- `-T`, `--temperature`: Temperature to use. - `-T`, `--temperature`: Temperature to use.

View File

@ -55,7 +55,7 @@ def process_and_display_chat(args: argparse.Namespace,
question_parts.append(f"```\n{r.read().strip()}\n```") question_parts.append(f"```\n{r.read().strip()}\n```")
full_question = '\n\n'.join(question_parts) full_question = '\n\n'.join(question_parts)
chat = create_chat(full_question, tags, extags, config) chat = create_chat(full_question, tags, extags, config, args.with_tags)
display_chat(chat, dump, args.only_source_code) display_chat(chat, dump, args.only_source_code)
return chat, full_question, tags return chat, full_question, tags
@ -94,6 +94,7 @@ def create_parser() -> argparse.ArgumentParser:
parser.add_argument('-n', '--number', help='Number of answers to produce', type=int, default=1) 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', '--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('-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')
tags_arg = parser.add_argument('-t', '--tags', nargs='*', help='List of tag names', metavar='TAGS') tags_arg = parser.add_argument('-t', '--tags', nargs='*', help='List of tag names', metavar='TAGS')
tags_arg.completer = tags_completer # type: ignore 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 = parser.add_argument('-e', '--extags', nargs='*', help='List of tag names to exclude', metavar='EXTAGS')

View File

@ -62,7 +62,8 @@ def save_answers(question: str,
def create_chat(question: Optional[str], def create_chat(question: Optional[str],
tags: Optional[List[str]], tags: Optional[List[str]],
extags: Optional[List[str]], extags: Optional[List[str]],
config: Dict[str, Any] config: Dict[str, Any],
with_tags: bool = False
) -> List[Dict[str, str]]: ) -> List[Dict[str, str]]:
chat: List[Dict[str, str]] = [] chat: List[Dict[str, str]] = []
append_message(chat, 'system', config['system'].strip()) append_message(chat, 'system', config['system'].strip())
@ -80,7 +81,7 @@ def create_chat(question: Optional[str],
extags_do_not_match = \ extags_do_not_match = \
not extags or not data_tags.intersection(extags) not extags or not data_tags.intersection(extags)
if tags_match and extags_do_not_match: if tags_match and extags_do_not_match:
message_to_chat(data, chat) message_to_chat(data, chat, with_tags)
if question: if question:
append_message(chat, 'user', question) append_message(chat, 'user', question)
return chat return chat

View File

@ -34,10 +34,14 @@ def append_message(chat: List[Dict[str, str]],
def message_to_chat(message: Dict[str, str], def message_to_chat(message: Dict[str, str],
chat: List[Dict[str, str]] chat: List[Dict[str, str]],
with_tags: bool = False
) -> None: ) -> None:
append_message(chat, 'user', message['question']) append_message(chat, 'user', message['question'])
append_message(chat, 'assistant', message['answer']) append_message(chat, 'assistant', message['answer'])
if with_tags:
tags = ", ".join(message['tags'])
append_message(chat, 'tags', tags)
def display_source_code(content: str) -> None: def display_source_code(content: str) -> None: