-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_tests.py
113 lines (96 loc) · 3.3 KB
/
run_tests.py
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
import os
import json
import time
import json
import uuid
from dotenv import dotenv_values
from openai import AzureOpenAI
# Load environment variables from .env file
CONFIG = dotenv_values(".env")
API_BASE = CONFIG["AZURE_OPENAI_API_BASE"]
DEPLOYMENT = CONFIG["AZURE_OPENAI_API_MODEL"]
API_VERSION = CONFIG["AZURE_OPENAI_API_VERSION"]
API_KEY = CONFIG["AZURE_OPENAI_API_KEY"]
ENDPOINT = API_BASE + "openai/deployments/" + DEPLOYMENT + "/chat/completions?api-version=" + API_VERSION
ITERATIONS = int(CONFIG["ITERATIONS_PER_PROMPT"])
TEMP = float(CONFIG["TEMPERATURE"])
TOP_P = float(CONFIG["TOP_P"])
MAX_TOKENS = int(CONFIG["MAX_TOKENS"])
SLEEP = int(CONFIG["SLEEP_TIME"])
EXTENSION = CONFIG["OUTPUT_EXTENSION"]
# headers
headers = {
"Content-Type": "application/json",
"api-key": API_KEY
}
client = AzureOpenAI(
azure_endpoint=API_BASE,
api_key=API_KEY,
api_version=API_VERSION,
)
instructions = [
"You are an AI programming assistant.",
"You return only code snippets with NO OTHER TEXT, code fences, etc.",
"Assume your responses will be used in a code editor within an existing HTML document.",
# "Your responses only include the amount of HTML required to properly and validly fulfill the request.",
"You may include inline CSS or JavaScript, but only as much as absolutely necessary."
]
def get_code_response(system_prompt, prompt):
print(f"Prompt: {prompt}")
messages = [
{
"role": "system",
"content": [{
"type": "text",
"text": " ".join(instructions)
}]
},
{
"role": "user",
"content": [{
"type": "text",
"text": prompt
}]
}
]
completion = client.chat.completions.create(
model=DEPLOYMENT,
messages=messages,
max_tokens=MAX_TOKENS,
temperature=TEMP,
top_p=TOP_P,
frequency_penalty=0,
presence_penalty=0,
stop=None,
stream=False
)
result = json.loads(completion.to_json())
code = result['choices'][0]['message']['content']
print(f"Response: {code}")
return code
def process_prompts(test_folder, system_prompt, prefix, prompt, prompt_index):
prompt_folder = os.path.join(test_folder, str(prompt_index))
os.makedirs(prompt_folder, exist_ok=True)
unique_responses = set()
for _ in range(ITERATIONS):
full_prompt = f"{prefix} {prompt}".strip()
response = get_code_response(system_prompt, full_prompt)
if response not in unique_responses:
unique_responses.add(response)
filename = os.path.join(prompt_folder, f"{uuid.uuid4()}.html")
with open(filename, 'w') as response_file:
response_file.write(response)
time.sleep(SLEEP) # To avoid hitting rate limits
def main():
# Load the JSON file
with open('tests.json', 'r') as file:
data = json.load(file)
for test in data['tests']:
test_title = test['title']
test_folder = os.path.join('output', test_title)
os.makedirs(test_folder, exist_ok=True)
prefix = test.get('prefix', '')
for prompt_index, prompt in enumerate(test['prompts'], start=1):
process_prompts(test_folder, instructions, prefix, prompt, prompt_index)
if __name__ == "__main__":
main()