Skip to content
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

Character job #49

Merged
merged 2 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,7 @@ Metrics/BlockLength:
Exclude:
- 'spec/models/*'
- 'spec/jobs/*'
- 'spec/jobs/stories/*'
- 'spec/jobs/images/*'
- 'spec/jobs/tweets/*'
- 'spec/jobs/discussions/*'
- 'app/lib/publisher.rb'
- 'spec/jobs/pillars/populate_pillar_columns_job_spec.rb'
- 'lib/tasks/import.rake'

# Offense count: 1
# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
Expand Down Expand Up @@ -175,3 +170,19 @@ Metrics/PerceivedComplexity:
- 'app/controllers/tweets_controller.rb'
- 'app/controllers/instapins_controller.rb'
- 'app/controllers/stories_controller.rb'

Rails/HasAndBelongsToMany:
Exclude:
- 'app/models/character.rb'
- 'app/models/archetype.rb'
- 'app/models/character_type.rb'
- 'app/models/moral_alignment.rb'
- 'app/models/personality_trait.rb'

Style/MutableConstant:
Exclude:
- 'bin/setup'

Style/ClassAndModuleChildren:
Exclude:
- 'app/controllers/writing_style/versions_controller.rb'
1 change: 1 addition & 0 deletions app/controllers/characters_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ def index

def generate_prompt
set_component
GenerateCharacterPromptJob.perform_later(@component)
end

private
Expand Down
51 changes: 51 additions & 0 deletions app/jobs/generate_character_prompt_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
class GenerateCharacterPromptJob < ApplicationJob
queue_as :default

def perform(character)
@client = OpenAI::Client.new

system_role = <<~SYSTEM_ROLE
You are a college level english teacher. Analyze the following JSON Object and create a 2 pargraph character description for ChatGPT. DONT MAKE ANYTHING UP.
SYSTEM_ROLE

messages = [
{ role: "system", content: system_role },
{ role: "user", content: character.to_json }
]

sleep(0.5)

character.update(pending: true)
broadcast_character_update(character)

sleep(10)

response = chat(messages:)

character.update(prompt: response["choices"][0]["message"]["content"], pending: false)

broadcast_character_update(character)
end

private

def chat(messages:)
@client.chat(
parameters: {
model: ENV['OPENAI_GPT_MODEL'], # Required.
messages:,
temperature: 0.7
}
)
end

def broadcast_character_update(character)
# This broadcasts to the specific Turbo Stream channel for the writing style
Turbo::StreamsChannel.broadcast_update_to(
"character_#{character.id}", # unique identifier for the writing style
target: "character_#{character.id}_prompt", # the DOM ID where the prompt will be inserted
partial: "characters/prompt", # partial view to render the updated content
locals: { character: }
)
end
end
14 changes: 14 additions & 0 deletions app/models/character.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
# values :text
# created_at :datetime not null
# updated_at :datetime not null
# prompt :text
# pending :boolean
#
class Character < ApplicationRecord
has_and_belongs_to_many :character_types
Expand All @@ -31,4 +33,16 @@ class Character < ApplicationRecord
health fears desires backstory skills values], versions: {
scope: -> { order('id desc') }
}

def as_json(options = {})
super(options.merge(
except: [:id, :prompt, :pending, :created_at, :updated_at],
include: {
character_types: { only: [:name, :definition, :purpose, :example] },
moral_alignments: { only: [:name, :description, :examples] },
personality_traits: { only: [:name, :description] },
archetypes: { only: [:name, :traits, :examples] }
}
))
end
end
6 changes: 6 additions & 0 deletions app/views/characters/_iterate_form.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
= turbo_frame_tag "writing_style_form"
= form_with url: iterate_writing_style_path(parent), data: {'controller': 'form'}, class: 'ui reply form !mb-20', method: :post, local: true do |form|
.field
= form.text_area :message, rows: 3, placeholder: 'Instructions. I.e. "Remove Lines 10 and 11"', id: 'form_text_area', data: {'form-target': 'textarea', 'action': 'input->form#onInput'}
.actions
= form.submit 'Iterate On Prompt', class: 'btn-primary', data: {'form-target': 'button'}
16 changes: 16 additions & 0 deletions app/views/characters/_prompt.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
- if character.pending
=r ux.h5 text: 'Prompt Instructions'
=r ux.segment '!bg-yellow-100 !mb-10 !h-32'
div class="ui active inverted dimmer"
div class="ui text loader small elastic" Processing Your Request

- if character.prompt.present? && !character.pending
=r ux.h5
| Prompt Instructions
/-if writing_style.versions.present?
/ span.ml-2
/ |[
/ = link_to "Previous Versions", writing_style_versions_path(writing_style_id: writing_style.id), class: 'text-blue-700 hover:text-blue-700', data: {turbo_frame: "_top"}
/ |]
=r ux.segment '!bg-yellow-100 !mb-10'
= simple_format character.prompt
7 changes: 3 additions & 4 deletions app/views/characters/show.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
=r ux.component_header
= breadcrumb_parent @component_name, send(@component_list_path)
= @component.name
/=r ux.component_return_action text: '< Back', url: send(@component_list_path)
= button_to 'Generate Prompt', generate_prompt_character_path, method: :post, class: 'btn-primary', data: { 'turbo-action': 'replace'}
=r ux.component_divider

/=r ux.segment
/ pre
/ = @component.corpus
= turbo_stream_from "character_#{@component.id}"
= turbo_frame_tag "character_#{@component.id}_prompt"
= render partial: 'prompt', locals: { character: @component }
Loading