Files
risuai/src/ts/process/stringlize.ts
2023-06-26 00:17:23 +09:00

75 lines
1.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import type { OpenAIChat } from ".";
import { tokenize } from "../tokenizer";
export function multiChatReplacer(){
}
export function stringlizeChat(formated:OpenAIChat[], char:string = ''){
let resultString:string[] = []
for(const form of formated){
if(form.role === 'system'){
resultString.push("system: " + form.content)
}
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 = ''){
console.log(text)
let minIndex = -1
let chunks:string[] = ["system note:", "system:","system note", "system"]
if(char){
chunks.push(`${char}:`)
}
for(const form of formated){
if(form.name){
chunks.push(`${form.name}:`)
chunks.push(`${form.name}`)
}
}
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 = text.substring(0, minIndex).trim()
}
return text
}
export function stringlizeAINChat(formated:OpenAIChat[], char:string = ''){
let resultString:string[] = []
for(const form of formated){
if(form.memo.startsWith("newChat")){
resultString.push("[新しいチャットの始まり]")
continue
}
if(form.role === 'system'){
resultString.push(form.content)
}
else if(form.name){
resultString.push(form.name + " " + form.content)
}
else{
resultString.push(form.content)
}
}
return resultString.join('\n\n') + `\n\n${char}:`
}