|
| 1 | +RSpec.describe AnswerAnalysis::AnswerRelevancyJob do |
| 2 | + include ActiveJob::TestHelper |
| 3 | + |
| 4 | + let(:answer) { create(:answer) } |
| 5 | + let(:question) { answer.question } |
| 6 | + let(:first_result) do |
| 7 | + AutoEvaluation::AnswerRelevancy::Result.new( |
| 8 | + score: 0.8, |
| 9 | + reason: "The first reason.", |
| 10 | + success: true, |
| 11 | + llm_responses: { |
| 12 | + "response_1" => { "content" => "LLM response content 1" }, |
| 13 | + "response_2" => { "content" => "LLM response content 2" }, |
| 14 | + }, |
| 15 | + metrics: { |
| 16 | + "metric_1" => { "detail" => "Metric detail 1" }, |
| 17 | + "metric_2" => { "detail" => "Metric detail 2" }, |
| 18 | + }, |
| 19 | + ) |
| 20 | + end |
| 21 | + let(:second_result) do |
| 22 | + AutoEvaluation::AnswerRelevancy::Result.new( |
| 23 | + score: 0.7, |
| 24 | + reason: "The second reason.", |
| 25 | + success: true, |
| 26 | + llm_responses: { |
| 27 | + "response_3" => { "content" => "LLM response content 3" }, |
| 28 | + "response_4" => { "content" => "LLM response content 4" }, |
| 29 | + }, |
| 30 | + metrics: { |
| 31 | + "metric_3" => { "detail" => "Metric detail 3" }, |
| 32 | + "metric_4" => { "detail" => "Metric detail 4" }, |
| 33 | + }, |
| 34 | + ) |
| 35 | + end |
| 36 | + let(:third_result) do |
| 37 | + AutoEvaluation::AnswerRelevancy::Result.new( |
| 38 | + score: 0.9, |
| 39 | + reason: "The third reason.", |
| 40 | + success: true, |
| 41 | + llm_responses: { |
| 42 | + "response_5" => { "content" => "LLM response content 5" }, |
| 43 | + "response_6" => { "content" => "LLM response content 6" }, |
| 44 | + }, |
| 45 | + metrics: { |
| 46 | + "metric_5" => { "detail" => "Metric detail 5" }, |
| 47 | + "metric_6" => { "detail" => "Metric detail 6" }, |
| 48 | + }, |
| 49 | + ) |
| 50 | + end |
| 51 | + |
| 52 | + before do |
| 53 | + allow(AutoEvaluation::AnswerRelevancy) |
| 54 | + .to receive(:call).and_return(first_result, second_result, third_result) |
| 55 | + stub_const("AnswerAnalysis::BaseMetricJob::NUMBER_OF_RUNS", 3) |
| 56 | + end |
| 57 | + |
| 58 | + it_behaves_like "a job in queue", "default" |
| 59 | + |
| 60 | + describe "#perform" do |
| 61 | + it "calls AutoEvaluation::AnswerRelevancy the configured number of times with the correct arguments" do |
| 62 | + described_class.new.perform(answer.id) |
| 63 | + |
| 64 | + expect(AutoEvaluation::AnswerRelevancy) |
| 65 | + .to have_received(:call) |
| 66 | + .with( |
| 67 | + question_message: question.message, |
| 68 | + answer_message: answer.message, |
| 69 | + ) |
| 70 | + .exactly(3).times |
| 71 | + end |
| 72 | + |
| 73 | + it "creates answer relevancy aggregate with the correct score" do |
| 74 | + expect { |
| 75 | + described_class.new.perform(answer.id) |
| 76 | + }.to change(AnswerAnalysis::AnswerRelevancyAggregate, :count).by(1) |
| 77 | + answer = Answer.includes(:answer_relevancy_aggregate) |
| 78 | + .find(AnswerAnalysis::AnswerRelevancyAggregate.last.answer_id) |
| 79 | + expect(answer.answer_relevancy_aggregate.score.round(2)).to eq(0.8) |
| 80 | + end |
| 81 | + |
| 82 | + it "creates answer relevancy runs for each result" do |
| 83 | + expect { |
| 84 | + described_class.new.perform(answer.id) |
| 85 | + }.to change(AnswerAnalysis::AnswerRelevancyRun, :count).by(3) |
| 86 | + |
| 87 | + answer = Answer.includes(answer_relevancy_aggregate: :runs) |
| 88 | + .find(AnswerAnalysis::AnswerRelevancyAggregate.last.answer_id) |
| 89 | + |
| 90 | + [first_result, second_result, third_result].each_with_index do |result, index| |
| 91 | + expect(answer.answer_relevancy_aggregate.runs[index]) |
| 92 | + .to have_attributes( |
| 93 | + score: result.score.round(2), |
| 94 | + reason: result.reason, |
| 95 | + llm_responses: result.llm_responses, |
| 96 | + metrics: result.metrics, |
| 97 | + ) |
| 98 | + end |
| 99 | + end |
| 100 | + |
| 101 | + context "when the answer has a rephrased_question" do |
| 102 | + let(:rephrased_question) { "This is a rephrased_question" } |
| 103 | + |
| 104 | + it "passes the rephrased question to AutoEvaluation::AnswerRelevancy as the question_message" do |
| 105 | + answer = create(:answer, rephrased_question: rephrased_question) |
| 106 | + |
| 107 | + described_class.new.perform(answer.id) |
| 108 | + |
| 109 | + expect(AutoEvaluation::AnswerRelevancy) |
| 110 | + .to have_received(:call) |
| 111 | + .with( |
| 112 | + question_message: rephrased_question, |
| 113 | + answer_message: answer.message, |
| 114 | + ) |
| 115 | + .exactly(3).times |
| 116 | + end |
| 117 | + end |
| 118 | + |
| 119 | + context "when aggegate data is persisted mid job" do |
| 120 | + before do |
| 121 | + allow(AnswerAnalysis::AnswerRelevancyAggregate) |
| 122 | + .to receive(:exists?) |
| 123 | + .with(answer_id: answer.id) |
| 124 | + .and_return(true) |
| 125 | + end |
| 126 | + |
| 127 | + it "logs a warning" do |
| 128 | + expect(described_class.logger) |
| 129 | + .to receive(:warn) |
| 130 | + .with("Answer #{answer.id} has already been evaluated for relevancy") |
| 131 | + |
| 132 | + described_class.new.perform(answer.id) |
| 133 | + end |
| 134 | + |
| 135 | + it "doesn't create an aggregate or runs" do |
| 136 | + expect { |
| 137 | + described_class.new.perform(answer.id) |
| 138 | + }.to not_change(AnswerAnalysis::AnswerRelevancyAggregate, :count) |
| 139 | + .and not_change(AnswerAnalysis::AnswerRelevancyRun, :count) |
| 140 | + end |
| 141 | + end |
| 142 | + |
| 143 | + context "when the answer does not exist" do |
| 144 | + let(:answer_id) { 999 } |
| 145 | + |
| 146 | + it "logs a warning" do |
| 147 | + expect(described_class.logger) |
| 148 | + .to receive(:warn) |
| 149 | + .with("No answer found for #{answer_id}") |
| 150 | + |
| 151 | + described_class.new.perform(answer_id) |
| 152 | + end |
| 153 | + |
| 154 | + it "doesn't call AutoEvaluation::AnswerRelevancy" do |
| 155 | + described_class.new.perform(answer_id) |
| 156 | + expect(AutoEvaluation::AnswerRelevancy).not_to have_received(:call) |
| 157 | + end |
| 158 | + end |
| 159 | + |
| 160 | + context "when answer relevancy has already been evaluated" do |
| 161 | + let(:aggregate) { create(:answer_relevancy_aggregate) } |
| 162 | + let(:answer) { aggregate.answer } |
| 163 | + |
| 164 | + it "logs a warning" do |
| 165 | + expect(described_class.logger) |
| 166 | + .to receive(:warn) |
| 167 | + .with("Answer #{answer.id} has already been evaluated for relevancy") |
| 168 | + |
| 169 | + described_class.new.perform(answer.id) |
| 170 | + end |
| 171 | + |
| 172 | + it "doesn't call AutoEvaluation::AnswerRelevancy" do |
| 173 | + described_class.new.perform(answer.id) |
| 174 | + expect(AutoEvaluation::AnswerRelevancy).not_to have_received(:call) |
| 175 | + end |
| 176 | + end |
| 177 | + |
| 178 | + context "when the AnswerRelevancy metric raises an Aws::Errors::ServiceError" do |
| 179 | + it "retries the job the max number of times" do |
| 180 | + allow(AutoEvaluation::AnswerRelevancy).to receive(:call) |
| 181 | + .and_raise(Aws::Errors::ServiceError.new(nil, "error")) |
| 182 | + |
| 183 | + (described_class::MAX_RETRIES - 1).times do |
| 184 | + described_class.perform_later(answer.id) |
| 185 | + expect { perform_enqueued_jobs }.not_to raise_error |
| 186 | + end |
| 187 | + |
| 188 | + described_class.perform_later(answer.id) |
| 189 | + expect { perform_enqueued_jobs }.to raise_error(Aws::Errors::ServiceError) |
| 190 | + end |
| 191 | + end |
| 192 | + |
| 193 | + context "when the answer is not eligible for auto-evaluation" do |
| 194 | + let(:answer) { create(:answer, status: Answer.statuses.except(:answered).keys.sample) } |
| 195 | + |
| 196 | + it "logs an info message" do |
| 197 | + expect(described_class.logger) |
| 198 | + .to receive(:info) |
| 199 | + .with("Answer #{answer.id} is not eligible for auto-evaluation") |
| 200 | + |
| 201 | + described_class.new.perform(answer.id) |
| 202 | + end |
| 203 | + |
| 204 | + it "does not call AutoEvaluation::AnswerRelevancy" do |
| 205 | + expect(AutoEvaluation::AnswerRelevancy).not_to receive(:call) |
| 206 | + described_class.new.perform(answer.id) |
| 207 | + end |
| 208 | + end |
| 209 | + end |
| 210 | +end |
0 commit comments