From 6fdd3148cbeed503df440f19fe7da2cf616c8b04 Mon Sep 17 00:00:00 2001
From: Andrew Brown
Date: Mon, 17 Apr 2023 10:45:01 -0700
Subject: [PATCH] basic Notebook.ai-->OpenCharacters character export working
---
app/controllers/conversation_controller.rb | 109 +++++++++++++++++-
.../conversation/character_landing.html.erb | 2 -
2 files changed, 104 insertions(+), 7 deletions(-)
diff --git a/app/controllers/conversation_controller.rb b/app/controllers/conversation_controller.rb
index 7c1c5910..514106f4 100644
--- a/app/controllers/conversation_controller.rb
+++ b/app/controllers/conversation_controller.rb
@@ -3,14 +3,28 @@ class ConversationController < ApplicationController
before_action :ensure_character_privacy
def character_landing
- @first_greeting = "Hello, friend!"
-
- @personality = personality_for_character
- @description = description_for_character
+ @first_greeting = default_character_greeting
+ @personality = personality_for_character
+ @description = description_for_character
end
def export
- raise open_characters_persona_params.inspect
+ add_character_hash = base_open_characters_export.merge({
+ "name": open_characters_persona_params.fetch('name', 'New character'),
+ "roleInstruction": "You are to act as #{@character.name}, whos personality is detailed below:\n\n#{description_for_character}",
+ "reminderMessage": "#{personality_for_character} Do not break character!",
+ })
+
+ # Add a character image if one has been uploaded to the page
+ avatar = @character.random_image_including_private
+ add_character_hash[:avatar][:url] = avatar if avatar.present?
+
+ # Redirect to OpenCharacters
+
+ base_oc_url = 'https://josephrocca.github.io/OpenCharacters/'
+ oc_params = { addCharacter: add_character_hash }
+
+ redirect_to "#{base_oc_url}##{ERB::Util.url_encode(oc_params)}"
end
private
@@ -65,10 +79,95 @@ class ConversationController < ApplicationController
def ensure_character_privacy
unless (user_signed_in? && @character.user == current_user) || @character.privacy == 'public'
redirect_to root_path, notice: "That character is private!"
+ return
end
end
def open_characters_persona_params
params.permit(:name, :avatar, :scenario, :char_greeting, :personality, :description, :example_dialogue)
end
+
+ def default_scenario
+ ""
+ end
+
+ def default_reminder_message
+ ""
+ end
+
+ def default_custom_code
+ ""
+
+ # TODO maybe include our standard (whisper) formatting on messages?
+ end
+
+ def base_open_characters_export
+ {
+ "name": "New character",
+ # "roleInstruction": default_scenario,
+ # "reminderMessage": default_reminder_message,
+ "modelName": "gpt-3.5-turbo",
+ "fitMessagesInContextMethod": "summarizeOld",
+ "associativeMemoryMethod": "v1",
+ "associativeMemoryEmbeddingModelName": "text-embedding-ada-002",
+ "temperature": 0.7,
+ "customCode": default_custom_code,
+ "initialMessages": default_initial_messages,
+ "avatar": {
+ "url": "",
+ "size": 1,
+ "shape": "square"
+ },
+ "scene": {
+ "background": {
+ "url": ""
+ },
+ "music": {
+ "url": ""
+ }
+ },
+ "userCharacter": {
+ "avatar": {
+ "url": current_user.avatar.url,
+ "size": 1,
+ "shape": "circle"
+ }
+ },
+ "streamingResponse": true
+ }
+ end
+
+ def default_initial_messages
+ [
+ {
+ "author": "system",
+ "content": open_characters_persona_params.fetch('scenario', default_scenario),
+ "hiddenFrom": [] # "ai", "user", "both", "neither"
+ },
+ {
+ "author": "ai",
+ "content": open_characters_persona_params.fetch('char_greeting', default_character_greeting),
+ "hiddenFrom": []
+ }
+ ]
+ end
+
+ def default_export_metadata
+ {
+ "version": 1,
+ "created": @content.created_at.to_i,
+ "modified": @content.updated_at.to_i,
+ "ncid": @content.id,
+ "source": "https://www.notebook.ai/plan/characters#{@content.id}",
+ "tool": {
+ "name": "Notebook.ai Persona Export",
+ "version": "1.0.0",
+ "url": "https://www.notebook.ai"
+ }
+ }
+ end
+
+ def default_character_greeting
+ "Hello, friend!"
+ end
end
diff --git a/app/views/conversation/character_landing.html.erb b/app/views/conversation/character_landing.html.erb
index 43502665..161ba38b 100644
--- a/app/views/conversation/character_landing.html.erb
+++ b/app/views/conversation/character_landing.html.erb
@@ -19,10 +19,8 @@
character talks. If your character is public, you can also share this page with others
to let them talk to your character!
-
-
<%= form_tag export_character_path(@character.id), method: :post do |f| %>
<%= hidden_field_tag "name", @character.name %>