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

adding the ability to merge characters #52

Merged
merged 1 commit into from
Oct 18, 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
2 changes: 1 addition & 1 deletion app/controllers/character/versions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def revert

def merge
version_to_merge = @version.reify
MergeWritingStylesJob.perform_later(@writing_style, version_to_merge)
MergeCharacterPromptsJob.perform_later(@character, version_to_merge)

redirect_to character_path(@character)
end
Expand Down
4 changes: 2 additions & 2 deletions app/jobs/iterate_on_character_prompt_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ def perform(character, message)
@client = OpenAI::Client.new

system_role = <<~SYSTEM_ROLE
You are a college level english teacher.#{' '}
You will be provided with a character description for ChatGPT and
You are a college level english teacher.
You will be provided with a character description for ChatGPT and
a request asking to modify it. Return only the character description.
SYSTEM_ROLE

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

def perform(character, version_to_merge)
prompt_1 = character.prompt
prompt_2 = version_to_merge.prompt

@client = OpenAI::Client.new

system_role = <<~SYSTEM_ROLE
You are a college level english teacher.
You will be provided with two character descriptions for ChatGPT.
Your task is to join the two character descriptions and factor out commonalities, merging them into a cohesive, unified character description.#{' '}
Return only the character description.
DONT MAKE ANYTHING UP.
SYSTEM_ROLE

messages = [
{ role: "system", content: system_role },
{ role: "user", content: "description_1:\n#{prompt_1}\n--------\n#description_2:\n#{prompt_2}" }
]

sleep(0.5)

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

sleep(3)

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
Loading