aboutsummaryrefslogtreecommitdiff
path: root/src/gpt_chat_cli
diff options
context:
space:
mode:
authorflu0r1ne <flu0r1ne@flu0r1ne.net>2023-05-05 03:00:02 -0500
committerflu0r1ne <flu0r1ne@flu0r1ne.net>2023-05-05 03:00:02 -0500
commit21ecdc25389ca9ba770062f1cc9b84091a2bc6db (patch)
tree8a78fae000ff3019560bfa596fdfa2a761fca82c /src/gpt_chat_cli
parentf6cd64a7870dafa80cc81e52ad9a8f7fe6529154 (diff)
downloadgpt-chat-cli-21ecdc25389ca9ba770062f1cc9b84091a2bc6db.tar.xz
gpt-chat-cli-21ecdc25389ca9ba770062f1cc9b84091a2bc6db.zip
Refactor command structure, add validation for interactive sessions
Diffstat (limited to 'src/gpt_chat_cli')
-rw-r--r--src/gpt_chat_cli/argparsing.py31
-rw-r--r--src/gpt_chat_cli/gcli.py40
2 files changed, 51 insertions, 20 deletions
diff --git a/src/gpt_chat_cli/argparsing.py b/src/gpt_chat_cli/argparsing.py
index 4597e84..e9183a9 100644
--- a/src/gpt_chat_cli/argparsing.py
+++ b/src/gpt_chat_cli/argparsing.py
@@ -19,7 +19,7 @@ def die_validation_err(err : str):
print(err, file=sys.stderr)
sys.exit(1)
-def validate_args(args: argparse.Namespace) -> None:
+def validate_args(args: argparse.Namespace, debug : bool = False) -> None:
if not 0 <= args.temperature <= 2:
die_validation_err("Temperature must be between 0 and 2.")
@@ -38,6 +38,16 @@ def validate_args(args: argparse.Namespace) -> None:
if args.n_completions < 1:
die_validation_err("Number of completions must be greater than or equal to 1.")
+ if args.interactive and args.n_completions != 1:
+ die_validation_err("Only a single completion can be used in interactive mode")
+
+ if debug and args.interactive:
+
+ if args.interactive and (
+ args.save_response_to_file or args.load_response_from_file
+ ):
+ die_validation_err("Save and load operations cannot be used in interactive mode")
+
@dataclass
class CompletionArguments:
model: str
@@ -66,6 +76,7 @@ class Arguments:
display_args: DisplayArguments
version: bool
list_models: bool
+ interactive: bool
debug_args: Optional[DebugArguments] = None
def split_arguments(args: argparse.Namespace) -> Arguments:
@@ -96,6 +107,7 @@ def split_arguments(args: argparse.Namespace) -> Arguments:
debug_args=debug_args,
version=args.version,
list_models=args.list_models,
+ interactive=args.interactive
)
def parse_args() -> Arguments:
@@ -217,12 +229,19 @@ def parse_args() -> Arguments:
)
parser.add_argument(
+ "-i",
+ "--interactive",
+ action="store_true",
+ help="Start an interactive session"
+ )
+
+ parser.add_argument(
"message",
type=str,
nargs='?',
help=(
- "The contents of the message. When used in chat mode, this is the initial "
- "message if provided."
+ "The contents of the message. When in a interactive session, this is "
+ " the initial prompt provided."
),
)
@@ -268,10 +287,14 @@ def parse_args() -> Arguments:
else:
args.adornments = AutoDetectedOption.OFF
+ if args.message is None:
+ if sys.stdin.isatty():
+ args.interactive = True
+
if not debug:
args.load_response_from_file = None
args.save_response_to_file = None
- validate_args(args)
+ validate_args(args, debug=debug)
return split_arguments(args)
diff --git a/src/gpt_chat_cli/gcli.py b/src/gpt_chat_cli/gcli.py
index a9d36e4..2d40cf2 100644
--- a/src/gpt_chat_cli/gcli.py
+++ b/src/gpt_chat_cli/gcli.py
@@ -132,22 +132,21 @@ def print_streamed_response(
else:
print(end='\n', flush=True)
-def main():
- args = parse_args()
-
- if args.version:
- print(f'version {VERSION}')
- sys.exit(0)
+def cmd_version():
+ print(f'version {VERSION}')
- if args.list_models:
+def cmd_list_models():
+ for model in list_models():
+ print(model)
- for model in list_models():
- print(model)
+def cmd_interactive(args : Arguments):
+ COLOR_CODE = get_color_codes(no_color = not args.display_args.color)
- sys.exit(0)
+ print(f'GPT Chat CLI {VERSION}')
+ print(f'[{COLOR_CODE.WHITE}#{COLOR_CODE.RESET}]', end=' ', flush=True)
+def cmd_singleton(args: Arguments):
completion_args = args.completion_args
- COLOR_CODE = get_color_codes(no_color = not args.display_args.color)
debug_args : DebugArguments = args.debug_args
@@ -157,11 +156,7 @@ def main():
elif debug_args.load_response_from_file:
completion_args, completion = load_response_and_arguments(args)
else:
- if args.completion_args.message is None:
- if sys.stdin.isatty():
- print(f'GPT Chat CLI {VERSION}')
- print(f'[{COLOR_CODE.WHITE}#{COLOR_CODE.RESET}]', end=' ', flush=True)
-
+ if completion_args.message is None:
completion_args.message = sys.stdin.read()
completion = create_chat_completion_from_args(completion_args)
@@ -172,5 +167,18 @@ def main():
completion_args.n_completions
)
+
+def main():
+ args = parse_args()
+
+ if args.version:
+ cmd_version()
+ elif args.list_models:
+ cmd_list_models()
+ elif args.interactive:
+ cmd_interactive(args)
+ else:
+ cmd_singleton(args)
+
if __name__ == "__main__":
main()