xiongzhu il y a 2 ans
Parent
commit
7febab914f
1 fichiers modifiés avec 6 ajouts et 0 suppressions
  1. 6 0
      src/chat-pdf/chat-pdf.service.ts

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

@@ -195,6 +195,7 @@ export class ChatPdfService {
     }
 
     cutContext(context: string[]) {
+        if (!context || !context.length) return []
         let max = 4096 - 1024
         for (let i = 0; i < context.length; i++) {
             max -= this.tokenizer.encode(context[i]).length
@@ -208,6 +209,11 @@ export class ChatPdfService {
         const keywords = await this.getKeywords(q)
         const { embedding: keywordEmbedding } = await this.getEmbedding(keywords)
         const context = this.cutContext((await this.searchEmbedding(name, keywordEmbedding)).map((item) => item.text))
+        if (!context.length) {
+            return {
+                answer: '未找到相关内容'
+            }
+        }
         const content = dedent`You are a helpful AI article assistant. '
         The following are the relevant article content fragments found from the article. 
         The relevance is sorted from high to low. '