wangqifan 2 lat temu
rodzic
commit
0e4b6f5398
2 zmienionych plików z 27 dodań i 32 usunięć
  1. 5 8
      src/chat-pdf/chat-pdf.service.ts
  2. 22 24
      src/chat/chat.service.ts

+ 5 - 8
src/chat-pdf/chat-pdf.service.ts

@@ -273,12 +273,8 @@ export class ChatPdfService {
         return context
     }
 
-    async getSystemConfig(code: string) {
+    async getSystemConfig(q: string, name: string, 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))
@@ -287,14 +283,15 @@ export class ChatPdfService {
                 answer: '未找到相关内容'
             }
         }
-        return dedent`根据相关性从高到低排序。 '
+        return dedent`${apiUser.desc}'
+        根据相关性从高到低排序。 '
         这是用户提出的问题:
         ${q}
-        你只能根据用户的问题,以下面的内容和之前的聊天记录为准进行回答:
+        你只能根据用户的问题,以下面的内容和之前的聊天记录为准结合你的身份进行回答:
         \`\`\`
         ${context.join('\n')}
         \`\`\`
-        你要确保你的回答全部基于上面的问题和内容,如果无法找到答案,你需要根据给你设定的角色表述你不能回答这个问题。
+        你要确保你的回答全部基于上面的问题和内容还有你的身份,如果无法找到答案,你需要根据给你设定的角色表述你不能回答这个问题。
         你只能用中文回答.`
     }
     async ask(q: string, name: string) {

+ 22 - 24
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,10 +64,9 @@ export class ChatService {
 
     public async chat1(req, res) {
         res.setHeader('Content-type', 'application/octet-stream')
-        let defSysMsg = (await this.sysConfigService.findByName('system_message'))?.value
+        const 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('请先成为会员')
@@ -83,14 +82,13 @@ export class ChatService {
             let content = ''
             const promptTime = new Date()
             if (!!code) {
-                content = await this.chatPdfService.getSystemConfig(code)
-                asyncPrompt = await this.chatPdfService.getPrompt(prompt, code, code)
-            } else {
+                content = await this.chatPdfService.getSystemConfig(prompt, code, code)
+            }else {
                 content = systemMessage
             }
             let firstChunk = true
             const result = await chatReplyProcess({
-                message: asyncPrompt,
+                message: prompt,
                 lastContext: options,
                 process: (chat: ChatMessage) => {
                     res.write(firstChunk ? JSON.stringify(chat) : `\n${JSON.stringify(chat)}`)
@@ -142,15 +140,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,
@@ -159,9 +157,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,
@@ -170,14 +168,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');
         }
     }
 
@@ -217,8 +215,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()
                     }
@@ -273,4 +271,4 @@ export class ChatService {
             date: And(MoreThanOrEqual(start || '2023-04-01'), LessThanOrEqual(end || date))
         })
     }
-}
+}