[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", settings: "Settings",
selective: "Selective", selective: "Selective",
SecondaryKeys: 'Secondary keys', 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="flex w-full justify-center mt-4 max-w-100vw">
<div class="w-3xl max-w-80p bg-darkbg rounded-md p-3 text-white text-sm"> <div class="w-5/6 max-w-80vw bg-darkbg rounded-md p-3 text-white text-sm">
<h1 class="text-xl font-bold mb-2">{language.creatorNotes} <h1 class="font-bold mb-2">{language.creatorNotes}
<button class="float-right" on:click={onRemove}> <button class="float-right" on:click={onRemove}>
<XIcon /> <XIcon />
</button> </button>

View File

@@ -5,6 +5,7 @@
import { DownloadIcon, FolderUpIcon, ImportIcon, PlusIcon } from "lucide-svelte"; import { DownloadIcon, FolderUpIcon, ImportIcon, PlusIcon } from "lucide-svelte";
import { addLorebook, exportLoreBook, importLoreBook } from "../../ts/process/lorebook"; import { addLorebook, exportLoreBook, importLoreBook } from "../../ts/process/lorebook";
import LoreBookData from "./LoreBookData.svelte"; import LoreBookData from "./LoreBookData.svelte";
import Check from "../Others/Check.svelte";
let submenu = 0 let submenu = 0
</script> </script>
@@ -25,8 +26,8 @@
<span>{language.settings}</span> <span>{language.settings}</span>
</button> </button>
</div> </div>
{#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> <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"> <div class="border-solid border-borderc p-2 flex flex-col border-1">
{#if submenu === 0} {#if submenu === 0}
{#if $DataBase.characters[$selectedCharID].globalLore.length === 0} {#if $DataBase.characters[$selectedCharID].globalLore.length === 0}
@@ -43,7 +44,7 @@
}}/> }}/>
{/each} {/each}
{/if} {/if}
{:else} {:else if submenu === 1}
{#if $DataBase.characters[$selectedCharID].chats[$DataBase.characters[$selectedCharID].chatPage].localLore.length === 0} {#if $DataBase.characters[$selectedCharID].chats[$DataBase.characters[$selectedCharID].chatPage].localLore.length === 0}
<span class="text-gray-500">No Lorebook</span> <span class="text-gray-500">No Lorebook</span>
{:else} {:else}
@@ -59,8 +60,37 @@
{/each} {/each}
{/if} {/if}
{/if} {/if}
</div> </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}
<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}
{/if}
{#if submenu !== 2}
<div class="text-gray-500 mt-2 flex"> <div class="text-gray-500 mt-2 flex">
<button on:click={() => {addLorebook(submenu)}} class="hover:text-neutral-200 cursor-pointer"> <button on:click={() => {addLorebook(submenu)}} class="hover:text-neutral-200 cursor-pointer">
@@ -77,7 +107,7 @@
<FolderUpIcon /> <FolderUpIcon />
</button> </button>
</div> </div>
{/if}
<style> <style>
.seperator{ .seperator{
border-top: 0px; border-top: 0px;

View File

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

View File

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

View File

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