大模型使用次数改为图片生成三次或提问上限30个字

master
whb 8 months ago
parent d6dcb4680d
commit 5034170539

@ -43,13 +43,27 @@ public class AiAspect {
*/
// 定义一个Pointcut匹配带有StuCreateImgDTO参数的方法
@Pointcut("execution(* com.sztzjy.marketing.service.impl.QianFanBigModuleServiceImpl.*(..))")
@Pointcut("execution(* com.sztzjy.marketing.service.impl.QianFanBigModuleServiceImpl.createArticleByMessage(..))")
public void useInspect() {
}
@Before("useInspect()")
public void timesCheck(JoinPoint joinPoint) throws IllegalAccessException {
/**
*
*
* @param
* @return
*/
// 定义一个Pointcut匹配带有StuCreateImgDTO参数的方法
@Pointcut("execution(* com.sztzjy.marketing.service.impl.QianFanBigModuleServiceImpl.createImgByAi(..))")
public void limitImg() {
}
@Before("limitImg()")
public void imgTimesCheck(JoinPoint joinPoint) throws IllegalAccessException {
log.info("开始检查使用次数...");
@ -59,17 +73,17 @@ public class AiAspect {
StuAiLimitExample limitExample = new StuAiLimitExample();
limitExample.createCriteria().andUserIdEqualTo(jwtUser.getUserId());
List<StuAiLimit> stuAiLimits = stuAiLimitMapper.selectByExample(limitExample);
if (!stuAiLimits.isEmpty())
{
if (stuAiLimits.get(0).getLimitState()== (byte) 1 || 3 < stuAiLimits.get(0).getNumberOfTimes()){
throw new RuntimeException("超出使用限制,请明日再来!");
throw new RuntimeException("您的使用已达到今日限额,请明日再试。");
}
StuAiLimit stuAiLimit = stuAiLimits.get(0);
stuAiLimit.setNumberOfTimes(stuAiLimit.getNumberOfTimes()+1);
if (3 == stuAiLimit.getNumberOfTimes())
if (3 <= stuAiLimit.getNumberOfTimes())
{
stuAiLimit.setLimitState((byte)1);
}
@ -83,16 +97,63 @@ public class AiAspect {
stuAiLimit.setUserId(jwtUser.getUserId());
stuAiLimit.setLimitState((byte)0);
stuAiLimit.setCreateTime(new Date());
stuAiLimit.setNumberOfTimes(1);
stuAiLimit.setNumberOfTimes(0);
stuAiLimit.setWordNumber(0);
stuAiLimitMapper.insertSelective(stuAiLimit);
}
} else {
throw new UnAuthorizedException("未经授权登录");
}
}
@Before("useInspect()")
public void timesCheck(JoinPoint joinPoint) throws IllegalAccessException {
log.info("开始检查使用次数...");
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication != null && authentication.isAuthenticated()) {
JwtUser jwtUser = (JwtUser) authentication.getPrincipal();
StuAiLimitExample limitExample = new StuAiLimitExample();
limitExample.createCriteria().andUserIdEqualTo(jwtUser.getUserId());
List<StuAiLimit> stuAiLimits = stuAiLimitMapper.selectByExample(limitExample);
if (!stuAiLimits.isEmpty())
{
if (stuAiLimits.get(0).getLimitState()== (byte) 1 || 30 < stuAiLimits.get(0).getWordNumber()){
throw new RuntimeException("您的使用已达到今日限额,请明日再试。");
}
StuAiLimit stuAiLimit = stuAiLimits.get(0);
if (30 <= stuAiLimit.getWordNumber())
{
stuAiLimit.setLimitState((byte)1);
}
stuAiLimitMapper.updateByPrimaryKeySelective(stuAiLimit);
}else {
StuAiLimit stuAiLimit = new StuAiLimit();
stuAiLimit.setUserId(jwtUser.getUserId());
stuAiLimit.setLimitState((byte)0);
stuAiLimit.setCreateTime(new Date());
stuAiLimit.setNumberOfTimes(1);
stuAiLimit.setWordNumber(0);
stuAiLimitMapper.insertSelective(stuAiLimit);
}
} else {
throw new UnAuthorizedException("未经授权登录");
}
}

@ -20,7 +20,9 @@ import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Message {
/**
* "user", "assistant", "function"
@ -37,6 +39,8 @@ public class Message {
*/
private String name;
private String userId;
/**
*
*/

@ -5,9 +5,12 @@ package com.sztzjy.marketing.service.impl;/**
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sztzjy.marketing.entity.StuAiLimit;
import com.sztzjy.marketing.entity.StuAiLimitExample;
import com.sztzjy.marketing.entity.dto.ReqChatMessage;
import com.sztzjy.marketing.entity.dto.StuCreateArticleDTO;
import com.sztzjy.marketing.entity.dto.StuCreateImgDTO;
import com.sztzjy.marketing.mapper.StuAiLimitMapper;
import com.sztzjy.marketing.qianfan.Qianfan;
import com.sztzjy.marketing.qianfan.model.chat.ChatResponse;
import com.sztzjy.marketing.qianfan.model.chat.Message;
@ -49,15 +52,19 @@ public class QianFanBigModuleServiceImpl implements QianFanBigModuleService {
@Autowired
private AsyncTaskExecutor asyncTaskExecutor;
@Autowired
private StuAiLimitMapper stuAiLimitMapper;
@Override
public ResultEntity createImgByAi(StuCreateImgDTO stuCreateImgDTO) {
String accesstoken = null;
try {
accesstoken = getAccesstoken();
} catch (IOException e) {
throw new RuntimeException(e);
}
// String accesstoken = null;
// try {
// accesstoken = getAccesstoken();
// } catch (IOException e) {
// throw new RuntimeException(e);
// }
// chat(accesstoken);
@ -107,6 +114,23 @@ public class QianFanBigModuleServiceImpl implements QianFanBigModuleService {
@Override
public Flux<String> createArticleByMessage(List<Message> messageList) {
int count = 0;
for (Message message : messageList) {
int length = message.getContent().length();
count += length;
}
System.out.println(count);
StuAiLimitExample limitExample = new StuAiLimitExample();
limitExample.createCriteria().andUserIdEqualTo(messageList.get(0).getUserId());
List<StuAiLimit> stuAiLimits = stuAiLimitMapper.selectByExample(limitExample);
if (stuAiLimits.get(0).getLimitState()== (byte) 1 || 30 < stuAiLimits.get(0).getWordNumber()) {
throw new RuntimeException("您的使用已达到今日限额,请明日再试。");
}
stuAiLimits.get(0).setWordNumber(stuAiLimits.get(0).getWordNumber() + count);
stuAiLimitMapper.updateByPrimaryKeySelective(stuAiLimits.get(0));
String accesstoken = null;
@ -116,8 +140,6 @@ public class QianFanBigModuleServiceImpl implements QianFanBigModuleService {
throw new RuntimeException(e);
}
return (chat(accesstoken,messageList));
}

Loading…
Cancel
Save