From b19eb74874a91b0c8e372ffb37d34e833de07cad Mon Sep 17 00:00:00 2001 From: Alrik Olson <10505065+AlrikOlson@users.noreply.github.com> Date: Tue, 11 Apr 2023 09:09:59 -0700 Subject: [PATCH 1/7] Refactor the seed prompt to be generated programmatically This removes the tedium of having to re-number every numbered item in the prompt.txt if you want to add/remove commands. --- scripts/ai_config.py | 3 +- scripts/data.py | 18 ---------- scripts/data/prompt.txt | 63 --------------------------------- scripts/main.py | 3 +- scripts/prompt.py | 51 +++++++++++++++++++++++++++ scripts/promptgenerator.py | 71 ++++++++++++++++++++++++++++++++++++++ 6 files changed, 126 insertions(+), 83 deletions(-) delete mode 100644 scripts/data.py delete mode 100644 scripts/data/prompt.txt create mode 100644 scripts/prompt.py create mode 100644 scripts/promptgenerator.py diff --git a/scripts/ai_config.py b/scripts/ai_config.py index 1d5832c18231..332c7f06af28 100644 --- a/scripts/ai_config.py +++ b/scripts/ai_config.py @@ -1,6 +1,7 @@ import yaml import data import os +from prompt import get_prompt class AIConfig: """ @@ -90,6 +91,6 @@ def construct_full_prompt(self) -> str: for i, goal in enumerate(self.ai_goals): full_prompt += f"{i+1}. {goal}\n" - full_prompt += f"\n\n{data.load_prompt()}" + full_prompt += f"\n\n{get_prompt()}" return full_prompt diff --git a/scripts/data.py b/scripts/data.py deleted file mode 100644 index f80c2875d8ef..000000000000 --- a/scripts/data.py +++ /dev/null @@ -1,18 +0,0 @@ -import os -from pathlib import Path - -def load_prompt(): - """Load the prompt from data/prompt.txt""" - try: - # get directory of this file: - file_dir = Path(__file__).parent - prompt_file_path = file_dir / "data" / "prompt.txt" - - # Load the prompt from data/prompt.txt - with open(prompt_file_path, "r") as prompt_file: - prompt = prompt_file.read() - - return prompt - except FileNotFoundError: - print("Error: Prompt file not found", flush=True) - return "" diff --git a/scripts/data/prompt.txt b/scripts/data/prompt.txt deleted file mode 100644 index fc68f3ae0d28..000000000000 --- a/scripts/data/prompt.txt +++ /dev/null @@ -1,63 +0,0 @@ -CONSTRAINTS: - -1. ~4000 word limit for short term memory. Your short term memory is short, so immediately save important information to files. -2. If you are unsure how you previously did something or want to recall past events, thinking about similar events will help you remember. -3. No user assistance -4. Exclusively use the commands listed in double quotes e.g. "command name" - -COMMANDS: - -1. Google Search: "google", args: "input": "" -5. Browse Website: "browse_website", args: "url": "", "question": "" -6. Start GPT Agent: "start_agent", args: "name": "", "task": "", "prompt": "" -7. Message GPT Agent: "message_agent", args: "key": "", "message": "" -8. List GPT Agents: "list_agents", args: "" -9. Delete GPT Agent: "delete_agent", args: "key": "" -10. Write to file: "write_to_file", args: "file": "", "text": "" -11. Read file: "read_file", args: "file": "" -12. Append to file: "append_to_file", args: "file": "", "text": "" -13. Delete file: "delete_file", args: "file": "" -14. Search Files: "search_files", args: "directory": "" -15. Evaluate Code: "evaluate_code", args: "code": "" -16. Get Improved Code: "improve_code", args: "suggestions": "", "code": "" -17. Write Tests: "write_tests", args: "code": "", "focus": "" -18. Execute Python File: "execute_python_file", args: "file": "" -19. Task Complete (Shutdown): "task_complete", args: "reason": "" -20. Generate Image: "generate_image", args: "prompt": "" -21. Do Nothing: "do_nothing", args: "" - -RESOURCES: - -1. Internet access for searches and information gathering. -2. Long Term memory management. -3. GPT-3.5 powered Agents for delegation of simple tasks. -4. File output. - -PERFORMANCE EVALUATION: - -1. Continuously review and analyze your actions to ensure you are performing to the best of your abilities. -2. Constructively self-criticize your big-picture behavior constantly. -3. Reflect on past decisions and strategies to refine your approach. -4. Every command has a cost, so be smart and efficient. Aim to complete tasks in the least number of steps. - -You should only respond in JSON format as described below - -RESPONSE FORMAT: -{ - "thoughts": - { - "text": "thought", - "reasoning": "reasoning", - "plan": "- short bulleted\n- list that conveys\n- long-term plan", - "criticism": "constructive self-criticism", - "speak": "thoughts summary to say to user" - }, - "command": { - "name": "command name", - "args":{ - "arg name": "value" - } - } -} - -Ensure the response can be parsed by Python json.loads diff --git a/scripts/main.py b/scripts/main.py index 4be0b2aa267e..a3d4f9dfbbce 100644 --- a/scripts/main.py +++ b/scripts/main.py @@ -18,6 +18,7 @@ import yaml import argparse import logging +from prompt import get_prompt cfg = Config() @@ -171,7 +172,7 @@ def load_variables(config_file="config.yaml"): with open(config_file, "w") as file: documents = yaml.dump(config, file) - prompt = data.load_prompt() + prompt = get_prompt() prompt_start = """Your decisions must always be made independently without seeking user assistance. Play to your strengths as an LLM and pursue simple strategies with no legal complications.""" # Construct full prompt diff --git a/scripts/prompt.py b/scripts/prompt.py new file mode 100644 index 000000000000..fd2a84a0f9e6 --- /dev/null +++ b/scripts/prompt.py @@ -0,0 +1,51 @@ +from promptgenerator import PromptGenerator + +def get_prompt(): + prompt_generator = PromptGenerator() + + # Add constraints + prompt_generator.add_constraint("~4000 word limit for short term memory. Your short term memory is short, so immediately save important information to files.") + prompt_generator.add_constraint("If you are unsure how you previously did something or want to recall past events, thinking about similar events will help you remember.") + prompt_generator.add_constraint("No user assistance") + prompt_generator.add_constraint('Exclusively use the commands listed in double quotes e.g. "command name"') + + # Add commands + commands = [ + ("Google Search", "google", {"input": ""}), + ("Browse Website", "browse_website", {"url": "", "question": ""}), + ("Start GPT Agent", "start_agent", {"name": "", "task": "", "prompt": ""}), + ("Message GPT Agent", "message_agent", {"key": "", "message": ""}), + ("List GPT Agents", "list_agents", {}), + ("Delete GPT Agent", "delete_agent", {"key": ""}), + ("Write to file", "write_to_file", {"file": "", "text": ""}), + ("Read file", "read_file", {"file": ""}), + ("Append to file", "append_to_file", {"file": "", "text": ""}), + ("Delete file", "delete_file", {"file": ""}), + ("Search Files", "search_files", {"directory": ""}), + ("Evaluate Code", "evaluate_code", {"code": ""}), + ("Get Improved Code", "improve_code", {"suggestions": "", "code": ""}), + ("Write Tests", "write_tests", {"code": "", "focus": ""}), + ("Execute Python File", "execute_python_file", {"file": ""}), + ("Task Complete (Shutdown)", "task_complete", {"reason": ""}), + ("Generate Image", "generate_image", {"prompt": ""}), + ("Do Nothing", "do_nothing", {}), + ] + + for command_label, command_name, args in commands: + prompt_generator.add_command(command_label, command_name, args) + + # Add resources + prompt_generator.add_resource("Internet access for searches and information gathering.") + prompt_generator.add_resource("Long Term memory management.") + prompt_generator.add_resource("GPT-3.5 powered Agents for delegation of simple tasks.") + prompt_generator.add_resource("File output.") + + # Add performance evaluation + prompt_generator.add_performance_evaluation("Continuously review and analyze your actions to ensure you are performing to the best of your abilities.") + prompt_generator.add_performance_evaluation("Constructively self-criticize your big-picture behavior constantly.") + prompt_generator.add_performance_evaluation("Reflect on past decisions and strategies to refine your approach.") + prompt_generator.add_performance_evaluation("Every command has a cost, so be smart and efficient. Aim to complete tasks in the least number of steps.") + + # Generate prompt string + prompt_string = prompt_generator.generate_prompt_string() + return prompt_string diff --git a/scripts/promptgenerator.py b/scripts/promptgenerator.py new file mode 100644 index 000000000000..1ae43aa71e3b --- /dev/null +++ b/scripts/promptgenerator.py @@ -0,0 +1,71 @@ +import json + + +class PromptGenerator: + def __init__(self): + self.constraints = [] + self.commands = [] + self.resources = [] + self.performance_evaluation = [] + self.response_format = { + "thoughts": { + "text": "thought", + "reasoning": "reasoning", + "plan": "- short bulleted\n- list that conveys\n- long-term plan", + "criticism": "constructive self-criticism", + "speak": "thoughts summary to say to user" + }, + "command": { + "name": "command name", + "args": { + "arg name": "value" + } + } + } + + def add_constraint(self, constraint): + self.constraints.append(constraint) + + # {CommandLabel}: "{CommandName}", args: "{arg#Name}": "{arg#Prompt}" + def add_command(self, command_label, command_name, args=None): + if args is None: + args = {} + + command_args = {arg_key: arg_value for arg_key, arg_value in args.items()} + + command = { + "label": command_label, + "name": command_name, + "args": command_args, + } + + self.commands.append(command) + + def _generate_command_string(self, command): + args_string = ', '.join(f'"{key}": "{value}"' for key, value in command['args'].items()) + return f'{command["label"]}: "{command["name"]}", args: {args_string}' + + def add_resource(self, resource): + self.resources.append(resource) + + def add_performance_evaluation(self, evaluation): + self.performance_evaluation.append(evaluation) + + + def _generate_numbered_list(self, items, item_type='list'): + if item_type == 'command': + return "\n".join(f"{i+1}. {self._generate_command_string(item)}" for i, item in enumerate(items)) + else: + return "\n".join(f"{i+1}. {item}" for i, item in enumerate(items)) + + def generate_prompt_string(self): + formatted_response_format = json.dumps(self.response_format, indent=4) + prompt_string = ( + f"Constraints:\n{self._generate_numbered_list(self.constraints)}\n\n" + f"Commands:\n{self._generate_numbered_list(self.commands, item_type='command')}\n\n" + f"Resources:\n{self._generate_numbered_list(self.resources)}\n\n" + f"Performance Evaluation:\n{self._generate_numbered_list(self.performance_evaluation)}\n\n" + f"You should only respond in JSON format as described below \nResponse Format: \n{formatted_response_format} \nEnsure the response can be parsed by Python json.loads" + ) + + return prompt_string From fd1cfd2eff6bcb46af6e7728fa57dc980dde0e61 Mon Sep 17 00:00:00 2001 From: Alrik Olson <10505065+AlrikOlson@users.noreply.github.com> Date: Tue, 11 Apr 2023 09:15:45 -0700 Subject: [PATCH 2/7] Add docs and format code --- scripts/prompt.py | 69 +++++++++++++++++++++++++------------ scripts/promptgenerator.py | 70 ++++++++++++++++++++++++++++++++++---- 2 files changed, 112 insertions(+), 27 deletions(-) diff --git a/scripts/prompt.py b/scripts/prompt.py index fd2a84a0f9e6..e499a5f699fc 100644 --- a/scripts/prompt.py +++ b/scripts/prompt.py @@ -1,51 +1,78 @@ from promptgenerator import PromptGenerator + def get_prompt(): + """ + This function generates a prompt string that includes various constraints, commands, resources, and performance evaluations. + + Returns: + str: The generated prompt string. + """ + + # Initialize the PromptGenerator object prompt_generator = PromptGenerator() - # Add constraints - prompt_generator.add_constraint("~4000 word limit for short term memory. Your short term memory is short, so immediately save important information to files.") - prompt_generator.add_constraint("If you are unsure how you previously did something or want to recall past events, thinking about similar events will help you remember.") + # Add constraints to the PromptGenerator object + prompt_generator.add_constraint( + "~4000 word limit for short term memory. Your short term memory is short, so immediately save important information to files.") + prompt_generator.add_constraint( + "If you are unsure how you previously did something or want to recall past events, thinking about similar events will help you remember.") prompt_generator.add_constraint("No user assistance") - prompt_generator.add_constraint('Exclusively use the commands listed in double quotes e.g. "command name"') + prompt_generator.add_constraint( + 'Exclusively use the commands listed in double quotes e.g. "command name"') - # Add commands + # Define the command list commands = [ ("Google Search", "google", {"input": ""}), - ("Browse Website", "browse_website", {"url": "", "question": ""}), - ("Start GPT Agent", "start_agent", {"name": "", "task": "", "prompt": ""}), - ("Message GPT Agent", "message_agent", {"key": "", "message": ""}), + ("Browse Website", "browse_website", { + "url": "", "question": ""}), + ("Start GPT Agent", "start_agent", { + "name": "", "task": "", "prompt": ""}), + ("Message GPT Agent", "message_agent", { + "key": "", "message": ""}), ("List GPT Agents", "list_agents", {}), ("Delete GPT Agent", "delete_agent", {"key": ""}), - ("Write to file", "write_to_file", {"file": "", "text": ""}), + ("Write to file", "write_to_file", { + "file": "", "text": ""}), ("Read file", "read_file", {"file": ""}), - ("Append to file", "append_to_file", {"file": "", "text": ""}), + ("Append to file", "append_to_file", { + "file": "", "text": ""}), ("Delete file", "delete_file", {"file": ""}), ("Search Files", "search_files", {"directory": ""}), ("Evaluate Code", "evaluate_code", {"code": ""}), - ("Get Improved Code", "improve_code", {"suggestions": "", "code": ""}), - ("Write Tests", "write_tests", {"code": "", "focus": ""}), + ("Get Improved Code", "improve_code", { + "suggestions": "", "code": ""}), + ("Write Tests", "write_tests", { + "code": "", "focus": ""}), ("Execute Python File", "execute_python_file", {"file": ""}), ("Task Complete (Shutdown)", "task_complete", {"reason": ""}), ("Generate Image", "generate_image", {"prompt": ""}), ("Do Nothing", "do_nothing", {}), ] + # Add commands to the PromptGenerator object for command_label, command_name, args in commands: prompt_generator.add_command(command_label, command_name, args) - # Add resources - prompt_generator.add_resource("Internet access for searches and information gathering.") + # Add resources to the PromptGenerator object + prompt_generator.add_resource( + "Internet access for searches and information gathering.") prompt_generator.add_resource("Long Term memory management.") - prompt_generator.add_resource("GPT-3.5 powered Agents for delegation of simple tasks.") + prompt_generator.add_resource( + "GPT-3.5 powered Agents for delegation of simple tasks.") prompt_generator.add_resource("File output.") - # Add performance evaluation - prompt_generator.add_performance_evaluation("Continuously review and analyze your actions to ensure you are performing to the best of your abilities.") - prompt_generator.add_performance_evaluation("Constructively self-criticize your big-picture behavior constantly.") - prompt_generator.add_performance_evaluation("Reflect on past decisions and strategies to refine your approach.") - prompt_generator.add_performance_evaluation("Every command has a cost, so be smart and efficient. Aim to complete tasks in the least number of steps.") + # Add performance evaluations to the PromptGenerator object + prompt_generator.add_performance_evaluation( + "Continuously review and analyze your actions to ensure you are performing to the best of your abilities.") + prompt_generator.add_performance_evaluation( + "Constructively self-criticize your big-picture behavior constantly.") + prompt_generator.add_performance_evaluation( + "Reflect on past decisions and strategies to refine your approach.") + prompt_generator.add_performance_evaluation( + "Every command has a cost, so be smart and efficient. Aim to complete tasks in the least number of steps.") - # Generate prompt string + # Generate the prompt string prompt_string = prompt_generator.generate_prompt_string() + return prompt_string diff --git a/scripts/promptgenerator.py b/scripts/promptgenerator.py index 1ae43aa71e3b..6cfd9bcd623f 100644 --- a/scripts/promptgenerator.py +++ b/scripts/promptgenerator.py @@ -2,7 +2,14 @@ class PromptGenerator: + """ + A class for generating custom prompt strings based on constraints, commands, resources, and performance evaluations. + """ + def __init__(self): + """ + Initialize the PromptGenerator object with empty lists of constraints, commands, resources, and performance evaluations. + """ self.constraints = [] self.commands = [] self.resources = [] @@ -24,14 +31,28 @@ def __init__(self): } def add_constraint(self, constraint): + """ + Add a constraint to the constraints list. + + Args: + constraint (str): The constraint to be added. + """ self.constraints.append(constraint) - # {CommandLabel}: "{CommandName}", args: "{arg#Name}": "{arg#Prompt}" def add_command(self, command_label, command_name, args=None): + """ + Add a command to the commands list with a label, name, and optional arguments. + + Args: + command_label (str): The label of the command. + command_name (str): The name of the command. + args (dict, optional): A dictionary containing argument names and their values. Defaults to None. + """ if args is None: args = {} - - command_args = {arg_key: arg_value for arg_key, arg_value in args.items()} + + command_args = {arg_key: arg_value for arg_key, + arg_value in args.items()} command = { "label": command_label, @@ -42,23 +63,60 @@ def add_command(self, command_label, command_name, args=None): self.commands.append(command) def _generate_command_string(self, command): - args_string = ', '.join(f'"{key}": "{value}"' for key, value in command['args'].items()) + """ + Generate a formatted string representation of a command. + + Args: + command (dict): A dictionary containing command information. + + Returns: + str: The formatted command string. + """ + args_string = ', '.join( + f'"{key}": "{value}"' for key, value in command['args'].items()) return f'{command["label"]}: "{command["name"]}", args: {args_string}' - + def add_resource(self, resource): + """ + Add a resource to the resources list. + + Args: + resource (str): The resource to be added. + """ self.resources.append(resource) def add_performance_evaluation(self, evaluation): - self.performance_evaluation.append(evaluation) + """ + Add a performance evaluation item to the performance_evaluation list. + Args: + evaluation (str): The evaluation item to be added. + """ + self.performance_evaluation.append(evaluation) def _generate_numbered_list(self, items, item_type='list'): + """ + Generate a numbered list from given items based on the item_type. + + Args: + items (list): A list of items to be numbered. + item_type (str, optional): The type of items in the list. Defaults to 'list'. + + Returns: + str: The formatted numbered list. + """ if item_type == 'command': return "\n".join(f"{i+1}. {self._generate_command_string(item)}" for i, item in enumerate(items)) else: return "\n".join(f"{i+1}. {item}" for i, item in enumerate(items)) def generate_prompt_string(self): + """ + Generate a prompt string based on the constraints, commands, resources, and performance evaluations. + + Returns: + str: The generated prompt string. + """ formatted_response_format = json.dumps(self.response_format, indent=4) prompt_string = ( f"Constraints:\n{self._generate_numbered_list(self.constraints)}\n\n" From 72d4783a1d0e399972e16bdbcc2ac93e2c8b0f1d Mon Sep 17 00:00:00 2001 From: Alrik Olson <10505065+AlrikOlson@users.noreply.github.com> Date: Tue, 11 Apr 2023 09:21:20 -0700 Subject: [PATCH 3/7] formatting --- scripts/prompt.py | 53 ++++++++++++++++------------------------------- 1 file changed, 18 insertions(+), 35 deletions(-) diff --git a/scripts/prompt.py b/scripts/prompt.py index e499a5f699fc..bbdfa5ec6bd3 100644 --- a/scripts/prompt.py +++ b/scripts/prompt.py @@ -1,10 +1,9 @@ from promptgenerator import PromptGenerator - def get_prompt(): """ This function generates a prompt string that includes various constraints, commands, resources, and performance evaluations. - + Returns: str: The generated prompt string. """ @@ -13,37 +12,27 @@ def get_prompt(): prompt_generator = PromptGenerator() # Add constraints to the PromptGenerator object - prompt_generator.add_constraint( - "~4000 word limit for short term memory. Your short term memory is short, so immediately save important information to files.") - prompt_generator.add_constraint( - "If you are unsure how you previously did something or want to recall past events, thinking about similar events will help you remember.") + prompt_generator.add_constraint("~4000 word limit for short term memory. Your short term memory is short, so immediately save important information to files.") + prompt_generator.add_constraint("If you are unsure how you previously did something or want to recall past events, thinking about similar events will help you remember.") prompt_generator.add_constraint("No user assistance") - prompt_generator.add_constraint( - 'Exclusively use the commands listed in double quotes e.g. "command name"') + prompt_generator.add_constraint('Exclusively use the commands listed in double quotes e.g. "command name"') # Define the command list commands = [ ("Google Search", "google", {"input": ""}), - ("Browse Website", "browse_website", { - "url": "", "question": ""}), - ("Start GPT Agent", "start_agent", { - "name": "", "task": "", "prompt": ""}), - ("Message GPT Agent", "message_agent", { - "key": "", "message": ""}), + ("Browse Website", "browse_website", {"url": "", "question": ""}), + ("Start GPT Agent", "start_agent", {"name": "", "task": "", "prompt": ""}), + ("Message GPT Agent", "message_agent", {"key": "", "message": ""}), ("List GPT Agents", "list_agents", {}), ("Delete GPT Agent", "delete_agent", {"key": ""}), - ("Write to file", "write_to_file", { - "file": "", "text": ""}), + ("Write to file", "write_to_file", {"file": "", "text": ""}), ("Read file", "read_file", {"file": ""}), - ("Append to file", "append_to_file", { - "file": "", "text": ""}), + ("Append to file", "append_to_file", {"file": "", "text": ""}), ("Delete file", "delete_file", {"file": ""}), ("Search Files", "search_files", {"directory": ""}), ("Evaluate Code", "evaluate_code", {"code": ""}), - ("Get Improved Code", "improve_code", { - "suggestions": "", "code": ""}), - ("Write Tests", "write_tests", { - "code": "", "focus": ""}), + ("Get Improved Code", "improve_code", {"suggestions": "", "code": ""}), + ("Write Tests", "write_tests", {"code": "", "focus": ""}), ("Execute Python File", "execute_python_file", {"file": ""}), ("Task Complete (Shutdown)", "task_complete", {"reason": ""}), ("Generate Image", "generate_image", {"prompt": ""}), @@ -55,24 +44,18 @@ def get_prompt(): prompt_generator.add_command(command_label, command_name, args) # Add resources to the PromptGenerator object - prompt_generator.add_resource( - "Internet access for searches and information gathering.") + prompt_generator.add_resource("Internet access for searches and information gathering.") prompt_generator.add_resource("Long Term memory management.") - prompt_generator.add_resource( - "GPT-3.5 powered Agents for delegation of simple tasks.") + prompt_generator.add_resource("GPT-3.5 powered Agents for delegation of simple tasks.") prompt_generator.add_resource("File output.") # Add performance evaluations to the PromptGenerator object - prompt_generator.add_performance_evaluation( - "Continuously review and analyze your actions to ensure you are performing to the best of your abilities.") - prompt_generator.add_performance_evaluation( - "Constructively self-criticize your big-picture behavior constantly.") - prompt_generator.add_performance_evaluation( - "Reflect on past decisions and strategies to refine your approach.") - prompt_generator.add_performance_evaluation( - "Every command has a cost, so be smart and efficient. Aim to complete tasks in the least number of steps.") + prompt_generator.add_performance_evaluation("Continuously review and analyze your actions to ensure you are performing to the best of your abilities.") + prompt_generator.add_performance_evaluation("Constructively self-criticize your big-picture behavior constantly.") + prompt_generator.add_performance_evaluation("Reflect on past decisions and strategies to refine your approach.") + prompt_generator.add_performance_evaluation("Every command has a cost, so be smart and efficient. Aim to complete tasks in the least number of steps.") # Generate the prompt string prompt_string = prompt_generator.generate_prompt_string() - + return prompt_string From 8bbfdeb04a5b98070bf0b44c9dc819c66159f05f Mon Sep 17 00:00:00 2001 From: Alrik Olson <10505065+AlrikOlson@users.noreply.github.com> Date: Tue, 11 Apr 2023 09:43:37 -0700 Subject: [PATCH 4/7] Add unit tests for prompt generator class --- tests/promptgenerator_tests.py | 99 ++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 tests/promptgenerator_tests.py diff --git a/tests/promptgenerator_tests.py b/tests/promptgenerator_tests.py new file mode 100644 index 000000000000..ac5c3a79040b --- /dev/null +++ b/tests/promptgenerator_tests.py @@ -0,0 +1,99 @@ +# Import the required libraries for unit testing +import unittest +import sys +import os + +# Add the path to the "scripts" directory to import the PromptGenerator module +sys.path.append(os.path.abspath("../scripts")) +from promptgenerator import PromptGenerator + +# Create a test class for the PromptGenerator, subclassed from unittest.TestCase +class promptgenerator_tests(unittest.TestCase): + + # Set up the initial state for each test method by creating an instance of PromptGenerator + def setUp(self): + self.generator = PromptGenerator() + + # Test whether the add_constraint() method adds a constraint to the generator's constraints list + def test_add_constraint(self): + constraint = "Constraint1" + self.generator.add_constraint(constraint) + self.assertIn(constraint, self.generator.constraints) + + # Test whether the add_command() method adds a command to the generator's commands list + def test_add_command(self): + command_label = "Command Label" + command_name = "command_name" + args = {"arg1": "value1", "arg2": "value2"} + self.generator.add_command(command_label, command_name, args) + command = { + "label": command_label, + "name": command_name, + "args": args, + } + self.assertIn(command, self.generator.commands) + + # Test whether the add_resource() method adds a resource to the generator's resources list + def test_add_resource(self): + resource = "Resource1" + self.generator.add_resource(resource) + self.assertIn(resource, self.generator.resources) + + # Test whether the add_performance_evaluation() method adds an evaluation to the generator's performance_evaluation list + def test_add_performance_evaluation(self): + evaluation = "Evaluation1" + self.generator.add_performance_evaluation(evaluation) + self.assertIn(evaluation, self.generator.performance_evaluation) + + # Test whether the generate_prompt_string() method generates a prompt string with all the added constraints, commands, resources and evaluations + def test_generate_prompt_string(self): + constraints = ["Constraint1", "Constraint2"] + commands = [ + { + "label": "Command1", + "name": "command_name1", + "args": {"arg1": "value1"}, + }, + { + "label": "Command2", + "name": "command_name2", + "args": {}, + }, + ] + resources = ["Resource1", "Resource2"] + evaluations = ["Evaluation1", "Evaluation2"] + + # Add all the constraints, commands, resources, and evaluations to the generator + for constraint in constraints: + self.generator.add_constraint(constraint) + for command in commands: + self.generator.add_command( + command["label"], command["name"], command["args"]) + for resource in resources: + self.generator.add_resource(resource) + for evaluation in evaluations: + self.generator.add_performance_evaluation(evaluation) + + # Generate the prompt string and verify its correctness + prompt_string = self.generator.generate_prompt_string() + self.assertIsNotNone(prompt_string) + for constraint in constraints: + self.assertIn(constraint, prompt_string) + for command in commands: + self.assertIn(command["name"], prompt_string) + + # Check for each key-value pair in the command args dictionary + for key, value in command["args"].items(): + self.assertIn(f'"{key}": "{value}"', prompt_string) + for resource in resources: + self.assertIn(resource, prompt_string) + for evaluation in evaluations: + self.assertIn(evaluation, prompt_string) + self.assertIn("constraints", prompt_string.lower()) + self.assertIn("commands", prompt_string.lower()) + self.assertIn("resources", prompt_string.lower()) + self.assertIn("performance evaluation", prompt_string.lower()) + +# Run the tests when this script is executed +if __name__ == '__main__': + unittest.main() From 7a0c9e8a9d13ef5930b56ee13e885c3b69deb293 Mon Sep 17 00:00:00 2001 From: Alrik Olson <10505065+AlrikOlson@users.noreply.github.com> Date: Tue, 11 Apr 2023 10:30:53 -0700 Subject: [PATCH 5/7] fix attempts to import a non-existent module --- scripts/ai_config.py | 1 - scripts/main.py | 1 - 2 files changed, 2 deletions(-) diff --git a/scripts/ai_config.py b/scripts/ai_config.py index 9aa01332d466..36e8be3c0d91 100644 --- a/scripts/ai_config.py +++ b/scripts/ai_config.py @@ -1,5 +1,4 @@ import yaml -import data import os from prompt import get_prompt diff --git a/scripts/main.py b/scripts/main.py index 0946f21fe8bc..b51d486af620 100644 --- a/scripts/main.py +++ b/scripts/main.py @@ -3,7 +3,6 @@ import commands as cmd import utils from memory import get_memory -import data import chat from colorama import Fore, Style from spinner import Spinner From 2a623941127c03f900853ff201b75f5e40fc0adb Mon Sep 17 00:00:00 2001 From: Alrik Olson <10505065+AlrikOlson@users.noreply.github.com> Date: Thu, 13 Apr 2023 07:56:56 -0700 Subject: [PATCH 6/7] add: execute shell command to prompt.py --- scripts/prompt.py | 1 + 1 file changed, 1 insertion(+) diff --git a/scripts/prompt.py b/scripts/prompt.py index bbdfa5ec6bd3..286002ee2daa 100644 --- a/scripts/prompt.py +++ b/scripts/prompt.py @@ -34,6 +34,7 @@ def get_prompt(): ("Get Improved Code", "improve_code", {"suggestions": "", "code": ""}), ("Write Tests", "write_tests", {"code": "", "focus": ""}), ("Execute Python File", "execute_python_file", {"file": ""}), + ("Execute Shell Command, non-interactive commands only", "execute_shell", { "command_line": ""}), ("Task Complete (Shutdown)", "task_complete", {"reason": ""}), ("Generate Image", "generate_image", {"prompt": ""}), ("Do Nothing", "do_nothing", {}), From 8186ccb56a52841bc859d1bcafffaba79c27d10a Mon Sep 17 00:00:00 2001 From: Alrik Olson <10505065+AlrikOlson@users.noreply.github.com> Date: Thu, 13 Apr 2023 11:36:48 -0700 Subject: [PATCH 7/7] formatting --- scripts/prompt.py | 5 +++-- tests/promptgenerator_tests.py | 4 +++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/scripts/prompt.py b/scripts/prompt.py index 286002ee2daa..188603a3bada 100644 --- a/scripts/prompt.py +++ b/scripts/prompt.py @@ -1,9 +1,10 @@ from promptgenerator import PromptGenerator + def get_prompt(): """ This function generates a prompt string that includes various constraints, commands, resources, and performance evaluations. - + Returns: str: The generated prompt string. """ @@ -58,5 +59,5 @@ def get_prompt(): # Generate the prompt string prompt_string = prompt_generator.generate_prompt_string() - + return prompt_string diff --git a/tests/promptgenerator_tests.py b/tests/promptgenerator_tests.py index ac5c3a79040b..181fdea63f26 100644 --- a/tests/promptgenerator_tests.py +++ b/tests/promptgenerator_tests.py @@ -7,6 +7,7 @@ sys.path.append(os.path.abspath("../scripts")) from promptgenerator import PromptGenerator + # Create a test class for the PromptGenerator, subclassed from unittest.TestCase class promptgenerator_tests(unittest.TestCase): @@ -81,7 +82,7 @@ def test_generate_prompt_string(self): self.assertIn(constraint, prompt_string) for command in commands: self.assertIn(command["name"], prompt_string) - + # Check for each key-value pair in the command args dictionary for key, value in command["args"].items(): self.assertIn(f'"{key}": "{value}"', prompt_string) @@ -94,6 +95,7 @@ def test_generate_prompt_string(self): self.assertIn("resources", prompt_string.lower()) self.assertIn("performance evaluation", prompt_string.lower()) + # Run the tests when this script is executed if __name__ == '__main__': unittest.main()