File size: 25,016 Bytes
24c2665 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 |
from typing import List, Dict, Tuple
code_input_prompt = """
## Task: Create a Python Code Snippet (where custom classes are allowed, which should be defined at the top of the code snippet) with one Matching Input
Using the reference code snippets provided below as examples, design a new and unique Python code snippet that demands deep algorithmic reasoning to deduce one possible input from a given output. Your submission should include both a code snippet and test input pair, where the input will be plugged into the code snippet to produce the output, which that function output be given to a test subject to come up with any input that will produce the same function output. This is meant to be an I.Q. test.
### Code Requirements:
- Name the entry function `f` (e.g., `def f(...): ...`), you can have nested definitions inside `f`
- Ensure the function returns a value
- Include at least one input parameter
- Make the function deterministic
- Make the snippet require state tracking across multiple data transformations, ensuring the task requires long multi step reasoning
- AVOID THE FOLLOWING:
* Random functions or variables
* Date/time operations
* I/O operations (reading files, network requests)
* Printing or logging
* Any external state
- Ensure execution completes within 10 seconds on a modern CPU
- All imports and class definitions should be at the very top of the code snippet
- The snippet should end with a return statement from the main function `f`, anything after will be removed
{remove_input_from_snippet_prompt}{remove_after_return_prompt}
### Input Requirements:
- Provide exactly one test input for your function
- Format multiple arguments with commas between them
- Remember to add quotes around string arguments
### Formatting:
- Format your code with: ```python
def f(...):
# your code here
return ...
```
- Format your input with: ```input
arg1, arg2, ...
```
### Example Format:
```python
def f(name: str, info: dict):
# code logic here
return result
```
```input
'John', {{'age': 20, 'city': 'New York'}}
```
### Evaluation Criteria:
- Executability, your code should be executable given your input
- Difficulty in predicting the output from your provided input and code snippet. Focus on either algorithmic reasoning or logic complexity. For example, you can define complex data structure classes and operate on them like trees, heaps, stacks, queues, graphs, etc, or use complex control flow, dynamic programming, recursions, divide and conquer, greedy, backtracking, etc
- Creativity, the code needs to be sufficiently different from the provided reference snippets
- Restricted usage of certain keywords and packages, you are not allowed to use the following words in any form, even in comments: <|BANNED_KEYWORDS|>
First, carefully devise a clear plan: e.g., identify how your snippet will be challenging, distinct from reference snippets, and creative. Then, write the final code snippet and its inputs.
### Reference Code Snippets:
"""
code_output_prompt = """
## Task: Create a New Python Code Snippet (where custom classes are allowed, which should be defined at the top of the code snippet) with one Matching Input
Using the reference code snippets provided below as examples, design a new and unique Python code snippet that demands deep algorithmic reasoning to deduce the output from the input. Your submission should include a code snippet and a test input pair, where the input will be plugged into the code snippet to produce the output. The input will be given to a test subject to deduce the output, which is meant to be an I.Q. test.
### Code Requirements:
- Name the entry function `f` (e.g., `def f(...): ...`), you can have nested definitions inside `f`
- Ensure the function returns a value
- Include at least one input parameter
- Make the function deterministic
- Make the snippet require state tracking across multiple data transformations, ensuring the task requires long multi step reasoning
- AVOID THE FOLLOWING:
* Random functions or variables
* Date/time operations
* I/O operations (reading files, network requests)
* Printing or logging
* Any external state
- Ensure execution completes within 10 seconds on a modern CPU
- All imports and class definitions should be at the very top of the code snippet
- The snippet should end with a return statement from the main function `f`, anything after will be removed
{remove_input_from_snippet_prompt}{remove_after_return_prompt}
### Input Requirements:
- Provide exactly one test input for your function
- Format multiple arguments with commas between them
- Remember to add quotes around string arguments
### Formatting:
- Format your code with:
```python
def f(...):
# your code here
return ...
```
- Format your input with:
```input
arg1, arg2, ...
```
### Example Format:
```python
def f(name: str, info: dict):
# code logic here
return result
```
```input
'John', {{'age': 20, 'city': 'New York'}}
```
### Evaluation Criteria:
- Executability, your code should be executable given your input
- Difficulty in predicting your ```input``` from 1) your ```python``` code and 2) the deterministic ```output``` that will be obtained from your ```input```. Focus on either algorithmic reasoning or logic complexity. For example, you can define complex data structure classes and operate on them like trees, heaps, stacks, queues, graphs, etc, or use complex control flow, dynamic programming, recursions, divide and conquer, greedy, backtracking, etc
- Creativity, the code needs to be sufficiently different from the provided reference snippets
- Restricted usage of certain keywords and packages, you are not allowed to use the following words in any form, even in comments: <|BANNED_KEYWORDS|>
First, carefully devise a clear plan: e.g., identify how your snippet will be challenging, distinct from reference snippets, and creative. Then, write the final code snippet and its inputs.
### Reference Code Snippets:
"""
code_error_prompt = """
## Task: Create a New Python Code Snippet (where custom classes are allowed, which should be defined at the top of the code snippet) with one Matching Input
Using the reference code snippets provided below as examples, design a new and unique Python code snippet that demands deep algorithmic reasoning to deduce what type of error will be raised when the code is executed. Your submission should include a code snippet and a test input pair, where the input will be plugged into the code snippet to produce the error. You can also choose to include a custom error type in your code snippet. However, the code can also be designed to raise no error. The input and the code will be given to a test subject to deduce the error type, which is meant to be an I.Q. test.
### Code Requirements:
- Name the entry function `f` (e.g., `def f(...): ...`), you can have nested definitions inside `f`
- Ensure the function returns a value
- Include at least one input parameter
- Make the function deterministic
- Make the snippet require state tracking across multiple data transformations, ensuring the task requires long multi step reasoning
- AVOID THE FOLLOWING:
* Random functions or variables
* Date/time operations
* I/O operations (reading files, network requests)
* Printing or logging
* Any external state
- Ensure execution completes within 10 seconds on a modern CPU
- All imports and class definitions should be at the very top of the code snippet
- The snippet should end with a return statement from the main function `f`, anything after will be removed
{remove_after_return_prompt}
### Input Requirements:
- Provide exactly one test input for your function
- Format multiple arguments with commas between them
- Remember to add quotes around string arguments
### Formatting:
- Format your code with:
```python
def f(...):
# your code here
return ...
```
- Format your input with:
```input
arg1, arg2, ...
```
### Example Format:
```python
def f(name: str, info: dict):
# code logic here
return result
```
```input
'John', {{'age': 20, 'city': 'New York'}}
```
### Evaluation Criteria:
- Executability, your code should be executable given your input
- Difficulty in deducing the error type (or no error) from 1) your ```python``` code and ```input```. Focus on either algorithmic reasoning or logic complexity. For example, you can define complex data structure classes and operate on them like trees, heaps, stacks, queues, graphs, etc, or use complex control flow, dynamic programming, recursions, divide and conquer, greedy, backtracking, etc
- Creativity, the code needs to be sufficiently different from the provided reference snippets
- Restricted usage of certain keywords and packages, you are not allowed to use the following words in any form, even in comments: <|BANNED_KEYWORDS|>
<|BANNED_ASSERTION_KEYWORDS|>
First, carefully devise a clear plan: e.g., identify how your snippet will be challenging, distinct from reference snippets, and creative. Then, write the final code snippet and its inputs. The code needs to compile and pass AST checks, but it is intended to raise an error or not.
### Reference Code Snippets:
"""
code_function_prompt = """
## Task: Output {num_inputs} Inputs that can be plugged into the following Code Snippet to produce diverse Outputs, and give a message related to the given snippet.
Using the code snippet provided below, design {num_inputs} inputs that can be plugged into the code snippet to produce a diverse set of outputs. A subset of your given input and its deterministically produced outputs will be given to a test subject to deduce the function, which is meant to be an I.Q. test. You can also leave a message to the test subject to help them deduce the code snippet.
### Input Requirements:
- Provide {num_inputs} valid inputs for the code snippet
- For each input, format multiple arguments with commas between them
- Remember to add quotes around string arguments
- Each input should be individually wrapped in ```input``` tags
### Message Requirements:
- Leave a message to the test subject to help them deduce the code snippet
- The message should be wrapped in ```message``` tags
- The message can be in any form, can even be formed into a coding question, or a natural language instruction what the code snippet does
- You cannot provide the code snippet in the message
### Formatting:
- Format your input with:
```input
arg1, arg2, ...
```
### Example Format:
```input
'John', {{'age': 20, 'city': 'New York'}}
```
```input
'Sammy', {{'age': 37, 'city': 'Los Angeles'}}
```
### Evaluation Criteria:
- Executability, your code should be executable given your inputs
- Coverage, the inputs and outputs should cover the whole input space of the code snippet, able to deduce the code snippet from the inputs and outputs
- Creativity, the inputs need to be sufficiently different from each other
- The overall selection of inputs and message combined should be challenging for the test subject, but not impossible for them to solve
First, carefully devise a clear plan: e.g., understand the code snippet, then identify how your proposed inputs have high coverage, and why the inputs will be challenging and creative. Then, write the inputs and message. Remember to wrap your inputs in ```input``` tags, and your message in ```message``` tags.
### Code Snippet:
```python
{snippet}
```
"""
# code_input_predictor_prompt = """
# # Task: Provide One Possible Input of a Python Code Snippet Given the Code and Output
# Given the following Code Snippet and the Output, think step by step then provide one possible input that produced the output. The input needs to be wrapped in ```input``` tags. Remember if an argument is a string, wrap it in quotes. If the function requires multiple arguments, separate them with commas.
# # Code Snippet:
# ```python
# {snippet}
# ```
# # Output:
# ```output
# {output}
# ```
# # Output Format:
# ```input
# arg1, arg2, ...
# ```
# # Example Output:
# ```input
# 'John', {{'age': 20, 'city': 'New York'}}
# ```
# """
# code_output_predictor_prompt = """
# # Task: Deduce the Output of a Python Code Snippet Given the Code and Input
# Given the following Code Snippet and the Input, think step by step then deduce the output that will be produced from plugging the Input into the Code Snippet. Put your output in ```output``` tags. Remember if the output is a string, wrap it in quotes. If the function returns multiple values, remember to use a tuple to wrap them.
# # Code Snippet:
# ```python
# {snippet}
# ```
# # Input:
# ```input
# {input_args}
# ```
# # Example Output:
# ```output
# {{'age': 20, 'city': 'New York'}}
# ```
# """
code_error_predictor_prompt = """
# Task: Deduce the Error Type of a Python Code Snippet Given the Code and Input
Given the following Code Snippet and the Input, think step by step to deduce the error type that will be raised when the code is executed. Put your final output in ```output``` tags. If there are no errors, put "NoError" in the ```output``` tags.
# Code Snippet:
```python
{snippet}
```
# Input:
```input
{input_args}
```
# Example Output:
```output
ValueError
```
"""
# code_suffix = "\nf(<|YOUR INPUT WILL BE PLUGGED HERE|>)"
# code_function_predictor_prompt = """
# # Task: Deduce the Function that Produced the Outputs from the Inputs
# Given a set of input/output pairs and a message that describes the function, think through the problem step by step to deduce a general code snippet. This code should produce the hidden outputs from the hidden inputs, matching the original data-generating code that created the input/output pairs. Place your final answer inside python tags! It may be helpful to work through each input/output pair individually to test your function. If your function doesn’t work as expected, revise it until it does. The final code snippet will be used to evaluate your response, which is wrapped in ```python``` tags.
# # Code Requirements:
# - Name the entry function `f` (e.g., `def f(...): ...`), you can have nested definitions inside `f`
# - Ensure the function returns a value
# - Include at least one input parameter
# - Make the function deterministic
# - AVOID THE FOLLOWING:
# * Random functions or variables
# * Date/time operations
# * I/O operations (reading files, network requests)
# * Printing or logging
# * Any external state
# - Ensure execution completes within 10 seconds on a modern CPU
# - All imports and class definitions should be at the very top of the code snippet
# - The snippet should end with a return statement from the main function `f()`, anything after will be removed
# # Input and Output Pairs:
# {input_output_pairs}
# # Message:
# ```message
# {message}
# ```
# # Example Output:
# ```python
# def f(a):
# return a
# ```
# Name your entry function `f()`!!!
# """
#################################
# Changed Prompt #
#################################
code_input_predictor_prompt = """
A conversation between User and Assistant.
The User provides a Python code snippet and its observed output. The Assistant must:
1. **Privately think step-by-step** about which input produces that output.
2. **Output exactly one** `<think>...</think>` block containing your full reasoning.
3. **Then output exactly one** `<answer>...</answer>` block containing **only** the input values—no labels, no comments, no extra text.
4. **Do not** generate any text outside these two blocks.
5. Adhere to the **input rules**.
# Input Rules:
- If an argument is a string, wrap it in quotes.
- For multiple arguments, separate by commas.
- Use Python literal notation for lists, dicts, tuples.
- Boolean values must be `True` or `False`.
User:
# Python Code Snippet:
{snippet}
# Observed Output:
{output}
# Assitant should follow this format:
# Example Response format:
<think>
# 1. Analyze the function signature.
# 2. Walk through the code to see how the observed output arises.
# 3. Identify specific input values that yield that output.
</think>
<answer>
<your input here>
</answer>
Assistant:
"""
code_output_predictor_prompt = """
A conversation between User and Assistant.
The User provides a Python code snippet and specific input values. The Assistant must:
1. **Privately think step-by-step** about how the code executes with the given inputs.
2. **Output exactly one** `<think>...</think>` block containing your full reasoning.
3. **Then output exactly one** `<answer>...</answer>` block containing **only** the output values—no labels, no comments, no extra text.
4. **Do not** generate any text outside these two blocks.
5. Adhere to the **output rules**.
# Output Rules:
- If the output is a string, wrap it in quotes.
- For dicts, lists, and other literals, use valid Python literal notation.
User:
# Python Code Snippet:
{snippet}
# Input:
{input_args}
# Assitant should follow this format:
<think>
# 1. Examine the code and input.
# 2. Walk through execution step by step.
# 3. Determine the exact output produced.
</think>
<answer>
<your output here>
</answer>
Assistant:
"""
code_suffix = "\nf(<|YOUR INPUT WILL BE PLUGGED HERE|>)"
code_function_predictor_prompt = """
A conversation between User and Assistant.
The User provides a set of input/output pairs and a message describing the hidden function. The Assistant must:
1. **Privately think step-by-step** about how to reconstruct the general function based on the provided examples.
2. **Output exactly one** `<think>...</think>` block containing the full reasoning process.
3. **Then output exactly one** `<answer>...</answer>` block containing **only** the Python code snippet defining the function `f`—no labels, no comments, no extra text.
4. **Do not** generate any text outside these two blocks.
5. Follow to the **code requirements** and **formatting rules**.
# Code Requirements:
- Name the entry function `f` (e.g., `def f(...): ...`), you may include nested definitions inside `f`.
- Ensure the function returns a value.
- Include at least one input parameter.
- Make the function deterministic.
- AVOID the FOLLOWING:
* Random functions or variables
* Date/time operations
* I/O operations (reading files, network requests)
* Printing or logging
* Any external state
- Ensure execution completes within 10 seconds on a modern CPU.
- All imports and custom class definitions must be at the very top of the code snippet.
- The snippet must end with a return statement from the main function `f`; anything after will be removed.
User:
# Input and Output Pairs:
{input_output_pairs}
# Message:
{message}
# Assistant should follow this format:
<think>
# 1. Review each input/output pair and the message to understand the pattern.
# 2. Infer the general algorithm or transformation being applied.
# 3. Outline the structure of function `f` that would reproduce all examples.
# 4. Ensure the function meets all requirements.
</think>
<answer>
def f(...):
# your code here
return ...
</answer>
Assistant:
"""
# composite_requirements_prompt = "\n[IMPORTANT CRITERIA!!!] The main function `f` MUST make calls to ALL these functions {function_names} in its body, and you SHOULD NOT provide the definition of {function_names} in your output code snippet. You should first reason step by step about what these functions, {function_names}, do, then write the code snippet.\n" + '\n### The Functions that Must ALL be Called in your Code Snippet: \n```python\n{composite_functions}\n```\n'
composite_requirements_prompt = "\n[IMPORTANT CRITERIA!!!] The main function `f` MUST make calls to ALL these functions {function_names} in its body, and you SHOULD NOT provide the definition of {function_names} in your output code snippet. The function `f` should build on top of {function_names} with extra functionalities, not just a simple wrapper. You should first reason step by step about what these functions, {function_names}, do, then write the code snippet.\n" + '\n### The Functions that Must ALL be Called in your Code Snippet: \n```python\n{composite_functions}\n```\n'
remove_input_from_snippet_prompt = "- Do not have the test input anywhere in the code snippet, provide it in the input section."
remove_singleton_variables_prompt = "- All variable declarations must be inside the main function `f` or within functions `f` make calls to. Any variables declared outside of functions will be removed.\n"
def get_code_problem_generator_prompt(
problem_type: str,
reference_snippets: List[Dict[str, str]],
banned_keywords: List[str],
banned_assertion_keywords: List[str],
composite_functions: List[str] = None,
remove_after_return: bool = False,
num_inputs: int = 10,
remove_input_from_snippet: bool = False,
) -> str:
# assert not (remove_after_return and not remove_input_from_snippet)
composite_functions = list(composite_functions)
snippet_string = ""
if problem_type != 'code_f':
output_key = 'output' if problem_type != 'code_e' else 'error'
for i, snippet in enumerate(reference_snippets):
snippet_string += f"<snippet_{i}>\n```python\n{snippet['snippet']}\n```\n```input\n{snippet['input']}\n```\n```{output_key}\n{snippet['output']}\n```\n</snippet_{i}>\n"
if problem_type == "code_i":
return code_input_prompt.format(
remove_after_return_prompt=(remove_singleton_variables_prompt if remove_after_return else '\n'),
remove_input_from_snippet_prompt=(remove_input_from_snippet_prompt if remove_input_from_snippet else '')
).replace(
'<|BANNED_KEYWORDS|>', ', '.join(banned_keywords)
) + snippet_string + (
composite_requirements_prompt.format(
function_names=', '.join([f'`g_{i}`' for i in range(len(composite_functions))]),
composite_functions="\n".join([d['snippet'] for d in composite_functions])
) if composite_functions else '\n'
)
elif problem_type == "code_o":
return code_output_prompt.format(
remove_after_return_prompt=(remove_singleton_variables_prompt if remove_after_return else '\n'),
remove_input_from_snippet_prompt=(remove_input_from_snippet_prompt if remove_input_from_snippet else '')
).replace(
'<|BANNED_KEYWORDS|>', ', '.join(banned_keywords)
) + snippet_string + (
composite_requirements_prompt.format(
function_names=', '.join([f'`g_{i}`' for i in range(len(composite_functions))]),
composite_functions="\n".join([d['snippet'] for d in composite_functions])
) if composite_functions else '\n'
)
elif problem_type == "code_f":
return code_function_prompt.format(
num_inputs=num_inputs,
snippet=reference_snippets[0]['snippet'] + code_suffix,
)
elif problem_type == "code_e":
if banned_assertion_keywords:
assertion_keywords_string = '- The following error handling keywords are not allowed to be used in the code snippet: ' + ', '.join(banned_assertion_keywords) + '\n'
else:
assertion_keywords_string = '\n'
return code_error_prompt.format(
remove_after_return_prompt=(remove_singleton_variables_prompt if remove_after_return else '\n'),
).replace(
'<|BANNED_KEYWORDS|>', ', '.join(banned_keywords)
).replace(
'<|BANNED_ASSERTION_KEYWORDS|>', assertion_keywords_string
) + snippet_string + (
composite_requirements_prompt.format(
function_names=', '.join([f'`g_{i}`' for i in range(len(composite_functions))]),
composite_functions="\n".join([d['snippet'] for d in composite_functions])
) if composite_functions else '\n'
)
else:
raise ValueError(f"Invalid problem type: {problem_type}")
def get_code_problem_predictor_prompt(problem_type: str, snippet: str, input_args: str = None, output: str = None, message: str = None, input_output_pairs: List[Tuple[str, str]] = None) -> str:
if problem_type.endswith("code_i"):
return code_input_predictor_prompt.format(snippet=snippet, output=output)
elif problem_type.endswith("code_o"):
return code_output_predictor_prompt.format(snippet=snippet, input_args=input_args)
elif problem_type.endswith("code_f"):
input_output_pairs_string = ""
for i, (input, output) in enumerate(input_output_pairs):
input_output_pairs_string += f"```input_{i}\n{input}\n```\n```output_{i}\n{output}\n```\n"
return code_function_predictor_prompt.format(input_output_pairs=input_output_pairs_string, message=message)
elif problem_type.endswith("code_e"):
return code_error_predictor_prompt.format(snippet=snippet, input_args=input_args)
else:
raise ValueError(f"Invalid problem type: {problem_type}")
|