|
| 1 | +class ConversationController < ApplicationController |
| 2 | + before_action :authenticate_user!, only: [:character_index] |
| 3 | + |
| 4 | + before_action :set_character, only: [:character_landing, :export] |
| 5 | + before_action :ensure_character_privacy, only: [:character_landing, :export] |
| 6 | + |
| 7 | + def character_index |
| 8 | + @characters = @current_user_content.fetch('Character', []) |
| 9 | + end |
| 10 | + |
| 11 | + def character_landing |
| 12 | + @first_greeting = default_character_greeting |
| 13 | + @personality = personality_for_character |
| 14 | + @description = description_for_character |
| 15 | + end |
| 16 | + |
| 17 | + def export |
| 18 | + name = open_characters_persona_params.fetch('name', 'New character').strip |
| 19 | + description = open_characters_persona_params.fetch('description', '') |
| 20 | + |
| 21 | + add_character_hash = base_open_characters_export.merge({ |
| 22 | + "uuid": deterministic_uuid(@character.id), |
| 23 | + "name": name, |
| 24 | + "roleInstruction": full_role_instruction, |
| 25 | + "reminderMessage": reminder_message, |
| 26 | + }) |
| 27 | + |
| 28 | + # Add a character image if one has been uploaded to the page |
| 29 | + avatar = @character.random_image_including_private |
| 30 | + add_character_hash[:avatar][:url] = avatar if avatar.present? |
| 31 | + |
| 32 | + # Provide a default scenario if one wasn't given |
| 33 | + add_character_hash[:scenario] ||= default_scenario |
| 34 | + |
| 35 | + # Redirect to OpenCharacters |
| 36 | + base_oc_url = 'https://josephrocca.github.io/OpenCharacters/' |
| 37 | + oc_params = { addCharacter: add_character_hash } |
| 38 | + |
| 39 | + redirect_to "#{base_oc_url}##{ERB::Util.url_encode(oc_params.to_json)}" |
| 40 | + end |
| 41 | + |
| 42 | + private |
| 43 | + |
| 44 | + def deterministic_uuid(id) |
| 45 | + static_prefix = "notebook-" |
| 46 | + hashed_id = Digest::SHA1.hexdigest(static_prefix + id.to_s) |
| 47 | + uuid = "#{hashed_id[0..7]}-#{hashed_id[8..11]}-#{hashed_id[12..15]}-#{hashed_id[16..19]}-#{hashed_id[20..31]}" |
| 48 | + uuid |
| 49 | + end |
| 50 | + |
| 51 | + def full_role_instruction |
| 52 | + final_text = [ |
| 53 | + "[SYSTEM]: You are roleplaying as #{@character.name}, #{personality_for_character}", |
| 54 | + "", |
| 55 | + "Follow this pattern:", |
| 56 | + "\"Hello!\" - dialogue", |
| 57 | + "*He jumps out of the bushes.* - action", |
| 58 | + "", |
| 59 | + "#{@character.name}'s personality is below:", |
| 60 | + "#{open_characters_persona_params.fetch('description', '(not included)')}", |
| 61 | + "", |
| 62 | + "#{@character.name} will now respond while staying in character in an extremely descriptive manner at length, avoiding being repetitive, without advancing events by herself, avoiding implying conversations without a reply from the user first, and wait for the user's reply to advance events. Describe what #{@character.name} is feeling, saying, and doing with rich detail, but do not include any parenthetical thoughts and focus primarily on dialogue.", |
| 63 | + ].join("\n") |
| 64 | + end |
| 65 | + |
| 66 | + def reminder_message |
| 67 | + "[SYSTEM]: (Thought: I need to rememeber to be creative, descriptive, and engaging! I should work very hard to avoid being repetitive as well! Unless the user speaks to me OOC, with parentheses around their input, first, I will not say anything OOC or in parentheses. I shouldn't ignore parts of the user's post, even if they move on to a new scene. I should at least write my characters thoughts and feelings towards the prior scene before continuing with the new one. I don't need to feel the need to write a long response if the user's post is short. In that case, I can feel free to write a short response myself - making sure to not take over writing the user's character's dialogue, thoughts, or actions!)" |
| 68 | + end |
| 69 | + |
| 70 | + def personality_for_character |
| 71 | + hobbies = @character.get_field_value('Nature', 'Hobbies') |
| 72 | + |
| 73 | + personality_parts = [ |
| 74 | + "a #{@character.get_field_value('Overview', 'Gender', fallback='')} #{@character.get_field_value('Overview', 'Role', fallback='character')}, age #{@character.get_field_value('Overview', 'Age', fallback='irrelevant')}." |
| 75 | + ] |
| 76 | + personality_parts << "Their hobbies include #{hobbies}." if hobbies.present? |
| 77 | + |
| 78 | + ContentFormatterService.plaintext_show(text: personality_parts.join(' '), viewing_user: current_user) |
| 79 | + end |
| 80 | + |
| 81 | + def description_for_character |
| 82 | + occupation = @character.get_field_value('Social', 'Occupation') |
| 83 | + background = @character.get_field_value('History', 'Background') |
| 84 | + motivations = @character.get_field_value('Nature', 'Motivations') |
| 85 | + mannerisms = @character.get_field_value('Nature', 'Mannerisms') |
| 86 | + flaws = @character.get_field_value('Nature', 'Flaws') |
| 87 | + prejudices = @character.get_field_value('Nature', 'Prejudices') |
| 88 | + talents = @character.get_field_value('Nature', 'Talents') |
| 89 | + hobbies = @character.get_field_value('Nature', 'Hobbies') |
| 90 | + |
| 91 | + description_parts = [] |
| 92 | + description_parts.concat ["OCCUPATION", occupation, nil] if occupation.present? |
| 93 | + description_parts.concat ["BACKGROUND", background, nil] if background.present? |
| 94 | + description_parts.concat ["MOTIVATIONS", motivations, nil] if motivations.present? |
| 95 | + description_parts.concat ["MANNERISMS", mannerisms, nil] if mannerisms.present? |
| 96 | + description_parts.concat ["FLAWS", flaws, nil] if flaws.present? |
| 97 | + description_parts.concat ["PREJUDICES", prejudices, nil] if prejudices.present? |
| 98 | + description_parts.concat ["TALENTS", talents, nil] if talents.present? |
| 99 | + description_parts.concat ["HOBBIES", hobbies, nil] if hobbies.present? |
| 100 | + |
| 101 | + ContentFormatterService.plaintext_show(text: description_parts.join("\n"), viewing_user: current_user) |
| 102 | + end |
| 103 | + |
| 104 | + def set_character |
| 105 | + @character = Character.find(params[:character_id].to_i) |
| 106 | + end |
| 107 | + |
| 108 | + def ensure_character_privacy |
| 109 | + unless (user_signed_in? && @character.user == current_user) || @character.privacy == 'public' |
| 110 | + redirect_to root_path, notice: "That character is private!" |
| 111 | + return |
| 112 | + end |
| 113 | + end |
| 114 | + |
| 115 | + def open_characters_persona_params |
| 116 | + params.permit(:name, :avatar, :scenario, :char_greeting, :personality, :description, :example_dialogue) |
| 117 | + end |
| 118 | + |
| 119 | + def default_scenario |
| 120 | + "This character is interacting with the user within their own fictional universe. The character will respond in a way that is consistent with their personality and background." |
| 121 | + end |
| 122 | + |
| 123 | + def default_custom_code |
| 124 | + "" |
| 125 | + |
| 126 | + # TODO maybe include our standard (whisper) formatting on messages? |
| 127 | + end |
| 128 | + |
| 129 | + def base_open_characters_export |
| 130 | + { |
| 131 | + "name": "New character", |
| 132 | + "modelName": "gpt-3.5-turbo", |
| 133 | + "fitMessagesInContextMethod": "summarizeOld", |
| 134 | + "associativeMemoryMethod": "v1", |
| 135 | + "associativeMemoryEmbeddingModelName": "text-embedding-ada-002", |
| 136 | + "temperature": 0.7, |
| 137 | + "customCode": default_custom_code, |
| 138 | + "initialMessages": default_initial_messages, |
| 139 | + "avatar": { |
| 140 | + "url": "", |
| 141 | + "size": 1, |
| 142 | + "shape": "square" |
| 143 | + }, |
| 144 | + "scene": { |
| 145 | + "background": { |
| 146 | + "url": "" |
| 147 | + }, |
| 148 | + "music": { |
| 149 | + "url": "" |
| 150 | + } |
| 151 | + }, |
| 152 | + "userCharacter": { |
| 153 | + "avatar": { |
| 154 | + "url": current_user.avatar.url, |
| 155 | + "size": 1, |
| 156 | + "shape": "circle" |
| 157 | + } |
| 158 | + }, |
| 159 | + "streamingResponse": true |
| 160 | + } |
| 161 | + end |
| 162 | + |
| 163 | + def default_initial_messages |
| 164 | + [ |
| 165 | + { |
| 166 | + "author": "system", |
| 167 | + "content": "Scenario: " + (open_characters_persona_params.fetch('scenario', nil).presence || default_scenario), |
| 168 | + "hiddenFrom": [] # "ai", "user", "both", "neither" |
| 169 | + }, |
| 170 | + { |
| 171 | + "author": "ai", |
| 172 | + "content": open_characters_persona_params.fetch('char_greeting', default_character_greeting), |
| 173 | + "hiddenFrom": [] |
| 174 | + } |
| 175 | + ] |
| 176 | + end |
| 177 | + |
| 178 | + def default_export_metadata |
| 179 | + { |
| 180 | + "version": 1, |
| 181 | + "created": @content.created_at.to_i, |
| 182 | + "modified": @content.updated_at.to_i, |
| 183 | + "ncid": @content.id, |
| 184 | + "source": "https://www.notebook.ai/plan/characters#{@content.id}", |
| 185 | + "tool": { |
| 186 | + "name": "Notebook.ai Persona Export", |
| 187 | + "version": "1.0.0", |
| 188 | + "url": "https://www.notebook.ai" |
| 189 | + } |
| 190 | + } |
| 191 | + end |
| 192 | + |
| 193 | + def default_character_greeting |
| 194 | + "Hello!" |
| 195 | + end |
| 196 | +end |
0 commit comments