From fe4f739eb31f0ce1447b6a2f371743f23ccfdf13 Mon Sep 17 00:00:00 2001 From: Hayden Johnson Date: Thu, 10 Apr 2025 20:21:17 -0700 Subject: [PATCH] feat: improve input handling and add context length option This commit introduces several improvements to the input handling process: - Implemented `improved_input()` to handle multi-line input and prevent accidental sending of pasted content. The user signals completion by pressing Ctrl+D or entering two consecutive empty lines. - Added a new command-line argument `--context` to allow users to specify the context length for the model. - Updated the help message to reflect the new argument. --- assistant.py | 66 ++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 59 insertions(+), 7 deletions(-) diff --git a/assistant.py b/assistant.py index 8062309..ec41920 100755 --- a/assistant.py +++ b/assistant.py @@ -15,6 +15,7 @@ server = 'localhost:11434' model = 'gemma3:12b' reasoning_model='deepseek-r1:14b' temp = 0.2 +num_ctx = 4096 pattern = r'```[a-z]*\n[\s\S]*?\n```' @@ -161,7 +162,7 @@ def chat(message, stream=True): history.append({"role": "user", "content": message}) completion = client.chat( model=model, - options={"temperature":temp, "num_ctx":4096}, + options={"temperature":temp, "num_ctx":num_ctx}, messages=history, stream=stream ) @@ -256,10 +257,14 @@ def parse_args(): parser.add_argument('--model', '-m', nargs='?', const=True, default=False, help='Specify model') # Add the --temp (-t) argument parser.add_argument('--temp', '-t', nargs='?', const=True, default=False, help='Specify temperature') + # Add the --context + parser.add_argument('--context', nargs='?', const=True, default=False, help='Specify temperature') # Add the --host argument parser.add_argument('--host', nargs='?', const=True, default=False, help='Specify host of ollama server') # Add the --reflect argument parser.add_argument('--reasoning', '-r', action='store_true', help='Use the default reasoning model deepseek-r1:14b') + # Add the --new argument + parser.add_argument('--new', '-n', action='store_true', help='Start a chat with a fresh history') # Parse the arguments return parser.parse_args() @@ -328,6 +333,36 @@ def handle_piped_input(args): if args.copy and len(blocks): copy_string_to_clipboard(blocks[0]) +def improved_input(): + """ + Handles multi-line input and prevents accidental sending of pasted content. + Returns the complete input text when the user indicates they're done. + """ + lines = [] + while True: + try: + line = sys.stdin.readline() + if not line: + break # EOF + if line.strip() == '': + continue # ignore empty lines + lines.append(line) + + # Check for termination signal (Ctrl+D or two consecutive empty lines) + if len(lines) > 1 and lines[-2] == '\n' and lines[-1] == '\n': + break + + except KeyboardInterrupt: + print("\nUser aborted input") + return None + except Exception as e: + print(f"Error reading input: {e}") + return None + + # Join the lines and strip any trailing newlines + full_input = ''.join(lines).rstrip('\n') + return full_input + def handle_non_piped_input(args): if args.shell: arg_shell(args) @@ -336,18 +371,28 @@ def handle_non_piped_input(args): user_input = arg_follow_up(args) result = chat2(args, user_input) exit() + global history history = load_history(history_path) + + print("\033[91massistant\033[0m: Type your message (press Ctrl+D to send):") while True: try: - user_input = input('> ') - except (EOFError, KeyboardInterrupt): - print() - exit() - else: - result = chat2(args, user_input) + full_input = improved_input() + if full_input is None: + break # User aborted + if full_input.strip() == '': + continue # Skip empty messages + + result = chat2(args, full_input) save_history(history, history_path) + except (EOFError, KeyboardInterrupt): + print("\nExiting...") + break + except Exception as e: + print(f"Error: {e}") + client = None def main(): @@ -363,6 +408,13 @@ def main(): if args.temp: global temp temp = float(args.temp) + if args.context: + global num_ctx + num_ctx = float(args.context) + if args.new: + global history + history = [system_prompt] + save_history(history, history_path) if not sys.stdin.isatty(): handle_piped_input(args) else: