From d320ab29b506e402fdbc4ba4f72ba9f319eba4fe Mon Sep 17 00:00:00 2001 From: CoralineAda Date: Wed, 30 Oct 2024 18:28:48 -0500 Subject: [PATCH 01/10] Update ruby version to 3.3.5 --- .tool-versions | 2 +- Gemfile | 2 +- Gemfile.lock | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.tool-versions b/.tool-versions index f2a971a..1dd1998 100644 --- a/.tool-versions +++ b/.tool-versions @@ -1 +1 @@ -ruby 3.2.2 +ruby 3.3.5 diff --git a/Gemfile b/Gemfile index 9b3c25c..c687986 100644 --- a/Gemfile +++ b/Gemfile @@ -1,6 +1,6 @@ source "https://rubygems.org" -ruby "3.2.2" +ruby "3.3.5" # TMI-specific gem 'sidekiq' diff --git a/Gemfile.lock b/Gemfile.lock index 7c7ff74..840ba1d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -356,7 +356,7 @@ DEPENDENCIES yard RUBY VERSION - ruby 3.2.2p53 + ruby 3.3.5p100 BUNDLED WITH 2.5.6 From 94e806d76fe88803a42af6c3692c48fdc67e8134 Mon Sep 17 00:00:00 2001 From: CoralineAda Date: Wed, 30 Oct 2024 18:30:23 -0500 Subject: [PATCH 02/10] Wire up route for enqueuing category suggestion --- app/controllers/categories_controller.rb | 4 ++++ app/views/categories/index.html.erb | 2 +- config/routes.rb | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/app/controllers/categories_controller.rb b/app/controllers/categories_controller.rb index e571777..47743f8 100644 --- a/app/controllers/categories_controller.rb +++ b/app/controllers/categories_controller.rb @@ -52,6 +52,10 @@ def update end end + def enqueue_category_suggestions + + end + private def category_params diff --git a/app/views/categories/index.html.erb b/app/views/categories/index.html.erb index f5dbcd8..d330390 100644 --- a/app/views/categories/index.html.erb +++ b/app/views/categories/index.html.erb @@ -19,7 +19,7 @@ <% if @enqueued_at %>

Category suggestions were requested at <%= @enqueued_at %>. Please allow up to 2 minutes for the process to complete. You can reload the page to monitor progress.

<% else %> - <%= button_to "Get Suggestions", codebook_enqueue_categories_path(@question.id, params: { enqueued: true }, method: :put ) %> + <%= button_to "Get Suggestions", codebook_enqueue_category_suggestions_path(@question.id, params: { enqueued: true }, method: :put ) %> <% end %> <%= render partial: "/shared/filtering" %> diff --git a/config/routes.rb b/config/routes.rb index 3e9893a..ae1ada0 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -19,7 +19,7 @@ resources :themes resources :codebooks do - post "enqueue_categories", action: "enqueue_categories" + post "enqueue_category_suggestions", action: "enqueue_category_suggestions" end resources :questions do From e08598d1d431d9c6b812c7b6b17217554af340cc Mon Sep 17 00:00:00 2001 From: CoralineAda Date: Wed, 30 Oct 2024 18:45:50 -0500 Subject: [PATCH 03/10] Update gemfile --- Gemfile | 2 ++ Gemfile.lock | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/Gemfile b/Gemfile index c687986..39f7147 100644 --- a/Gemfile +++ b/Gemfile @@ -10,6 +10,8 @@ gem 'ruby-openai' gem 'activegraph', '11.5.0.beta.3' gem 'neo4j-ruby-driver' gem 'yard' +gem 'csv' +gem 'ostruct' # Bundle edge Rails instead: gem "rails", github: "rails/rails", branch: "main" gem "rails", "~> 7.2.1" diff --git a/Gemfile.lock b/Gemfile.lock index 840ba1d..8a64e4d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -109,6 +109,7 @@ GEM fiber-local json crass (1.0.6) + csv (3.3.0) date (3.3.4) debug (1.9.2) irb (~> 1.10) @@ -185,6 +186,7 @@ GEM nokogiri (1.16.7-x86_64-linux) racc (~> 1.4) orm_adapter (0.5.0) + ostruct (0.6.0) pg (1.5.7) pry (0.14.2) coderay (~> 1.1) @@ -334,10 +336,12 @@ DEPENDENCIES activegraph (= 11.5.0.beta.3) bootsnap capybara + csv debug dotenv faker (~> 3.4, >= 3.4.2) neo4j-ruby-driver + ostruct pg pry puma (>= 5.0) From 13e512e6e91fcd881569d589f5bca8cf7b5a217c Mon Sep 17 00:00:00 2001 From: CoralineAda Date: Wed, 30 Oct 2024 19:25:26 -0500 Subject: [PATCH 04/10] Service is stubbed out --- app/controllers/categories_controller.rb | 2 +- app/jobs/category_suggestions_job.rb | 13 ++++++++++++ app/models/context.rb | 5 +++++ app/models/services/suggest_categories.rb | 20 +++++++++++-------- ...325_add_suggested_categories_to_context.rb | 5 +++++ db/schema.rb | 3 ++- 6 files changed, 38 insertions(+), 10 deletions(-) create mode 100644 app/jobs/category_suggestions_job.rb create mode 100644 db/migrate/20241031002325_add_suggested_categories_to_context.rb diff --git a/app/controllers/categories_controller.rb b/app/controllers/categories_controller.rb index 47743f8..9bc2b62 100644 --- a/app/controllers/categories_controller.rb +++ b/app/controllers/categories_controller.rb @@ -53,7 +53,7 @@ def update end def enqueue_category_suggestions - + CategorySuggestionsJob.perform_async(@question.context_id) end private diff --git a/app/jobs/category_suggestions_job.rb b/app/jobs/category_suggestions_job.rb new file mode 100644 index 0000000..a0d9bdb --- /dev/null +++ b/app/jobs/category_suggestions_job.rb @@ -0,0 +1,13 @@ +# This background job performs the Category derivation process. +class CategorySuggestionsJob + + include Sidekiq::Job + + queue_as :default + + def perform(context_id) + Rails.logger.info("CategorySuggestionsJob running with context id #{context_id}") + Context.suggest_categories + end + +end diff --git a/app/models/context.rb b/app/models/context.rb index f054b60..0d838c1 100644 --- a/app/models/context.rb +++ b/app/models/context.rb @@ -7,4 +7,9 @@ class Context < ApplicationRecord has_many :questions + def suggest_categories + categories = Services::SuggestCategories.perform(self.id).map{|category| category['category'] } + update_attribute(:suggested_categories, categories) + end + end diff --git a/app/models/services/suggest_categories.rb b/app/models/services/suggest_categories.rb index b56e4af..db5671e 100644 --- a/app/models/services/suggest_categories.rb +++ b/app/models/services/suggest_categories.rb @@ -1,7 +1,7 @@ module Services class SuggestCategories - attr_accessor :text + attr_accessor :context # This is the prompt sent to the selected AI agent to provide instructions on category derivision. PROMPT = %{ @@ -19,21 +19,25 @@ class SuggestCategories ] } - The list of codes is: + The array of codes is: } - def self.perform(text) - new(text).perform + def self.perform(context_id) + new(context_id).perform end - def initialize(text) - @text = text + def initialize(context_id) + @context = Context.find(context_id) end # Uses the OpenAI client to pass the prompt and text through the API for sentiment analysis. def perform - return false unless text.present? - response = Clients::OpenAi.request("#{PROMPT} #{text}") + return false unless context.present? + codes = Code.where(context: context.name).map(&:name) + + # response = Clients::OpenAi.request("#{PROMPT} #{codes}") + response = { "categories" => [ { "category" => "foo" } ] } + return false unless response['categories'].present? return response['categories'] end diff --git a/db/migrate/20241031002325_add_suggested_categories_to_context.rb b/db/migrate/20241031002325_add_suggested_categories_to_context.rb new file mode 100644 index 0000000..a2e43c1 --- /dev/null +++ b/db/migrate/20241031002325_add_suggested_categories_to_context.rb @@ -0,0 +1,5 @@ +class AddSuggestedCategoriesToContext < ActiveRecord::Migration[7.2] + def change + add_column :contexts, :suggested_categories, :string, array: true, default: [] + end +end diff --git a/db/schema.rb b/db/schema.rb index c09986e..6aa4163 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.2].define(version: 2024_10_30_190525) do +ActiveRecord::Schema[7.2].define(version: 2024_10_31_002325) do # These are extensions that must be enabled in order to support this database enable_extension "pg_stat_statements" enable_extension "plpgsql" @@ -56,6 +56,7 @@ t.string "display_name" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.string "suggested_categories", default: [], array: true end create_table "questions", force: :cascade do |t| From dcf54e6a037298e0bdfd223625fa8bfc60ee6ab6 Mon Sep 17 00:00:00 2001 From: CoralineAda Date: Wed, 30 Oct 2024 19:28:24 -0500 Subject: [PATCH 05/10] Job wired up --- app/jobs/category_suggestions_job.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/jobs/category_suggestions_job.rb b/app/jobs/category_suggestions_job.rb index a0d9bdb..4e3c237 100644 --- a/app/jobs/category_suggestions_job.rb +++ b/app/jobs/category_suggestions_job.rb @@ -7,7 +7,8 @@ class CategorySuggestionsJob def perform(context_id) Rails.logger.info("CategorySuggestionsJob running with context id #{context_id}") - Context.suggest_categories + return unless context = Context.find(context_id) + context.suggest_categories end end From 1f56999fbfd47c81ab9b039a5b2ed9999a90632d Mon Sep 17 00:00:00 2001 From: CoralineAda Date: Wed, 30 Oct 2024 19:42:07 -0500 Subject: [PATCH 06/10] Controller action working with job and stub service --- app/controllers/categories_controller.rb | 5 +---- app/controllers/codebooks_controller.rb | 5 +++++ app/views/categories/index.html.erb | 6 ++++++ 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/app/controllers/categories_controller.rb b/app/controllers/categories_controller.rb index 9bc2b62..19aa7a8 100644 --- a/app/controllers/categories_controller.rb +++ b/app/controllers/categories_controller.rb @@ -4,6 +4,7 @@ class CategoriesController < ApplicationController def index @section_name = @question.label + @context = @question.context @categories = Category.where(context: @question.context.name).order(:name) @enqueued_at = params[:enqueued_at].present? ? Time.at(params[:enqueued_at].to_i).strftime("%T %Z") : nil end @@ -52,10 +53,6 @@ def update end end - def enqueue_category_suggestions - CategorySuggestionsJob.perform_async(@question.context_id) - end - private def category_params diff --git a/app/controllers/codebooks_controller.rb b/app/controllers/codebooks_controller.rb index 7890ec4..ae4fb08 100644 --- a/app/controllers/codebooks_controller.rb +++ b/app/controllers/codebooks_controller.rb @@ -35,4 +35,9 @@ def show end + def enqueue_category_suggestions + @question = Question.find(params[:codebook_id]) + CategorySuggestionsJob.perform_async(@question.context_id) + end + end diff --git a/app/views/categories/index.html.erb b/app/views/categories/index.html.erb index d330390..7c9ed48 100644 --- a/app/views/categories/index.html.erb +++ b/app/views/categories/index.html.erb @@ -16,6 +16,12 @@

Suggested Categories:

+
    + <% @context.suggested_categories.each do |suggestion| %> +
  • <%= suggestion %>
  • + <% end %> +
+ <% if @enqueued_at %>

Category suggestions were requested at <%= @enqueued_at %>. Please allow up to 2 minutes for the process to complete. You can reload the page to monitor progress.

<% else %> From 028c5cca7b161f7fe5a3dda3a50ec0ad00ff891c Mon Sep 17 00:00:00 2001 From: CoralineAda Date: Wed, 30 Oct 2024 20:41:53 -0500 Subject: [PATCH 07/10] Controller action and view including throbber --- app/assets/stylesheets/icons.css | 52 +++++++++++++++++++ app/controllers/codebooks_controller.rb | 11 +++- app/jobs/category_suggestions_job.rb | 2 + app/models/context.rb | 1 + app/models/services/suggest_categories.rb | 2 +- app/views/categories/_suggestions.html.erb | 26 ++++++++++ app/views/categories/index.html.erb | 12 +---- ...325_add_suggested_categories_to_context.rb | 1 + db/schema.rb | 1 + 9 files changed, 94 insertions(+), 14 deletions(-) create mode 100644 app/views/categories/_suggestions.html.erb diff --git a/app/assets/stylesheets/icons.css b/app/assets/stylesheets/icons.css index 039afb2..4bafd59 100644 --- a/app/assets/stylesheets/icons.css +++ b/app/assets/stylesheets/icons.css @@ -8,3 +8,55 @@ --icon-zoom: url(data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz4KPHN2ZyB3aWR0aD0iMTIwMHB0IiBoZWlnaHQ9IjEyMDBwdCIgdmVyc2lvbj0iMS4xIiB2aWV3Qm94PSIwIDAgMTIwMCAxMjAwIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPgogPHBhdGggZD0ibTExMTUuOCA5NzIuOC0yMzUuMzgtMjM1LjMxYzQyLjgzNi02OC42OTEgNjcuOTY1LTE0OS40OSA2Ny45NjUtMjM2LjE0IDAtMjQ2LjI5LTIwMC41MS00NDYuOC00NDYuOTUtNDQ2LjhzLTQ0Ni44NyAyMDAuNTEtNDQ2Ljg3IDQ0Ni44YzAgMjQ2LjUxIDIwMC40NCA0NDcuMDkgNDQ2Ljg3IDQ0Ny4wOSA4Ni42NTYgMCAxNjcuNDItMjUuMTk5IDIzNi02Ny45NjVsMjM1LjQ1IDIzNS4zOGMxOS43NDYgMTkuNjAyIDQ1LjYwMiAyOS42MDIgNzEuNjAyIDI5LjYwMiAyNS43MTEgMCA1MS42MzctMTAgNzEuMzA5LTI5LjYwMiAzOS41MjctMzkuNDg4IDM5LjUyNy0xMDMuNDkgMC0xNDMuMDV6bS05MzQuOC00NzEuNDZjMC0xNzYuNTUgMTQzLjc1LTMyMC40IDMyMC40Ny0zMjAuNCAxNzYuODQgMCAzMjAuNTggMTQzLjgyIDMyMC41OCAzMjAuNCAwIDE3Ni43Ni0xNDMuNzUgMzIwLjY2LTMyMC41OCAzMjAuNjYtMTc2LjczIDAtMzIwLjQ3LTE0My44OS0zMjAuNDctMzIwLjY2eiIgZmlsbD0iIzAwMDAwMCIvPgo8L3N2Zz4=); --icon-zoom-light: url(data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz4KPHN2ZyB3aWR0aD0iMTIwMHB0IiBoZWlnaHQ9IjEyMDBwdCIgdmVyc2lvbj0iMS4xIiB2aWV3Qm94PSIwIDAgMTIwMCAxMjAwIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPgogPHBhdGggZD0ibTExMTUuOCA5NzIuOC0yMzUuMzgtMjM1LjMxYzQyLjgzNi02OC42OTEgNjcuOTY1LTE0OS40OSA2Ny45NjUtMjM2LjE0IDAtMjQ2LjI5LTIwMC41MS00NDYuOC00NDYuOTUtNDQ2LjhzLTQ0Ni44NyAyMDAuNTEtNDQ2Ljg3IDQ0Ni44YzAgMjQ2LjUxIDIwMC40NCA0NDcuMDkgNDQ2Ljg3IDQ0Ny4wOSA4Ni42NTYgMCAxNjcuNDItMjUuMTk5IDIzNi02Ny45NjVsMjM1LjQ1IDIzNS4zOGMxOS43NDYgMTkuNjAyIDQ1LjYwMiAyOS42MDIgNzEuNjAyIDI5LjYwMiAyNS43MTEgMCA1MS42MzctMTAgNzEuMzA5LTI5LjYwMiAzOS41MjctMzkuNDg4IDM5LjUyNy0xMDMuNDkgMC0xNDMuMDV6bS05MzQuOC00NzEuNDZjMC0xNzYuNTUgMTQzLjc1LTMyMC40IDMyMC40Ny0zMjAuNCAxNzYuODQgMCAzMjAuNTggMTQzLjgyIDMyMC41OCAzMjAuNCAwIDE3Ni43Ni0xNDMuNzUgMzIwLjY2LTMyMC41OCAzMjAuNjYtMTc2LjczIDAtMzIwLjQ3LTE0My44OS0zMjAuNDctMzIwLjY2eiIgZmlsbD0iI2ZmZiIvPgo8L3N2Zz4K); } + +.throbber, +.throbber div { + box-sizing: border-box; +} +.throbber { + display: inline-block; + position: relative; + width: 80px; + height: 80px; +} +.throbber div { + position: absolute; + border: 4px solid currentColor; + opacity: 1; + border-radius: 50%; + animation: throbber 2s cubic-bezier(0, 0.2, 0.8, 1) infinite; +} +.throbber div:nth-child(2) { + animation-delay: -0.5s; +} +@keyframes throbber { + 0% { + top: 36px; + left: 36px; + width: 8px; + height: 8px; + opacity: 0; + } + 4.9% { + top: 36px; + left: 36px; + width: 8px; + height: 8px; + opacity: 0; + } + 5% { + top: 36px; + left: 36px; + width: 8px; + height: 8px; + opacity: 1; + } + 100% { + top: 0; + left: 0; + width: 80px; + height: 80px; + opacity: 0; + } +} + diff --git a/app/controllers/codebooks_controller.rb b/app/controllers/codebooks_controller.rb index ae4fb08..306e126 100644 --- a/app/controllers/codebooks_controller.rb +++ b/app/controllers/codebooks_controller.rb @@ -36,8 +36,15 @@ def show end def enqueue_category_suggestions - @question = Question.find(params[:codebook_id]) - CategorySuggestionsJob.perform_async(@question.context_id) + question = Question.find(params[:codebook_id]) + context = question.context + 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, question: question, enqueued: params[:enqueued] }) + end + end + end end diff --git a/app/jobs/category_suggestions_job.rb b/app/jobs/category_suggestions_job.rb index 4e3c237..cdbcc49 100644 --- a/app/jobs/category_suggestions_job.rb +++ b/app/jobs/category_suggestions_job.rb @@ -8,7 +8,9 @@ class CategorySuggestionsJob def perform(context_id) Rails.logger.info("CategorySuggestionsJob running with context id #{context_id}") return unless context = Context.find(context_id) + sleep(2) context.suggest_categories + context.update_attribute(:suggestions_updated_at, DateTime.now) end end diff --git a/app/models/context.rb b/app/models/context.rb index 0d838c1..513c4a4 100644 --- a/app/models/context.rb +++ b/app/models/context.rb @@ -8,6 +8,7 @@ class Context < ApplicationRecord has_many :questions def suggest_categories + update_attribute(:suggested_categories, []) categories = Services::SuggestCategories.perform(self.id).map{|category| category['category'] } update_attribute(:suggested_categories, categories) end diff --git a/app/models/services/suggest_categories.rb b/app/models/services/suggest_categories.rb index db5671e..fe9a469 100644 --- a/app/models/services/suggest_categories.rb +++ b/app/models/services/suggest_categories.rb @@ -36,7 +36,7 @@ def perform codes = Code.where(context: context.name).map(&:name) # response = Clients::OpenAi.request("#{PROMPT} #{codes}") - response = { "categories" => [ { "category" => "foo" } ] } + response = { "categories" => [ { "category" => "divisions" }, { "category" => "third space" }, { "category" => "intergenerational" } ] } return false unless response['categories'].present? return response['categories'] diff --git a/app/views/categories/_suggestions.html.erb b/app/views/categories/_suggestions.html.erb new file mode 100644 index 0000000..ed28900 --- /dev/null +++ b/app/views/categories/_suggestions.html.erb @@ -0,0 +1,26 @@ +<%= turbo_frame_tag "frame-suggestions" do %> + + <% if enqueued && DateTime.parse(enqueued) > context.suggestions_updated_at %> +
+ + + <% else %> +
    + <% context.suggested_categories.each do |suggestion| %> +
  • <%= suggestion %>
  • + <% end %> +
+ <% end %> + + <%= button_to "Get Suggestions", codebook_enqueue_category_suggestions_path(question.id, params: { enqueued: DateTime.now }, method: :put, form: { data: { turbo: true, turbo_stream: true } } ) %> + + +<% end %> diff --git a/app/views/categories/index.html.erb b/app/views/categories/index.html.erb index 7c9ed48..1e5d260 100644 --- a/app/views/categories/index.html.erb +++ b/app/views/categories/index.html.erb @@ -16,16 +16,6 @@

Suggested Categories:

-
    - <% @context.suggested_categories.each do |suggestion| %> -
  • <%= suggestion %>
  • - <% end %> -
- -<% if @enqueued_at %> -

Category suggestions were requested at <%= @enqueued_at %>. Please allow up to 2 minutes for the process to complete. You can reload the page to monitor progress.

-<% else %> - <%= button_to "Get Suggestions", codebook_enqueue_category_suggestions_path(@question.id, params: { enqueued: true }, method: :put ) %> -<% end %> +<%= render partial: "suggestions", locals: { context: @context, question: @question, enqueued: nil } %> <%= render partial: "/shared/filtering" %> diff --git a/db/migrate/20241031002325_add_suggested_categories_to_context.rb b/db/migrate/20241031002325_add_suggested_categories_to_context.rb index a2e43c1..9449947 100644 --- a/db/migrate/20241031002325_add_suggested_categories_to_context.rb +++ b/db/migrate/20241031002325_add_suggested_categories_to_context.rb @@ -1,5 +1,6 @@ class AddSuggestedCategoriesToContext < ActiveRecord::Migration[7.2] def change add_column :contexts, :suggested_categories, :string, array: true, default: [] + add_column :contexts, :suggestions_updated_at, :datetime end end diff --git a/db/schema.rb b/db/schema.rb index 6aa4163..0200d9f 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -57,6 +57,7 @@ t.datetime "created_at", null: false t.datetime "updated_at", null: false t.string "suggested_categories", default: [], array: true + t.datetime "suggestions_updated_at" end create_table "questions", force: :cascade do |t| From df7ca3defef84369dec4fa4d8ac50a5843a712a1 Mon Sep 17 00:00:00 2001 From: CoralineAda Date: Wed, 30 Oct 2024 20:48:41 -0500 Subject: [PATCH 08/10] Adjust spinner color scheme --- app/assets/stylesheets/icons.css | 2 +- app/views/categories/_suggestions.html.erb | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/app/assets/stylesheets/icons.css b/app/assets/stylesheets/icons.css index 4bafd59..678de7d 100644 --- a/app/assets/stylesheets/icons.css +++ b/app/assets/stylesheets/icons.css @@ -21,7 +21,7 @@ } .throbber div { position: absolute; - border: 4px solid currentColor; + border: 4px solid var(--color-green); opacity: 1; border-radius: 50%; animation: throbber 2s cubic-bezier(0, 0.2, 0.8, 1) infinite; diff --git a/app/views/categories/_suggestions.html.erb b/app/views/categories/_suggestions.html.erb index ed28900..6d58ad4 100644 --- a/app/views/categories/_suggestions.html.erb +++ b/app/views/categories/_suggestions.html.erb @@ -11,7 +11,6 @@ } refreshSpinner(); - <% else %>
    <% context.suggested_categories.each do |suggestion| %> @@ -22,5 +21,4 @@ <%= button_to "Get Suggestions", codebook_enqueue_category_suggestions_path(question.id, params: { enqueued: DateTime.now }, method: :put, form: { data: { turbo: true, turbo_stream: true } } ) %> - <% end %> From 80fad8c43c5fffdf55712f56c603a07aedfa40c3 Mon Sep 17 00:00:00 2001 From: CoralineAda Date: Wed, 30 Oct 2024 21:08:38 -0500 Subject: [PATCH 09/10] Wire up openai client for reals --- app/models/services/suggest_categories.rb | 4 ++-- app/views/categories/_suggestions.html.erb | 3 +-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/app/models/services/suggest_categories.rb b/app/models/services/suggest_categories.rb index fe9a469..0c91e32 100644 --- a/app/models/services/suggest_categories.rb +++ b/app/models/services/suggest_categories.rb @@ -35,8 +35,8 @@ 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" } ] } + response = Clients::OpenAi.request("#{PROMPT} #{codes}") + # response = { "categories" => [ { "category" => "divisions" }, { "category" => "third space" }, { "category" => "intergenerational" } ] } return false unless response['categories'].present? return response['categories'] diff --git a/app/views/categories/_suggestions.html.erb b/app/views/categories/_suggestions.html.erb index 6d58ad4..ed50ebd 100644 --- a/app/views/categories/_suggestions.html.erb +++ b/app/views/categories/_suggestions.html.erb @@ -17,8 +17,7 @@
  • <%= suggestion %>
  • <% end %>
+ <%= button_to "Get Suggestions", codebook_enqueue_category_suggestions_path(question.id, params: { enqueued: DateTime.now }, method: :put, form: { data: { turbo: true, turbo_stream: true } } ) %> <% end %> - <%= button_to "Get Suggestions", codebook_enqueue_category_suggestions_path(question.id, params: { enqueued: DateTime.now }, method: :put, form: { data: { turbo: true, turbo_stream: true } } ) %> - <% end %> From a670b740fae2f01209b29c6766ea4a5e2b6b059f Mon Sep 17 00:00:00 2001 From: CoralineAda Date: Wed, 30 Oct 2024 21:24:00 -0500 Subject: [PATCH 10/10] Show suggestions on new category page --- app/assets/stylesheets/styles.css | 2 +- app/controllers/categories_controller.rb | 3 +- app/jobs/category_suggestions_job.rb | 1 - app/views/categories/new.html.erb | 41 +++++++++++++++++------- 4 files changed, 32 insertions(+), 15 deletions(-) diff --git a/app/assets/stylesheets/styles.css b/app/assets/stylesheets/styles.css index 7cc3f72..20732c6 100644 --- a/app/assets/stylesheets/styles.css +++ b/app/assets/stylesheets/styles.css @@ -76,7 +76,7 @@ ul, ol { list-style-position: inside; - margin: 1.5rem 0 1.5rem 1.5rem; + margin: 0 0 1.5rem 1.5rem; } em { diff --git a/app/controllers/categories_controller.rb b/app/controllers/categories_controller.rb index 19aa7a8..e255b91 100644 --- a/app/controllers/categories_controller.rb +++ b/app/controllers/categories_controller.rb @@ -15,7 +15,8 @@ def show end def new - @category = Category.new(name: "New Category", context: @question.context.name) + @category = Category.new(name: "New Category", context: @question.context.name) + @context = @question.context end def create diff --git a/app/jobs/category_suggestions_job.rb b/app/jobs/category_suggestions_job.rb index cdbcc49..9a91e3b 100644 --- a/app/jobs/category_suggestions_job.rb +++ b/app/jobs/category_suggestions_job.rb @@ -8,7 +8,6 @@ class CategorySuggestionsJob def perform(context_id) Rails.logger.info("CategorySuggestionsJob running with context id #{context_id}") return unless context = Context.find(context_id) - sleep(2) context.suggest_categories context.update_attribute(:suggestions_updated_at, DateTime.now) end diff --git a/app/views/categories/new.html.erb b/app/views/categories/new.html.erb index f2c8fb9..550568d 100644 --- a/app/views/categories/new.html.erb +++ b/app/views/categories/new.html.erb @@ -2,15 +2,32 @@

Add a Category

-<%= form_with model: @category, url: question_categories_path(question_id: @question.id) do |f| %> -
- <%= label :name, "Name" %> - <%= f.text_field :name %> -
-
- <%= label :description, "Description" %> - <%= f.text_area :description %> -
- <%= f.hidden_field :context %> - <%= f.submit "Create" %> -<% end %> +
+ +
+ <%= form_with model: @category, url: question_categories_path(question_id: @question.id) do |f| %> +
+ <%= label :name, "Name" %> + <%= f.text_field :name %> +
+
+ <%= label :description, "Description" %> + <%= f.text_area :description %> +
+ <%= f.hidden_field :context %> + <%= f.submit "Create" %> + <% end %> +
+ + <% if @context.suggested_categories.any? %> +
+

Suggested categories:

+
    + <% @context.suggested_categories.each do |suggestion| %> +
  • <%= suggestion %>
  • + <% end %> +
+
+ <% end %> + +