Class: Evals::PromptEvaluator
- Inherits:
-
Object
- Object
- Evals::PromptEvaluator
- Defined in:
- lib/evals/prompt_evaluator.rb
Instance Method Summary collapse
- #add_assistant_message(messages, text) ⇒ Object
- #add_user_message(messages, text) ⇒ Object
- #chat(messages, system: nil, temperature: 1.0, stop_sequences: []) ⇒ Object
- #generate_dataset(task_description, prompt_inputs_spec: {}, num_cases: 1, output_file: "dataset.json") ⇒ Object
- #generate_test_case(task_description, idea, prompt_inputs_spec = {}) ⇒ Object
- #generate_unique_ideas(task_description, prompt_inputs_spec, num_cases) ⇒ Object
- #grade_output(test_case, output, extra_criteria) ⇒ Object
-
#initialize(max_concurrent_tasks: 3) ⇒ PromptEvaluator
constructor
A new instance of PromptEvaluator.
- #render(template_string, variables) ⇒ Object
- #run_evaluation(run_prompt_function, dataset_file, extra_criteria: nil, json_output_file: "output.json", html_output_file: "output.html") ⇒ Object
- #run_test_case(test_case, run_prompt_function, extra_criteria = nil) ⇒ Object
Constructor Details
#initialize(max_concurrent_tasks: 3) ⇒ PromptEvaluator
Returns a new instance of PromptEvaluator.
7 8 9 10 11 |
# File 'lib/evals/prompt_evaluator.rb', line 7 def initialize(max_concurrent_tasks: 3) @max_concurrent_tasks = max_concurrent_tasks @client = Anthropic::Client.new(api_key: ENV["ANTHROPIC_API_KEY"]) @model = "claude-3-5-haiku-latest" end |
Instance Method Details
#add_assistant_message(messages, text) ⇒ Object
30 31 32 |
# File 'lib/evals/prompt_evaluator.rb', line 30 def (, text) << {role: "assistant", content: text} end |
#add_user_message(messages, text) ⇒ Object
26 27 28 |
# File 'lib/evals/prompt_evaluator.rb', line 26 def (, text) << {role: "user", content: text} end |
#chat(messages, system: nil, temperature: 1.0, stop_sequences: []) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/evals/prompt_evaluator.rb', line 34 def chat(, system: nil, temperature: 1.0, stop_sequences: []) params = { model: @model, max_tokens: 1000, messages: , temperature: temperature, stop_sequences: stop_sequences } params[:system] = system if system response = @client..create(params) response.content[0].text end |
#generate_dataset(task_description, prompt_inputs_spec: {}, num_cases: 1, output_file: "dataset.json") ⇒ Object
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/evals/prompt_evaluator.rb', line 195 def generate_dataset(task_description, prompt_inputs_spec: {}, num_cases: 1, output_file: "dataset.json") ideas = generate_unique_ideas(task_description, prompt_inputs_spec, num_cases) dataset = [] completed = 0 total = ideas.length last_reported_percentage = 0 threads = ideas.map do |idea| Thread.new do generate_test_case(task_description, idea, prompt_inputs_spec) end end threads.each do |thread| result = thread.value completed += 1 current_percentage = ((completed.to_f / total) * 100).to_i milestone_percentage = (current_percentage / 20) * 20 if milestone_percentage > last_reported_percentage puts "Generated #{completed}/#{total} test cases" last_reported_percentage = milestone_percentage end dataset << result rescue => e puts "Error generating test case: #{e}" end File.write(output_file, JSON.pretty_generate(dataset)) dataset end |
#generate_test_case(task_description, idea, prompt_inputs_spec = {}) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/evals/prompt_evaluator.rb', line 117 def generate_test_case(task_description, idea, prompt_inputs_spec = {}) example_prompt_inputs = "" prompt_inputs_spec.each do |key, value| val = value.gsub("\n", "\\n") example_prompt_inputs += "\"#{key}\": \"EXAMPLE_VALUE\", // #{val}\n" end allowed_keys = prompt_inputs_spec.keys.map { |key| "\"#{key}\"" }.join(", ") prompt = " Generate a single detailed test case for a prompt evaluation based on:\n\n <task_description>\n \#{task_description}\n </task_description>\n\n <specific_idea>\n \#{idea}\n </specific_idea>\n\n <allowed_input_keys>\n \#{allowed_keys}\n </allowed_input_keys>\n\n Output Format:\n ```json\n {\n \"prompt_inputs\": {\n \#{example_prompt_inputs}\n },\n \"solution_criteria\": [\"criterion 1\", \"criterion 2\", ...] // Concise list of criteria for evaluating the solution, 1 to 4 items\n }\n ```\n\n IMPORTANT REQUIREMENTS:\n - You MUST ONLY use these exact input keys in your prompt_inputs: \#{allowed_keys}\n - Do NOT add any additional keys to prompt_inputs\n - All keys listed in allowed_input_keys must be included in your response\n - Make the test case realistic and practically useful\n - Include measurable, concise solution criteria\n - The solution criteria should ONLY address the direct requirements of the task description and the generated prompt_inputs\n - Avoid over-specifying criteria with requirements that go beyond the core task\n - Keep solution criteria simple, focused, and directly tied to the fundamental task\n - The test case should be tailored to the specific idea provided\n - Quick to solve without requiring extensive computation or multi-step processing\n - Solvable with no more than 400 tokens of output\n - DO NOT include any fields beyond those specified in the output format\n TEXT\n\n system_prompt = \"You are a test case creator specializing in designing evaluation scenarios.\"\n\n rendered_prompt = render(\n prompt.strip,\n {\n \"allowed_keys\" => allowed_keys,\n \"task_description\" => task_description,\n \"idea\" => idea,\n \"example_prompt_inputs\" => example_prompt_inputs\n }\n )\n\n messages = []\n add_user_message(messages, rendered_prompt)\n add_assistant_message(messages, \"```json\")\n text = chat(\n messages,\n stop_sequences: [\"```\"],\n system: system_prompt,\n temperature: 0.7\n )\n\n test_case = JSON.parse(text)\n test_case[\"task_description\"] = task_description\n test_case[\"scenario\"] = idea\n\n test_case\nend\n" |
#generate_unique_ideas(task_description, prompt_inputs_spec, num_cases) ⇒ Object
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 114 115 |
# File 'lib/evals/prompt_evaluator.rb', line 49 def generate_unique_ideas(task_description, prompt_inputs_spec, num_cases) prompt = " Generate \#{num_cases} unique, diverse ideas for testing a prompt that accomplishes this task:\n\n <task_description>\n \#{task_description}\n </task_description>\n\n The prompt will receive the following inputs\n <prompt_inputs>\n \#{prompt_inputs_spec}\n </prompt_inputs>\n\n Each idea should represent a distinct scenario or example that tests different aspects of the task.\n\n Output Format:\n Provide your response as a structured JSON array where each item is a brief description of the idea.\n\n Example:\n ```json\n [\n \"Testing with technical computer science terminology\",\n \"Testing with medical research findings\",\n \"Testing with complex mathematical concepts\",\n ...\n ]\n ```\n\n Ensure each idea is:\n - Clearly distinct from the others\n - Relevant to the task description\n - Specific enough to guide generation of a full test case\n - Quick to solve without requiring extensive computation or multi-step processing\n - Solvable with no more than 400 tokens of output\n\n Remember, only generate \#{num_cases} unique ideas\n TEXT\n\n system_prompt = \"You are a test scenario designer specialized in creating diverse, unique testing scenarios.\"\n\n example_prompt_inputs = \"\"\n prompt_inputs_spec.each do |key, value|\n val = value.gsub(\"\\n\", \"\\\\n\")\n example_prompt_inputs += \"\\\"\#{key}\\\": str # \#{val},\"\n end\n\n rendered_prompt = render(\n prompt.strip,\n {\n \"task_description\" => task_description,\n \"num_cases\" => num_cases,\n \"prompt_inputs\" => example_prompt_inputs\n }\n )\n\n messages = []\n add_user_message(messages, rendered_prompt)\n add_assistant_message(messages, \"```json\")\n text = chat(\n messages,\n stop_sequences: [\"```\"],\n system: system_prompt,\n temperature: 1.0\n )\n\n JSON.parse(text)\nend\n" |
#grade_output(test_case, output, extra_criteria) ⇒ Object
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 |
# File 'lib/evals/prompt_evaluator.rb', line 229 def grade_output(test_case, output, extra_criteria) prompt_inputs = "" test_case["prompt_inputs"].each do |key, value| val = value.gsub("\n", "\\n") prompt_inputs += "\"#{key}\":\"#{val}\",\n" end extra_criteria_section = "" if extra_criteria extra_criteria_template = " Mandatory Requirements - ANY VIOLATION MEANS AUTOMATIC FAILURE (score of 3 or lower):\n <extra_important_criteria>\n \#{extra_criteria}\n </extra_important_criteria>\n TEXT\n extra_criteria_section = render(\n extra_criteria_template.strip,\n {\"extra_criteria\" => extra_criteria}\n )\n end\n\n eval_template = <<~TEXT\n Your task is to evaluate the following AI-generated solution with EXTREME RIGOR.\n\n Original task description:\n <task_description>\n \#{test_case[\"task_description\"]}\n </task_description>\n\n Original task inputs:\n <task_inputs>\n { \#{prompt_inputs} }\n </task_inputs>\n\n Solution to Evaluate:\n <solution>\n \#{output}\n </solution>\n\n Criteria you should use to evaluate the solution:\n <criteria>\n \#{test_case[\"solution_criteria\"].join(\"\\n\")}\n </criteria>\n\n \#{extra_criteria_section}\n\n Scoring Guidelines:\n * Score 1-3: Solution fails to meet one or more MANDATORY requirements\n * Score 4-6: Solution meets all mandatory requirements but has significant deficiencies in secondary criteria\n * Score 7-8: Solution meets all mandatory requirements and most secondary criteria, with minor issues\n * Score 9-10: Solution meets all mandatory and secondary criteria\n\n IMPORTANT SCORING INSTRUCTIONS:\n * Grade the output based ONLY on the listed criteria. Do not add your own extra requirements.\n * If a solution meets all of the mandatory and secondary criteria give it a 10\n * Don't complain that the solution \"only\" meets the mandatory and secondary criteria. Solutions shouldn't go above and beyond - they should meet the exact listed criteria.\n * ANY violation of a mandatory requirement MUST result in a score of 3 or lower\n * The full 1-10 scale should be utilized - don't hesitate to give low scores when warranted\n\n Output Format\n Provide your evaluation as a structured JSON object with the following fields, in this specific order:\n - \"strengths\": An array of 1-3 key strengths\n - \"weaknesses\": An array of 1-3 key areas for improvement\n - \"reasoning\": A concise explanation of your overall assessment\n - \"score\": A number between 1-10\n\n Respond with JSON. Keep your response concise and direct.\n TEXT\n\n eval_prompt = render(\n eval_template.strip,\n {\n \"task_description\" => test_case[\"task_description\"],\n \"prompt_inputs\" => prompt_inputs,\n \"output\" => output,\n \"solution_criteria\" => test_case[\"solution_criteria\"].join(\"\\n\"),\n \"extra_criteria_section\" => extra_criteria_section\n }\n )\n\n messages = []\n add_user_message(messages, eval_prompt)\n add_assistant_message(messages, \"```json\")\n eval_text = chat(\n messages,\n stop_sequences: [\"```\"],\n temperature: 0.0\n )\n\n JSON.parse(eval_text)\nend\n" |
#render(template_string, variables) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/evals/prompt_evaluator.rb', line 13 def render(template_string, variables) placeholders = template_string.scan(/{([^{}]+)}/) result = template_string placeholders.flatten.each do |placeholder| if variables.key?(placeholder) result = result.gsub("{#{placeholder}}", variables[placeholder].to_s) end end result.gsub("{{", "{").gsub("}}", "}") end |
#run_evaluation(run_prompt_function, dataset_file, extra_criteria: nil, json_output_file: "output.json", html_output_file: "output.html") ⇒ Object
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 |
# File 'lib/evals/prompt_evaluator.rb', line 336 def run_evaluation(run_prompt_function, dataset_file, extra_criteria: nil, json_output_file: "output.json", html_output_file: "output.html") dataset = JSON.parse(File.read(dataset_file)) results = [] completed = 0 total = dataset.length last_reported_percentage = 0 threads = dataset.map do |test_case| Thread.new do run_test_case(test_case, run_prompt_function, extra_criteria) end end threads.each do |thread| result = thread.value completed += 1 current_percentage = ((completed.to_f / total) * 100).to_i milestone_percentage = (current_percentage / 20) * 20 if milestone_percentage > last_reported_percentage puts "Graded #{completed}/#{total} test cases" last_reported_percentage = milestone_percentage end results << result end average_score = results.sum { |result| result["score"] } / results.length.to_f puts "Average score: #{average_score}" File.write(json_output_file, JSON.pretty_generate(results)) html = generate_prompt_evaluation_report(results) File.write(html_output_file, html) results end |
#run_test_case(test_case, run_prompt_function, extra_criteria = nil) ⇒ Object
321 322 323 324 325 326 327 328 329 330 331 332 333 334 |
# File 'lib/evals/prompt_evaluator.rb', line 321 def run_test_case(test_case, run_prompt_function, extra_criteria = nil) output = run_prompt_function.call(test_case["prompt_inputs"]) model_grade = grade_output(test_case, output, extra_criteria) model_score = model_grade["score"] reasoning = model_grade["reasoning"] { "output" => output, "test_case" => test_case, "score" => model_score, "reasoning" => reasoning } end |