|
|
@@ -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)
|
|
|
})
|
|
|
@@ -63,11 +63,12 @@ export class ChatService {
|
|
|
}
|
|
|
|
|
|
public async chat1(req, res) {
|
|
|
+ const user = await this.chatPdfService.getUser(req.user.id)
|
|
|
res.setHeader('Content-type', 'application/octet-stream')
|
|
|
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
|
|
|
- if (!code) {
|
|
|
+ if (!code && !user.apiUserId) {
|
|
|
if (!membership) {
|
|
|
throw new ForbiddenException('请先成为会员')
|
|
|
}
|
|
|
@@ -81,11 +82,7 @@ export class ChatService {
|
|
|
try {
|
|
|
let content = ''
|
|
|
const promptTime = new Date()
|
|
|
- if (!!code) {
|
|
|
- content = await this.chatPdfService.getSystemConfig(prompt, code, code)
|
|
|
- }else {
|
|
|
- content = systemMessage
|
|
|
- }
|
|
|
+ content = await this.chatPdfService.getSystemConfig(req.userId, prompt, code, code, systemMessage)
|
|
|
let firstChunk = true
|
|
|
const result = await chatReplyProcess({
|
|
|
message: prompt,
|
|
|
@@ -140,15 +137,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 +154,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 +165,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 +212,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 +268,4 @@ export class ChatService {
|
|
|
date: And(MoreThanOrEqual(start || '2023-04-01'), LessThanOrEqual(end || date))
|
|
|
})
|
|
|
}
|
|
|
-}
|
|
|
+}
|