mirror of
https://gitee.com/dromara/liteFlow.git
synced 2026-06-11 12:06:53 +08:00
Refactor: 重构 ChatModel 相关的实现
This commit is contained in:
@@ -0,0 +1,118 @@
|
||||
package com.yomahub.liteflow.ai.engine.model.chat;
|
||||
|
||||
import com.yomahub.liteflow.ai.engine.interact.InteractClient;
|
||||
import com.yomahub.liteflow.ai.engine.interact.LlmInteractClient;
|
||||
import com.yomahub.liteflow.ai.engine.model.chat.entity.ChatConfig;
|
||||
import com.yomahub.liteflow.ai.engine.model.chat.entity.ChatRequest;
|
||||
import com.yomahub.liteflow.ai.engine.model.chat.entity.ChatResponse;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
/**
|
||||
* 抽象聊天模型类
|
||||
*
|
||||
* @author 苍镜月
|
||||
* @since TODO
|
||||
*/
|
||||
|
||||
public abstract class AbstractChatModel implements ChatModel {
|
||||
|
||||
protected final ChatConfig config;
|
||||
protected final InteractClient interactClient;
|
||||
|
||||
public AbstractChatModel(ChatConfig config) {
|
||||
this.config = config;
|
||||
this.interactClient = new LlmInteractClient();
|
||||
}
|
||||
|
||||
public AbstractChatModel(ChatConfig config, InteractClient interactClient) {
|
||||
this.config = config;
|
||||
this.interactClient = interactClient;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ChatResponse chat(ChatRequest request) {
|
||||
return interactClient.chat(config, request);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<ChatResponse> chatAsync(ChatRequest request) {
|
||||
return interactClient.chatAsync(config, request);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stream(ChatRequest request) {
|
||||
interactClient.stream(config, request);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ChatConfig getModelConfig() {
|
||||
return config;
|
||||
}
|
||||
|
||||
public static abstract class Builder<B extends Builder<B>> {
|
||||
|
||||
protected InteractClient interactClient;
|
||||
protected abstract ChatConfig.Builder<?> getConfigBuilder();
|
||||
|
||||
public Builder() {
|
||||
this.interactClient = new LlmInteractClient();
|
||||
}
|
||||
|
||||
public B interactClient(InteractClient interactClient) {
|
||||
this.interactClient = interactClient;
|
||||
return self();
|
||||
}
|
||||
|
||||
public B apiUrl(String apiUrl) {
|
||||
getConfigBuilder().apiUrl(apiUrl);
|
||||
return self();
|
||||
}
|
||||
|
||||
public B endPoint(String endPoint) {
|
||||
getConfigBuilder().endPoint(endPoint);
|
||||
return self();
|
||||
}
|
||||
|
||||
public B apiKey(String apiKey) {
|
||||
getConfigBuilder().apiKey(apiKey);
|
||||
return self();
|
||||
}
|
||||
|
||||
public B provider(String provider) {
|
||||
getConfigBuilder().provider(provider);
|
||||
return self();
|
||||
}
|
||||
|
||||
public B model(String model) {
|
||||
getConfigBuilder().model(model);
|
||||
return self();
|
||||
}
|
||||
|
||||
public B connectTimeout(Duration connectTimeout) {
|
||||
getConfigBuilder().connectTimeout(connectTimeout);
|
||||
return self();
|
||||
}
|
||||
|
||||
public B readTimeout(Duration readTimeout) {
|
||||
getConfigBuilder().readTimeout(readTimeout);
|
||||
return self();
|
||||
}
|
||||
|
||||
public B headersConfig(Map<String, Object> headersConfig) {
|
||||
getConfigBuilder().headersConfig(headersConfig);
|
||||
return self();
|
||||
}
|
||||
|
||||
public B autoToolCallEnabled(boolean autoToolCallEnabled) {
|
||||
getConfigBuilder().autoToolCallEnabled(autoToolCallEnabled);
|
||||
return self();
|
||||
}
|
||||
|
||||
public abstract B self();
|
||||
|
||||
public abstract ChatModel build();
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,7 @@ import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* TODO
|
||||
* Ollama 模型自动配置类
|
||||
*
|
||||
* @author 苍镜月
|
||||
* @since TODO
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package com.yomahub.liteflow.ai.model.ollama.constants;
|
||||
|
||||
/**
|
||||
* TODO
|
||||
* Ollama 模型常量定义
|
||||
*
|
||||
* @author 苍镜月
|
||||
* @since TODO
|
||||
|
||||
@@ -6,7 +6,7 @@ import java.time.Duration;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* TODO
|
||||
* Ollama 聊天配置类
|
||||
*
|
||||
* @author 苍镜月
|
||||
* @since TODO
|
||||
|
||||
@@ -1,15 +1,9 @@
|
||||
package com.yomahub.liteflow.ai.model.ollama.model.chat;
|
||||
|
||||
import com.yomahub.liteflow.ai.engine.interact.InteractClient;
|
||||
import com.yomahub.liteflow.ai.engine.interact.LlmInteractClient;
|
||||
import com.yomahub.liteflow.ai.engine.model.chat.AbstractChatModel;
|
||||
import com.yomahub.liteflow.ai.engine.model.chat.ChatModel;
|
||||
import com.yomahub.liteflow.ai.engine.model.chat.entity.ChatConfig;
|
||||
import com.yomahub.liteflow.ai.engine.model.chat.entity.ChatRequest;
|
||||
import com.yomahub.liteflow.ai.engine.model.chat.entity.ChatResponse;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
/**
|
||||
* Ollama 聊天模型
|
||||
@@ -18,41 +12,21 @@ import java.util.concurrent.CompletableFuture;
|
||||
* @since TODO
|
||||
*/
|
||||
|
||||
public class OllamaChatModel implements ChatModel {
|
||||
|
||||
private final ChatConfig config;
|
||||
private final InteractClient interactClient;
|
||||
public class OllamaChatModel extends AbstractChatModel {
|
||||
|
||||
public OllamaChatModel(ChatConfig config) {
|
||||
this.config = config;
|
||||
this.interactClient = new LlmInteractClient();
|
||||
super(config);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ChatResponse chat(ChatRequest request) {
|
||||
return interactClient.chat(config, request);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<ChatResponse> chatAsync(ChatRequest request) {
|
||||
return interactClient.chatAsync(config, request);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stream(ChatRequest request) {
|
||||
interactClient.stream(config, request);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ChatConfig getModelConfig() {
|
||||
return config;
|
||||
public OllamaChatModel(ChatConfig config, InteractClient interactClient) {
|
||||
super(config, interactClient);
|
||||
}
|
||||
|
||||
public static Builder builder() {
|
||||
return new Builder();
|
||||
}
|
||||
|
||||
public static class Builder {
|
||||
public static class Builder extends AbstractChatModel.Builder<Builder> {
|
||||
|
||||
private final OllamaChatConfig.Builder configBuilder;
|
||||
|
||||
@@ -60,55 +34,19 @@ public class OllamaChatModel implements ChatModel {
|
||||
this.configBuilder = OllamaChatConfig.builder();
|
||||
}
|
||||
|
||||
public Builder apiUrl(String apiUrl) {
|
||||
this.configBuilder.apiUrl(apiUrl);
|
||||
@Override
|
||||
protected ChatConfig.Builder<?> getConfigBuilder() {
|
||||
return this.configBuilder;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder self() {
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder endPoint(String endPoint) {
|
||||
this.configBuilder.endPoint(endPoint);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder apiKey(String apiKey) {
|
||||
this.configBuilder.apiKey(apiKey);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder provider(String provider) {
|
||||
this.configBuilder.provider(provider);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder model(String model) {
|
||||
this.configBuilder.model(model);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder connectTimeout(Duration connectTimeout) {
|
||||
this.configBuilder.connectTimeout(connectTimeout);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder readTimeout(Duration readTimeout) {
|
||||
this.configBuilder.readTimeout(readTimeout);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder headersConfig(Map<String, Object> headersConfig) {
|
||||
this.configBuilder.headersConfig(headersConfig);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder autoToolCallEnabled(boolean autoToolCallEnabled) {
|
||||
this.configBuilder.autoToolCallEnabled(autoToolCallEnabled);
|
||||
return this;
|
||||
}
|
||||
|
||||
public OllamaChatModel build() {
|
||||
OllamaChatConfig config = this.configBuilder.build();
|
||||
|
||||
return new OllamaChatModel(config);
|
||||
@Override
|
||||
public ChatModel build() {
|
||||
return new OllamaChatModel(this.configBuilder.build(), this.interactClient);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
package com.yomahub.liteflow.ai.model.ollama.model.chat;
|
||||
|
||||
/**
|
||||
* Ollama 聊天响应
|
||||
*
|
||||
* @author 苍镜月
|
||||
* @since TODO
|
||||
*/
|
||||
|
||||
public class OllamaChatResponse {
|
||||
|
||||
// private String model;
|
||||
// private String createdAt;
|
||||
// private
|
||||
}
|
||||
Reference in New Issue
Block a user