From 0f39ab1ce4a59b58e067eb1c8901274a19386f77 Mon Sep 17 00:00:00 2001 From: Coraline Date: Tue, 10 Dec 2024 11:50:06 -0600 Subject: [PATCH] Move category suggestion concerns to context controller/view --- app/controllers/codebooks_controller.rb | 11 ----------- app/controllers/contexts_controller.rb | 9 +++++++++ app/models/services/suggest_categories.rb | 5 +---- app/views/contexts/_suggestions.html.erb | 2 +- config/routes.rb | 8 +++----- 5 files changed, 14 insertions(+), 21 deletions(-) diff --git a/app/controllers/codebooks_controller.rb b/app/controllers/codebooks_controller.rb index 038d1ff..7dcac6f 100644 --- a/app/controllers/codebooks_controller.rb +++ b/app/controllers/codebooks_controller.rb @@ -36,15 +36,4 @@ def show end - def enqueue_category_suggestions - context = Context.find(params[:context_id]) - CategorySuggestionsJob.perform_async(context.id) - respond_to do |format| - format.turbo_stream do - render turbo_stream: turbo_stream.replace("frame-suggestions", partial: "/categories/suggestions", locals: { context: context, enqueued: params[:enqueued] }) - end - end - - end - end diff --git a/app/controllers/contexts_controller.rb b/app/controllers/contexts_controller.rb index b92d886..fd5f178 100644 --- a/app/controllers/contexts_controller.rb +++ b/app/controllers/contexts_controller.rb @@ -17,8 +17,17 @@ def show next_index = (context_ids.index(@context.id) + 1) % context_ids.length @previous_context_id = context_ids[previous_index] @next_context_id = context_ids[next_index] + end + def enqueue_category_suggestions + CategorySuggestionsJob.perform_async(params[:context_id]) + respond_to do |format| + format.turbo_stream do + render turbo_stream: turbo_stream.replace("frame-suggestions", partial: "/contexts/suggestions", locals: { context: @context, enqueued: params[:enqueued] }) + end + end end + private diff --git a/app/models/services/suggest_categories.rb b/app/models/services/suggest_categories.rb index 0c91e32..a3df6f3 100644 --- a/app/models/services/suggest_categories.rb +++ b/app/models/services/suggest_categories.rb @@ -5,7 +5,7 @@ class SuggestCategories # This is the prompt sent to the selected AI agent to provide instructions on category derivision. PROMPT = %{ - You are a social researcher doing qualitative analysis on survey data. Please generate a list of suggested categories from a list of codes. The categories should be all lowercase and contain no punctuation. Use this JSON as the output format: + You are a social researcher doing qualitative analysis on identity-related survey data. Please generate a list of no more than 7 suggested categories from a list of codes. The categories should be all lowercase and contain no punctuation. Use this JSON as the output format: { "categories" : [ @@ -34,10 +34,7 @@ def initialize(context_id) def perform return false unless context.present? codes = Code.where(context: context.name).map(&:name) - response = Clients::OpenAi.request("#{PROMPT} #{codes}") - # response = { "categories" => [ { "category" => "divisions" }, { "category" => "third space" }, { "category" => "intergenerational" } ] } - return false unless response['categories'].present? return response['categories'] end diff --git a/app/views/contexts/_suggestions.html.erb b/app/views/contexts/_suggestions.html.erb index 9c9f3ee..d0ca039 100644 --- a/app/views/contexts/_suggestions.html.erb +++ b/app/views/contexts/_suggestions.html.erb @@ -16,7 +16,7 @@
  • <%= suggestion %>
  • <% end %> - <%= button_to "Get Suggestions", codebook_enqueue_category_suggestions_path(context.id, params: { enqueued: DateTime.now }, method: :put, form: { data: { turbo: true, turbo_stream: true } } ) %> + <%= button_to "Get Suggestions", context_enqueue_category_suggestions_path(context.id, params: { enqueued: DateTime.now }, method: :put, form: { data: { turbo: true, turbo_stream: true } } ) %> <% end %> <% end %> diff --git a/config/routes.rb b/config/routes.rb index 871174e..ac0f182 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -15,9 +15,12 @@ resources :contexts do resources :categories + post "enqueue_category_suggestions", action: "enqueue_category_suggestions" end resources :annotations + resources :codebooks + resources :questions resources :responses resources :statistics resources :themes @@ -26,10 +29,5 @@ post "enqueue_keywords", action: "enqueue_keywords" end - # Move these into contexts block - resources :codebooks do - post "enqueue_category_suggestions", action: "enqueue_category_suggestions" - end - resources :questions end