Skip to content

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.

graph instance-attribute

graph: List[Tuple[int, str]]

documentation instance-attribute

documentation: Dict[str, str]

others instance-attribute

others: Dict[str, Any]

roots instance-attribute

roots: Dict[str, Any]

output instance-attribute

output: Dict[str, Any]

user_feedback instance-attribute

user_feedback: str

ProblemInstance dataclass

ProblemInstance(
    instruction: str,
    code: str,
    documentation: str,
    variables: str,
    inputs: str,
    others: str,
    outputs: str,
    feedback: str,
    constraints: str,
)

instruction instance-attribute

instruction: str

code instance-attribute

code: str

documentation instance-attribute

documentation: str

variables instance-attribute

variables: str

inputs instance-attribute

inputs: str

others instance-attribute

others: str

outputs instance-attribute

outputs: str

feedback instance-attribute

feedback: str

constraints instance-attribute

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 class-attribute instance-attribute

final_prompt = dedent('\n        Your response:\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

default_json_keys = {
    "reasoning": "reasoning",
    "answer": "answer",
    "suggestion": "suggestion",
}

ignore_extraction_error instance-attribute

ignore_extraction_error = ignore_extraction_error

llm instance-attribute

llm = llm or LLM()

objective instance-attribute

objective = objective or default_objective

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            '
)

include_example instance-attribute

include_example = include_example

max_tokens instance-attribute

max_tokens = max_tokens

log instance-attribute

log = [] if log else None

summary_log instance-attribute

summary_log = [] if log else None

memory instance-attribute

memory = FIFOBuffer(memory_size)

prompt_symbols instance-attribute

prompt_symbols = deepcopy(default_prompt_symbols)

output_format_prompt instance-attribute

output_format_prompt = format(**(default_json_keys))

use_json_object_format instance-attribute

use_json_object_format = use_json_object_format

highlight_variables instance-attribute

highlight_variables = highlight_variables

default_propagator

default_propagator()

Return the default Propagator object of the optimizer.

summarize

summarize()

repr_node_value staticmethod

repr_node_value(node_dict)

repr_node_constraint staticmethod

repr_node_constraint(node_dict)

problem_instance

problem_instance(summary, mask=None)

construct_prompt

construct_prompt(summary, mask=None, *args, **kwargs)

Construct the system and user prompt.

replace_symbols

replace_symbols(text: str, symbols: Dict[str, str]) -> str

construct_update_dict

construct_update_dict(
    suggestion: Dict[str, Any],
) -> Dict[ParameterNode, Any]

Convert the suggestion in text into the right data type.

extract_llm_suggestion

extract_llm_suggestion(response: str)

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.

save

save(path: str)

Save the optimizer state to a file.

load

load(path: str)

Load the optimizer state from a file.

get_fun_name

get_fun_name(node: MessageNode)

repr_function_call

repr_function_call(child: MessageNode)

node_to_function_feedback

node_to_function_feedback(node_feedback: TraceGraph)

Convert a TraceGraph to a FunctionFeedback. roots, others, outputs are dict of variable name and its data and constraints.