-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathanswer_topics_job.rb
More file actions
23 lines (20 loc) · 966 Bytes
/
answer_topics_job.rb
File metadata and controls
23 lines (20 loc) · 966 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class AnswerTopicsJob < ApplicationJob
MAX_RETRIES = 5
retry_on Anthropic::Errors::APIError, wait: 1.minute, attempts: MAX_RETRIES
def perform(answer_id)
answer = Answer.includes(:topics, question: :conversation).find_by(id: answer_id)
return logger.warn("No answer found for #{answer_id}") unless answer
return logger.warn("Answer #{answer_id} has already been tagged with topics") if answer.topics&.primary_topic.present?
unless answer.eligible_for_topic_analysis?
return logger.info("Answer #{answer_id} is not eligible for topic analysis")
end
result = AutoEvaluation::TopicTagger.call(answer.rephrased_question || answer.question.message)
topics = answer.build_topics(
primary_topic: result.primary_topic,
secondary_topic: result.secondary_topic,
)
topics.assign_metrics("topic_tagger", result.metrics)
topics.assign_llm_response("topic_tagger", result.llm_response)
topics.save!
end
end