Просмотр исходного кода

没传过的文件上传新code

wangqifan 3 лет назад
Родитель
Сommit
0f06be5088
2 измененных файлов с 60 добавлено и 0 удалено
  1. 7 0
      src/chat-pdf/chat-pdf.controller.ts
  2. 53 0
      src/chat-pdf/chat-pdf.service.ts

+ 7 - 0
src/chat-pdf/chat-pdf.controller.ts

@@ -14,6 +14,13 @@ export class ChatPdfController {
         return await this.chatPdfService.upload(file, code)
     }
 
+    @Public()
+    @Post('upload')
+    @UseInterceptors(FileInterceptor('file'))
+    public async firstUpload(@UploadedFile() file: Express.Multer.File) {
+        return await this.chatPdfService.firstUpload(file)
+    }
+
     // @Public()
     // @Post('apiUpload')
     // @UseInterceptors(FileInterceptor('file'))

+ 53 - 0
src/chat-pdf/chat-pdf.service.ts

@@ -129,6 +129,59 @@ export class ChatPdfService {
         }
     }
 
+    public async firstUpload(file: Express.Multer.File) {
+        const { originalname, buffer, mimetype } = file
+        let md5 = this.calculateMD5(buffer)
+        const res = await ChatEmbedding.findAll({
+            where: {
+                name: md5
+            }
+        })
+        if (res.length) {
+            return {
+                name: md5
+            }
+        }
+        const pdf = await PdfParse(buffer)
+        const contents = []
+        let paragraph = ''
+        pdf.text
+            .trim()
+            .split('\n')
+            .forEach((line) => {
+                line = line.trim()
+                paragraph += line
+                if (this.isFullSentence(line)) {
+                    contents.push(paragraph)
+                    paragraph = ''
+                }
+            })
+        if (paragraph) {
+            contents.push(paragraph)
+        }
+
+        const embeddings = await this.createEmbeddings(contents)
+        Logger.log(
+            `create embeddings finished, total token usage: ${embeddings.reduce((acc, cur) => acc + cur.token, 0)}`
+        )
+        let i = 0
+        for (const item of embeddings) {
+            try {
+                await ChatEmbedding.create({
+                    name: md5,
+                    text: item.text,
+                    num: i++,
+                    embedding: formatEmbedding(item.embedding)
+                })
+            } catch (error) {
+                Logger.error(error.message)
+            }
+        }
+        return {
+            name: md5
+        }
+    }
+
     isFullSentence(str) {
         return /[.!?。!?…;;::”’)】》」』〕〉》〗〞〟»"'\])}]+$/.test(str)
     }