Skip to content

Commit 1a43792

Browse files
committed
Update all analysis references to topics
This goes through the codebase and updates all references from "analysis" to "topics" where relevant. Normally i'd: - add a new table - update the code to write to both tables - backfill data from old table to new table - switch over to the new table But since we're currently not live I don't think it's necessary. API users on integration may have momnentary issues but this should be minimal. I'll need to ensure I rename the tables on BigQuery before we run our next export after this PR is merged.
1 parent f34542f commit 1a43792

25 files changed

Lines changed: 126 additions & 139 deletions

app/controllers/admin/metrics_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def question_routing_guardrails_failures
104104
end
105105

106106
def topics
107-
scope = AnswerAnalysis.joins(:answer)
107+
scope = AnswerTopics.joins(:answer)
108108
.where(answer: { created_at: start_time.. })
109109

110110
primary_topic_scope = scope.where.not(primary_topic: nil)

app/controllers/admin/questions_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ def index
77
def show
88
question_scope = Question.includes(
99
conversation: :signon_user,
10-
answer: [{ sources: :chunk }, :feedback, :analysis],
10+
answer: [{ sources: :chunk }, :feedback, :topics],
1111
)
1212

1313
@question = question_scope.find(params[:id])

app/jobs/answer_topics_job.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,21 @@ class AnswerTopicsJob < ApplicationJob
33
retry_on Anthropic::Errors::APIError, wait: 1.minute, attempts: MAX_RETRIES
44

55
def perform(answer_id)
6-
answer = Answer.includes(:analysis, question: :conversation).find_by(id: answer_id)
6+
answer = Answer.includes(:topics, question: :conversation).find_by(id: answer_id)
77

88
return logger.warn("No answer found for #{answer_id}") unless answer
9-
return logger.warn("Answer #{answer_id} has already been tagged with topics") if answer.analysis&.primary_topic.present?
9+
return logger.warn("Answer #{answer_id} has already been tagged with topics") if answer.topics.present?
1010
unless answer.eligible_for_topic_analysis?
1111
return logger.info("Answer #{answer_id} is not eligible for topic analysis")
1212
end
1313

14-
result = AnswerAnalysisGeneration::TopicTagger.call(answer.rephrased_question || answer.question.message)
15-
analysis = answer.build_analysis(
14+
result = TopicTagger.call(answer.rephrased_question || answer.question.message)
15+
topics = answer.build_topics(
1616
primary_topic: result.primary_topic,
1717
secondary_topic: result.secondary_topic,
18+
llm_response: result.llm_response,
19+
metrics: result.metrics,
1820
)
19-
analysis.assign_metrics("topic_tagger", result.metrics)
20-
analysis.assign_llm_response("topic_tagger", result.llm_response)
21-
analysis.save!
21+
topics.save!
2222
end
2323
end

app/models/admin/filters/questions_filter.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def initialize(...)
3030

3131
def results
3232
@results ||= begin
33-
scope = Question.includes(answer: %i[feedback analysis])
33+
scope = Question.includes(answer: %i[feedback topics])
3434
.left_outer_joins(:answer)
3535
scope = search_scope(scope)
3636
scope = status_scope(scope)
@@ -152,15 +152,15 @@ def question_routing_label_scope(scope)
152152
def primary_topic_scope(scope)
153153
return scope if primary_topic.blank?
154154

155-
scope.joins(answer: :analysis)
156-
.where(answer_analyses: { primary_topic: primary_topic })
155+
scope.joins(answer: :topics)
156+
.where(answer_topics: { primary_topic: primary_topic })
157157
end
158158

159159
def secondary_topic_scope(scope)
160160
return scope if secondary_topic.blank?
161161

162-
scope.joins(answer: :analysis)
163-
.where(answer_analyses: { secondary_topic: secondary_topic })
162+
scope.joins(answer: :topics)
163+
.where(answer_topics: { secondary_topic: secondary_topic })
164164
end
165165

166166
def completeness_scope(scope)

app/models/answer.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def self.response_for_question_routing_label(label)
5454
belongs_to :question
5555
has_many :sources, -> { order(relevancy: :asc) }, class_name: "AnswerSource"
5656
has_one :feedback, class_name: "AnswerFeedback"
57-
has_one :analysis, class_name: "AnswerAnalysis"
57+
has_one :topics, class_name: "AnswerTopics"
5858

5959
enum :status,
6060
{
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
class AnswerAnalysis < ApplicationRecord
2-
include LlmCallsRecordable
3-
1+
class AnswerTopics < ApplicationRecord
42
belongs_to :answer
53

64
scope :exportable, lambda { |start_date, end_date|
@@ -10,6 +8,6 @@ class AnswerAnalysis < ApplicationRecord
108
}
119

1210
def serialize_for_export
13-
as_json(except: :llm_responses).merge("llm_responses" => llm_responses.to_json)
11+
as_json(except: :llm_response).merge("llm_response" => llm_response.to_json)
1412
end
1513
end

app/views/admin/questions/_analysis_tab.html.erb

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,39 @@
22
items: [
33
{
44
field: "Primary topic",
5-
value: analysis.primary_topic&.humanize,
5+
value: topics.primary_topic&.humanize,
66
},
77
{
88
field: "Secondary topic",
9-
value: analysis.secondary_topic&.humanize,
9+
value: topics.secondary_topic&.humanize,
1010
},
1111
],
1212
} %>
1313

14-
<% if analysis.llm_responses.present? %>
14+
<% if topics.llm_response.present? %>
1515
<%= render "govuk_publishing_components/components/details", {
1616
title: "LLM responses",
1717
} do %>
18-
<% analysis.llm_responses.each do |namespace, response| %>
19-
<h3 class="govuk-heading-m"><%= namespace %></h3>
20-
<p class="govuk-body">
21-
<%= render("components/code_snippet", content: JSON.pretty_generate(response)) %>
22-
</p>
23-
<% end %>
18+
<h3 class="govuk-heading-m">Topic tagger</h3>
19+
<p class="govuk-body">
20+
<%= render("components/code_snippet", content: JSON.pretty_generate(topics.llm_response)) %>
21+
</p>
2422
<% end %>
2523
<% end %>
2624

27-
<% if analysis.metrics.present? %>
25+
<% if topics.metrics.present? %>
2826
<%= render "govuk_publishing_components/components/details", {
2927
title: "Metrics",
3028
} do %>
31-
<% analysis.metrics.sort.each do |namespace, metrics| %>
32-
<%= render "govuk_publishing_components/components/summary_list", {
33-
title: namespace,
34-
items: metrics.map do |metric, value|
35-
{
36-
field: metric,
37-
value: value,
38-
}
39-
end,
40-
borderless: true,
41-
} %>
42-
<% end %>
29+
<%= render "govuk_publishing_components/components/summary_list", {
30+
title: "Topic tagger",
31+
items: topics.metrics.map do |metric, value|
32+
{
33+
field: metric,
34+
value: value,
35+
}
36+
end,
37+
borderless: true,
38+
} %>
4339
<% end %>
4440
<% end %>

app/views/admin/questions/show.html.erb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ content_for(:active_navigation_item, admin_questions_path)
3838
label: "Analysis",
3939
content: render(
4040
"analysis_tab",
41-
analysis: @answer.analysis,
41+
topics: @answer.topics,
4242
),
43-
} if @answer&.analysis.present?
43+
} if @answer&.topics.present?
4444
%>
4545

4646
<div class="govuk-grid-row">

config/initializers/inflections.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@
1515
inflect.acronym "OpenAI"
1616
inflect.plural "reason_for_visit", "reason_for_visit"
1717
inflect.plural "found_chat", "found_chat"
18+
inflect.uncountable "answer_topics"
1819
end

lib/bigquery.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ module Bigquery
44
TABLES_TO_EXPORT = [
55
ExportTable.new(name: "questions", time_partitioning_field: "created_at"),
66
ExportTable.new(name: "answer_feedback", time_partitioning_field: "created_at"),
7-
ExportTable.new(name: "answer_analysis", time_partitioning_field: "created_at"),
7+
ExportTable.new(name: "answer_topics", time_partitioning_field: "created_at"),
88
].freeze
99
end

0 commit comments

Comments
 (0)