diff --git a/lib/openai/talk.py b/lib/openai/talk.py index 386c306..159c347 100644 --- a/lib/openai/talk.py +++ b/lib/openai/talk.py @@ -10,12 +10,14 @@ def func_talk(response: ChatCompletion): if function_name == 'talk': function_arguments = function_call.arguments try: - j = json.loads(function_arguments) - if 'message' in j.get('message'): - print(response) + j = json.loads(escape_json_string(function_arguments)) return j.get('message') except json.decoder.JSONDecodeError: # Sometimes the AI doesn't do JSON. return function_arguments else: print('THE AI DID NOT CALL A FUNCTION IN TALK:', response) + + +def escape_json_string(s): + return s.replace("\\", "\\\\") diff --git a/run.py b/run.py index ddddeae..865dc8f 100755 --- a/run.py +++ b/run.py @@ -37,7 +37,11 @@ def main(): print(colored(f'System Management Intelligence Interface', 'green', attrs=['bold']) + ' ' + colored(temp_name, 'green', attrs=['bold', 'underline']) + colored(' on ', 'green', attrs=['bold']) + colored(socket.gethostname(), 'green', attrs=['bold', 'underline']) + '\n') while True: - next_input = str(input('> ')) + try: + next_input = str(input('> ')) + except EOFError: + print('Exit') + sys.exit(0) print('') context.append({'role': 'user', 'content': next_input}) @@ -49,7 +53,7 @@ def main(): temp_context.append({'role': 'system', 'content': 'Run another command or call "talk" to communicate with the user.'}) response = client.chat.completions.create( - model="gpt-4", + model="gpt-4-1106-preview", # TODO: config messages=temp_context, functions=function_description, temperature=0.7 @@ -65,10 +69,11 @@ def main(): print(colored(response_text, 'blue') + '\n') break elif function_name == 'end_chat': + # TODO: add a config arg to control whether or not the AI is allowed to do this. print(colored('The AI has terminated the connection.', 'red', attrs=['bold'])) sys.exit(1) - print(f'Executing {function_name}("{function_arguments}")' + '\n') + print(f'{function_name}("{function_arguments}")' + '\n') if function_name != 'run_bash': context.append({'role': 'system', 'content': f'"{function_name}" is not a valid function.'})