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 {
1 month ago
String signature = joinPoint.getSignature().toString();
1 month ago
// 获取原始参数
Object[] args = joinPoint.getArgs();
// 如果是String类型的call方法修改其参数
if (StrUtil.equals(signature, callStringMessage) && args.length > 0) {
args[0] = args[0] + "\n /no_think";
1 month ago
}
// 执行原方法
Object result = joinPoint.proceed(args);
1 month ago
if (StrUtil.equals(model,"qwen3:30b-a3b") ) {
if(StrUtil.equals(signature, callStringMessage)){
result = ((String) result).replaceAll("(?is)<think\\b[^>]*>(.*?)</think>", "").trim();
}
}
return result;
}
}