[feat] added character lore settings

This commit is contained in:
kwaroran
2023-05-12 23:01:55 +09:00
parent 3e34c17f3b
commit ece4163034
6 changed files with 135 additions and 68 deletions

View File

@@ -239,5 +239,6 @@ export const languageEnglish = {
settings: "Settings",
selective: "Selective",
SecondaryKeys: 'Secondary keys',
useGlobalSettings: "Use Global Settings",
recursiveScanning: "Recursive Scanning"
}

View File

@@ -1,6 +1,6 @@
<div class="flex w-full justify-center">
<div class="w-3xl max-w-80p bg-darkbg rounded-md p-3 text-white text-sm">
<h1 class="text-xl font-bold mb-2">{language.creatorNotes}
<div class="flex w-full justify-center mt-4 max-w-100vw">
<div class="w-5/6 max-w-80vw bg-darkbg rounded-md p-3 text-white text-sm">
<h1 class="font-bold mb-2">{language.creatorNotes}
<button class="float-right" on:click={onRemove}>
<XIcon />
</button>

View File

@@ -5,6 +5,7 @@
import { DownloadIcon, FolderUpIcon, ImportIcon, PlusIcon } from "lucide-svelte";
import { addLorebook, exportLoreBook, importLoreBook } from "../../ts/process/lorebook";
import LoreBookData from "./LoreBookData.svelte";
import Check from "../Others/Check.svelte";
let submenu = 0
</script>
@@ -25,42 +26,71 @@
<span>{language.settings}</span>
</button>
</div>
<span class="text-gray-500 mt-2 mb-6 text-sm">{submenu === 0 ? $DataBase.characters[$selectedCharID].type === 'group' ? language.groupLoreInfo : language.globalLoreInfo : language.localLoreInfo}</span>
<div class="border-solid border-borderc p-2 flex flex-col border-1">
{#if submenu === 0}
{#if $DataBase.characters[$selectedCharID].globalLore.length === 0}
<span class="text-gray-500">No Lorebook</span>
{:else}
{#each $DataBase.characters[$selectedCharID].globalLore as book, i}
{#if i !== 0}
<div class="border-borderc mt-2 mb-2 w-full border-solid border-b-1 seperator"></div>
{/if}
<LoreBookData bind:value={$DataBase.characters[$selectedCharID].globalLore[i]} onRemove={() => {
let lore = $DataBase.characters[$selectedCharID].globalLore
lore.splice(i, 1)
$DataBase.characters[$selectedCharID].globalLore = lore
}}/>
{/each}
{#if submenu !== 2}
<span class="text-gray-500 mt-2 mb-6 text-sm">{submenu === 0 ? $DataBase.characters[$selectedCharID].type === 'group' ? language.groupLoreInfo : language.globalLoreInfo : language.localLoreInfo}</span>
<div class="border-solid border-borderc p-2 flex flex-col border-1">
{#if submenu === 0}
{#if $DataBase.characters[$selectedCharID].globalLore.length === 0}
<span class="text-gray-500">No Lorebook</span>
{:else}
{#each $DataBase.characters[$selectedCharID].globalLore as book, i}
{#if i !== 0}
<div class="border-borderc mt-2 mb-2 w-full border-solid border-b-1 seperator"></div>
{/if}
<LoreBookData bind:value={$DataBase.characters[$selectedCharID].globalLore[i]} onRemove={() => {
let lore = $DataBase.characters[$selectedCharID].globalLore
lore.splice(i, 1)
$DataBase.characters[$selectedCharID].globalLore = lore
}}/>
{/each}
{/if}
{:else if submenu === 1}
{#if $DataBase.characters[$selectedCharID].chats[$DataBase.characters[$selectedCharID].chatPage].localLore.length === 0}
<span class="text-gray-500">No Lorebook</span>
{:else}
{#each $DataBase.characters[$selectedCharID].chats[$DataBase.characters[$selectedCharID].chatPage].localLore as book, i}
{#if i !== 0}
<div class="border-borderc mt-2 mb-2 w-full border-solid border-b-1 seperator"></div>
{/if}
<LoreBookData bind:value={$DataBase.characters[$selectedCharID].chats[$DataBase.characters[$selectedCharID].chatPage].localLore[i]} onRemove={() => {
let lore = $DataBase.characters[$selectedCharID].chats[$DataBase.characters[$selectedCharID].chatPage].localLore
lore.splice(i, 1)
$DataBase.characters[$selectedCharID].chats[$DataBase.characters[$selectedCharID].chatPage].localLore = lore
}}/>
{/each}
{/if}
{/if}
</div>
{:else}
{#if $DataBase.characters[$selectedCharID].loreSettings}
<div class="flex items-center mt-4">
<Check check={false} onChange={() => {
$DataBase.characters[$selectedCharID].loreSettings = undefined
}}/>
<span>{language.useGlobalSettings}</span>
</div>
<div class="flex items-center mt-4">
<Check bind:check={$DataBase.characters[$selectedCharID].loreSettings.recursiveScanning}/>
<span>{language.recursiveScanning}</span>
</div>
<span class="text-neutral-200 mt-4 mb-2">{language.loreBookDepth}</span>
<input class="text-neutral-200 mb-4 p-2 bg-transparent input-text focus:bg-selected text-sm" type="number" min={0} max="20" bind:value={$DataBase.characters[$selectedCharID].loreSettings.scanDepth}>
<span class="text-neutral-200">{language.loreBookToken}</span>
<input class="text-neutral-200 mb-4 p-2 bg-transparent input-text focus:bg-selected text-sm" type="number" min={0} max="4096" bind:value={$DataBase.characters[$selectedCharID].loreSettings.tokenBudget}>
{:else}
{#if $DataBase.characters[$selectedCharID].chats[$DataBase.characters[$selectedCharID].chatPage].localLore.length === 0}
<span class="text-gray-500">No Lorebook</span>
{:else}
{#each $DataBase.characters[$selectedCharID].chats[$DataBase.characters[$selectedCharID].chatPage].localLore as book, i}
{#if i !== 0}
<div class="border-borderc mt-2 mb-2 w-full border-solid border-b-1 seperator"></div>
{/if}
<LoreBookData bind:value={$DataBase.characters[$selectedCharID].chats[$DataBase.characters[$selectedCharID].chatPage].localLore[i]} onRemove={() => {
let lore = $DataBase.characters[$selectedCharID].chats[$DataBase.characters[$selectedCharID].chatPage].localLore
lore.splice(i, 1)
$DataBase.characters[$selectedCharID].chats[$DataBase.characters[$selectedCharID].chatPage].localLore = lore
}}/>
{/each}
{/if}
<div class="flex items-center mt-4">
<Check check={true} onChange={() => {
$DataBase.characters[$selectedCharID].loreSettings = {
tokenBudget: $DataBase.loreBookToken,
scanDepth:$DataBase.loreBookDepth,
recursiveScanning: false
}
}}/>
<span>{language.useGlobalSettings}</span>
</div>
{/if}
</div>
{/if}
{#if submenu !== 2}
<div class="text-gray-500 mt-2 flex">
<button on:click={() => {addLorebook(submenu)}} class="hover:text-neutral-200 cursor-pointer">
@@ -77,7 +107,7 @@
<FolderUpIcon />
</button>
</div>
{/if}
<style>
.seperator{
border-top: 0px;

View File

@@ -254,6 +254,14 @@ export interface character{
personality:string
scenario:string
firstMsgIndex:number
loreSettings?:loreSettings
}
export interface loreSettings{
tokenBudget: number
scanDepth:number
recursiveScanning: boolean
}
export interface groupChat{
@@ -275,6 +283,7 @@ export interface groupChat{
creatorNotes?:string,
removedQuotes?:boolean
firstMsgIndex?:number,
loreSettings?:loreSettings
}
export interface botPreset{

View File

@@ -43,6 +43,7 @@ interface formatedLore{
secondKey:string[]
content: string
order: number
activatied: boolean
}
const rmRegex = / |\n/g
@@ -50,11 +51,14 @@ const rmRegex = / |\n/g
export async function loadLoreBookPrompt(){
const selectedID = get(selectedCharID)
const db = get(DataBase)
const page = db.characters[selectedID].chatPage
const globalLore = db.characters[selectedID].globalLore
const charLore = db.characters[selectedID].chats[page].localLore
const char = db.characters[selectedID]
const page = char.chatPage
const globalLore = char.globalLore
const charLore = char.chats[page].localLore
const fullLore = globalLore.concat(charLore)
const currentChat = db.characters[selectedID].chats[page].message
const currentChat = char.chats[page].message
const loreDepth = char.loreSettings?.scanDepth ?? db.loreBookDepth
const loreToken = char.loreSettings?.tokenBudget ?? db.loreBookToken
let activatiedPrompt: string[] = []
@@ -70,7 +74,8 @@ export async function loadLoreBookPrompt(){
return a.length > 1
}) : [],
content: lore.content,
order: lore.insertorder
order: lore.insertorder,
activatied: false
})
}
}
@@ -79,40 +84,58 @@ export async function loadLoreBookPrompt(){
return b.order - a.order
})
const formatedChat = currentChat.slice(currentChat.length - db.loreBookDepth,currentChat.length).map((msg) => {
const formatedChat = currentChat.slice(currentChat.length - loreDepth,currentChat.length).map((msg) => {
return msg.data
}).join('||').replace(rmRegex,'').toLocaleLowerCase()
for(const lore of formatedLore){
const totalTokens = await tokenize(activatiedPrompt.concat([lore.content]).join('\n\n'))
if(totalTokens > db.loreBookToken){
break
}
if(lore.keys === 'always'){
activatiedPrompt.push(lore.content)
continue
}
let firstKeyActivation = false
for(const key of lore.keys){
if(formatedChat.includes(key)){
firstKeyActivation = true
break
}
}
if(firstKeyActivation){
if(lore.secondKey.length === 0){
activatiedPrompt.push(lore.content)
let loreListUpdated = true
while(loreListUpdated){
loreListUpdated = false
for(let i=0;i<formatedLore.length;i++){
const lore = formatedLore[i]
if(lore.activatied){
continue
}
for(const key of lore.secondKey){
const totalTokens = await tokenize(activatiedPrompt.concat([lore.content]).join('\n\n'))
if(totalTokens > loreToken){
break
}
if(lore.keys === 'always'){
activatiedPrompt.push(lore.content)
lore.activatied = true
loreListUpdated = true
continue
}
let firstKeyActivation = false
for(const key of lore.keys){
if(formatedChat.includes(key)){
activatiedPrompt.push(lore.content)
firstKeyActivation = true
break
}
}
if(firstKeyActivation){
if(lore.secondKey.length === 0){
activatiedPrompt.push(lore.content)
lore.activatied = true
loreListUpdated = true
continue
}
for(const key of lore.secondKey){
if(formatedChat.includes(key)){
activatiedPrompt.push(lore.content)
lore.activatied = true
loreListUpdated = true
break
}
}
}
}
if(!(char.loreSettings?.recursiveScanning)){
break
}
}
@@ -129,6 +152,8 @@ export async function importLoreBook(mode:'global'|'local'){
if(!lorebook){
return
}
try {
const importedlore = JSON.parse(Buffer.from(lorebook).toString('utf-8'))

View File

@@ -21,7 +21,9 @@ export default {
maxWidth:{
'half': '50%',
'80p': '80%',
'80vw': '80vw',
'14': '3.5rem',
'100vw': '100vw'
},
borderWidth: {
'1': '1px',