Skip to content

opto.optimizers.optoprime_v2

OptimizerPromptSymbolSet

By inheriting this class and pass into the optimizer. People can change the optimizer documentation

This divides into three parts: - Section titles: the title of each section in the prompt - Node tags: the tags that capture the graph structure (only tag names are allowed to be changed) - Output format: the format of the output of the optimizer

variables_section_title class-attribute instance-attribute

variables_section_title = '# Variables'

inputs_section_title class-attribute instance-attribute

inputs_section_title = '# Inputs'

outputs_section_title class-attribute instance-attribute

outputs_section_title = '# Outputs'

others_section_title class-attribute instance-attribute

others_section_title = '# Others'

feedback_section_title class-attribute instance-attribute

feedback_section_title = '# Feedback'

instruction_section_title class-attribute instance-attribute

instruction_section_title = '# Instruction'

code_section_title class-attribute instance-attribute

code_section_title = '# Code'

documentation_section_title class-attribute instance-attribute

documentation_section_title = '# Documentation'

node_tag class-attribute instance-attribute

node_tag = 'node'

variable_tag class-attribute instance-attribute

variable_tag = 'variable'

value_tag class-attribute instance-attribute

value_tag = 'value'

constraint_tag class-attribute instance-attribute

constraint_tag = 'constraint'

reasoning_tag class-attribute instance-attribute

reasoning_tag = 'reasoning'

improved_variable_tag class-attribute instance-attribute

improved_variable_tag = 'variable'

name_tag class-attribute instance-attribute

name_tag = 'name'

expect_json class-attribute instance-attribute

expect_json = False

custom_output_format_instruction class-attribute instance-attribute

custom_output_format_instruction = None

output_format property

output_format: str

This function defines the input to:

{output_format}
In the self.output_format_prompt_template in the OptoPrimeV2

default_prompt_symbols property

default_prompt_symbols: Dict[str, str]

example_output

example_output(reasoning, variables)

reasoning: str variables: format {variable_name, value}

output_response_extractor

output_response_extractor(response: str) -> Dict[str, Any]

OptimizerPromptSymbolSetJSON

Bases: OptimizerPromptSymbolSet

We enforce a JSON output format extraction

expect_json class-attribute instance-attribute

expect_json = True

custom_output_format_instruction class-attribute instance-attribute

custom_output_format_instruction = '\n    {{\n        "reasoning": <Your reasoning>,\n        "suggestion": {{\n            <variable_1>: <suggested_value_1>,\n            <variable_2>: <suggested_value_2>,\n        }}\n    }}\n    '

example_output

example_output(reasoning, variables)

reasoning: str variables: format {variable_name, value}

output_response_extractor

output_response_extractor(response: str) -> Dict[str, Any]

OptimizerPromptSymbolSet2

Bases: OptimizerPromptSymbolSet

variables_section_title class-attribute instance-attribute

variables_section_title = '# Variables'

inputs_section_title class-attribute instance-attribute

inputs_section_title = '# Inputs'

outputs_section_title class-attribute instance-attribute

outputs_section_title = '# Outputs'

others_section_title class-attribute instance-attribute

others_section_title = '# Others'

feedback_section_title class-attribute instance-attribute

feedback_section_title = '# Feedback'

instruction_section_title class-attribute instance-attribute

instruction_section_title = '# Instruction'

code_section_title class-attribute instance-attribute

code_section_title = '# Code'

documentation_section_title class-attribute instance-attribute

documentation_section_title = '# Documentation'

node_tag class-attribute instance-attribute

node_tag = 'const'

variable_tag class-attribute instance-attribute

variable_tag = 'var'

value_tag class-attribute instance-attribute

value_tag = 'data'

constraint_tag class-attribute instance-attribute

constraint_tag = 'constraint'

reasoning_tag class-attribute instance-attribute

reasoning_tag = 'reason'

improved_variable_tag class-attribute instance-attribute

improved_variable_tag = 'var'

name_tag class-attribute instance-attribute

name_tag = 'name'

ProblemInstance dataclass

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

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

optimizer_prompt_symbol_set instance-attribute

optimizer_prompt_symbol_set: OptimizerPromptSymbolSet

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        # Inputs\n        {inputs}\n\n        # Others\n        {others}\n\n        # Outputs\n        {outputs}\n\n        # Feedback\n        {feedback}\n        "
)

OptoPrimeV2

OptoPrimeV2(
    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,
    initial_var_char_limit=100,
    optimizer_prompt_symbol_set: OptimizerPromptSymbolSet = OptimizerPromptSymbolSet(),
    use_json_object_format=True,
    truncate_expression=truncate_expression,
    **kwargs
)

Bases: OptoPrime

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_section_title}: the instruction which describes the things you need to do or the question you should answer.\n        - {code_section_title}: the code defined in the problem.\n        - {documentation_section_title}: 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_section_title}: the input variables that you can change/tweak (trainable).\n        - {inputs_section_title}: the values of fixed inputs to the code, which CANNOT be changed (fixed).\n        - {others_section_title}: the intermediate values created through the code execution.\n        - {outputs_section_title}: the result of the code output.\n        - {feedback_section_title}: the feedback about the code's execution result.\n\n        In `{variables_section_title}`, `{inputs_section_title}`, `{outputs_section_title}`, and `{others_section_title}`, the format is:\n\n        For variables we express as this:\n        {variable_expression_format}\n\n        If `data_type` is `code`, it means `{value_tag}` 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_tag}` of the variables in {variables_section_title} to improve the output in accordance to {feedback_section_title}."

output_format_prompt_template class-attribute instance-attribute

output_format_prompt_template = dedent(
    "\n        Output_format: Your output should be in the following XML/HTML format:\n\n        ```\n        {output_format}\n        ```\n\n        In <{reasoning_tag}>, explain the problem: 1. what the {instruction_section_title} means 2. what the {feedback_section_title} on {outputs_section_title} means to {variables_section_title} considering how {variables_section_title} are used in {code_section_title} and other values in {documentation_section_title}, {inputs_section_title}, {others_section_title}. 3. Reasoning about the suggested changes in {variables_section_title} (if needed) and the expected result.\n\n        If you need to suggest a change in the values of {variables_section_title}, write down the suggested values in <{improved_variable_tag}>. Remember you can change only the values in {variables_section_title}, 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        What are your suggestions on variables {names}?\n\n        Your response:\n        "
)

truncate_expression instance-attribute

truncate_expression = truncate_expression

use_json_object_format instance-attribute

use_json_object_format = (
    use_json_object_format
    if expect_json and use_json_object_format
    else False
)

ignore_extraction_error instance-attribute

ignore_extraction_error = ignore_extraction_error

llm instance-attribute

llm = llm or LLM()

objective instance-attribute

objective = objective or format(
    value_tag=value_tag,
    variables_section_title=variables_section_title,
    feedback_section_title=feedback_section_title,
)

initial_var_char_limit instance-attribute

initial_var_char_limit = initial_var_char_limit

optimizer_prompt_symbol_set instance-attribute

optimizer_prompt_symbol_set = optimizer_prompt_symbol_set

example_problem_summary instance-attribute

example_problem_summary = FunctionFeedback(
    graph=[
        (1, "y = add(x=a,y=b)"),
        (2, "z = subtract(x=y, y=c)"),
    ],
    documentation={
        "add": "This is an add operator of x and y.",
        "subtract": "subtract y from x",
    },
    others={"y": (6, None)},
    roots={
        "a": (5, "a > 0"),
        "b": (1, None),
        "c": (5, None),
    },
    output={"z": (1, None)},
    user_feedback="The result of the code is not as expected. The result should be 10, but the code returns 1",
)

example_problem instance-attribute

example_problem = problem_instance(example_problem_summary)

example_response instance-attribute

example_response = example_output(
    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.",
    variables={"a": 10},
)

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)

default_prompt_symbols instance-attribute

default_prompt_symbols = default_prompt_symbols

prompt_symbols instance-attribute

prompt_symbols = deepcopy(default_prompt_symbols)

initialize_prompt

initialize_prompt()

repr_node_value staticmethod

repr_node_value(node_dict)

repr_node_value_compact

repr_node_value_compact(
    node_dict,
    node_tag="node",
    value_tag="value",
    constraint_tag="constraint",
)

construct_prompt

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

Construct the system and user prompt.

problem_instance

problem_instance(summary, mask=None)

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.