Spaces:
Running
Running
File size: 2,785 Bytes
a807fc1 49c830d a807fc1 49c830d 3f91eb4 682ff2e 49c830d 682ff2e 49c830d 3f91eb4 a807fc1 3f91eb4 a807fc1 |
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 |
import os
import re
from typing import Optional, Union
from llms import build_prompt, get_completion, PROMPT_TEMPLATE, SYSTEM_PROMPT
def generate_code(problem_description: str, prompt_template: str, system_prompt: str, model: str, temperature: float):
prompt = build_prompt(problem_description, prompt_template)
completion = get_completion(prompt, system_prompt, model, temperature)
return completion
def load_txt_file(file_path: str):
with open(file_path, "r") as file:
return file.read()
def load_problem_description(day: int):
day_str = f"{day:02d}"
file_path = f"day{day_str}/problem.txt"
return load_txt_file(file_path)
def extract_code(completion: str) -> str:
"""Extracts the code from the completion, should be contained in ```python ...``` code block."""
code_block = re.search(r"```python\s*([\s\S]*?)\s*```", completion)
if code_block:
return code_block.group(1)
def get_solution_file_path(model: str, day: Optional[Union[str, int]] = None):
"""Returns the path to the solution file for the given day and model. If day is None, returns the path to the solution file only."""
if day is None:
return f"solution_{model}.py"
# We want it formatted properly, so we convert to int and back if its already a string for convenience
if isinstance(day, str):
day = int(day)
day_str = f"{day:02d}"
return f"day{day_str}/solution_{model}.py"
def save_code(day: int, code: str, model: str):
"""Saves the code to the solution file for the given day and model."""
file_path = get_solution_file_path(day, model)
with open(file_path, "w") as file:
file.write(code)
print(f"Saved code to {file_path}")
all_models = {
"openai": ["gpt-4o"],
"gemini": ["gemini-1.5-pro"],
"anthropic": ["claude-3-5-sonnet-20241022"],
}
if __name__ == "__main__":
for day in range(1, 26):
problem_description = load_problem_description(day)
print(f"***Generating code for day {day}***")
for provider in all_models:
for model in all_models[provider]:
print("-" * 80)
print(f"Generating code for {provider} {model}")
if os.path.exists(get_solution_file_path(model=model, day=day)):
print(f"Skipping {provider} {model} for day {day} because it already exists")
continue
prompt = build_prompt(problem_description, PROMPT_TEMPLATE)
completion = get_completion(provider=provider, user_prompt=prompt, system_prompt=SYSTEM_PROMPT, model=model, temperature=0)
code = extract_code(completion)
save_code(day, code, model)
print("-" * 80)
print("*" * 80) |