-
Notifications
You must be signed in to change notification settings - Fork 3
Add answer relevancy models and integrate into analysis workflow #713
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
davidgisbey
merged 7 commits into
main
from
add-metrics-data-models-and-integrate-into-workflow
Jan 7, 2026
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a3c1e4b
Add AnswerRelevancyAggregate & AnswerRelevancyRun
davidgisbey 73780fd
Add additional bedrock stub to stub all answer relevancy calls
davidgisbey f8fb85e
Add auto_evaluation_results_creatable concern
davidgisbey ccbda92
Add AnswerAnalysis AnswerRelevancy and Base jobs
davidgisbey 9ace316
Integraate Answer Relevancy Analysis into analysis workflow
davidgisbey 6f36362
Expose answer relevancy metrics in admin UI
davidgisbey 4cabe13
Add Answer#question_used
davidgisbey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| module AnswerAnalysis | ||
| class AnswerRelevancyJob < BaseJob | ||
| def perform(answer_id) | ||
| return unless eligible_for_answer_analysis?(answer_id) | ||
|
|
||
| answer = Answer.includes(:question, :answer_relevancy_aggregate).find(answer_id) | ||
| return logger.warn(aggregate_exists_warn_message(answer.id)) if answer.answer_relevancy_aggregate.present? | ||
|
|
||
| results = NUMBER_OF_RUNS.times.map { AutoEvaluation::AnswerRelevancy.call(answer) } | ||
|
|
||
| begin | ||
| AnswerAnalysis::AnswerRelevancyAggregate.create_mean_aggregate_and_score_runs(answer, results) | ||
| rescue ActiveRecord::RecordNotUnique | ||
| logger.warn(aggregate_exists_warn_message(answer.id)) | ||
| end | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def aggregate_exists_warn_message(answer_id) | ||
| "Answer #{answer_id} has already been evaluated for relevancy" | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| module AnswerAnalysis | ||
| class BaseJob < ApplicationJob | ||
| NUMBER_OF_RUNS = 3 | ||
| MAX_RETRIES = 5 | ||
| retry_on Aws::Errors::ServiceError, wait: 1.minute, attempts: MAX_RETRIES | ||
|
|
||
| private | ||
|
|
||
| def eligible_for_answer_analysis?(answer_id) | ||
| eligible = Answer.status_answered.exists?(id: answer_id) | ||
|
|
||
| unless eligible | ||
| logger.warn("Couldn't find an answer #{answer_id} that was eligible for auto-evaluation") | ||
| end | ||
|
|
||
| eligible | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| module AnswerAnalysis | ||
| class AnswerRelevancyAggregate < ApplicationRecord | ||
| include AutoEvaluationResultsCreatable | ||
|
|
||
| self.table_name = "answer_analysis_answer_relevancy_aggregates" | ||
|
|
||
| belongs_to :answer | ||
| has_many :runs, | ||
|
davidgisbey marked this conversation as resolved.
|
||
| -> { order(:created_at) }, | ||
| class_name: "AnswerAnalysis::AnswerRelevancyRun", | ||
| foreign_key: :answer_analysis_answer_relevancy_aggregate_id | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| module AnswerAnalysis | ||
| class AnswerRelevancyRun < ApplicationRecord | ||
| include LlmCallsRecordable | ||
|
|
||
| self.table_name = "answer_analysis_answer_relevancy_runs" | ||
|
|
||
| belongs_to :aggregate, | ||
| class_name: "AnswerAnalysis::AnswerRelevancyAggregate", | ||
| foreign_key: :answer_analysis_answer_relevancy_aggregate_id | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| module AutoEvaluationResultsCreatable | ||
| extend ActiveSupport::Concern | ||
|
|
||
| class_methods do | ||
| def create_mean_aggregate_and_score_runs(answer, results) | ||
| mean_score = results.map { |result| result.score.to_d }.sum / results.size | ||
| aggregate = new(answer:, mean_score:) | ||
|
|
||
| results.each do |result| | ||
| run = aggregate.runs.build( | ||
| aggregate:, | ||
| score: result.score, | ||
| reason: result.reason, | ||
| ) | ||
|
|
||
| result.llm_responses.stringify_keys.each do |name, llm_response| | ||
| run.assign_llm_response(name, llm_response) | ||
| end | ||
| result.metrics.stringify_keys.each do |name, metrics| | ||
| run.assign_metrics(name, metrics) | ||
| end | ||
| end | ||
|
|
||
| aggregate.save! | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,41 +1,51 @@ | ||
| <%= render "govuk_publishing_components/components/summary_list", { | ||
| items: [ | ||
| { | ||
| field: "Primary topic", | ||
| value: topics.primary_topic&.humanize, | ||
| }, | ||
| { | ||
| field: "Secondary topic", | ||
| value: topics.secondary_topic&.humanize, | ||
| }, | ||
| ], | ||
| } %> | ||
| <% if topics.present? %> | ||
| <%= render "govuk_publishing_components/components/summary_list", { | ||
| title: "Topics", | ||
| heading_size: "l", | ||
| heading_level: 2, | ||
| margin_bottom: 4, | ||
| items: [ | ||
| { | ||
| field: "Primary topic", | ||
| value: topics.primary_topic.humanize, | ||
| }, | ||
| { | ||
| field: "Secondary topic", | ||
| value: topics.secondary_topic&.humanize, | ||
| }, | ||
| ], | ||
| } %> | ||
|
|
||
| <% if topics.llm_responses.present? %> | ||
| <%= render "govuk_publishing_components/components/details", { | ||
| title: "LLM responses", | ||
| } do %> | ||
| <% topics.llm_responses.each do |namespace, response| %> | ||
| <h3 class="govuk-heading-m"><%= namespace %></h3> | ||
| <p class="govuk-body"> | ||
| <%= render("components/code_snippet", content: JSON.pretty_generate(response)) %> | ||
| </p> | ||
| <% if topics.llm_responses.present? %> | ||
| <%= render "govuk_publishing_components/components/details", { | ||
| title: "LLM responses", | ||
| } do %> | ||
| <% topics.llm_responses.each do |namespace, response| %> | ||
| <h3 class="govuk-heading-m"><%= namespace %></h3> | ||
| <p class="govuk-body"> | ||
| <%= render("components/code_snippet", content: JSON.pretty_generate(response)) %> | ||
| </p> | ||
| <% end %> | ||
| <% end %> | ||
| <% end %> | ||
| <% end %> | ||
|
|
||
| <% if topics.metrics.present? %> | ||
| <%= render "govuk_publishing_components/components/details", { | ||
| title: "Metrics", | ||
| } do %> | ||
| <%= render "govuk_publishing_components/components/summary_list", { | ||
| items: topics.metrics.map do |metric, value| | ||
| { | ||
| field: metric, | ||
| value: value, | ||
| } | ||
| end, | ||
| borderless: true, | ||
| } %> | ||
| <% if topics.metrics.present? %> | ||
| <%= render "govuk_publishing_components/components/details", { | ||
| title: "Metrics", | ||
| } do %> | ||
| <%= render "govuk_publishing_components/components/summary_list", { | ||
| items: topics.metrics.map do |metric, value| | ||
| { | ||
| field: metric, | ||
| value: value, | ||
| } | ||
| end, | ||
| borderless: true, | ||
| } %> | ||
| <% end %> | ||
| <% end %> | ||
| <% end %> | ||
|
|
||
| <% if answer_relevancy_aggregate.present? %> | ||
| <%= render "generic_aggregate_auto_evaluation", aggregate: answer_relevancy_aggregate, title: "Answer relevancy" %> | ||
| <% end %> |
75 changes: 75 additions & 0 deletions
75
app/views/admin/questions/_generic_aggregate_auto_evaluation.html.erb
|
davidgisbey marked this conversation as resolved.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| <% | ||
| items = [ | ||
| { | ||
| field: "Mean score", | ||
| value: aggregate.mean_score, | ||
| }, | ||
| ] | ||
|
|
||
| items += aggregate.runs.flat_map.with_index(1) do |run, index| | ||
| [ | ||
| { field: "Run #{index} score", value: run.score }, | ||
| { field: "Run #{index} reason", value: run.reason }, | ||
| ] | ||
| end | ||
| %> | ||
|
|
||
| <%= render "govuk_publishing_components/components/summary_list", { | ||
| title:, | ||
| heading_level: 2, | ||
| margin_bottom: 4, | ||
| heading_size: "l", | ||
| items: items, | ||
| } %> | ||
|
|
||
| <%= render "govuk_publishing_components/components/details", { | ||
| title: "LLM responses", | ||
| } do %> | ||
| <% aggregate.runs.each.with_index(1) do |run, index| %> | ||
| <%= render "govuk_publishing_components/components/heading", { | ||
| text: "Run #{index}", | ||
| font_size: "m", | ||
| heading_level: 2, | ||
| margin_bottom: 4, | ||
| } %> | ||
|
|
||
| <% run.llm_responses.each do |namespace, response| %> | ||
| <%= render "govuk_publishing_components/components/heading", { | ||
| text: namespace.capitalize, | ||
| font_size: "s", | ||
| heading_level: 3, | ||
| } %> | ||
|
|
||
| <p class="govuk-body"> | ||
| <%= render("components/code_snippet", content: JSON.pretty_generate(response)) %> | ||
| </p> | ||
| <% end %> | ||
| <% end %> | ||
| <% end %> | ||
|
|
||
| <%= render "govuk_publishing_components/components/details", { | ||
| title: "Metrics", | ||
| } do %> | ||
| <% aggregate.runs.each.with_index(1) do |run, index| %> | ||
| <%= render "govuk_publishing_components/components/heading", { | ||
| text: "Run #{index}", | ||
| font_size: "m", | ||
| heading_level: 2, | ||
| } %> | ||
|
|
||
| <% run.metrics.sort.each do |namespace, metrics| %> | ||
| <%= render "govuk_publishing_components/components/summary_list", { | ||
| title: namespace.capitalize, | ||
| items: metrics.map do |metric, value| | ||
| { | ||
| field: metric, | ||
| value: value, | ||
| } | ||
| end, | ||
| borderless: true, | ||
| heading_size: "s", | ||
| margin_bottom: 6, | ||
| } %> | ||
| <% end %> | ||
| <% end %> | ||
| <% end %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| class AddAnswerRelevancyTables < ActiveRecord::Migration[8.0] | ||
| def change | ||
| create_table :answer_analysis_answer_relevancy_aggregates, id: :uuid do |t| | ||
| t.decimal :mean_score, null: false | ||
| t.references :answer, type: :uuid, null: false, foreign_key: { on_delete: :cascade }, index: { unique: true } | ||
| t.timestamps | ||
| end | ||
|
|
||
| create_table :answer_analysis_answer_relevancy_runs, id: :uuid do |t| | ||
| t.decimal :score, null: false | ||
| t.string :reason, null: false | ||
| t.jsonb :llm_responses | ||
| t.jsonb :metrics | ||
| t.references :answer_analysis_answer_relevancy_aggregate, type: :uuid, null: false, foreign_key: { on_delete: :cascade } | ||
| t.timestamps | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.