Skip to content

Commit

Permalink
Merge pull request #211 from identity-research-lab/category-suggestio…
Browse files Browse the repository at this point in the history
…ns-to-contexts-controller

Move category suggestion concerns to context controller/view
  • Loading branch information
CoralineAda authored Dec 10, 2024
2 parents 1f15f5e + 0f39ab1 commit 03fc38a
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 21 deletions.
11 changes: 0 additions & 11 deletions app/controllers/codebooks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 9 additions & 0 deletions app/controllers/contexts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 1 addition & 4 deletions app/models/services/suggest_categories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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" : [
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/views/contexts/_suggestions.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<li><%= suggestion %></li>
<% end %>
</ul>
<%= 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 %>
8 changes: 3 additions & 5 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

0 comments on commit 03fc38a

Please sign in to comment.