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

Inherits:
Raif::Evals::LlmJudge show all
Defined in:
app/models/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, #json_response_schema, #messages, prompt, #prompt_studio_task_attributes, #re_run, run, #run, #status, system_prompt

Methods included from Concerns::JsonSchemaDefinition

#schema_for_instance

Methods included from Concerns::LlmResponseParsing

#parse_html_response, #parse_json_response, #parsed_response

Methods included from Concerns::HasRuntimeDuration

#runtime_duration, #runtime_duration_seconds, #runtime_ended_at

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, where_json_not_blank

Instance Attribute Details

#content_aObject

Returns the value of attribute content_a.



49
50
51
# File 'app/models/raif/evals/llm_judges/comparative.rb', line 49

def content_a
  @content_a
end

#content_bObject

Returns the value of attribute content_b.



49
50
51
# File 'app/models/raif/evals/llm_judges/comparative.rb', line 49

def content_b
  @content_b
end

#expected_winnerObject

Returns the value of attribute expected_winner.



49
50
51
# File 'app/models/raif/evals/llm_judges/comparative.rb', line 49

def expected_winner
  @expected_winner
end

Instance Method Details

#build_promptObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'app/models/raif/evals/llm_judges/comparative.rb', line 81

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



69
70
71
72
73
74
75
76
77
78
79
# File 'app/models/raif/evals/llm_judges/comparative.rb', line 69

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)


107
108
109
110
111
# File 'app/models/raif/evals/llm_judges/comparative.rb', line 107

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

  parsed_response["winner"] == expected_winner
end

#tie?Boolean

Returns:

  • (Boolean)


101
102
103
104
105
# File 'app/models/raif/evals/llm_judges/comparative.rb', line 101

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

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

#winnerObject



97
98
99
# File 'app/models/raif/evals/llm_judges/comparative.rb', line 97

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