Browse Source

考试任务调用知识点簇聚类

master
luoweijian 1 month ago
parent
commit
12908ead3a
  1. 2
      src/main/java/com/project/interaction/application/AlgorithmApplicationService.java
  2. 13
      src/main/java/com/project/interaction/application/impl/AlgorithmApplicationServiceImpl.java
  3. 10
      src/main/java/com/project/interaction/domain/service/PostToGenerateQuestionDomainService.java
  4. 22
      src/main/java/com/project/interaction/domain/service/impl/PostToGenerateQuestionDomainServiceImpl.java
  5. 18
      src/main/java/com/project/question/domain/service/impl/GenerateQuestionDomainServiceImpl.java
  6. 9
      src/main/java/com/project/question/domain/service/impl/RandomSamplingStrategy.java

2
src/main/java/com/project/interaction/application/AlgorithmApplicationService.java

@ -18,7 +18,7 @@ public interface AlgorithmApplicationService {
void postToClustering(Long taskId, List<KnowledgePointDTO> kpList); void postToClustering(Long taskId, List<KnowledgePointDTO> kpList);
void generateQuestion(List<TaskKnowledgePointDTO> dtoList , QuestionTypeEnum questionType , int num); void postToGenerateQuestion(List<TaskKnowledgePointDTO> dtoList , QuestionTypeEnum questionType , int num) throws Exception;
void saveCluster(Long taskId, List<ClusterCallbackDTO.ClusterItem> clusters) throws Exception; void saveCluster(Long taskId, List<ClusterCallbackDTO.ClusterItem> clusters) throws Exception;
} }

13
src/main/java/com/project/interaction/application/impl/AlgorithmApplicationServiceImpl.java

@ -4,6 +4,7 @@ import com.project.information.domain.dto.KnowledgePointDTO;
import com.project.interaction.application.AlgorithmApplicationService; import com.project.interaction.application.AlgorithmApplicationService;
import com.project.interaction.domain.dto.ClusterCallbackDTO; import com.project.interaction.domain.dto.ClusterCallbackDTO;
import com.project.interaction.domain.service.PostToClusteringDomainService; import com.project.interaction.domain.service.PostToClusteringDomainService;
import com.project.interaction.domain.service.PostToGenerateQuestionDomainService;
import com.project.interaction.domain.service.SaveClusterDomainService; import com.project.interaction.domain.service.SaveClusterDomainService;
import com.project.question.domain.dto.TaskKnowledgePointDTO; import com.project.question.domain.dto.TaskKnowledgePointDTO;
import com.project.question.domain.enums.QuestionSourceTypeEnum; import com.project.question.domain.enums.QuestionSourceTypeEnum;
@ -20,6 +21,10 @@ public class AlgorithmApplicationServiceImpl implements AlgorithmApplicationServ
@Autowired @Autowired
private SaveClusterDomainService saveClusterDomainService; private SaveClusterDomainService saveClusterDomainService;
@Autowired
private PostToGenerateQuestionDomainService postToGenerateQuestionDomainService;
@Override @Override
public void postToClustering(Long taskId, List<KnowledgePointDTO> kpList) { public void postToClustering(Long taskId, List<KnowledgePointDTO> kpList) {
postToClusteringDomainService.postToClustering(taskId , kpList); postToClusteringDomainService.postToClustering(taskId , kpList);
@ -27,12 +32,8 @@ public class AlgorithmApplicationServiceImpl implements AlgorithmApplicationServ
@Override @Override
public void generateQuestion(List<TaskKnowledgePointDTO> dtoList, QuestionTypeEnum questionType, int num) { public void postToGenerateQuestion(List<TaskKnowledgePointDTO> dtoList, QuestionTypeEnum questionType, int num) throws Exception {
System.out.println(String.format("<=======生成了%d道%s, 结合了知识点【%s】属于%s=======>" , postToGenerateQuestionDomainService.postToGenerateQuestion(dtoList , questionType , num);
num ,
questionType.getDescription() ,
String.join("," , dtoList.stream().map(dto -> dto.getId().toString()).toList()) ,
dtoList.size() > 1 ? QuestionSourceTypeEnum.Multi_Concept.getDesc() : QuestionSourceTypeEnum.Single_Concept.getDesc()));
} }

10
src/main/java/com/project/interaction/domain/service/PostToGenerateQuestionDomainService.java

@ -0,0 +1,10 @@
package com.project.interaction.domain.service;
import com.project.question.domain.dto.TaskKnowledgePointDTO;
import com.project.task.domain.enums.QuestionTypeEnum;
import java.util.List;
public interface PostToGenerateQuestionDomainService {
void postToGenerateQuestion(List<TaskKnowledgePointDTO> dtoList, QuestionTypeEnum questionType, int num) throws Exception;
}

22
src/main/java/com/project/interaction/domain/service/impl/PostToGenerateQuestionDomainServiceImpl.java

@ -0,0 +1,22 @@
package com.project.interaction.domain.service.impl;
import com.project.interaction.domain.service.PostToGenerateQuestionDomainService;
import com.project.question.domain.dto.TaskKnowledgePointDTO;
import com.project.question.domain.enums.QuestionSourceTypeEnum;
import com.project.task.domain.enums.QuestionTypeEnum;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class PostToGenerateQuestionDomainServiceImpl implements PostToGenerateQuestionDomainService {
@Override
public void postToGenerateQuestion(List<TaskKnowledgePointDTO> dtoList, QuestionTypeEnum questionType, int num) throws Exception {
System.out.println(String.format("<=======生成了%d道%s, 结合了知识点【%s】属于%s=======>" ,
num ,
questionType.getDescription() ,
String.join("," , dtoList.stream().map(dto -> dto.getId().toString()).toList()) ,
dtoList.size() > 1 ? QuestionSourceTypeEnum.Multi_Concept.getDesc() : QuestionSourceTypeEnum.Single_Concept.getDesc()));
}
}

18
src/main/java/com/project/question/domain/service/impl/GenerateQuestionDomainServiceImpl.java

@ -1,11 +1,14 @@
package com.project.question.domain.service.impl; package com.project.question.domain.service.impl;
import com.project.interaction.application.AlgorithmApplicationService;
import com.project.interaction.domain.service.PostToGenerateQuestionDomainService;
import com.project.question.domain.dto.TaskKnowledgePointDTO; import com.project.question.domain.dto.TaskKnowledgePointDTO;
import com.project.question.domain.enums.QuestionSourceTypeEnum; import com.project.question.domain.enums.QuestionSourceTypeEnum;
import com.project.question.domain.service.GenerateQuestionDomainService; import com.project.question.domain.service.GenerateQuestionDomainService;
import com.project.question.domain.service.SamplingStrategy; import com.project.question.domain.service.SamplingStrategy;
import com.project.question.domain.service.TaskKnowledgePointBaseService; import com.project.question.domain.service.TaskKnowledgePointBaseService;
import com.project.task.domain.enums.QuestionTypeEnum; import com.project.task.domain.enums.QuestionTypeEnum;
import io.vavr.control.Try;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -29,6 +32,9 @@ public class GenerateQuestionDomainServiceImpl implements GenerateQuestionDomain
@Autowired @Autowired
private SamplingStrategy samplingStrategy; private SamplingStrategy samplingStrategy;
@Autowired
private PostToGenerateQuestionDomainService postToGenerateQuestionDomainService;
@Override @Override
public void produce(Long sourceId, QuestionTypeEnum questionType , QuestionSourceTypeEnum sourceType, int needCount) { public void produce(Long sourceId, QuestionTypeEnum questionType , QuestionSourceTypeEnum sourceType, int needCount) {
if (needCount <= 0) { if (needCount <= 0) {
@ -37,25 +43,25 @@ public class GenerateQuestionDomainServiceImpl implements GenerateQuestionDomain
CompletableFuture.runAsync(() -> { CompletableFuture.runAsync(() -> {
log.info(">>> [题库预生成] 准备生产任务: SourceID={}, Type={}, Count={}", sourceId, sourceType, needCount); log.info(">>> [题库预生成] 准备生产任务: SourceID={}, Type={}, Count={}", sourceId, sourceType, needCount);
processBatch(sourceId,questionType , sourceType , needCount); Try.run(() -> processBatch(sourceId,questionType , sourceType , needCount));
}, questionGenExecutor); }, questionGenExecutor);
} }
private void processBatch(Long sourceId, QuestionTypeEnum questionType , QuestionSourceTypeEnum sourceType, int count) { private void processBatch(Long sourceId, QuestionTypeEnum questionType , QuestionSourceTypeEnum sourceType, int count) throws Exception{
// 如果是簇复合题,执行随机采样 2-4 个知识点 // 如果是簇复合题,执行随机采样 2-4 个知识点
List<TaskKnowledgePointDTO> kpList; List<TaskKnowledgePointDTO> kpList;
if (QuestionSourceTypeEnum.Multi_Concept.equals(sourceType)) { if (QuestionSourceTypeEnum.Multi_Concept.equals(sourceType)) {
for (int i = 0; i < count; i++) {
kpList = samplingStrategy.sample(sourceId); // 获取随机组合的 KPs kpList = samplingStrategy.sample(sourceId); // 获取随机组合的 KPs
postToGenerateQuestionDomainService.postToGenerateQuestion(kpList , questionType , 1);
}
} else { } else {
// 单 KP 逻辑:直接查出对应 KP 的内容 // 单 KP 逻辑:直接查出对应 KP 的内容
kpList = Collections.singletonList(taskKnowledgePointBaseService.getById(sourceId) kpList = Collections.singletonList(taskKnowledgePointBaseService.getById(sourceId)
.toDTO(TaskKnowledgePointDTO::new)); .toDTO(TaskKnowledgePointDTO::new));
postToGenerateQuestionDomainService.postToGenerateQuestion(kpList , questionType , count);
} }
// 2. 调用大模型并保存题目及其多对多关系 (QuestionPool & QuestionKpRel)
//
//aiLlmService.generateAndBatchSave(context, sourceType, count);
} }
} }

9
src/main/java/com/project/question/domain/service/impl/RandomSamplingStrategy.java

@ -10,6 +10,7 @@ import com.project.question.domain.service.TaskKnowledgePointBaseService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
@ -20,13 +21,17 @@ public class RandomSamplingStrategy implements SamplingStrategy {
@Override @Override
public List<TaskKnowledgePointDTO> sample(Long clusterId) { public List<TaskKnowledgePointDTO> sample(Long clusterId) {
List<TaskKnowledgePointDTO> dtoList = taskKnowledgePointBaseService.list(new LambdaQueryWrapper<TaskKnowledgePointEntity>() List<TaskKnowledgePointDTO> dtoList = new ArrayList<>(taskKnowledgePointBaseService.list(new LambdaQueryWrapper<TaskKnowledgePointEntity>()
.eq(TaskKnowledgePointEntity::getClusterId, clusterId)) .eq(TaskKnowledgePointEntity::getClusterId, clusterId))
.stream().map(entity -> entity.toDTO(TaskKnowledgePointDTO::new)).toList(); .stream().map(entity -> entity.toDTO(TaskKnowledgePointDTO::new)).toList());
if (CollUtil.isEmpty(dtoList)) { if (CollUtil.isEmpty(dtoList)) {
return Collections.emptyList(); return Collections.emptyList();
} }
// 集合为两个不需要采样
if (dtoList.size() == 2) {
return dtoList;
}
// 确定采样数量:2 到 4 之间的随机数 // 确定采样数量:2 到 4 之间的随机数
int totalSize = dtoList.size(); int totalSize = dtoList.size();
int min = Math.min(totalSize, 2); int min = Math.min(totalSize, 2);

Loading…
Cancel
Save