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", exportAsDataset: "Export Save as Dataset",
editTranslationDisplay: "Edit Translation Display", editTranslationDisplay: "Edit Translation Display",
selectModel: "Select Model", 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 { DataBase } from "src/ts/storage/database";
import Check from "src/lib/UI/GUI/CheckInput.svelte"; import Check from "src/lib/UI/GUI/CheckInput.svelte";
import TextInput from "src/lib/UI/GUI/TextInput.svelte"; import TextInput from "src/lib/UI/GUI/TextInput.svelte";
import NumberInput from "src/lib/UI/GUI/NumberInput.svelte";
let sorted = 0 let sorted = 0
let opened = 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.sendChatAsSystem} name={language.sendChatAsSystem} className="mt-4"/>
<Check bind:check={$DataBase.proomptSettings.sendName} name={language.sendName} 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.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} {/if}

View File

@@ -25,7 +25,7 @@
<span>{language.type} <span>{language.type}
</span> </span>
<SelectInput bind:value={proompt.type} on:change={() => { <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.text = ""
proompt.role = "system" proompt.role = "system"
} }
@@ -42,7 +42,9 @@
<OptionInput value="authornote">{language.formating.authorNote}</OptionInput> <OptionInput value="authornote">{language.formating.authorNote}</OptionInput>
<OptionInput value="lorebook">{language.formating.lorebook}</OptionInput> <OptionInput value="lorebook">{language.formating.lorebook}</OptionInput>
<OptionInput value="memory">{language.formating.memory}</OptionInput> <OptionInput value="memory">{language.formating.memory}</OptionInput>
{#if $DataBase.proomptSettings.customChainOfThought}
<OptionInput value="cot">{language.cot}</OptionInput>
{/if}
</SelectInput> </SelectInput>
{#if proompt.type === 'plain' || proompt.type === 'jailbreak'} {#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 { loadAsset, saveAsset } from 'src/ts/storage/globalApi';
import { selectSingleFile } from 'src/ts/util'; import { selectSingleFile } from 'src/ts/util';
import { v4 } from 'uuid'; import { v4 } from 'uuid';
let tfCache:Cache = null let tfCache:Cache = null
let tfLoaded = false let tfLoaded = false
let tfMap:{[key:string]:string} = {} 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({ unformated.postEverything.push({
role: 'system', 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>` 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 break
} }
case 'plain': case 'plain':
case 'jailbreak':{ case 'jailbreak':
case 'cot':{
if((!db.jailbreakToggle) && (card.type === 'jailbreak')){ if((!db.jailbreakToggle) && (card.type === 'jailbreak')){
continue continue
} }
if((!db.chainOfThought) && (card.type === 'cot')){
continue
}
const convertRole = { const convertRole = {
"system": "system", "system": "system",
@@ -521,8 +525,9 @@ export async function sendChat(chatProcessIndex = -1,arg:{chatAdditonalTokens?:n
currentTokens += triggerResult.tokens currentTokens += triggerResult.tokens
} }
let index = 0
for(const msg of ms){ 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 = '' let name = ''
if(msg.role === 'char'){ if(msg.role === 'char'){
if(msg.saying){ if(msg.saying){
@@ -541,10 +546,10 @@ export async function sendChat(chatProcessIndex = -1,arg:{chatAdditonalTokens?:n
let inlays:string[] = [] let inlays:string[] = []
if(db.inlayImage){ if(db.inlayImage){
if(msg.role === 'char'){ if(msg.role === 'char'){
formedChat = formedChat.replace(/{{inlay::(.+?)}}/g, '') formatedChat = formatedChat.replace(/{{inlay::(.+?)}}/g, '')
} }
else{ else{
const inlayMatch = formedChat.match(/{{inlay::(.+?)}}/g) const inlayMatch = formatedChat.match(/{{inlay::(.+?)}}/g)
if(inlayMatch){ if(inlayMatch){
for(const inlay of inlayMatch){ for(const inlay of inlayMatch){
inlays.push(inlay) inlays.push(inlay)
@@ -568,25 +573,32 @@ export async function sendChat(chatProcessIndex = -1,arg:{chatAdditonalTokens?:n
currentTokens += await tokenizer.tokenizeChat(imgchat) currentTokens += await tokenizer.tokenizeChat(imgchat)
} }
} }
formedChat = formedChat.replace(inlay, '') formatedChat = formatedChat.replace(inlay, '')
} }
} }
let attr:string[] = [] let attr:string[] = []
if(nowChatroom.type === 'group' || (usingPromptTemplate && db.proomptSettings.sendName)){ if(nowChatroom.type === 'group' || (usingPromptTemplate && db.proomptSettings.sendName)){
formedChat = name + ': ' + formedChat formatedChat = name + ': ' + formatedChat
attr.push('nameAdded') 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 = { const chat:OpenAIChat = {
role: msg.role === 'user' ? 'user' : 'assistant', role: msg.role === 'user' ? 'user' : 'assistant',
content: formedChat, content: formatedChat,
memo: msg.chatId, memo: msg.chatId,
attr: attr attr: attr
} }
chats.push(chat) chats.push(chat)
currentTokens += await tokenizer.tokenizeChat(chat) currentTokens += await tokenizer.tokenizeChat(chat)
index++
} }
if(nowChatroom.supaMemory && db.supaMemoryType !== 'none'){ if(nowChatroom.supaMemory && db.supaMemoryType !== 'none'){
@@ -762,10 +774,14 @@ export async function sendChat(chatProcessIndex = -1,arg:{chatAdditonalTokens?:n
break break
} }
case 'plain': case 'plain':
case 'jailbreak':{ case 'jailbreak':
case 'cot':{
if((!db.jailbreakToggle) && (card.type === 'jailbreak')){ if((!db.jailbreakToggle) && (card.type === 'jailbreak')){
continue continue
} }
if((!db.chainOfThought) && (card.type === 'cot')){
continue
}
const convertRole = { const convertRole = {
"system": "system", "system": "system",

View File

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

View File

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