From 4f2edb015f5a4d085b090d95f4931f1ee1d18a8d Mon Sep 17 00:00:00 2001 From: Zvonimir Sabljic Date: Tue, 1 Aug 2023 08:46:29 +0200 Subject: [PATCH] Refactor so we use array_of_objects_to_string + added debugging file and AgentConvo method to copy js code to clipboard so it can be pasted into OpenAI playground for easier debugging --- euclid/const/convert_to_playground_convo.js | 34 +++++++++++++++++++++ euclid/helpers/AgentConvo.py | 12 ++++++-- euclid/utils/utils.py | 6 ++-- 3 files changed, 48 insertions(+), 4 deletions(-) create mode 100644 euclid/const/convert_to_playground_convo.js diff --git a/euclid/const/convert_to_playground_convo.js b/euclid/const/convert_to_playground_convo.js new file mode 100644 index 0000000..2fa1b09 --- /dev/null +++ b/euclid/const/convert_to_playground_convo.js @@ -0,0 +1,34 @@ +let messages = {{messages}} + +function sleep(ms) { + return new Promise(resolve => setTimeout(resolve, ms)); +} + +async function fill_playground(messages) { + let system_messages = messages.filter(msg => msg.role === 'system'); + if (system_messages.length > 0) { + let system_message_textarea = document.querySelector('.chat-pg-instructions').querySelector('textarea'); + system_message_textarea.focus(); + document.execCommand("insertText", false, system_messages[0].content); + await sleep(100); + } + + let other_messages = messages.filter(msg => msg.role !== 'system'); + + for (let i = 0; i < other_messages.length - 1; i++) { + document.querySelector('.add-message').click() + await sleep(100); + } + + for (let i = 0; i < other_messages.length; i++) { + let all_elements = document.querySelectorAll('.text-input-with-focus'); + let last_user_document = all_elements[i]; + + textarea_to_fill = last_user_document.querySelector('textarea'); + textarea_to_fill.focus(); + document.execCommand("insertText", false, other_messages[i].content); + await sleep(100); + } +} + +fill_playground(messages) \ No newline at end of file diff --git a/euclid/helpers/AgentConvo.py b/euclid/helpers/AgentConvo.py index c3a05dc..2c87577 100644 --- a/euclid/helpers/AgentConvo.py +++ b/euclid/helpers/AgentConvo.py @@ -1,3 +1,5 @@ +import subprocess +from utils.utils import array_of_objects_to_string from utils.llm_connection import get_prompt, create_gpt_chat_completion from utils.utils import get_sys_message, find_role_from_step, capitalize_first_word_with_underscores from logger.logger import logger @@ -33,7 +35,7 @@ class AgentConvo: if isinstance(response, list): if isinstance(response[0], dict): string_response = [ - f'#{i + 1}\n' + '\n'.join([f'{key}: {value}' for key, value in d.items()]) + f'#{i + 1}\n' + array_of_objects_to_string(d) for i, d in enumerate(response) ] else: @@ -70,4 +72,10 @@ class AgentConvo: print_msg = capitalize_first_word_with_underscores(self.high_level_step) print(colored(f"{print_msg}:\n", "green")) print(f"{content}\n") - logger.info(f"{print_msg}: {content}\n") \ No newline at end of file + logger.info(f"{print_msg}: {content}\n") + + def to_playground(self, path): + with open('const/convert_to_playground_convo.js', 'r', encoding='utf-8') as file: + content = file.read() + process = subprocess.Popen('pbcopy', stdin=subprocess.PIPE) + process.communicate(content.replace('{{messages}}', str(self.messages)).encode('utf-8')) \ No newline at end of file diff --git a/euclid/utils/utils.py b/euclid/utils/utils.py index db32465..9284927 100644 --- a/euclid/utils/utils.py +++ b/euclid/utils/utils.py @@ -133,8 +133,7 @@ def get_os_info(): os_info["Mac Version"] = platform.mac_ver()[0] # Convert the dictionary to a readable text format - output = "\n".join(f"{key}: {value}" for key, value in os_info.items()) - return output + return array_of_objects_to_string(os_info) def execute_step(matching_step, current_step): @@ -146,3 +145,6 @@ def execute_step(matching_step, current_step): def generate_app_data(args): return {'app_id': args['app_id'], 'app_type': args['app_type']} + +def array_of_objects_to_string(array): + return '\n'.join([f'{key}: {value}' for key, value in array.items()]) \ No newline at end of file