-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfuzzing_engine.py
More file actions
89 lines (74 loc) · 3.09 KB
/
fuzzing_engine.py
File metadata and controls
89 lines (74 loc) · 3.09 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
81
82
83
84
85
86
87
88
89
import time
from groq import Groq
from tqdm import tqdm
import json
from config import *
from data.benchmark_prompts import get_all_prompts
class LLMFuzzingEngine:
def __init__(self):
if not GROQ_API_KEY:
raise ValueError("GROQ_API_KEY not found in environment")
self.client = Groq(api_key=GROQ_API_KEY)
self.model = MODEL_NAME
def run_single_inference(self, prompt, temperature):
try:
response = self.client.chat.completions.create(
model=self.model,
messages=[
{
"role": "system",
"content": "You are a helpful assistant. Provide concise, accurate answers."
},
{
"role": "user",
"content": prompt
}
],
temperature=temperature,
max_tokens=MAX_TOKENS,
top_p=1.0,
)
return response.choices[0].message.content
except Exception as e:
print(f"\n Error during inference: {e}")
return None
def fuzz_single_prompt(self, prompt_data, temperature, run_id):
output = self.run_single_inference(prompt_data['prompt'], temperature)
return {
'prompt_id': prompt_data['id'],
'category': prompt_data['category'],
'prompt': prompt_data['prompt'],
'temperature': temperature,
'run_id': run_id,
'output': output,
'timestamp': time.time()
}
def run_full_experiment(self):
prompts = get_all_prompts()
total_experiments = len(prompts) * len(TEMPERATURES) * RUNS_PER_CONDITION
print(f"\n{'='*60}")
print(f"LLM FUZZ-BENCH: Systematic Consistency Analysis")
print(f"{'='*60}")
print(f"Model: {self.model}")
print(f"Prompts: {len(prompts)} ({len(set(p['category'] for p in prompts))} categories)")
print(f"Temperatures: {TEMPERATURES}")
print(f"Runs per condition: {RUNS_PER_CONDITION}")
print(f"Total experiments: {total_experiments}")
print(f"{'='*60}\n")
results = []
with tqdm(total=total_experiments, desc="Running experiments") as pbar:
for prompt_data in prompts:
for temperature in TEMPERATURES:
for run_id in range(RUNS_PER_CONDITION):
result = self.fuzz_single_prompt(prompt_data, temperature, run_id)
results.append(result)
pbar.update(1)
time.sleep(0.1)
output_file = f"{RESULTS_DIR}/raw_results.json"
with open(output_file, 'w') as f:
json.dump(results, f, indent=2)
print(f"\n Experiment complete! Results saved to {output_file}")
return results
if __name__ == "__main__":
engine = LLMFuzzingEngine()
results = engine.run_full_experiment()