Add ChatML and Rework display

This commit is contained in:
kwaroran
2024-12-01 17:05:50 +09:00
parent 5ebb934d3b
commit 8a6cfedcda
4 changed files with 234 additions and 86 deletions

View File

@@ -10,6 +10,7 @@
import TextInput from "./GUI/TextInput.svelte"; import TextInput from "./GUI/TextInput.svelte";
import { DBState } from 'src/ts/stores.svelte'; import { DBState } from 'src/ts/stores.svelte';
let opened = $state(false)
interface Props { interface Props {
promptItem: PromptItem; promptItem: PromptItem;
onRemove?: () => void; onRemove?: () => void;
@@ -36,101 +37,227 @@
promptItem = currentprompt promptItem = currentprompt
} }
</script> function getName(promptItem:PromptItem){
<div class="flex flex-col first:mt-0 mt-2 border border-selected p-4 rounded-md bg-darkbg"> if(promptItem.name){
<span class="mb-2"> return promptItem.name
<button class="float-right" onclick={onRemove}><XIcon /></button> }
<button class="float-right" onclick={moveDown}><ArrowDown /></button>
<button class="float-right" onclick={moveUp}><ArrowUp /></button> if(promptItem.type === 'plain'){
</span> return language.formating.plain
<span>{language.type} }
</span> if(promptItem.type === 'jailbreak'){
<SelectInput bind:value={promptItem.type} onchange={() => { return language.formating.jailbreak
if(promptItem.type === 'plain' || promptItem.type === 'jailbreak' || promptItem.type === 'cot'){
promptItem.text = ""
promptItem.role = "system"
} }
if(promptItem.type === 'chat'){ if(promptItem.type === 'chat'){
promptItem.rangeStart = -1000 return language.Chat
promptItem.rangeEnd = 'end'
} }
}} > if(promptItem.type === 'persona'){
<OptionInput value="plain">{language.formating.plain}</OptionInput> return language.formating.personaPrompt
<OptionInput value="jailbreak">{language.formating.jailbreak}</OptionInput> }
<OptionInput value="chat">{language.Chat}</OptionInput> if(promptItem.type === 'description'){
<OptionInput value="persona">{language.formating.personaPrompt}</OptionInput> return language.formating.description
<OptionInput value="description">{language.formating.description}</OptionInput> }
<OptionInput value="authornote">{language.formating.authorNote}</OptionInput> if(promptItem.type === 'authornote'){
<OptionInput value="lorebook">{language.formating.lorebook}</OptionInput> return language.formating.authorNote
<OptionInput value="memory">{language.formating.memory}</OptionInput> }
<OptionInput value="postEverything">{language.formating.postEverything}</OptionInput> if(promptItem.type === 'lorebook'){
{#if DBState.db.promptSettings.customChainOfThought} return language.formating.lorebook
<OptionInput value="cot">{language.cot}</OptionInput> }
{/if} if(promptItem.type === 'memory'){
</SelectInput> return language.formating.memory
}
if(promptItem.type === 'postEverything'){
return language.formating.postEverything
}
if(promptItem.type === 'cot'){
return language.cot
}
if(promptItem.type === 'chatML'){
return 'ChatML'
}
return ""
}
{#if promptItem.type === 'plain' || promptItem.type === 'jailbreak' || promptItem.type === 'cot'} function replacePrompt(prompt:PromptItem){
<span>{language.specialType}</span> if(JSON.stringify(promptItem) === JSON.stringify(prompt)){
<SelectInput bind:value={promptItem.type2}> return
<OptionInput value="normal">{language.noSpecialType}</OptionInput> }
<OptionInput value="main">{language.mainPrompt}</OptionInput>
<OptionInput value="globalNote">{language.globalNote}</OptionInput> const ind = DBState.db.promptTemplate.findIndex((item, index) => {
return JSON.stringify(item) === JSON.stringify(prompt)
})
if(ind !== -1){
DBState.db.promptTemplate[ind] = promptItem
promptItem = prompt
}
else{
const myInd = DBState.db.promptTemplate.findIndex((item, index) => {
return JSON.stringify(item) === JSON.stringify(promptItem)
})
DBState.db.promptTemplate.splice(myInd, 0, prompt)
}
}
</script>
<div class="first:mt-0 w-full h-2" role="doc-pagebreak" ondrop={(e) => {
e.preventDefault()
e.stopPropagation()
const data = e.dataTransfer.getData('text')
if(data === 'prompt'){
const prompt = JSON.parse(e.dataTransfer.getData('prompt'))
replacePrompt(prompt)
}
}} ondragover={(e) => {
e.preventDefault()
}} draggable="true" ondragstart={(e) => {
e.dataTransfer.setData('text', 'prompt')
e.dataTransfer.setData('prompt', JSON.stringify(promptItem))
}}>
</div>
<!-- svelte-ignore a11y_no_static_element_interactions -->
<div
class="flex flex-col border border-selected p-4 rounded-md bg-darkbg"
ondragover={(e) => {
e.preventDefault()
}}
ondrop={(e) => {
e.preventDefault()
const data = e.dataTransfer.getData('text')
if(data === 'prompt'){
const prompt = JSON.parse(e.dataTransfer.getData('prompt'))
replacePrompt(prompt)
}
}}
draggable={opened ? false : true}
ondragstart={(e) => {
e.dataTransfer.setData('text', 'prompt')
e.dataTransfer.setData('prompt', JSON.stringify(promptItem))
}}
>
<!-- svelte-ignore a11y_click_events_have_key_events -->
<div class="flex items-center w-full" onclick={() => {
opened = !opened
}}>
<span>{getName(promptItem)}</span>
<div class="flex flex-1 justify-end">
<button onclick={(e) => {
e.stopPropagation()
onRemove()
}}><XIcon /></button>
<button onclick={(e) => {
e.stopPropagation()
moveDown()
}}><ArrowDown /></button>
<button onclick={(e) => {
e.stopPropagation()
moveUp()
}}><ArrowUp /></button>
</div>
</div>
{#if opened}
<span class="mt-6">{language.name}</span>
<TextInput bind:value={promptItem.name} />
<span class="mt-2">{language.type} </span>
<SelectInput bind:value={promptItem.type} onchange={() => {
if(promptItem.type === 'plain' || promptItem.type === 'jailbreak' || promptItem.type === 'cot'){
promptItem.text = ""
promptItem.role = "system"
}
if(promptItem.type === 'chat'){
promptItem.rangeStart = -1000
promptItem.rangeEnd = 'end'
}
}} >
<OptionInput value="plain">{language.formating.plain}</OptionInput>
<OptionInput value="jailbreak">{language.formating.jailbreak}</OptionInput>
<OptionInput value="chat">{language.Chat}</OptionInput>
<OptionInput value="persona">{language.formating.personaPrompt}</OptionInput>
<OptionInput value="description">{language.formating.description}</OptionInput>
<OptionInput value="authornote">{language.formating.authorNote}</OptionInput>
<OptionInput value="lorebook">{language.formating.lorebook}</OptionInput>
<OptionInput value="memory">{language.formating.memory}</OptionInput>
<OptionInput value="postEverything">{language.formating.postEverything}</OptionInput>
<OptionInput value="chatML">{"chatML"}</OptionInput>
{#if DBState.db.promptSettings.customChainOfThought}
<OptionInput value="cot">{language.cot}</OptionInput>
{/if}
</SelectInput> </SelectInput>
<span>{language.prompt}</span>
<TextAreaInput highlight bind:value={promptItem.text} /> {#if promptItem.type === 'plain' || promptItem.type === 'jailbreak' || promptItem.type === 'cot'}
<span>{language.role}</span> <span>{language.specialType}</span>
<SelectInput bind:value={promptItem.role}> <SelectInput bind:value={promptItem.type2}>
<OptionInput value="user">{language.user}</OptionInput> <OptionInput value="normal">{language.noSpecialType}</OptionInput>
<OptionInput value="bot">{language.character}</OptionInput> <OptionInput value="main">{language.mainPrompt}</OptionInput>
<OptionInput value="system">{language.systemPrompt}</OptionInput> <OptionInput value="globalNote">{language.globalNote}</OptionInput>
</SelectInput> </SelectInput>
{/if} <span>{language.prompt}</span>
{#if promptItem.type === 'chat'} <TextAreaInput highlight bind:value={promptItem.text} />
{#if promptItem.rangeStart !== -1000} <span>{language.role}</span>
<span>{language.rangeStart}</span> <SelectInput bind:value={promptItem.role}>
<NumberInput bind:value={promptItem.rangeStart} /> <OptionInput value="user">{language.user}</OptionInput>
<span>{language.rangeEnd}</span> <OptionInput value="bot">{language.character}</OptionInput>
{#if promptItem.rangeEnd === 'end'} <OptionInput value="system">{language.systemPrompt}</OptionInput>
<NumberInput value={0} marginBottom disabled/> </SelectInput>
<CheckInput name={language.untilChatEnd} check={true} onChange={() => { {/if}
if(promptItem.type === 'chat'){ {#if promptItem.type === 'chatML'}
promptItem.rangeEnd = 0 <span>{language.prompt}</span>
<TextAreaInput highlight bind:value={promptItem.text} />
{/if}
{#if promptItem.type === 'chat'}
{#if promptItem.rangeStart !== -1000}
<span>{language.rangeStart}</span>
<NumberInput bind:value={promptItem.rangeStart} />
<span>{language.rangeEnd}</span>
{#if promptItem.rangeEnd === 'end'}
<NumberInput value={0} marginBottom disabled/>
<CheckInput name={language.untilChatEnd} check={true} onChange={() => {
if(promptItem.type === 'chat'){
promptItem.rangeEnd = 0
}
}} />
{:else}
<NumberInput bind:value={promptItem.rangeEnd} marginBottom />
<CheckInput name={language.untilChatEnd} check={false} onChange={() => {
if(promptItem.type === 'chat'){
promptItem.rangeEnd = 'end'
}
}} />
{/if}
{#if DBState.db.promptSettings.sendChatAsSystem}
<CheckInput name={language.chatAsOriginalOnSystem} bind:check={promptItem.chatAsOriginalOnSystem}/>
{/if}
{/if}
<CheckInput name={language.advanced} check={promptItem.rangeStart !== -1000} onChange={chatPromptChange} className="my-2"/>
{/if}
{#if promptItem.type === 'authornote'}
<span>{language.defaultPrompt}</span>
<TextInput bind:value={promptItem.defaultText} />
{/if}
{#if promptItem.type === 'persona' || promptItem.type === 'description' || promptItem.type === 'authornote' || promptItem.type === 'memory'}
{#if !promptItem.innerFormat}
<CheckInput name={language.customInnerFormat} check={false} className="mt-2" onChange={() => {
if(promptItem.type === 'persona' || promptItem.type === 'description' || promptItem.type === 'authornote' || promptItem.type === 'memory'){
promptItem.innerFormat = "{{slot}}"
} }
}} /> }} />
{:else} {:else}
<NumberInput bind:value={promptItem.rangeEnd} marginBottom /> <span>{language.innerFormat}</span>
<CheckInput name={language.untilChatEnd} check={false} onChange={() => { <TextAreaInput highlight bind:value={promptItem.innerFormat}/>
if(promptItem.type === 'chat'){ <CheckInput name={language.customInnerFormat} check={true} className="mt-2" onChange={() => {
promptItem.rangeEnd = 'end' if(promptItem.type === 'persona' || promptItem.type === 'description' || promptItem.type === 'authornote' || promptItem.type === 'memory'){
promptItem.innerFormat = null
} }
}} /> }} />
{/if} {/if}
{#if DBState.db.promptSettings.sendChatAsSystem}
<CheckInput name={language.chatAsOriginalOnSystem} bind:check={promptItem.chatAsOriginalOnSystem}/>
{/if}
{/if}
<CheckInput name={language.advanced} check={promptItem.rangeStart !== -1000} onChange={chatPromptChange} className="my-2"/>
{/if}
{#if promptItem.type === 'authornote'}
<span>{language.defaultPrompt}</span>
<TextInput bind:value={promptItem.defaultText} />
{/if}
{#if promptItem.type === 'persona' || promptItem.type === 'description' || promptItem.type === 'authornote' || promptItem.type === 'memory'}
{#if !promptItem.innerFormat}
<CheckInput name={language.customInnerFormat} check={false} className="mt-2" onChange={() => {
if(promptItem.type === 'persona' || promptItem.type === 'description' || promptItem.type === 'authornote' || promptItem.type === 'memory'){
promptItem.innerFormat = "{{slot}}"
}
}} />
{:else}
<span>{language.innerFormat}</span>
<TextAreaInput highlight bind:value={promptItem.innerFormat}/>
<CheckInput name={language.customInnerFormat} check={true} className="mt-2" onChange={() => {
if(promptItem.type === 'persona' || promptItem.type === 'description' || promptItem.type === 'authornote' || promptItem.type === 'memory'){
promptItem.innerFormat = null
}
}} />
{/if} {/if}
{/if} {/if}
</div> </div>

View File

@@ -2280,7 +2280,7 @@ export function parseChatML(data:string):OpenAIChat[]|null{
return { return {
role: role, role: role,
content: v content: risuChatParser(v)
} }
}) })
} }

View File

@@ -28,6 +28,7 @@ import { runImageEmbedding } from "./transformers";
import { hanuraiMemory } from "./memory/hanuraiMemory"; import { hanuraiMemory } from "./memory/hanuraiMemory";
import { hypaMemoryV2 } from "./memory/hypav2"; import { hypaMemoryV2 } from "./memory/hypav2";
import { runLuaEditTrigger } from "./lua"; import { runLuaEditTrigger } from "./lua";
import { parseChatML } from "../parser.svelte";
export interface OpenAIChat{ export interface OpenAIChat{
role: 'system'|'user'|'assistant'|'function' role: 'system'|'user'|'assistant'|'function'
@@ -548,6 +549,11 @@ export async function sendChat(chatProcessIndex = -1,arg:{
await tokenizeChatArray([prompt]) await tokenizeChatArray([prompt])
break break
} }
case 'chatML':{
let prompts = parseChatML(card.text)
await tokenizeChatArray(prompts)
break
}
case 'chat':{ case 'chat':{
let start = card.rangeStart let start = card.rangeStart
let end = (card.rangeEnd === 'end') ? unformated.chats.length : card.rangeEnd let end = (card.rangeEnd === 'end') ? unformated.chats.length : card.rangeEnd
@@ -1007,6 +1013,11 @@ export async function sendChat(chatProcessIndex = -1,arg:{
pushPrompts([prompt]) pushPrompts([prompt])
break break
} }
case 'chatML':{
let prompts = parseChatML(card.text)
pushPrompts(prompts)
break
}
case 'chat':{ case 'chat':{
let start = card.rangeStart let start = card.rangeStart
let end = (card.rangeEnd === 'end') ? unformated.chats.length : card.rangeEnd let end = (card.rangeEnd === 'end') ? unformated.chats.length : card.rangeEnd

View File

@@ -4,7 +4,7 @@ import { getDatabase, presetTemplate, setDatabase, type Database } from "../stor
import { alertError, alertNormal } from "../alert"; import { alertError, alertNormal } from "../alert";
import type { OobaChatCompletionRequestParams } from "../model/ooba"; import type { OobaChatCompletionRequestParams } from "../model/ooba";
export type PromptItem = PromptItemPlain|PromptItemTyped|PromptItemChat|PromptItemAuthorNote; export type PromptItem = PromptItemPlain|PromptItemTyped|PromptItemChat|PromptItemAuthorNote|PromptItemChatML
export type PromptType = PromptItem['type']; export type PromptType = PromptItem['type'];
export type PromptSettings = { export type PromptSettings = {
assistantPrefill: string assistantPrefill: string
@@ -21,17 +21,26 @@ export interface PromptItemPlain {
type2: 'normal'|'globalNote'|'main' type2: 'normal'|'globalNote'|'main'
text: string; text: string;
role: 'user'|'bot'|'system'; role: 'user'|'bot'|'system';
name?: string
}
export interface PromptItemChatML {
type: 'chatML'
text: string
name?: string
} }
export interface PromptItemTyped { export interface PromptItemTyped {
type: 'persona'|'description'|'lorebook'|'postEverything'|'memory' type: 'persona'|'description'|'lorebook'|'postEverything'|'memory'
innerFormat?: string innerFormat?: string,
name?: string
} }
export interface PromptItemAuthorNote { export interface PromptItemAuthorNote {
type : 'authornote' type : 'authornote'
innerFormat?: string innerFormat?: string
defaultText?: string defaultText?: string
name?: string
} }
@@ -40,6 +49,7 @@ export interface PromptItemChat {
rangeStart: number; rangeStart: number;
rangeEnd: number|'end'; rangeEnd: number|'end';
chatAsOriginalOnSystem?: boolean; chatAsOriginalOnSystem?: boolean;
name?: string
} }
export async function tokenizePreset(prompts:PromptItem[], consti:boolean = false){ export async function tokenizePreset(prompts:PromptItem[], consti:boolean = false){