Added option '-W' to print chat history with filenames

This commit is contained in:
2023-08-05 08:09:35 +02:00
parent b57d78a875
commit 32bd17594b
4 changed files with 20 additions and 12 deletions
+6 -4
View File
@@ -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')
+5 -3
View File
@@ -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
+4 -1
View File
@@ -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: