Kaynağa Gözat

自动回复

wuyi 2 yıl önce
ebeveyn
işleme
f0c04d5648

+ 4 - 2
src/app.module.ts

@@ -27,6 +27,7 @@ import { MomentsModule } from './moments/moments.module'
 import { CommentModule } from './comment/comment.module'
 import { ChatPdfModule } from './chat-pdf/chat-pdf.module'
 import { LikesModule } from './likes/likes.module'
+import { ConditionModule } from './condition/condition.module'
 @Module({
     imports: [
         DevtoolsModule.register({
@@ -106,7 +107,8 @@ import { LikesModule } from './likes/likes.module'
         CommentModule,
         ChatPdfModule,
         LikesModule,
-        ApiUserModule
+        ApiUserModule,
+        ConditionModule
     ],
     controllers: [],
     providers: [
@@ -116,4 +118,4 @@ import { LikesModule } from './likes/likes.module'
         }
     ]
 })
-export class AppModule {}
+export class AppModule { }

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

@@ -128,10 +128,13 @@ export class ChatService {
         }
     }
 
-    public async sendMessage(prompt: string, message: string,): Promise<string> {
+    public async sendMessage(prompt: string, parentMessageId: string, message: string): Promise<ChatHistory> {
+        const options = { parentMessageId: parentMessageId }
+        console.log("options:" + options.parentMessageId)
         try {
             const result = await chatReplyProcess({
                 message: prompt,
+                lastContext: options,
                 systemMessage: message,
                 process: (chat: ChatMessage) => { },
             });
@@ -147,7 +150,7 @@ export class ChatService {
                     time: new Date(),
                 }),
             );
-            this.chatHistoryRepository.save(
+            const chatHistory = this.chatHistoryRepository.save(
                 new ChatHistory({
                     messageId: chatMessage.id,
                     parentMessageId: chatMessage.parentMessageId,
@@ -160,7 +163,7 @@ export class ChatService {
             );
 
             Logger.log(`机器人回答:${chatMessage.text}`, 'SendMessage');
-            return chatMessage.text
+            return chatHistory
         } catch (error) {
             Logger.error(error, 'SendMessage');
         }

+ 5 - 1
src/comment/comment.service.ts

@@ -139,7 +139,11 @@ export class CommentService {
         const moments = await this.momentsRepository.findOneBy({ id: +commentDto.momentsId })
         moments.commentNum += 1
         await this.momentsRepository.save(moments)
-        return await this.commentRepository.save(commentDto)
+        const newComment = await this.commentRepository.save(commentDto)
+        
+
+        
+        return newComment
     }
 
     public async findChildList(req: PageRequest<Comment>): Promise<Pagination<Comment>> {

+ 11 - 0
src/condition/condition.controller.ts

@@ -0,0 +1,11 @@
+import {
+    Controller
+} from '@nestjs/common'
+import { ApiBearerAuth, ApiTags } from '@nestjs/swagger'
+
+@ApiTags('condition')
+@Controller('/condition')
+@ApiBearerAuth()
+export class ConditionController {
+
+}

+ 18 - 0
src/condition/condition.module.ts

@@ -0,0 +1,18 @@
+import { Module } from '@nestjs/common'
+import { TypeOrmModule } from '@nestjs/typeorm'
+import { Moments } from 'src/moments/entities/moments.entity'
+import { Comment } from 'src/comment/entities/comment.entity'
+import { MomentsModule } from 'src/moments/moments.module'
+import { ConditionController } from './condition.controller'
+import { ConditionService } from './condition.service'
+import { Condition } from './entities/condition.entity'
+import { CommentModule } from 'src/comment/comment.module'
+import { ChatModule } from 'src/chat/chat.module'
+
+@Module({
+    imports: [TypeOrmModule.forFeature([Condition, Moments, Comment]), MomentsModule, CommentModule, ChatModule],
+    controllers: [ConditionController],
+    providers: [ConditionService],
+    exports: [ConditionService]
+})
+export class ConditionModule { }

+ 61 - 0
src/condition/condition.service.ts

@@ -0,0 +1,61 @@
+import {
+    Injectable
+} from '@nestjs/common'
+import { FindManyOptions, FindOptionsOrder, In, Repository, UpdateResult } from 'typeorm'
+import { InjectRepository } from '@nestjs/typeorm'
+import { paginate, Pagination } from 'nestjs-typeorm-paginate'
+import { PageRequest } from '../common/dto/page-request'
+import { Condition } from './entities/condition.entity'
+import { Moments } from 'src/moments/entities/moments.entity'
+import { Comment } from 'src/comment/entities/comment.entity'
+import { ChatService } from 'src/chat/chat.service'
+import { CommentDto } from 'src/comment/dto/comment.dto'
+import { CommentService } from 'src/comment/comment.service'
+import { ConditionDto } from './dto/condition.dto'
+
+@Injectable()
+export class ConditionService {
+
+    constructor(
+        @InjectRepository(Condition)
+        private readonly conditionRepository: Repository<Condition>,
+        @InjectRepository(Moments)
+        private readonly momentsRepository: Repository<Moments>,
+        @InjectRepository(Comment)
+        private readonly commentRepository: Repository<Comment>,
+        private readonly chatService: ChatService,
+        private readonly commentService: CommentService
+    ) { }
+
+    public async create() {
+
+    }
+
+    public async reply(comment: Comment, moments: Moments) {
+        const chat = await this.chatService.sendMessage(null, moments.messageId, comment.content)
+
+        if (chat.message.trim() !== '') {
+
+            const favorabilityChat = await this.chatService.sendMessage(null, chat.messageId, "这是我的评论<" + comment.content + ">,请你就这条评论打分,分数在0到10这个区间,返回数字,不必做解释,不要出现文字")
+            const fraction = favorabilityChat.message
+
+            const dto = new ConditionDto
+            dto.userId = comment.userId
+            dto.favorability = fraction
+            dto.chatRoleId = moments.userId
+
+            const commentDto = new CommentDto
+            commentDto.content = chat.message
+            commentDto.userId = comment.userId
+            commentDto.userName = comment.userName
+            commentDto.momentsId = comment.momentsId
+            commentDto.rootCommentId = comment.rootCommentId
+            commentDto.toCommentId = comment.id
+
+            const replyComment = await this.commentService.create(commentDto)
+            await this.conditionRepository.save(dto)
+        }
+
+    }
+
+}

+ 11 - 0
src/condition/dto/condition.dto.ts

@@ -0,0 +1,11 @@
+import { IsNumber, IsString } from "class-validator";
+
+export class ConditionDto {
+ 
+    userId: number
+
+    chatRoleId: number
+
+    favorability: string
+
+}

+ 22 - 0
src/condition/entities/condition.entity.ts

@@ -0,0 +1,22 @@
+import { Exclude } from "class-transformer";
+import { Column, CreateDateColumn, Entity, PrimaryGeneratedColumn } from "typeorm";
+
+@Entity()
+export class Condition {
+
+    @PrimaryGeneratedColumn()
+    id: number
+
+    @Column()
+    userId: number
+
+    @Column()
+    chatRoleId: number
+
+    @Column()
+    favorability: string
+
+    @Column()
+    score: string
+
+}

+ 2 - 0
src/moments/dto/moments.dto.ts

@@ -7,6 +7,8 @@ export class MomentsDto {
     @IsString()
     content: string
 
+    messageId: string
+
     @IsBoolean()
     isPush: boolean
 }

+ 3 - 0
src/moments/entities/moments.entity.ts

@@ -10,6 +10,9 @@ export class Moments {
     @Column()
     userId: number
 
+    @Column()
+    messageId: string
+
     @Column()
     title: string
 

+ 6 - 3
src/moments/moments.service.ts

@@ -14,6 +14,7 @@ import { setInterval } from 'timers';
 import { ChatService } from 'src/chat/chat.service'
 import { Mask } from 'src/mask/entities/mask.entity'
 import { MomentsDto } from './dto/moments.dto'
+import { text } from 'stream/consumers'
 
 @Injectable()
 export class MomentsService {
@@ -143,11 +144,13 @@ export class MomentsService {
         const chatRoleList = await this.chatRoleRepository.createQueryBuilder().getMany()
 
         chatRoleList.forEach(async role => {
-            const text = await this.chatService.sendMessage(null, role.condition)
-            if (text.trim() !== '') {
+            const chatHistory = await this.chatService.sendMessage(null, null, role.condition)
+
+            if (chatHistory.message.trim() !== '') {
                 const momentsDto = new MomentsDto
-                momentsDto.content = text
+                momentsDto.content = chatHistory.message
                 momentsDto.userId = role.id
+                momentsDto.messageId = chatHistory.messageId
                 momentsDto.isPush = false
                 await this.momentsRepository.save(momentsDto)
             }