Add custom chain of thought feature and max thought tag depth setting

This commit is contained in:
kwaroran
2024-01-08 09:03:12 +09:00
parent ecbbc8e85c
commit aaa5843c51
7 changed files with 45 additions and 14 deletions

View File

@@ -485,4 +485,7 @@ export const languageEnglish = {
exportAsDataset: "Export Save as Dataset",
editTranslationDisplay: "Edit Translation Display",
selectModel: "Select Model",
autoRemoveThoughtTag: "Remove Thought Tag",
customChainOfThought: "Custom Chain of Thoughts",
maxThoughtTagDepth: "Max Thought Tag Depth",
}

View File

@@ -7,6 +7,7 @@
import { DataBase } from "src/ts/storage/database";
import Check from "src/lib/UI/GUI/CheckInput.svelte";
import TextInput from "src/lib/UI/GUI/TextInput.svelte";
import NumberInput from "src/lib/UI/GUI/NumberInput.svelte";
let sorted = 0
let opened = 0
@@ -111,4 +112,9 @@
<Check bind:check={$DataBase.proomptSettings.sendChatAsSystem} name={language.sendChatAsSystem} className="mt-4"/>
<Check bind:check={$DataBase.proomptSettings.sendName} name={language.sendName} className="mt-4"/>
<Check bind:check={$DataBase.proomptSettings.utilOverride} name={language.utilOverride} className="mt-4"/>
<Check bind:check={$DataBase.proomptSettings.customChainOfThought} name={language.customChainOfThought} className="mt-4"/>
{#if $DataBase.proomptSettings.customChainOfThought}
<span class="text-textcolor mt-4">{language.maxThoughtTagDepth}</span>
<NumberInput bind:value={$DataBase.proomptSettings.maxThoughtTagDepth}/>
{/if}
{/if}

View File

@@ -25,7 +25,7 @@
<span>{language.type}
</span>
<SelectInput bind:value={proompt.type} on:change={() => {
if(proompt.type === 'plain' || proompt.type === 'jailbreak'){
if(proompt.type === 'plain' || proompt.type === 'jailbreak' || proompt.type === 'cot'){
proompt.text = ""
proompt.role = "system"
}
@@ -42,7 +42,9 @@
<OptionInput value="authornote">{language.formating.authorNote}</OptionInput>
<OptionInput value="lorebook">{language.formating.lorebook}</OptionInput>
<OptionInput value="memory">{language.formating.memory}</OptionInput>
{#if $DataBase.proomptSettings.customChainOfThought}
<OptionInput value="cot">{language.cot}</OptionInput>
{/if}
</SelectInput>
{#if proompt.type === 'plain' || proompt.type === 'jailbreak'}

View File

@@ -3,7 +3,6 @@ import { unzip } from 'fflate';
import { loadAsset, saveAsset } from 'src/ts/storage/globalApi';
import { selectSingleFile } from 'src/ts/util';
import { v4 } from 'uuid';
let tfCache:Cache = null
let tfLoaded = false
let tfMap:{[key:string]:string} = {}

View File

@@ -262,7 +262,7 @@ export async function sendChat(chatProcessIndex = -1,arg:{chatAdditonalTokens?:n
})
}
if(db.chainOfThought){
if(db.chainOfThought && (!(usingPromptTemplate && db.proomptSettings.customChainOfThought))){
unformated.postEverything.push({
role: 'system',
content: `<instruction> - before respond everything, Think step by step as a ai assistant how would you respond inside <Thoughts> xml tag. this must be less than 5 paragraphs.</instruction>`
@@ -403,10 +403,14 @@ export async function sendChat(chatProcessIndex = -1,arg:{chatAdditonalTokens?:n
break
}
case 'plain':
case 'jailbreak':{
case 'jailbreak':
case 'cot':{
if((!db.jailbreakToggle) && (card.type === 'jailbreak')){
continue
}
if((!db.chainOfThought) && (card.type === 'cot')){
continue
}
const convertRole = {
"system": "system",
@@ -521,8 +525,9 @@ export async function sendChat(chatProcessIndex = -1,arg:{chatAdditonalTokens?:n
currentTokens += triggerResult.tokens
}
let index = 0
for(const msg of ms){
let formedChat = await processScript(nowChatroom,risuChatParser(msg.data, {chara: currentChar, rmVar: true}), 'editprocess')
let formatedChat = await processScript(nowChatroom,risuChatParser(msg.data, {chara: currentChar, rmVar: true}), 'editprocess')
let name = ''
if(msg.role === 'char'){
if(msg.saying){
@@ -541,10 +546,10 @@ export async function sendChat(chatProcessIndex = -1,arg:{chatAdditonalTokens?:n
let inlays:string[] = []
if(db.inlayImage){
if(msg.role === 'char'){
formedChat = formedChat.replace(/{{inlay::(.+?)}}/g, '')
formatedChat = formatedChat.replace(/{{inlay::(.+?)}}/g, '')
}
else{
const inlayMatch = formedChat.match(/{{inlay::(.+?)}}/g)
const inlayMatch = formatedChat.match(/{{inlay::(.+?)}}/g)
if(inlayMatch){
for(const inlay of inlayMatch){
inlays.push(inlay)
@@ -568,25 +573,32 @@ export async function sendChat(chatProcessIndex = -1,arg:{chatAdditonalTokens?:n
currentTokens += await tokenizer.tokenizeChat(imgchat)
}
}
formedChat = formedChat.replace(inlay, '')
formatedChat = formatedChat.replace(inlay, '')
}
}
let attr:string[] = []
if(nowChatroom.type === 'group' || (usingPromptTemplate && db.proomptSettings.sendName)){
formedChat = name + ': ' + formedChat
formatedChat = name + ': ' + formatedChat
attr.push('nameAdded')
}
if(usingPromptTemplate && db.proomptSettings.customChainOfThought && db.proomptSettings.maxThoughtTagDepth !== -1){
const depth = ms.length - index
if(depth >= db.proomptSettings.maxThoughtTagDepth){
formatedChat = formatedChat.replace(/<Thoughts>(.+?)<\/Thoughts>/gm, '')
}
}
const chat:OpenAIChat = {
role: msg.role === 'user' ? 'user' : 'assistant',
content: formedChat,
content: formatedChat,
memo: msg.chatId,
attr: attr
}
chats.push(chat)
currentTokens += await tokenizer.tokenizeChat(chat)
index++
}
if(nowChatroom.supaMemory && db.supaMemoryType !== 'none'){
@@ -762,10 +774,14 @@ export async function sendChat(chatProcessIndex = -1,arg:{chatAdditonalTokens?:n
break
}
case 'plain':
case 'jailbreak':{
case 'jailbreak':
case 'cot':{
if((!db.jailbreakToggle) && (card.type === 'jailbreak')){
continue
}
if((!db.chainOfThought) && (card.type === 'cot')){
continue
}
const convertRole = {
"system": "system",

View File

@@ -8,10 +8,12 @@ export type ProomptSettings = {
sendChatAsSystem: boolean
sendName: boolean
utilOverride: boolean
customChainOfThought?: boolean
maxThoughtTagDepth?: number
}
export interface ProomptPlain {
type: 'plain'|'jailbreak';
type: 'plain'|'jailbreak'|'cot';
type2: 'normal'|'globalNote'|'main'
text: string;
role: 'user'|'bot'|'system';

View File

@@ -367,10 +367,13 @@ export function setDatabase(data:Database){
postEndInnerFormat: '',
sendChatAsSystem: false,
sendName: false,
utilOverride: false
utilOverride: false,
customChainOfThought: false,
maxThoughtTagDepth: -1
}
data.keiServerURL ??= ''
data.top_k ??= 0
data.proomptSettings.maxThoughtTagDepth ??= -1
changeLanguage(data.language)
DataBase.set(data)