Skip to content

Commit cbad73d

Browse files
committed
Move topics job into AnswerAnalysis module
This is run as part of our answer analysis so it makes sense to move it into the AnswerAnalysis module.
1 parent 917b063 commit cbad73d

7 files changed

Lines changed: 38 additions & 29 deletions

File tree

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
module AnswerAnalysis
2+
class AnswerTopicsJob < ApplicationJob
3+
MAX_RETRIES = 5
4+
retry_on Anthropic::Errors::APIError, wait: 1.minute, attempts: MAX_RETRIES
5+
6+
def perform(answer_id)
7+
answer = Answer.includes(:topics, question: :conversation).find_by(id: answer_id)
8+
9+
return logger.warn("No answer found for #{answer_id}") unless answer
10+
return logger.warn("Answer #{answer_id} has already been tagged with topics") if answer.topics.present?
11+
unless answer.eligible_for_topic_analysis?
12+
return logger.info("Answer #{answer_id} is not eligible for topic analysis")
13+
end
14+
15+
result = TopicTagger.call(answer.rephrased_question || answer.question.message)
16+
topics = answer.build_topics(
17+
primary_topic: result.primary_topic,
18+
secondary_topic: result.secondary_topic,
19+
llm_response: result.llm_response,
20+
metrics: result.metrics,
21+
)
22+
topics.save!
23+
end
24+
end
25+
end

app/jobs/answer_topics_job.rb

Lines changed: 0 additions & 23 deletions
This file was deleted.

app/jobs/compose_answer_job.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def perform(question_id)
1717
if answer.persisted?
1818
# TODO: Once we've added a few metrics we should move these to a single job that
1919
# kicks off all analysis jobs.
20-
AnswerTopicsJob.perform_later(answer.id)
20+
AnswerAnalysis::AnswerTopicsJob.perform_later(answer.id)
2121
AnswerAnalysis::AnswerRelevancyJob.perform_later(answer.id)
2222
end
2323
end
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class AutoEvaluationMetricRun < ApplicationRecord
2+
include LlmCallsRecordable
3+
4+
belongs_to :metric_aggregate,
5+
class_name: "AutoEvaluationMetricAggregate",
6+
foreign_key: :auto_evaluation_metric_aggregate_id
7+
end

spec/jobs/answer_topics_job_spec.rb renamed to spec/jobs/answer_analysis/answer_topics_job_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
RSpec.describe AnswerTopicsJob do
1+
RSpec.describe AnswerAnalysis::AnswerTopicsJob do
22
include ActiveJob::TestHelper
33
let(:answer) { create(:answer) }
44
let(:question) { answer.question }

spec/jobs/compose_answer_job_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
before do
77
allow(AnswerComposition::Composer).to receive(:call).and_return(returned_answer)
8-
allow(AnswerTopicsJob).to receive(:perform_later)
8+
allow(AnswerAnalysis::AnswerTopicsJob).to receive(:perform_later)
99
allow(AnswerAnalysis::AnswerRelevancyJob).to receive(:perform_later)
1010
end
1111

@@ -18,9 +18,9 @@
1818
.and change(AnswerSource, :count).by(2)
1919
end
2020

21-
it "calls the AnswerTopicsJob with the answer_id" do
21+
it "calls the AnswerAnalysis::AnswerTopicsJob with the answer_id" do
2222
described_class.new.perform(question.id)
23-
expect(AnswerTopicsJob).to have_received(:perform_later).with(returned_answer.id)
23+
expect(AnswerAnalysis::AnswerTopicsJob).to have_received(:perform_later).with(returned_answer.id)
2424
end
2525

2626
it "calls the AnswerAnalysis::AnswerRelevancyJob with the answer_id" do

spec/requests/api/v1/conversation_flow_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def when_i_create_a_conversation
7373
status: :answered,
7474
)
7575
end
76-
allow(AnswerTopicsJob).to receive(:perform_later)
76+
allow(AnswerAnalysis::AnswerTopicsJob).to receive(:perform_later)
7777
allow(AnswerAnalysis::AnswerRelevancyJob).to receive(:perform_later)
7878

7979
post api_v1_create_conversation_path,

0 commit comments

Comments
 (0)