You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
1.6 KiB
Java

package com.supervision.pdfqaserver.config;
import cn.hutool.core.util.StrUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class OllamaChatModelAspect {
@Value("${spring.ai.ollama.chat.model}")
private String model;
private String callStringMessage = "String org.springframework.ai.chat.model.ChatModel.call(String)";
/**
* ollamacall/no_thinkthink
* @param joinPoint joinPoint
* @return Object
* @throws Throwable
*/
@Around("execution(* org.springframework.ai.chat.model.ChatModel.call(..))")
public Object aroundMethodExecution(ProceedingJoinPoint joinPoint) throws Throwable {
String signature = joinPoint.getSignature().toString();
if (StrUtil.equals(model,"qwen3:30b-a3b") && StrUtil.equals(signature, callStringMessage)) {
Object[] args = joinPoint.getArgs();
if (args.length > 0) {
String arg = (String) args[0];
args[0] = arg + "\n /no_think";
}
}
// 执行原方法
Object result = joinPoint.proceed();
if (StrUtil.equals(model,"qwen3:30b-a3b") && StrUtil.equals(signature, callStringMessage)) {
result = ((String) result).replaceAll("(?is)<think\\b[^>]*>(.*?)</think>", "").trim();
}
return result;
}
}