Class: Raif::Evals::LlmJudges::Comparative

Inherits:
Raif::Evals::LlmJudge show all
Defined in:
lib/raif/evals/llm_judges/comparative.rb

Constant Summary

Constants included from Concerns::LlmResponseParsing

Concerns::LlmResponseParsing::ASCII_CONTROL_CHARS

Instance Attribute Summary collapse

Attributes inherited from Task

#files, #images

Instance Method Summary collapse

Methods inherited from Raif::Evals::LlmJudge

#default_llm_model_key, #judgment_confidence, #judgment_reasoning, #low_confidence?

Methods inherited from Task

json_response_schema, prompt, #re_run, run, #run, #status, system_prompt

Methods included from Concerns::LlmResponseParsing

#parse_html_response, #parse_json_response, #parsed_response

Methods included from Concerns::HasAvailableModelTools

#available_model_tools_map

Methods included from Concerns::HasRequestedLanguage

#requested_language_name, #system_prompt_language_preference

Methods included from Concerns::HasLlm

#default_llm_model_key, #llm

Methods inherited from ApplicationRecord

table_name_prefix

Instance Attribute Details

#content_aObject

Returns the value of attribute content_a.



11
12
13
# File 'lib/raif/evals/llm_judges/comparative.rb', line 11

def content_a
  @content_a
end

#content_bObject

Returns the value of attribute content_b.



11
12
13
# File 'lib/raif/evals/llm_judges/comparative.rb', line 11

def content_b
  @content_b
end

#expected_winnerObject

Returns the value of attribute expected_winner.



11
12
13
# File 'lib/raif/evals/llm_judges/comparative.rb', line 11

def expected_winner
  @expected_winner
end

Instance Method Details

#build_promptObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/raif/evals/llm_judges/comparative.rb', line 43

def build_prompt
  <<~PROMPT.strip
    Comparison criteria: #{comparison_criteria}
    #{additional_context_prompt}
    Compare the following two pieces of content:

    CONTENT A:
    #{content_a}

    CONTENT B:
    #{content_b}

    Which content better meets the comparison criteria?
  PROMPT
end

#build_system_promptObject



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/raif/evals/llm_judges/comparative.rb', line 31

def build_system_prompt
  <<~PROMPT.strip
    You are an expert evaluator comparing two pieces of content to determine which better meets specified criteria.

    #{allow_ties ? "You may declare a tie if both pieces of content are equally good." : "You must choose a winner even if the difference is minimal."}

    First, provide detailed reasoning for your choice. Then, provide a precise winner #{allow_ties ? "(A, B, or tie)" : "(A or B)"}.

    Respond with JSON matching the required schema.
  PROMPT
end

#correct_expected_winner?Boolean

Returns:

  • (Boolean)


69
70
71
72
73
# File 'lib/raif/evals/llm_judges/comparative.rb', line 69

def correct_expected_winner?
  return unless completed? # rubocop:disable Style/ReturnNilInPredicateMethodDefinition

  parsed_response["winner"] == expected_winner
end

#tie?Boolean

Returns:

  • (Boolean)


63
64
65
66
67
# File 'lib/raif/evals/llm_judges/comparative.rb', line 63

def tie?
  return unless completed? # rubocop:disable Style/ReturnNilInPredicateMethodDefinition

  parsed_response["winner"] == "tie"
end

#winnerObject



59
60
61
# File 'lib/raif/evals/llm_judges/comparative.rb', line 59

def winner
  parsed_response["winner"] if completed?
end