[feat] added unstringlizer

This commit is contained in:
kwaroran
2023-05-24 04:27:15 +09:00
parent 26e0fb6413
commit 56264286b9
2 changed files with 40 additions and 10 deletions

View File

@@ -10,9 +10,45 @@ export function stringlizeChat(formated:OpenAIChat[], char:string = ''){
if(form.role === 'system'){
resultString.push("system note: " + form.content)
}
else{
else if(form.name){
resultString.push(form.name + ": " + form.content)
}
else{
resultString.push(form.content)
}
}
return resultString.join('\n\n') + `\n\n${char}:`
}
export function unstringlizeChat(text:string, formated:OpenAIChat[], char:string = ''){
let minIndex = -1
let chunks:string[] = ["system note:"]
if(char){
chunks.push(`${char}:`)
}
for(const form of formated){
if(form.name){
const chunk = `${form.name}:`
if(!chunks.includes(chunk)){
chunks.push(chunk)
}
}
}
for(const chunk of chunks){
const ind = text.indexOf(chunk)
if(ind === -1){
continue
}
if(minIndex === -1 || minIndex > ind){
minIndex = ind
}
}
if(minIndex !== -1){
text.substring(0, minIndex)
}
return text
}