[feat] added features in spec v2

This commit is contained in:
kwaroran
2023-05-12 20:14:03 +09:00
parent 52bd20d3fb
commit c4d44d2031
15 changed files with 353 additions and 162 deletions

View File

@@ -0,0 +1,62 @@
import type { OpenAIChat } from ".";
import type { character } from "../database";
import { replacePlaceholders } from "../util";
export function exampleMessage(char:character):OpenAIChat[]{
if(char.exampleMessage === ''){
return []
}
const messages = char.exampleMessage.split('\n')
let result:OpenAIChat[] = []
let currentMessage:OpenAIChat
function add(){
if(currentMessage){
result.push(currentMessage)
}
}
for(const mes of messages){
const trimed = mes.trim()
const lowered = trimed.toLocaleLowerCase()
if(lowered === '<start>'){
add()
result.push({
role: "system",
content: '[Start a new chat]'
})
}
else if(lowered.startsWith('{{char}}:') || lowered.startsWith('<bot>:') || lowered.startsWith(`${char.name}:`)){
add()
currentMessage = {
role: "assistant",
content: trimed.split(':', 2)[1]
}
}
else if(lowered.startsWith('{{user}}:') || lowered.startsWith('<user>:')){
add()
currentMessage = {
role: "user",
content: trimed.split(':', 2)[1]
}
}
else{
if(currentMessage){
currentMessage.content += "\n" + trimed
}
}
}
add()
result = result.map((r) => {
return {
role: r.role,
content: replacePlaceholders(r.content, char.name)
}
})
return result
}