Refactor loadLoreBookPrompt function in lorebook.ts
This commit is contained in:
@@ -52,7 +52,6 @@ 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 char = db.characters[selectedID]
|
const char = db.characters[selectedID]
|
||||||
@@ -65,9 +64,6 @@ export async function loadLoreBookPrompt(){
|
|||||||
const loreDepth = char.loreSettings?.scanDepth ?? db.loreBookDepth
|
const loreDepth = char.loreSettings?.scanDepth ?? db.loreBookDepth
|
||||||
const loreToken = char.loreSettings?.tokenBudget ?? db.loreBookToken
|
const loreToken = char.loreSettings?.tokenBudget ?? db.loreBookToken
|
||||||
const fullWordMatching = char.loreSettings?.fullWordMatching ?? false
|
const fullWordMatching = char.loreSettings?.fullWordMatching ?? false
|
||||||
if(char.lorePlus){
|
|
||||||
return await loadLoreBookPlusPrompt()
|
|
||||||
}
|
|
||||||
|
|
||||||
let activatiedPrompt: string[] = []
|
let activatiedPrompt: string[] = []
|
||||||
|
|
||||||
@@ -185,116 +181,73 @@ export async function loadLoreBookPrompt(){
|
|||||||
|
|
||||||
|
|
||||||
let sactivated:string[] = []
|
let sactivated:string[] = []
|
||||||
|
let decoratedArray:{
|
||||||
|
depth:number,
|
||||||
|
pos:string,
|
||||||
|
prompt:string
|
||||||
|
}[] = []
|
||||||
activatiedPrompt = activatiedPrompt.filter((v) => {
|
activatiedPrompt = activatiedPrompt.filter((v) => {
|
||||||
//deprecated three @ for special prompt
|
const decorated = decoratorParser(v)
|
||||||
if(v.startsWith("@@@end")){
|
if(decorated.decorators['dont_activate']){
|
||||||
sactivated.push(v.replace('@@@end','').trim())
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
if(v.startsWith('@@end')){
|
if(decorated.decorators['depth'] && decorated.decorators['depth'][0] === '0'){
|
||||||
sactivated.push(v.replace('@@end','').trim())
|
sactivated.push(decorated.prompt)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
if(decorated.decorators['position']){
|
||||||
|
decoratedArray.push({
|
||||||
|
depth: -1,
|
||||||
|
pos: decorated.decorators['position'][0],
|
||||||
|
prompt: decorated.prompt
|
||||||
|
})
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if(decorated.decorators)
|
||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
|
|
||||||
return {
|
return {
|
||||||
act: activatiedPrompt.reverse().join('\n\n'),
|
act: activatiedPrompt.reverse().join('\n\n'),
|
||||||
special_act: sactivated.reverse().join('\n\n')
|
special_act: sactivated.reverse().join('\n\n'),
|
||||||
|
decorated: decoratedArray
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function loadLoreBookPlusPrompt(){
|
const supportedDecorators = ['depth','dont_activate','position']
|
||||||
const selectedID = get(selectedCharID)
|
export function decoratorParser(prompt:string){
|
||||||
const db = get(DataBase)
|
const split = prompt.split('\n')
|
||||||
const char = db.characters[selectedID]
|
let decorators:{[name:string]:string[]} = {}
|
||||||
const page = char.chatPage
|
|
||||||
const characterLore = char.globalLore ?? []
|
|
||||||
const chatLore = char.chats[page].localLore ?? []
|
|
||||||
const fullLore = characterLore.concat(chatLore).concat(getModuleLorebooks()).filter((v) => { return v.content })
|
|
||||||
const currentChat = char.chats[page].message
|
|
||||||
const loreDepth = char.loreSettings?.scanDepth ?? db.loreBookDepth
|
|
||||||
const loreToken = char.loreSettings?.tokenBudget ?? db.loreBookToken
|
|
||||||
|
|
||||||
interface formatedLorePlus{
|
let fallbacking = false
|
||||||
content: string
|
for(let i=0;i<split.length;i++){
|
||||||
simularity:number
|
const line = split[i].trim()
|
||||||
}
|
if(line.startsWith('@@')){
|
||||||
|
const data = line.startsWith('@@@') ? line.replace('@@@','') : line.replace('@@','')
|
||||||
let formatedLores:formatedLorePlus[] = []
|
const name = data.split(' ')[0]
|
||||||
let activatiedPrompt: string[] = []
|
const values = data.replace(name,'').trim().split(',')
|
||||||
const hypaProcesser = new HypaProcesser('MiniLM')
|
if(!supportedDecorators.includes(name)){
|
||||||
|
fallbacking = true
|
||||||
|
continue
|
||||||
const formatedChatMain = currentChat.slice(currentChat.length - loreDepth,currentChat.length).map((msg) => {
|
}
|
||||||
return msg.data
|
if((!line.startsWith('@@@')) || fallbacking){
|
||||||
}).join('||').replace(rmRegex,'').toLocaleLowerCase()
|
decorators[name] = values
|
||||||
const chatVec = await hypaProcesser.testText(formatedChatMain)
|
}
|
||||||
|
}
|
||||||
|
else if(line === '@@end' || line === '@@@end'){
|
||||||
for(const lore of fullLore){
|
decorators['depth'] = ['0']
|
||||||
let key = (lore.key ?? '').replace(rmRegex, '').toLocaleLowerCase().split(',')
|
}
|
||||||
key.push(lore.comment)
|
else{
|
||||||
|
return {
|
||||||
let vec:number[]
|
prompt: split.slice(i).join('\n').trim(),
|
||||||
|
decorators: decorators
|
||||||
if(lore.loreCache && lore.loreCache.key === lore.content){
|
|
||||||
const vect = lore.loreCache.data[0]
|
|
||||||
const v = Buffer.from(vect, 'base64')
|
|
||||||
const f = new Float32Array(v.buffer)
|
|
||||||
vec = Array.from(f)
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
vec = await hypaProcesser.testText(lore.content)
|
|
||||||
lore.loreCache = {
|
|
||||||
key: lore.content,
|
|
||||||
data: [Buffer.from(new Float32Array(vec).buffer).toString('base64')]
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
formatedLores.push({
|
|
||||||
content: lore.content,
|
|
||||||
simularity: hypaProcesser.similarityCheck(chatVec, vec)
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
formatedLores.sort((a, b) => {
|
|
||||||
return b.simularity - a.simularity
|
|
||||||
})
|
|
||||||
|
|
||||||
let i=0;
|
|
||||||
while(i < formatedLores.length){
|
|
||||||
const lore = formatedLores[i]
|
|
||||||
const totalTokens = await tokenize(activatiedPrompt.concat([lore.content]).join('\n\n'))
|
|
||||||
if(totalTokens > loreToken){
|
|
||||||
break
|
|
||||||
}
|
|
||||||
activatiedPrompt.push(lore.content)
|
|
||||||
i++
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
let sactivated:string[] = []
|
|
||||||
activatiedPrompt = activatiedPrompt.filter((v) => {
|
|
||||||
//deprecated three @ for special prompt
|
|
||||||
if(v.startsWith("@@@end")){
|
|
||||||
sactivated.push(v.replace('@@@end','').trim())
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
if(v.startsWith('@@end')){
|
|
||||||
sactivated.push(v.replace('@@end','').trim())
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
})
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
act: activatiedPrompt.reverse().join('\n\n'),
|
prompt: '',
|
||||||
special_act: sactivated.reverse().join('\n\n')
|
decorators: decorators
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function importLoreBook(mode:'global'|'local'|'sglobal'){
|
export async function importLoreBook(mode:'global'|'local'|'sglobal'){
|
||||||
|
|||||||
Reference in New Issue
Block a user