wangqifan 2 роки тому
батько
коміт
7e7ccc93fe
2 змінених файлів з 6 додано та 3 видалено
  1. 2 1
      src/chat-pdf/chat-pdf.module.ts
  2. 4 2
      src/chat-pdf/chat-pdf.service.ts

+ 2 - 1
src/chat-pdf/chat-pdf.module.ts

@@ -3,9 +3,10 @@ import { ChatPdfService } from './chat-pdf.service'
 import { ChatPdfController } from './chat-pdf.controller'
 import { ChatPdfController } from './chat-pdf.controller'
 import { TypeOrmModule } from '@nestjs/typeorm'
 import { TypeOrmModule } from '@nestjs/typeorm'
 import { ConfigModule } from '@nestjs/config'
 import { ConfigModule } from '@nestjs/config'
+import { SysConfigModule } from '../sys-config/sys-config.module'
 
 
 @Module({
 @Module({
-    imports: [ConfigModule],
+    imports: [ConfigModule,SysConfigModule],
     providers: [ChatPdfService],
     providers: [ChatPdfService],
     controllers: [ChatPdfController]
     controllers: [ChatPdfController]
 })
 })

+ 4 - 2
src/chat-pdf/chat-pdf.service.ts

@@ -10,6 +10,7 @@ import { Sequelize, Model, DataTypes } from 'sequelize'
 import { ConfigService } from '@nestjs/config'
 import { ConfigService } from '@nestjs/config'
 import { ChatEmbedding } from './entities/chat-embedding.entity'
 import { ChatEmbedding } from './entities/chat-embedding.entity'
 import { VECTOR } from './pgvector'
 import { VECTOR } from './pgvector'
+import { SysConfigService } from '../sys-config/sys-config.service'
 
 
 function formatEmbedding(embedding: number[]) {
 function formatEmbedding(embedding: number[]) {
     return `[${embedding.join(', ')}]`
     return `[${embedding.join(', ')}]`
@@ -20,7 +21,7 @@ export class ChatPdfService {
     private readonly tokenizer: Tiktoken
     private readonly tokenizer: Tiktoken
     private readonly openai: OpenAIApi
     private readonly openai: OpenAIApi
     private readonly sequelize: Sequelize
     private readonly sequelize: Sequelize
-    constructor(private readonly configService: ConfigService) {
+    constructor(private readonly sysConfigService: SysConfigService, private readonly configService: ConfigService) {
         this.tokenizer = get_encoding('cl100k_base')
         this.tokenizer = get_encoding('cl100k_base')
         this.openai = new OpenAIApi(
         this.openai = new OpenAIApi(
             new Configuration({
             new Configuration({
@@ -251,6 +252,7 @@ export class ChatPdfService {
     }
     }
 
 
     async customerAsk(q: string, name: string) {
     async customerAsk(q: string, name: string) {
+        const defSysMsg = (await this.sysConfigService.findByName('customer_system_message'))?.value
         const keywords = await this.getKeywords(q)
         const keywords = await this.getKeywords(q)
         const { embedding: keywordEmbedding } = await this.getEmbedding(keywords)
         const { embedding: keywordEmbedding } = await this.getEmbedding(keywords)
         const context = this.cutContext((await this.searchEmbedding(name, keywordEmbedding)).map((item) => item.text))
         const context = this.cutContext((await this.searchEmbedding(name, keywordEmbedding)).map((item) => item.text))
@@ -259,7 +261,7 @@ export class ChatPdfService {
                 answer: '客服无法回答这个问题,换个试试吧'
                 answer: '客服无法回答这个问题,换个试试吧'
             }
             }
         }
         }
-        const content = dedent`You are a helpful AI article assistant. '
+        const content = dedent`${defSysMsg}'
         The following are the relevant article content fragments found from the article. 
         The following are the relevant article content fragments found from the article. 
         The relevance is sorted from high to low. '
         The relevance is sorted from high to low. '
         You can only answer according to the following content:
         You can only answer according to the following content: