question_cmd: implemented repetition of multiple messages

This commit is contained in:
juk0de 2023-09-21 07:48:24 +02:00
parent 4538624247
commit 42723d40ed

View File

@ -105,6 +105,25 @@ def make_request(ai: AI, chat: ChatDB, message: Message, args: argparse.Namespac
print(response.tokens) print(response.tokens)
def repeat_messages(messages: list[Message], ai: AI, chat: ChatDB, args: argparse.Namespace) -> None:
"""
Repeat the given messages using the given arguments.
"""
for msg in messages:
print(f"--------- Repeating message '{msg.msg_id()}': ---------")
# overwrite the latest message if requested or empty
# -> but not if it's in the DB!
if ((msg.answer is None or args.overwrite is True)
and (not chat.msg_in_db(msg))): # noqa: W503
msg.clear_answer()
make_request(ai, chat, msg, args)
# otherwise create a new one
else:
args.ask = [msg.question]
message = create_message(chat, args)
make_request(ai, chat, message, args)
def question_cmd(args: argparse.Namespace, config: Config) -> None: def question_cmd(args: argparse.Namespace, config: Config) -> None:
""" """
Handler for the 'question' command. Handler for the 'question' command.
@ -120,7 +139,6 @@ def question_cmd(args: argparse.Namespace, config: Config) -> None:
message = create_message(chat, args) message = create_message(chat, args)
if args.create: if args.create:
return return
# create the correct AI instance # create the correct AI instance
ai: AI = create_ai(args, config) ai: AI = create_ai(args, config)
@ -129,22 +147,18 @@ def question_cmd(args: argparse.Namespace, config: Config) -> None:
make_request(ai, chat, message, args) make_request(ai, chat, message, args)
# === REPEAT === # === REPEAT ===
elif args.repeat is not None: elif args.repeat is not None:
lmessage = chat.msg_latest(loc='cache') repeat_msgs: list[Message] = []
if lmessage is None: # repeat latest message
print("No message found to repeat!") if len(args.repeat) == 0:
sys.exit(1) lmessage = chat.msg_latest(loc='cache')
if lmessage is None:
print("No message found to repeat!")
sys.exit(1)
repeat_msgs.append(lmessage)
# repeat given message(s)
else: else:
print(f"Repeating message '{lmessage.msg_id()}':") repeat_msgs = chat.msg_find(args.repeat, loc='disk')
# overwrite the latest message if requested or empty repeat_messages(repeat_msgs, ai, chat, args)
if lmessage.answer is None or args.overwrite is True:
lmessage.clear_answer()
make_request(ai, chat, lmessage, args)
# otherwise create a new one
else:
args.ask = [lmessage.question]
message = create_message(chat, args)
make_request(ai, chat, message, args)
# === PROCESS === # === PROCESS ===
elif args.process is not None: elif args.process is not None:
# TODO: process either all questions without an # TODO: process either all questions without an