Feat: 适配 ModelConfig

This commit is contained in:
LuanY77
2025-07-23 18:11:05 +08:00
parent e7e15591af
commit af0e848ce0
8 changed files with 89 additions and 21 deletions

View File

@@ -19,7 +19,7 @@ public @interface AIChat {
/**
* 系统提示词
*/
String systemPrompt() default "你是一个意图分类助手";
String systemPrompt() default "";
/**
* 用户提示词

View File

@@ -23,7 +23,7 @@ public @interface AIClassify {
/**
* 系统提示词
*/
String systemPrompt() default "";
String systemPrompt() default "你是一个意图分类助手";
/**
* 用户提示词

View File

@@ -0,0 +1,21 @@
package com.yomahub.liteflow.ai.config;
import com.yomahub.liteflow.ai.proxy.AIComponentPostProcessor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* TODO
*
* @author 苍镜月
* @since TODO
*/
@Configuration
public class LiteflowAIAutoConfiguration {
@Bean
public AIComponentPostProcessor aiComponentPostProcessor() {
return new AIComponentPostProcessor();
}
}

View File

@@ -7,7 +7,8 @@ import com.yomahub.liteflow.log.LFLoggerManager;
import com.yomahub.liteflow.process.holder.SpringNodeIdHolder;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.core.Ordered;
import java.util.Objects;
/**
* AI组件后置处理器
@@ -15,7 +16,7 @@ import org.springframework.core.Ordered;
* @author 苍镜月
* @since TODO
*/
public class AIComponentPostProcessor implements BeanPostProcessor, Ordered {
public class AIComponentPostProcessor implements BeanPostProcessor {
private static final LFLog LOG = LFLoggerManager.getLogger(AIComponentPostProcessor.class);
@@ -38,6 +39,8 @@ public class AIComponentPostProcessor implements BeanPostProcessor, Ordered {
// 使用工厂创建AI组件
NodeComponent aiComponent = aiComponentFactory.createAIComponent(clazz, beanName);
if (Objects.isNull(aiComponent)) return bean;
LOG.info("AI proxy component[{}] has been created for interface: {}", beanName, clazz.getName());
String nodeId = StrUtil.isNotBlank(aiComponent.getNodeId()) ? aiComponent.getNodeId() : SpringNodeIdHolder.getRealBeanName(clazz, beanName);
@@ -54,10 +57,4 @@ public class AIComponentPostProcessor implements BeanPostProcessor, Ordered {
return bean;
}
@Override
public int getOrder() {
// 设置较高的优先级,确保在其他后置处理器之前执行
return Ordered.HIGHEST_PRECEDENCE + 100;
}
}

View File

@@ -158,7 +158,7 @@ public abstract class AbstractAIComponentHandler<T extends Annotation> {
private String generateProxyClassName(AIProxyWrapBean<T> wrapBean) {
return StrUtil.format("{}$ByteBuddy${}${}",
wrapBean.getInterfaceClass().getName(),
wrapBean.getAiComponent().nodeId(),
wrapBean.getNodeId(),
SerialsUtil.generateShortUUID());
}

View File

@@ -1,8 +1,11 @@
package com.yomahub.liteflow.ai.proxy.invocation;
import com.yomahub.liteflow.ai.annotation.AIChat;
import com.yomahub.liteflow.ai.model.ModelFactory;
import com.yomahub.liteflow.ai.proxy.wrap.AIProxyWrapBean;
import com.yomahub.liteflow.core.NodeComponent;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.chat.StreamingChatModel;
/**
* 聊天组件的调用处理器
@@ -18,7 +21,23 @@ public class ChatAIInvocationHandler extends AbstractAIInvocationHandler {
}
@Override
protected Object executeAIProcess(NodeComponent nodeComponent, Object[] args) {
protected Void executeAIProcess(NodeComponent nodeComponent, Object[] args) {
AIChat aiChatAnno = (AIChat) wrapBean.getAnnotation();
if (aiChatAnno.streaming()) {
return processStreaming(nodeComponent, args);
} else {
return processBlocking(nodeComponent, args);
}
}
private Void processStreaming(NodeComponent nodeComponent, Object[] args) {
StreamingChatModel streamingChatModel = ModelFactory.getStreamingChatModel(wrapBean.getConfig());
return null;
}
private Void processBlocking(NodeComponent nodeComponent, Object[] args) {
ChatModel chatModel = ModelFactory.getChatModel(wrapBean.getConfig());
return null;
}

View File

@@ -1,8 +1,10 @@
package com.yomahub.liteflow.ai.proxy.invocation;
import com.yomahub.liteflow.ai.annotation.AIClassify;
import com.yomahub.liteflow.ai.model.ModelFactory;
import com.yomahub.liteflow.ai.proxy.wrap.AIProxyWrapBean;
import com.yomahub.liteflow.core.NodeComponent;
import dev.langchain4j.model.chat.ChatModel;
/**
* 分类组件的调用处理器
@@ -19,6 +21,10 @@ public class ClassifyAIInvocationHandler extends AbstractAIInvocationHandler {
@Override
protected Object executeAIProcess(NodeComponent nodeComponent, Object[] args) {
AIClassify aiClassifyAnno = (AIClassify) wrapBean.getAnnotation();
ChatModel chatModel = ModelFactory.getChatModel(wrapBean.getConfig());
return null;
}
}

View File

@@ -1,6 +1,7 @@
package com.yomahub.liteflow.ai.proxy.wrap;
import com.yomahub.liteflow.ai.annotation.AIComponent;
import com.yomahub.liteflow.ai.config.ModelConfig;
import java.lang.annotation.Annotation;
@@ -13,35 +14,47 @@ import java.lang.annotation.Annotation;
public class AIProxyWrapBean<T extends Annotation> {
protected AIComponent aiComponent;
private AIComponent aiComponent;
protected T annotation;
private ModelConfig config;
protected Class<?> interfaceClass;
private String nodeId;
protected String beanName;
private String nodeName;
private T annotation;
private Class<?> interfaceClass;
private String beanName;
public AIProxyWrapBean() {
}
public AIProxyWrapBean(AIComponent aiComponent, T annotation,
Class<?> interfaceClass, String beanName) {
this.aiComponent = aiComponent;
this.config = ModelConfig.fromAnnotation(aiComponent);
this.nodeId = aiComponent.nodeId();
this.nodeName = aiComponent.nodeName();
this.annotation = annotation;
this.interfaceClass = interfaceClass;
this.beanName = beanName;
}
public AIComponent getAiComponent() {
return aiComponent;
}
public String getNodeId() {
return aiComponent.nodeId();
return nodeId;
}
public String getNodeName() {
return aiComponent.nodeName();
return nodeName;
}
public AIComponent getAiComponent() {
return aiComponent;
public ModelConfig getConfig() {
return config;
}
public T getAnnotation() {
@@ -60,6 +73,18 @@ public class AIProxyWrapBean<T extends Annotation> {
this.aiComponent = aiComponent;
}
public void setNodeId(String nodeId) {
this.nodeId = nodeId;
}
public void setNodeName(String nodeName) {
this.nodeName = nodeName;
}
public void setConfig(ModelConfig config) {
this.config = config;
}
public void setAnnotation(T annotation) {
this.annotation = annotation;
}