|
|
@@ -6,10 +6,17 @@ import { chatReplyProcess } from './chatgpt'
|
|
|
import { Repository } from 'typeorm'
|
|
|
import { ChatHistory } from './entities/chat.entity'
|
|
|
import { InjectRepository } from '@nestjs/typeorm'
|
|
|
+import { TokenUsage } from './entities/token-usage.entity'
|
|
|
+import { format } from 'date-fns'
|
|
|
|
|
|
@Injectable()
|
|
|
export class ChatService {
|
|
|
- constructor(@InjectRepository(ChatHistory) private readonly chatHistoryRepository: Repository<ChatHistory>) {}
|
|
|
+ constructor(
|
|
|
+ @InjectRepository(ChatHistory)
|
|
|
+ private readonly chatHistoryRepository: Repository<ChatHistory>,
|
|
|
+ @InjectRepository(TokenUsage)
|
|
|
+ private readonly tokenUsageRepository: Repository<TokenUsage>
|
|
|
+ ) {}
|
|
|
|
|
|
public chat(req, res): Observable<any> {
|
|
|
res.setHeader('Content-Type', 'application/octet-stream')
|
|
|
@@ -46,7 +53,7 @@ export class ChatService {
|
|
|
res.setHeader('Content-type', 'application/octet-stream')
|
|
|
|
|
|
try {
|
|
|
- Logger.log(JSON.stringify(req.body, null, 2), 'ASK')
|
|
|
+ const promptTime = new Date()
|
|
|
const { prompt, options = {}, systemMessage, temperature, top_p } = req.body as RequestProps
|
|
|
let firstChunk = true
|
|
|
const result = await chatReplyProcess({
|
|
|
@@ -61,11 +68,56 @@ export class ChatService {
|
|
|
top_p
|
|
|
})
|
|
|
let chatMessage = result.data as ChatMessage
|
|
|
- Logger.log(JSON.stringify(result, null, 2), 'ANSWER')
|
|
|
+
|
|
|
+ this.chatHistoryRepository
|
|
|
+ .save(
|
|
|
+ new ChatHistory({
|
|
|
+ messageId: chatMessage.parentMessageId,
|
|
|
+ parentMessageId: options.parentMessageId,
|
|
|
+ userId: req.user.id,
|
|
|
+ message: prompt,
|
|
|
+ role: 'user',
|
|
|
+ token: chatMessage.detail.usage.prompt_tokens,
|
|
|
+ time: promptTime
|
|
|
+ })
|
|
|
+ )
|
|
|
+ .catch((e) => {
|
|
|
+ Logger.error(e, 'SAVE CHAT HISTORY')
|
|
|
+ })
|
|
|
+ this.chatHistoryRepository
|
|
|
+ .save(
|
|
|
+ new ChatHistory({
|
|
|
+ messageId: chatMessage.id,
|
|
|
+ parentMessageId: chatMessage.parentMessageId,
|
|
|
+ userId: req.user.id,
|
|
|
+ message: chatMessage.text,
|
|
|
+ role: 'assistant',
|
|
|
+ token: chatMessage.detail.usage.completion_tokens,
|
|
|
+ time: new Date()
|
|
|
+ })
|
|
|
+ )
|
|
|
+ .catch((e) => {
|
|
|
+ Logger.error(e, 'SAVE CHAT HISTORY')
|
|
|
+ })
|
|
|
+ this.saveUsage(req.user.id, chatMessage.detail.usage.total_tokens)
|
|
|
} catch (error) {
|
|
|
res.write(JSON.stringify(error))
|
|
|
} finally {
|
|
|
res.end()
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ public async saveUsage(userId: number, usage: number) {
|
|
|
+ const date = format(new Date(), 'yyyy-MM-dd')
|
|
|
+ const tokenUsage = await this.tokenUsageRepository.findOneBy({
|
|
|
+ userId,
|
|
|
+ date
|
|
|
+ })
|
|
|
+ if (tokenUsage) {
|
|
|
+ tokenUsage.usage += usage
|
|
|
+ await this.tokenUsageRepository.save(tokenUsage)
|
|
|
+ } else {
|
|
|
+ await this.tokenUsageRepository.save(new TokenUsage({ userId, usage, date }))
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|