mirror of
https://gitee.com/dromara/liteFlow.git
synced 2026-06-11 15:56:53 +08:00
refactor: transport 实例化从反射更改为函数式
This commit is contained in:
@@ -8,7 +8,6 @@ import com.yomahub.liteflow.ai.engine.interact.protocol.ProtocolTransformer;
|
||||
import com.yomahub.liteflow.ai.engine.interact.protocol.ProtocolTransformerFactory;
|
||||
import com.yomahub.liteflow.ai.engine.interact.transport.Transport;
|
||||
import com.yomahub.liteflow.ai.engine.interact.transport.TransportListener;
|
||||
import com.yomahub.liteflow.ai.engine.interact.transport.TransportType;
|
||||
import com.yomahub.liteflow.ai.engine.log.EngineLog;
|
||||
import com.yomahub.liteflow.ai.engine.log.EngineLogManager;
|
||||
import com.yomahub.liteflow.ai.engine.model.chat.entity.ChatConfig;
|
||||
@@ -83,7 +82,7 @@ public class LlmInteractClient implements InteractClient {
|
||||
this.pipeline = request.isStreaming()
|
||||
? ChunkProcessPipeline.createStreamingPipeline(context, protocolTransformer, request.getChunkCallbackTransformer())
|
||||
: ChunkProcessPipeline.createBlockingPipeline(context, protocolTransformer);
|
||||
this.transport = TransportType.getTransportInstance(request.getTransportType());
|
||||
this.transport = request.getTransportType().getTransportInstance();
|
||||
this.externalTransportListener = request.getTransportListener();
|
||||
this.resultHandler = request.getResultHandler();
|
||||
this.internalTransportListener = new InternalTransportListener();
|
||||
@@ -205,8 +204,8 @@ public class LlmInteractClient implements InteractClient {
|
||||
.stream()
|
||||
.filter(tool -> Objects.equals(tool.getName(), toolCall.getName()))
|
||||
.findFirst()
|
||||
.orElseThrow(() ->
|
||||
new LiteFlowAIEngineException("Unable to find target tool with tool name: " + toolCall.getName()));
|
||||
.orElseThrow(() -> new LiteFlowAIEngineException(
|
||||
"Unable to find target tool with tool name: " + toolCall.getName()));
|
||||
// 调用工具
|
||||
String toolResult = toolCallBack.call(toolCall.getArguments().toString());
|
||||
// 返回工具调用结果
|
||||
|
||||
@@ -1,51 +1,29 @@
|
||||
package com.yomahub.liteflow.ai.engine.interact.transport;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import com.yomahub.liteflow.ai.engine.interact.transport.impl.DnJsonTransport;
|
||||
import com.yomahub.liteflow.ai.engine.interact.transport.impl.HttpTransport;
|
||||
import com.yomahub.liteflow.ai.engine.interact.transport.impl.SseTransport;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 传输方式枚举
|
||||
*
|
||||
* @author 苍镜月
|
||||
* @since 2.16.0
|
||||
*/
|
||||
|
||||
public enum TransportType {
|
||||
HTTP(HttpTransport.class),
|
||||
SSE(SseTransport.class),
|
||||
DnJson(DnJsonTransport.class),
|
||||
// TODO custom
|
||||
CUSTOM(null),
|
||||
;
|
||||
HTTP(HttpTransport::new),
|
||||
SSE(SseTransport::new),
|
||||
DN_JSON(DnJsonTransport::new);
|
||||
|
||||
private final Class<? extends Transport> transportClass;
|
||||
private final Supplier<Transport> transportSupplier;
|
||||
|
||||
TransportType(Class<? extends Transport> transportClass) {
|
||||
this.transportClass = transportClass;
|
||||
TransportType(Supplier<Transport> transportSupplier) {
|
||||
this.transportSupplier = transportSupplier;
|
||||
}
|
||||
|
||||
public Class<? extends Transport> getTransportClass() {
|
||||
return transportClass;
|
||||
}
|
||||
|
||||
public static Transport getTransportInstance(TransportType type) {
|
||||
try {
|
||||
return type.getTransportClass().getDeclaredConstructor().newInstance();
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Failed to create transport instance for " + type.getTransportClass().getName(), e);
|
||||
}
|
||||
}
|
||||
|
||||
private static final Set<Class<? extends Transport>> TRANSPORT_CLASSES = Arrays.stream(TransportType.values())
|
||||
.map(TransportType::getTransportClass)
|
||||
.collect(Collectors.toSet());
|
||||
|
||||
public static boolean contains(Class<? extends Transport> transportClass) {
|
||||
return TRANSPORT_CLASSES.contains(transportClass);
|
||||
public Transport getTransportInstance() {
|
||||
return transportSupplier.get();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user