2023-04-11 16:09:59 +00:00
import json
class PromptGenerator :
2023-04-11 16:15:45 +00:00
"""
A class for generating custom prompt strings based on constraints , commands , resources , and performance evaluations .
"""
2023-04-11 16:09:59 +00:00
def __init__ ( self ) :
2023-04-11 16:15:45 +00:00
"""
Initialize the PromptGenerator object with empty lists of constraints , commands , resources , and performance evaluations .
"""
2023-04-11 16:09:59 +00:00
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 " ,
2023-04-14 19:42:28 +00:00
" speak " : " thoughts summary to say to user " ,
2023-04-11 16:09:59 +00:00
} ,
2023-04-14 19:42:28 +00:00
" command " : { " name " : " command name " , " args " : { " arg name " : " value " } } ,
2023-04-11 16:09:59 +00:00
}
def add_constraint ( self , constraint ) :
2023-04-11 16:15:45 +00:00
"""
Add a constraint to the constraints list .
Args :
constraint ( str ) : The constraint to be added .
"""
2023-04-11 16:09:59 +00:00
self . constraints . append ( constraint )
def add_command ( self , command_label , command_name , args = None ) :
2023-04-11 16:15:45 +00:00
"""
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 .
"""
2023-04-11 16:09:59 +00:00
if args is None :
args = { }
2023-04-11 16:15:45 +00:00
2023-04-14 19:42:28 +00:00
command_args = { arg_key : arg_value for arg_key , arg_value in args . items ( ) }
2023-04-11 16:09:59 +00:00
command = {
" label " : command_label ,
" name " : command_name ,
" args " : command_args ,
}
self . commands . append ( command )
def _generate_command_string ( self , command ) :
2023-04-11 16:15:45 +00:00
"""
Generate a formatted string representation of a command .
Args :
command ( dict ) : A dictionary containing command information .
Returns :
str : The formatted command string .
"""
2023-04-14 19:42:28 +00:00
args_string = " , " . join (
f ' " { key } " : " { value } " ' for key , value in command [ " args " ] . items ( )
)
2023-04-11 16:09:59 +00:00
return f ' { command [ " label " ] } : " { command [ " name " ] } " , args: { args_string } '
2023-04-11 16:15:45 +00:00
2023-04-11 16:09:59 +00:00
def add_resource ( self , resource ) :
2023-04-11 16:15:45 +00:00
"""
Add a resource to the resources list .
Args :
resource ( str ) : The resource to be added .
"""
2023-04-11 16:09:59 +00:00
self . resources . append ( resource )
def add_performance_evaluation ( self , evaluation ) :
2023-04-11 16:15:45 +00:00
"""
Add a performance evaluation item to the performance_evaluation list .
2023-04-11 16:09:59 +00:00
2023-04-11 16:15:45 +00:00
Args :
evaluation ( str ) : The evaluation item to be added .
"""
self . performance_evaluation . append ( evaluation )
2023-04-11 16:09:59 +00:00
2023-04-14 19:42:28 +00:00
def _generate_numbered_list ( self , items , item_type = " list " ) :
2023-04-11 16:15:45 +00:00
"""
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 .
"""
2023-04-14 19:42:28 +00:00
if item_type == " command " :
return " \n " . join (
f " { i + 1 } . { self . _generate_command_string ( item ) } "
for i , item in enumerate ( items )
)
2023-04-11 16:09:59 +00:00
else :
return " \n " . join ( f " { i + 1 } . { item } " for i , item in enumerate ( items ) )
def generate_prompt_string ( self ) :
2023-04-11 16:15:45 +00:00
"""
Generate a prompt string based on the constraints , commands , resources , and performance evaluations .
Returns :
str : The generated prompt string .
"""
2023-04-11 16:09:59 +00:00
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 \n Response Format: \n { formatted_response_format } \n Ensure the response can be parsed by Python json.loads "
)
return prompt_string