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
  
¶
    
            instruction_section_title
  
      class-attribute
      instance-attribute
  
¶
    
            documentation_section_title
  
      class-attribute
      instance-attribute
  
¶
    
            custom_output_format_instruction
  
      class-attribute
      instance-attribute
  
¶
    
            output_format
  
      property
  
¶
    
example_output ¶
reasoning: str variables: format {variable_name, value}
OptimizerPromptSymbolSetJSON ¶
              Bases: OptimizerPromptSymbolSet
We enforce a JSON output format extraction
            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 ¶
reasoning: str variables: format {variable_name, value}
OptimizerPromptSymbolSet2 ¶
              Bases: OptimizerPromptSymbolSet
            ProblemInstance
  
      dataclass
  
¶
ProblemInstance(
    instruction: str,
    code: str,
    documentation: str,
    variables: str,
    inputs: str,
    others: str,
    outputs: str,
    feedback: str,
    optimizer_prompt_symbol_set: OptimizerPromptSymbolSet,
)
            optimizer_prompt_symbol_set
  
      instance-attribute
  
¶
    
            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
  
¶
    
            use_json_object_format
  
      instance-attribute
  
¶
use_json_object_format = (
    use_json_object_format
    if expect_json and use_json_object_format
    else False
)
            objective
  
      instance-attribute
  
¶
objective = objective or format(
    value_tag=value_tag,
    variables_section_title=variables_section_title,
    feedback_section_title=feedback_section_title,
)
            optimizer_prompt_symbol_set
  
      instance-attribute
  
¶
    
            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_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},
)
repr_node_value_compact ¶
repr_node_value_compact(
    node_dict,
    node_tag="node",
    value_tag="value",
    constraint_tag="constraint",
)
construct_prompt ¶
Construct the system and user prompt.
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.