opto.optimizers.optoprime¶
FunctionFeedback
dataclass
¶
FunctionFeedback(
graph: List[Tuple[int, str]],
documentation: Dict[str, str],
others: Dict[str, Any],
roots: Dict[str, Any],
output: Dict[str, Any],
user_feedback: str,
)
Feedback container used by FunctionPropagator.
ProblemInstance
dataclass
¶
ProblemInstance(
instruction: str,
code: str,
documentation: str,
variables: str,
inputs: str,
others: str,
outputs: str,
feedback: str,
constraints: str,
)
problem_template
class-attribute
instance-attribute
¶
problem_template = dedent(
"\n #Instruction\n {instruction}\n\n #Code\n {code}\n\n #Documentation\n {documentation}\n\n #Variables\n {variables}\n\n #Constraints\n {constraints}\n\n #Inputs\n {inputs}\n\n #Others\n {others}\n\n #Outputs\n {outputs}\n\n #Feedback\n {feedback}\n "
)
OptoPrime ¶
OptoPrime(
parameters: List[ParameterNode],
llm: AbstractModel = None,
*args,
propagator: Propagator = None,
objective: Union[None, str] = None,
ignore_extraction_error: bool = True,
include_example=False,
memory_size=0,
max_tokens=4096,
log=True,
prompt_symbols=None,
json_keys=None,
use_json_object_format=True,
highlight_variables=False,
**kwargs
)
Bases: Optimizer
representation_prompt
class-attribute
instance-attribute
¶
representation_prompt = dedent(
"\n You're tasked to solve a coding/algorithm problem. You will see the instruction, the code, the documentation of each function used in the code, and the feedback about the execution result.\n\n Specifically, a problem will be composed of the following parts:\n - #Instruction: the instruction which describes the things you need to do or the question you should answer.\n - #Code: the code defined in the problem.\n - #Documentation: the documentation of each function used in #Code. The explanation might be incomplete and just contain high-level description. You can use the values in #Others to help infer how those functions work.\n - #Variables: the input variables that you can change.\n - #Constraints: the constraints or descriptions of the variables in #Variables.\n - #Inputs: the values of other inputs to the code, which are not changeable.\n - #Others: the intermediate values created through the code execution.\n - #Outputs: the result of the code output.\n - #Feedback: the feedback about the code's execution result.\n\n In #Variables, #Inputs, #Outputs, and #Others, the format is:\n\n <data_type> <variable_name> = <value>\n\n If <type> is (code), it means <value> is the source code of a python code, which may include docstring and definitions.\n "
)
default_objective
class-attribute
instance-attribute
¶
default_objective = "You need to change the <value> of the variables in #Variables to improve the output in accordance to #Feedback."
output_format_prompt_original
class-attribute
instance-attribute
¶
output_format_prompt_original = dedent(
'\n Output_format: Your output should be in the following json format, satisfying the json syntax:\n\n {{\n "{reasoning}": <Your reasoning>,\n "{answer}": <Your answer>,\n "{suggestion}": {{\n <variable_1>: <suggested_value_1>,\n <variable_2>: <suggested_value_2>,\n }}\n }}\n\n In "{reasoning}", explain the problem: 1. what the #Instruction means 2. what the #Feedback on #Output means to #Variables considering how #Variables are used in #Code and other values in #Documentation, #Inputs, #Others. 3. Reasoning about the suggested changes in #Variables (if needed) and the expected result.\n\n If #Instruction asks for an answer, write it down in "{answer}".\n\n If you need to suggest a change in the values of #Variables, write down the suggested values in "{suggestion}". Remember you can change only the values in #Variables, not others. When <type> of a variable is (code), you should write the new definition in the format of python code without syntax errors, and you should not change the function name or the function signature.\n\n If no changes or answer are needed, just output TERMINATE.\n '
)
output_format_prompt_no_answer
class-attribute
instance-attribute
¶
output_format_prompt_no_answer = dedent(
'\n Output_format: Your output should be in the following json format, satisfying the json syntax:\n\n {{\n "{reasoning}": <Your reasoning>,\n "{suggestion}": {{\n <variable_1>: <suggested_value_1>,\n <variable_2>: <suggested_value_2>,\n }}\n }}\n\n In "{reasoning}", explain the problem: 1. what the #Instruction means 2. what the #Feedback on #Output means to #Variables considering how #Variables are used in #Code and other values in #Documentation, #Inputs, #Others. 3. Reasoning about the suggested changes in #Variables (if needed) and the expected result.\n\n If you need to suggest a change in the values of #Variables, write down the suggested values in "{suggestion}". Remember you can change only the values in #Variables, not others. When <type> of a variable is (code), you should write the new definition in the format of python code without syntax errors, and you should not change the function name or the function signature.\n\n If no changes are needed, just output TERMINATE.\n '
)
example_problem_template
class-attribute
instance-attribute
¶
example_problem_template = dedent(
"\n Here is an example of problem instance and response:\n\n ================================\n {example_problem}\n ================================\n\n Your response:\n {example_response}\n "
)
user_prompt_template
class-attribute
instance-attribute
¶
user_prompt_template = dedent(
"\n Now you see problem instance:\n\n ================================\n {problem_instance}\n ================================\n\n "
)
example_prompt
class-attribute
instance-attribute
¶
example_prompt = dedent(
"\n\n Here are some feasible but not optimal solutions for the current problem instance. Consider this as a hint to help you understand the problem better.\n\n ================================\n\n {examples}\n\n ================================\n "
)
final_prompt_with_variables
class-attribute
instance-attribute
¶
final_prompt_with_variables = dedent(
"\n What are your suggestions on variables {names}?\n\n Your response:\n "
)
default_prompt_symbols
class-attribute
instance-attribute
¶
default_prompt_symbols = {
"variables": "#Variables",
"constraints": "#Constraints",
"inputs": "#Inputs",
"outputs": "#Outputs",
"others": "#Others",
"feedback": "#Feedback",
"instruction": "#Instruction",
"code": "#Code",
"documentation": "#Documentation",
}
default_json_keys
class-attribute
instance-attribute
¶
example_problem
instance-attribute
¶
example_problem = format(
instruction=default_objective,
code="y = add(x=a,y=b)\nz = subtract(x=y, y=c)",
documentation="add: add x and y \nsubtract: subtract y from x",
variables="(int) a = 5",
constraints="a: a > 0",
outputs="(int) z = 1",
others="(int) y = 6",
inputs="(int) b = 1\n(int) c = 5",
feedback="The result of the code is not as expected. The result should be 10, but the code returns 1",
stepsize=1,
)
example_response
instance-attribute
¶
example_response = dedent(
'\n {"reasoning": \'In this case, the desired response would be to change the value of input a to 14, as that would make the code return 10.\',\n "answer", {},\n "suggestion": {"a": 10}\n }\n '
)
construct_prompt ¶
Construct the system and user prompt.
construct_update_dict ¶
Convert the suggestion in text into the right data type.
extract_llm_suggestion ¶
Extract the suggestion from the response.
call_llm ¶
call_llm(
system_prompt: str,
user_prompt: str,
verbose: Union[bool, str] = False,
max_tokens: int = 4096,
)
Call the LLM with a prompt and return the response.
node_to_function_feedback ¶
Convert a TraceGraph to a FunctionFeedback. roots, others, outputs are dict of variable name and its data and constraints.