|
1 | 1 | namespace "guardrails" do |
2 | | - desc "Output guardrail evaluation using Guardrails::MultipleChecker" |
3 | | - task :evaluate_guardrails, %i[guardrail_type dataset_path output_path llm_provider] => :environment do |_, args| |
4 | | - guardrail_type = args[:guardrail_type].to_sym |
5 | | - valid_guardrail_types = %i[answer_guardrails question_routing_guardrails] |
6 | | - if guardrail_type.blank? || valid_guardrail_types.exclude?(guardrail_type) |
7 | | - abort("Invalid guardrail type. Valid guardrail types are #{valid_guardrail_types.to_sentence}") |
8 | | - end |
9 | | - |
10 | | - dataset_path = args[:dataset_path] |
11 | | - if dataset_path.blank? |
12 | | - abort("No dataset path provided") |
13 | | - end |
14 | | - |
15 | | - dataset_absolute_path = Pathname.new(Dir.pwd).join(args[:dataset_path]) |
16 | | - unless File.exist?(dataset_absolute_path) |
17 | | - abort("No file found at #{dataset_absolute_path}") |
18 | | - end |
19 | | - |
20 | | - output_path = args[:output_path] |
21 | | - llm_provider = (args[:llm_provider] || :openai).to_sym |
22 | | - valid_providers = %i[openai claude] |
23 | | - if valid_providers.exclude?(llm_provider) |
24 | | - abort("Invalid LLM provider. Valid providers are #{valid_providers.to_sentence}") |
25 | | - end |
26 | | - |
27 | | - true_eval = ->(v) { v != "False | None" } |
28 | | - |
29 | | - prompt_token_counts = [] |
30 | | - |
31 | | - results = Guardrails::Evaluation.call(dataset_absolute_path, true_eval:) do |input| |
32 | | - result = Guardrails::MultipleChecker.call(input, guardrail_type, llm_provider) |
33 | | - prompt_token_counts << result.llm_prompt_tokens |
34 | | - result.llm_guardrail_result |
35 | | - rescue Guardrails::MultipleChecker::ResponseError => e |
36 | | - prompt_token_counts << e.llm_prompt_tokens |
37 | | - "ERR: #{e.llm_response}" |
38 | | - end |
39 | | - |
40 | | - average_prompt_token_count = prompt_token_counts.sum / prompt_token_counts.size |
41 | | - |
42 | | - results.merge!( |
43 | | - average_prompt_token_count:, |
44 | | - max_prompt_token_count: prompt_token_counts.max, |
45 | | - ) |
46 | | - |
47 | | - if output_path.nil? |
48 | | - pp results |
49 | | - else |
50 | | - pp results.slice(:model, :count, :percent_correct, :precision, :recall, :average_latency) |
51 | | - File.write(output_path, JSON.pretty_generate(results)) |
52 | | - |
53 | | - puts "Full results have been saved to: #{output_path}" |
54 | | - end |
55 | | - end |
56 | | - |
57 | 2 | desc "Print prompts for a guardrail type" |
58 | 3 | task :print_prompts, %i[guardrail_type llm_provider] => :environment do |_, args| |
59 | 4 | guardrail_type = args[:guardrail_type].to_sym |
|
0 commit comments