Add text file post functionality

This commit is contained in:
kwaroran
2024-02-27 01:29:58 +09:00
parent fbb53fcc61
commit ff16ffde0c
4 changed files with 75 additions and 23 deletions

View File

@@ -651,6 +651,10 @@
messageInput += res.data messageInput += res.data
updateInputSizeAll() updateInputSizeAll()
} }
if(res?.type === 'text'){
messageInput += `{{file::${res.name}::${res.data}}}`
updateInputSizeAll()
}
}}> }}>
<ImagePlusIcon /> <ImagePlusIcon />
<span class="ml-2">{language.postFile}</span> <span class="ml-2">{language.postFile}</span>

View File

@@ -163,3 +163,15 @@ html, body{
.text-bordered{ .text-bordered{
-webkit-text-stroke: 1px #000; -webkit-text-stroke: 1px #000;
} }
.x-risu-risu-file{
padding: 1rem;
border: 1px solid var(--risu-theme-selected);
border-radius: 0.5rem;
color: var(--FontColorStandard);
min-width: 0;
max-width: 20rem;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}

View File

@@ -393,6 +393,7 @@ type matcherArg = {
var?:{[key:string]:string} var?:{[key:string]:string}
tokenizeAccurate?:boolean tokenizeAccurate?:boolean
consistantChar?:boolean consistantChar?:boolean
displaying?:boolean
} }
const matcher = (p1:string,matcherArg:matcherArg) => { const matcher = (p1:string,matcherArg:matcherArg) => {
if(p1.length > 100000){ if(p1.length > 100000){
@@ -680,6 +681,12 @@ const matcher = (p1:string,matcherArg:matcherArg) => {
case 'not':{ case 'not':{
return (Number(arra[1]) === 0) ? '1' : '0' return (Number(arra[1]) === 0) ? '1' : '0'
} }
case 'file':{
if(matcherArg.displaying){
return `<br><div class="risu-file">${arra[1]}</div><br>`
}
return Buffer.from(arra[2], 'base64').toString('utf-8')
}
} }
} }
if(p1.startsWith('random')){ if(p1.startsWith('random')){
@@ -852,7 +859,8 @@ export function risuChatParser(da:string, arg:{
rmVar: arg.rmVar ?? false, rmVar: arg.rmVar ?? false,
db: db, db: db,
var: arg.var ?? null, var: arg.var ?? null,
tokenizeAccurate: arg.tokenizeAccurate ?? false tokenizeAccurate: arg.tokenizeAccurate ?? false,
displaying: arg.visualize ?? false,
} }
let pef = performance.now() let pef = performance.now()
while(pointer < da.length){ while(pointer < da.length){

View File

@@ -93,9 +93,10 @@ async function sendPofile(arg:sendFileArg){
} }
async function sendPDFFile(arg:sendFileArg) { async function sendPDFFile(arg:sendFileArg) {
const pdfjsLib = (await import('pdfjs-dist')).default; const pdfjsLib = (await import('pdfjs-dist'));
const pdfjsWorker = await import('pdfjs-dist/build/pdf.worker?worker&url');
pdfjsLib.GlobalWorkerOptions.workerSrc = pdfjsWorker.default;
const pdf = await pdfjsLib.getDocument({data: arg.file}).promise; const pdf = await pdfjsLib.getDocument({data: arg.file}).promise;
const db = get(DataBase)
const texts:string[] = [] const texts:string[] = []
for(let i = 1; i<=pdf.numPages; i++){ for(let i = 1; i<=pdf.numPages; i++){
const page = await pdf.getPage(i); const page = await pdf.getPage(i);
@@ -105,30 +106,40 @@ async function sendPDFFile(arg:sendFileArg) {
texts.push(item.str) texts.push(item.str)
} }
} }
console.log(texts)
const hypa = new HypaProcesser('MiniLM') const hypa = new HypaProcesser('MiniLM')
hypa.addText(texts) hypa.addText(texts)
let currentChar = db.characters[get(selectedCharID)]
let currentChat = currentChar.chats[currentChar.chatPage]
const result = await hypa.similaritySearch(arg.query) const result = await hypa.similaritySearch(arg.query)
let message = arg.query let message = ''
for(let i = 0; i<result.length; i++){ for(let i = 0; i<result.length; i++){
message += "\n" + texts[result[i]] message += "\n" + result[i]
if(i>5){ if(i>5){
break break
} }
} }
currentChat.message.push({ console.log(message)
role: 'user', return Buffer.from(`<File>\n${message}\n</File>\n`).toString('base64')
data: message
})
currentChar.chats[currentChar.chatPage] = currentChat
db.characters[get(selectedCharID)] = currentChar
setDatabase(db)
await sendChat(-1)
} }
type postFileResult = postFileResultImage | postFileResultVoid async function sendTxtFile(arg:sendFileArg) {
const lines = arg.file.split('\n').filter((a) => {
return a !== ''
})
const hypa = new HypaProcesser('MiniLM')
hypa.addText(lines)
const result = await hypa.similaritySearch(arg.query)
let message = ''
for(let i = 0; i<result.length; i++){
message += "\n" + result[i]
if(i>5){
break
}
}
console.log(message)
return Buffer.from(`<File>\n${message}\n</File>\n`).toString('base64')
}
type postFileResult = postFileResultImage | postFileResultVoid | postFileResultText
type postFileResultImage = { type postFileResultImage = {
data: string, data: string,
@@ -139,6 +150,11 @@ type postFileResultVoid = {
type: 'void', type: 'void',
} }
type postFileResultText = {
data: string,
type: 'text',
name: string
}
export async function postChatFile(query:string):Promise<postFileResult>{ export async function postChatFile(query:string):Promise<postFileResult>{
const file = await selectSingleFile([ const file = await selectSingleFile([
//image format //image format
@@ -147,7 +163,8 @@ export async function postChatFile(query:string):Promise<postFileResult>{
'png', 'png',
'webp', 'webp',
'po', 'po',
'pdf' // 'pdf',
'txt'
]) ])
if(!file){ if(!file){
@@ -168,12 +185,13 @@ export async function postChatFile(query:string):Promise<postFileResult>{
} }
} }
case 'pdf':{ case 'pdf':{
await sendPDFFile({
file: BufferToText(file.data),
query: query
})
return { return {
type: 'void' type: 'text',
data: await sendPDFFile({
file: BufferToText(file.data),
query: query
}),
name: file.name
} }
} }
case 'jpg': case 'jpg':
@@ -186,6 +204,16 @@ export async function postChatFile(query:string):Promise<postFileResult>{
type: 'image' type: 'image'
} }
} }
case 'txt':{
return {
type: 'text',
data: await sendTxtFile({
file: BufferToText(file.data),
query: query
}),
name: file.name
}
}
} }
return return