fix: reg failed

This commit is contained in:
archer 2023-08-31 20:06:49 +08:00
parent 0ea464f30f
commit 2ae8d43216
No known key found for this signature in database
GPG Key ID: 569A5660D2379E28
3 changed files with 74 additions and 69 deletions

View File

@ -6,11 +6,11 @@ export enum UserAuthTypeEnum {
export const PRICE_SCALE = 100000; export const PRICE_SCALE = 100000;
export const fileImgs = [ export const fileImgs = [
{ reg: /pdf/gi, src: '/imgs/files/pdf.svg' }, { suffix: 'pdf', src: '/imgs/files/pdf.svg' },
{ reg: /csv/gi, src: '/imgs/files/csv.svg' }, { suffix: 'csv', src: '/imgs/files/csv.svg' },
{ reg: /(doc|docs)/gi, src: '/imgs/files/doc.svg' }, { suffix: '(doc|docs)', src: '/imgs/files/doc.svg' },
{ reg: /txt/gi, src: '/imgs/files/txt.svg' }, { suffix: 'txt', src: '/imgs/files/txt.svg' },
{ reg: /md/gi, src: '/imgs/files/markdown.svg' } { suffix: 'md', src: '/imgs/files/markdown.svg' }
]; ];
export const htmlTemplate = `<!DOCTYPE html> export const htmlTemplate = `<!DOCTYPE html>

View File

@ -76,76 +76,76 @@ const FileSelect = ({
setSelecting(true); setSelecting(true);
try { try {
// Parse file by file // Parse file by file
let promise = Promise.resolve<FileItemType[]>([]); const chunkFiles: FileItemType[] = [];
files.forEach((file) => {
promise = promise.then(async (result) => {
const extension = file?.name?.split('.')?.pop()?.toLowerCase();
/* text file */ for await (let file of files) {
const icon = fileImgs.find((item) => new RegExp(item.reg).test(file.name))?.src; const extension = file?.name?.split('.')?.pop()?.toLowerCase();
let text = await (async () => {
switch (extension) {
case 'txt':
case 'md':
return readTxtContent(file);
case 'pdf':
return readPdfContent(file);
case 'doc':
case 'docx':
return readDocContent(file);
}
return '';
})();
if (!icon) return result; /* text file */
const icon = fileImgs.find((item) => new RegExp(item.suffix, 'gi').test(file.name))?.src;
if (text) { if (!icon) {
text = simpleText(text); continue;
const splitRes = splitText2Chunks({ }
text,
maxLen: chunkLen let text = await (async () => {
}); switch (extension) {
const fileItem: FileItemType = { case 'txt':
id: nanoid(), case 'md':
filename: file.name, return readTxtContent(file);
icon, case 'pdf':
text, return readPdfContent(file);
tokens: splitRes.tokens, case 'doc':
chunks: splitRes.chunks.map((chunk) => ({ case 'docx':
q: chunk, return readDocContent(file);
a: '',
source: file.name
}))
};
return [fileItem].concat(result);
} }
return '';
})();
/* csv file */ if (text) {
if (extension === 'csv') { text = simpleText(text);
const { header, data } = await readCsvContent(file); const splitRes = splitText2Chunks({
if (header[0] !== 'question' || header[1] !== 'answer') { text,
throw new Error('csv 文件格式有误,请确保 question 和 answer 两列'); maxLen: chunkLen
} });
const fileItem: FileItemType = { const fileItem: FileItemType = {
id: nanoid(), id: nanoid(),
filename: file.name, filename: file.name,
icon, icon,
tokens: 0, text,
text: '', tokens: splitRes.tokens,
chunks: data.map((item) => ({ chunks: splitRes.chunks.map((chunk) => ({
q: item[0], q: chunk,
a: item[1], a: '',
source: item[2] || file.name source: file.name
})) }))
}; };
return [fileItem].concat(result); chunkFiles.unshift(fileItem);
continue;
}
/* csv file */
if (extension === 'csv') {
const { header, data } = await readCsvContent(file);
if (header[0] !== 'question' || header[1] !== 'answer') {
throw new Error('csv 文件格式有误,请确保 question 和 answer 两列');
} }
return result; const fileItem: FileItemType = {
}); id: nanoid(),
}); filename: file.name,
icon,
const chunkFiles = await promise; tokens: 0,
text: '',
chunks: data.map((item) => ({
q: item[0],
a: item[1],
source: item[2] || file.name
}))
};
chunkFiles.unshift(fileItem);
}
}
onPushFiles(chunkFiles); onPushFiles(chunkFiles);
} catch (error: any) { } catch (error: any) {
console.log(error); console.log(error);

View File

@ -9,6 +9,7 @@ import { axiosConfig, getAIChatApi } from '../ai/openai';
import { ChatCompletionRequestMessage } from 'openai'; import { ChatCompletionRequestMessage } from 'openai';
import { modelToolMap } from '@/utils/plugin'; import { modelToolMap } from '@/utils/plugin';
import { gptMessage2ChatType } from '@/utils/adapt'; import { gptMessage2ChatType } from '@/utils/adapt';
import { addLog } from '../utils/tools';
const reduceQueue = () => { const reduceQueue = () => {
global.qaQueueLen = global.qaQueueLen > 0 ? global.qaQueueLen - 1 : 0; global.qaQueueLen = global.qaQueueLen > 0 ? global.qaQueueLen - 1 : 0;
@ -105,12 +106,16 @@ A2:
const result = formatSplitText(answer || ''); // 格式化后的QA对 const result = formatSplitText(answer || ''); // 格式化后的QA对
console.log(`split result length: `, result.length); console.log(`split result length: `, result.length);
// 计费 // 计费
result.length > 0 && if (result.length > 0) {
pushQABill({ pushQABill({
userId: data.userId, userId: data.userId,
totalTokens, totalTokens,
appName: 'QA 拆分' appName: 'QA 拆分'
}); });
} else {
addLog.info(`QA result 0:`, { answer });
}
return { return {
rawContent: answer, rawContent: answer,
result result