ソースを参照

区分prompt 和 sysconfig

wangqifan 2 年 前
コミット
c1227ac177
2 ファイル変更30 行追加25 行削除
  1. 6 3
      src/chat-pdf/chat-pdf.service.ts
  2. 24 22
      src/chat/chat.service.ts

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

@@ -273,8 +273,12 @@ export class ChatPdfService {
         return context
     }
 
-    async getSystemConfig(q: string, name: string, code: string) {
+    async getSystemConfig(code: string) {
         const apiUser = await this.apiUserService.findByCode(code)
+        return apiUser.desc
+    }
+
+    async getPrompt(q: string, name: string, code: string) {
         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))
@@ -283,8 +287,7 @@ export class ChatPdfService {
                 answer: '未找到相关内容'
             }
         }
-        return dedent`${apiUser.desc}'
-        根据相关性从高到低排序。 '
+        return dedent`根据相关性从高到低排序。 '
         这是用户提出的问题:
         ${q}
         你只能根据用户的问题,以下面的内容和之前的聊天记录为准进行回答:

+ 24 - 22
src/chat/chat.service.ts

@@ -15,7 +15,7 @@ import { fetchSSE } from '../chatapi/fetch-sse'
 import { HttpService } from '@nestjs/axios'
 import * as types from '../chatapi/types'
 import { SysConfigService } from '../sys-config/sys-config.service'
-import { ChatPdfService } from "../chat-pdf/chat-pdf.service";
+import { ChatPdfService } from '../chat-pdf/chat-pdf.service'
 
 @Injectable()
 export class ChatService {
@@ -29,7 +29,7 @@ export class ChatService {
         private readonly httpService: HttpService,
         private readonly sysConfigService: SysConfigService,
         private readonly chatPdfService: ChatPdfService
-    ) { }
+    ) {}
 
     public chat(req, res): Observable<any> {
         res.setHeader('Content-Type', 'application/octet-stream')
@@ -52,7 +52,7 @@ export class ChatService {
                 temperature,
                 top_p
             })
-                .then(() => { })
+                .then(() => {})
                 .catch((error) => {
                     observer.error(error)
                 })
@@ -64,9 +64,10 @@ export class ChatService {
 
     public async chat1(req, res) {
         res.setHeader('Content-type', 'application/octet-stream')
-        const defSysMsg = (await this.sysConfigService.findByName('system_message'))?.value
+        let defSysMsg = (await this.sysConfigService.findByName('system_message'))?.value
         const membership = await this.membershipService.getMembership(req.user.id)
         const { prompt, options = {}, systemMessage, code, temperature, top_p } = req.body as RequestProps
+        let asyncPrompt = prompt
         if (!code) {
             if (!membership) {
                 throw new ForbiddenException('请先成为会员')
@@ -82,13 +83,14 @@ export class ChatService {
             let content = ''
             const promptTime = new Date()
             if (!!code) {
-                content = await this.chatPdfService.getSystemConfig(prompt, code, code)
-            }else {
+                content = await this.chatPdfService.getSystemConfig(code)
+                asyncPrompt = await this.chatPdfService.getPrompt(prompt, code, code)
+            } else {
                 content = systemMessage
             }
             let firstChunk = true
             const result = await chatReplyProcess({
-                message: prompt,
+                message: asyncPrompt,
                 lastContext: options,
                 process: (chat: ChatMessage) => {
                     res.write(firstChunk ? JSON.stringify(chat) : `\n${JSON.stringify(chat)}`)
@@ -140,15 +142,15 @@ export class ChatService {
 
     public async sendMessage(prompt: string, parentMessageId: string, message: string): Promise<ChatHistory> {
         const options = { parentMessageId: parentMessageId }
-        console.log("options:" + options.parentMessageId)
+        console.log('options:' + options.parentMessageId)
         try {
             const result = await chatReplyProcess({
                 message: prompt,
                 lastContext: options,
                 systemMessage: message,
-                process: (chat: ChatMessage) => { },
-            });
-            const chatMessage = result.data as ChatMessage;
+                process: (chat: ChatMessage) => {}
+            })
+            const chatMessage = result.data as ChatMessage
             this.chatHistoryRepository.save(
                 new ChatHistory({
                     messageId: chatMessage.parentMessageId,
@@ -157,9 +159,9 @@ export class ChatService {
                     message: message,
                     role: 'system',
                     token: chatMessage.detail.usage.prompt_tokens,
-                    time: new Date(),
-                }),
-            );
+                    time: new Date()
+                })
+            )
             const chatHistory = this.chatHistoryRepository.save(
                 new ChatHistory({
                     messageId: chatMessage.id,
@@ -168,14 +170,14 @@ export class ChatService {
                     message: chatMessage.text,
                     role: 'assistant',
                     token: chatMessage.detail.usage.completion_tokens,
-                    time: new Date(),
-                }),
-            );
+                    time: new Date()
+                })
+            )
 
-            Logger.log(`机器人回答:${chatMessage.text}`, 'SendMessage');
+            Logger.log(`机器人回答:${chatMessage.text}`, 'SendMessage')
             return chatHistory
         } catch (error) {
-            Logger.error(error, 'SendMessage');
+            Logger.error(error, 'SendMessage')
         }
     }
 
@@ -215,8 +217,8 @@ export class ChatService {
                         this.tiktokenAndSave(
                             req.user.id,
                             req.body.messages.map((message) => `${message.role}:\n${message.content}`).join('\n\n') +
-                            '\n\nassistant:\n' +
-                            text
+                                '\n\nassistant:\n' +
+                                text
                         )
                         return subscriber.complete()
                     }
@@ -271,4 +273,4 @@ export class ChatService {
             date: And(MoreThanOrEqual(start || '2023-04-01'), LessThanOrEqual(end || date))
         })
     }
-}
+}