File size: 2,379 Bytes
01f199c | 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 | from typing import List
import tiktoken
import os
import re
from copy import deepcopy
from .Base import BaseStrategy
from models.Base import BaseModel
from datasets.Dataset import Dataset
from results.Results import Results
# self-generate exemplars and knowledge
class AnalogicalStrategy(BaseStrategy):
def parse_code(self, code: str):
if "Python3 code to solve the original problem:" in code:
code = code.split("Python3 code to solve the original problem:")[1].strip()
code_pattern = r'```((.|\n)*?)```'
if "```python" in code:
code_pattern = r'```python((.|\n)*?)```'
code_blocks = re.findall(code_pattern, code, re.DOTALL)
if len(code_blocks) == 0:
if "```" in code:
code = code.replace("```", "")
return code
if type(code_blocks[-1]) == tuple or type(code_blocks[-1]) == list:
code = "\n".join(code_blocks[-1])
elif type(code_blocks[-1]) == str:
code = code_blocks[-1]
return code
def run_single_pass(self, item: dict):
input = [
{
"role": "user",
"content":
f"""Your goal is to write {self.language} code to solve competitive programming problems. Given a problem , explain the core concepts in it and provide other relevant problems. Then solve the original problem.
# Problem:
{self.data.get_prompt(item)}
# Instruction: (Your response must include the following points sequentially)
## Algorithms:
Identify the core concepts or algorithms used to solve the problem.
## Tutorial:
Write a useful tutorial about these algorithms.
## Example Problems:
Provide three examples of relevant competitive programming problems that involve these algorithms. For each problem , describe the problem , explain the solution in detail , and then write the correct Python3 code.
## {self.language} code to solve the original problem:
Include the following points in your response:
- Explanation of the solution:
- {self.language} code to solve the problem (inside ``` ``` block):""",
},
]
print(input[0]['content'])
response, prompt_tokens, completion_tokens = self.gpt_chat(
processed_input=input
)
print(response)
return response, prompt_tokens, completion_tokens
|