-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode_execution_accuracy.py
More file actions
80 lines (72 loc) · 3.37 KB
/
code_execution_accuracy.py
File metadata and controls
80 lines (72 loc) · 3.37 KB
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 subprocess
import time
import openai # Replace with an appropriate LLM library (e.g., llama_index, transformers)
import pandas as pd
# Define OpenAI API Key
OPENAI_API_KEY = "api-key"
openai.api_key = OPENAI_API_KEY # Set API Key globally
# Define base coding challenges
challenges = {
"python": "Write a Python function to compute the sum of a list of numbers.",
"javascript": "Write a JavaScript function to compute the sum of an array of numbers.",
"cpp": "Write a C++ program to compute the sum of numbers in an array."
}
def generate_code_prompt_variations(base_prompt, model="gpt-4"):
"Use LLM to generate multiple optimized versions of the given coding challenge."
response = openai.ChatCompletion.create(
model=model,
messages=[
{"role": "system", "content": "You are an expert code evaluator. Generate optimized prompts for coding challenges."},
{"role": "user", "content": f"Generate 3 variations of this prompt for better code quality and performance:\n{base_prompt}"}
]
)
return [choice["message"]["content"] for choice in response["choices"]]
def execute_code(language, code):
"Executes code in the given language and captures output."
start_time = time.time()
try:
if language == "python":
result = subprocess.run(["python3", "-c", code], capture_output=True, text=True, timeout=5)
elif language == "javascript":
result = subprocess.run(["node", "-e", code], capture_output=True, text=True, timeout=5)
elif language == "cpp":
with open("test.cpp", "w") as f:
f.write(code)
subprocess.run(["g++", "test.cpp", "-o", "test.out"], capture_output=True, text=True)
result = subprocess.run(["./test.out"], capture_output=True, text=True, timeout=5)
else:
return "Unsupported language", "", 0.0
execution_time = time.time() - start_time
return result.stdout, result.stderr, execution_time
except subprocess.TimeoutExpired:
return "Timeout", "", 5.0
except Exception as e:
return "", str(e), 0.0
def evaluate_prompt_optimization():
"Runs the optimized prompt tests and evaluates performance."
results = []
for lang, base_prompt in challenges.items():
variations = generate_code_prompt_variations(base_prompt)
for i, variation in enumerate(variations):
response = openai.ChatCompletion.create(
model="gpt-4",
messages=[
{"role": "system", "content": "Generate optimized code based on the given prompt."},
{"role": "user", "content": variation}
]
)
generated_code = response["choices"][0]["message"]["content"]
output, error, exec_time = execute_code(lang, generated_code)
results.append({
"Language": lang,
"Prompt Variation": variation,
"Generated Code": generated_code,
"Output": output,
"Error": error,
"Execution Time (s)": exec_time
})
return results
if __name__ == "__main__":
results_df = pd.DataFrame(evaluate_prompt_optimization())
results_df.to_csv("prompt_optimization_results.csv", index=False)
print("Results saved to prompt_optimization_results.csv")