coding-alt/AutoGPT
0
1""" A module for generating custom prompt strings."""2from __future__ import annotations3 4import json5from typing import Any6 7 8class PromptGenerator:9 """10 A class for generating custom prompt strings based on constraints, commands,11 resources, and performance evaluations.12 """13 14 def __init__(self) -> None:15 """16 Initialize the PromptGenerator object with empty lists of constraints,17 commands, resources, and performance evaluations.18 """19 self.constraints = []20 self.commands = []21 self.resources = []22 self.performance_evaluation = []23 self.response_format = {24 "thoughts": {25 "text": "thought",26 "reasoning": "reasoning",27 "plan": "- short bulleted\n- list that conveys\n- long-term plan",28 "criticism": "constructive self-criticism",29 "speak": "thoughts summary to say to user",30 },31 "command": {"name": "command name", "args": {"arg name": "value"}},32 }33 34 def add_constraint(self, constraint: str) -> None:35 """36 Add a constraint to the constraints list.37 38 Args:39 constraint (str): The constraint to be added.40 """41 self.constraints.append(constraint)42 43 def add_command(self, command_label: str, command_name: str, args=None) -> None:44 """45 Add a command to the commands list with a label, name, and optional arguments.46 47 Args:48 command_label (str): The label of the command.49 command_name (str): The name of the command.50 args (dict, optional): A dictionary containing argument names and their51 values. Defaults to None.52 """53 if args is None:54 args = {}55 56 command_args = {arg_key: arg_value for arg_key, arg_value in args.items()}57 58 command = {59 "label": command_label,60 "name": command_name,61 "args": command_args,62 }63 64 self.commands.append(command)65 66 def _generate_command_string(self, command: dict[str, Any]) -> str:67 """68 Generate a formatted string representation of a command.69 70 Args:71 command (dict): A dictionary containing command information.72 73 Returns:74 str: The formatted command string.75 """76 args_string = ", ".join(77 f'"{key}": "{value}"' for key, value in command["args"].items()78 )79 return f'{command["label"]}: "{command["name"]}", args: {args_string}'80 81 def add_resource(self, resource: str) -> None:82 """83 Add a resource to the resources list.84 85 Args:86 resource (str): The resource to be added.87 """88 self.resources.append(resource)89 90 def add_performance_evaluation(self, evaluation: str) -> None:91 """92 Add a performance evaluation item to the performance_evaluation list.93 94 Args:95 evaluation (str): The evaluation item to be added.96 """97 self.performance_evaluation.append(evaluation)98 99 def _generate_numbered_list(self, items: list[Any], item_type="list") -> str:100 """101 Generate a numbered list from given items based on the item_type.102 103 Args:104 items (list): A list of items to be numbered.105 item_type (str, optional): The type of items in the list.106 Defaults to 'list'.107 108 Returns:109 str: The formatted numbered list.110 """111 if item_type == "command":112 return "\n".join(113 f"{i+1}. {self._generate_command_string(item)}"114 for i, item in enumerate(items)115 )116 else:117 return "\n".join(f"{i+1}. {item}" for i, item in enumerate(items))118 119 def generate_prompt_string(self) -> str:120 """121 Generate a prompt string based on the constraints, commands, resources,122 and performance evaluations.123 124 Returns:125 str: The generated prompt string.126 """127 formatted_response_format = json.dumps(self.response_format, indent=4)128 return (129 f"Constraints:\n{self._generate_numbered_list(self.constraints)}\n\n"130 "Commands:\n"131 f"{self._generate_numbered_list(self.commands, item_type='command')}\n\n"132 f"Resources:\n{self._generate_numbered_list(self.resources)}\n\n"133 "Performance Evaluation:\n"134 f"{self._generate_numbered_list(self.performance_evaluation)}\n\n"135 "You should only respond in JSON format as described below \nResponse"136 f" Format: \n{formatted_response_format} \nEnsure the response can be"137 " parsed by Python json.loads"138 )139 