From f49142f04c795d47a86d69487cf5a8bd3c5527dd Mon Sep 17 00:00:00 2001 From: Hayden Johnson Date: Tue, 1 Oct 2024 13:25:30 -0700 Subject: [PATCH] Add /clipboard command clipboard contents can be added as context in the conversation now. --- assistant.py | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/assistant.py b/assistant.py index 08e0db0..cfa2465 100755 --- a/assistant.py +++ b/assistant.py @@ -48,6 +48,10 @@ def parse_commands(text): global history history = [ system_prompt ] return True + case '/clipboard': + context_query = '\n\nThe following is context provided by the user:\n' + context_query += get_string_from_clipboard() + '\n' + return text.split('/clipboard ')[1] + context_query case '/exit': exit() return False @@ -124,6 +128,12 @@ def copy_string_to_clipboard(string): except: return +def get_string_from_clipboard(): + try: + result = pyperclip.paste() + except: + result = '' + return result code_history = [] @@ -176,18 +186,22 @@ def chat(message, stream=True): def chat2(args, user_input, stream=True): global conversation - if parse_commands(user_input): - result = '' - elif args.reflect: + command_result = parse_commands(user_input) + if command_result: + if type(command_result) == bool: + return '' + elif type(command_result) == str: # sometimes I want to change the user prompt with a command + user_input = command_result + + if args.reflect: print('assistant: ', end='') result = reflection_mode(user_input, stream) else: print('assistant: ', end='') result = chat(user_input, stream) - if result != '': - conversation += 'user: ' + user_input + '\n' - conversation += 'assistant: ' + result + '\n' + conversation += 'user: ' + user_input + '\n' + conversation += 'assistant: ' + result + '\n' return result def highlightify_text(full_text):