Compare commits
1 Commits
24d01bc3e1
...
feat/langc
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5267d29d97 |
@@ -12,4 +12,9 @@ ai:
|
|||||||
apiKey: sk-70cb426d7d1e4b54b4ffe71022e7d815
|
apiKey: sk-70cb426d7d1e4b54b4ffe71022e7d815
|
||||||
modelName: qwen3.5-plus
|
modelName: qwen3.5-plus
|
||||||
baseUrl: https://dashscope.aliyuncs.com/compatible-mode/v1
|
baseUrl: https://dashscope.aliyuncs.com/compatible-mode/v1
|
||||||
|
ollama:
|
||||||
|
configs:
|
||||||
|
qwen3:
|
||||||
|
modelName: qwen3
|
||||||
|
baseUrl: http://192.168.1.12:11434/api/chat
|
||||||
|
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ public class SimpleChatAgent implements BaseAgent {
|
|||||||
.modelName(openAIConfig.getModelName())
|
.modelName(openAIConfig.getModelName())
|
||||||
.temperature(openAIConfig.getTemperature())
|
.temperature(openAIConfig.getTemperature())
|
||||||
.build();
|
.build();
|
||||||
StreamingChatModel chat = router.route(ModelStrategyEnum.CHAT, modelType.getModelType(), modelConfig);
|
StreamingChatModel chat = router.routeStream(ModelStrategyEnum.CHAT, modelType.getModelType(), modelConfig);
|
||||||
if (chat == null) {
|
if (chat == null) {
|
||||||
throw new RuntimeException("chat model route failed");
|
throw new RuntimeException("chat model route failed");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package com.xiang.xservice.ai.core.provider;
|
|||||||
|
|
||||||
import com.xiang.xservice.ai.core.entity.ModelConfig;
|
import com.xiang.xservice.ai.core.entity.ModelConfig;
|
||||||
import com.xiang.xservice.ai.core.enums.ModelStrategyEnum;
|
import com.xiang.xservice.ai.core.enums.ModelStrategyEnum;
|
||||||
import com.xiang.xservice.ai.core.enums.ModelTypeEnum;
|
|
||||||
import dev.langchain4j.model.chat.ChatModel;
|
import dev.langchain4j.model.chat.ChatModel;
|
||||||
import dev.langchain4j.model.chat.StreamingChatModel;
|
import dev.langchain4j.model.chat.StreamingChatModel;
|
||||||
|
|
||||||
@@ -14,11 +13,18 @@ public interface BaseProvider {
|
|||||||
*/
|
*/
|
||||||
String providerName();
|
String providerName();
|
||||||
/**
|
/**
|
||||||
* 创建model
|
* 创建流式model
|
||||||
* @param config model配置文件
|
* @param config model配置文件
|
||||||
* @return ChatModel
|
* @return ChatModel
|
||||||
*/
|
*/
|
||||||
StreamingChatModel build(ModelConfig config);
|
StreamingChatModel build(ModelConfig config);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 同步聊天模型
|
||||||
|
* @param config
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
ChatModel buildChatModel(ModelConfig config);
|
||||||
/**
|
/**
|
||||||
* 用于标记这个 Provider 是否适合某个 TaskType
|
* 用于标记这个 Provider 是否适合某个 TaskType
|
||||||
* @param taskType ModelStrategyEnum
|
* @param taskType ModelStrategyEnum
|
||||||
|
|||||||
@@ -26,6 +26,15 @@ public class OllamaLlmProvider implements BaseProvider{
|
|||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ChatModel buildChatModel(ModelConfig config) {
|
||||||
|
return OllamaChatModel.builder()
|
||||||
|
.baseUrl(config.getBaseUrl())
|
||||||
|
.modelName(config.getModelName())
|
||||||
|
.temperature(config.getTemperature())
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean support(ModelStrategyEnum taskType) {
|
public boolean support(ModelStrategyEnum taskType) {
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
@@ -3,13 +3,14 @@ package com.xiang.xservice.ai.core.provider;
|
|||||||
import com.xiang.xservice.ai.core.entity.ModelConfig;
|
import com.xiang.xservice.ai.core.entity.ModelConfig;
|
||||||
import com.xiang.xservice.ai.core.enums.ModelStrategyEnum;
|
import com.xiang.xservice.ai.core.enums.ModelStrategyEnum;
|
||||||
import com.xiang.xservice.ai.core.enums.ModelTypeEnum;
|
import com.xiang.xservice.ai.core.enums.ModelTypeEnum;
|
||||||
import com.xiang.xservice.ai.core.storage.MemoryPersistentStore;
|
import dev.langchain4j.model.chat.ChatModel;
|
||||||
import dev.langchain4j.memory.chat.ChatMemoryProvider;
|
|
||||||
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
|
|
||||||
import dev.langchain4j.model.chat.StreamingChatModel;
|
import dev.langchain4j.model.chat.StreamingChatModel;
|
||||||
|
import dev.langchain4j.model.openai.OpenAiChatModel;
|
||||||
import dev.langchain4j.model.openai.OpenAiStreamingChatModel;
|
import dev.langchain4j.model.openai.OpenAiStreamingChatModel;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.time.Duration;
|
||||||
|
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
public class OpenAILlmProvider implements BaseProvider {
|
public class OpenAILlmProvider implements BaseProvider {
|
||||||
@@ -33,6 +34,18 @@ public class OpenAILlmProvider implements BaseProvider {
|
|||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ChatModel buildChatModel(ModelConfig config) {
|
||||||
|
return OpenAiChatModel.builder()
|
||||||
|
.baseUrl(config.getBaseUrl())
|
||||||
|
.apiKey(config.getApiKey())
|
||||||
|
.modelName(config.getModelName())
|
||||||
|
.temperature(config.getTemperature())
|
||||||
|
.maxTokens(config.getMaxTokens())
|
||||||
|
.timeout(Duration.ofSeconds(300))
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean support(ModelStrategyEnum taskType) {
|
public boolean support(ModelStrategyEnum taskType) {
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
@@ -3,10 +3,8 @@ package com.xiang.xservice.ai.core.route;
|
|||||||
import com.xiang.xservice.ai.core.entity.ModelConfig;
|
import com.xiang.xservice.ai.core.entity.ModelConfig;
|
||||||
import com.xiang.xservice.ai.core.enums.ModelStrategyEnum;
|
import com.xiang.xservice.ai.core.enums.ModelStrategyEnum;
|
||||||
import com.xiang.xservice.ai.core.provider.BaseProvider;
|
import com.xiang.xservice.ai.core.provider.BaseProvider;
|
||||||
import com.xiang.xservice.ai.core.strategy.BaseStrategy;
|
|
||||||
import dev.langchain4j.model.chat.ChatModel;
|
import dev.langchain4j.model.chat.ChatModel;
|
||||||
import dev.langchain4j.model.chat.StreamingChatModel;
|
import dev.langchain4j.model.chat.StreamingChatModel;
|
||||||
import lombok.RequiredArgsConstructor;
|
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -29,7 +27,7 @@ public class ModelRouter {
|
|||||||
/**
|
/**
|
||||||
* 根据 providerName 选择 Provider,并用 config 构建模型
|
* 根据 providerName 选择 Provider,并用 config 构建模型
|
||||||
*/
|
*/
|
||||||
public StreamingChatModel route(String providerName, ModelConfig config) {
|
public StreamingChatModel routeStream(String providerName, ModelConfig config) {
|
||||||
BaseProvider provider = providerMap.get(providerName);
|
BaseProvider provider = providerMap.get(providerName);
|
||||||
if (provider == null) {
|
if (provider == null) {
|
||||||
throw new RuntimeException("Provider " + providerName + " not found");
|
throw new RuntimeException("Provider " + providerName + " not found");
|
||||||
@@ -37,6 +35,14 @@ public class ModelRouter {
|
|||||||
return provider.build(config);
|
return provider.build(config);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ChatModel route(String providerName, ModelConfig config) {
|
||||||
|
BaseProvider provider = providerMap.get(providerName);
|
||||||
|
if (provider == null) {
|
||||||
|
throw new RuntimeException("Provider " + providerName + " not found");
|
||||||
|
}
|
||||||
|
return provider.buildChatModel(config);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取某个 TaskType 支持的所有 Provider 名称
|
* 获取某个 TaskType 支持的所有 Provider 名称
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package com.xiang.xservice.ai.core.route;
|
|||||||
import com.xiang.xservice.ai.core.entity.ModelConfig;
|
import com.xiang.xservice.ai.core.entity.ModelConfig;
|
||||||
import com.xiang.xservice.ai.core.enums.ModelStrategyEnum;
|
import com.xiang.xservice.ai.core.enums.ModelStrategyEnum;
|
||||||
import com.xiang.xservice.ai.core.provider.BaseProvider;
|
import com.xiang.xservice.ai.core.provider.BaseProvider;
|
||||||
|
import dev.langchain4j.model.chat.ChatModel;
|
||||||
import dev.langchain4j.model.chat.StreamingChatModel;
|
import dev.langchain4j.model.chat.StreamingChatModel;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
@@ -29,7 +30,17 @@ public class TaskRouter {
|
|||||||
/**
|
/**
|
||||||
* 第一层路由 + 第二层 ProviderRouter
|
* 第一层路由 + 第二层 ProviderRouter
|
||||||
*/
|
*/
|
||||||
public StreamingChatModel route(ModelStrategyEnum taskType, String providerName, ModelConfig config) {
|
public StreamingChatModel routeStream(ModelStrategyEnum taskType, String providerName, ModelConfig config) {
|
||||||
|
// 如果没有传 providerName,使用默认 Provider
|
||||||
|
if (providerName == null || providerName.isEmpty()) {
|
||||||
|
providerName = taskDefaultProviderMap.get(taskType).get(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 第二层路由
|
||||||
|
return providerRouter.routeStream(providerName, config);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ChatModel route(ModelStrategyEnum taskType, String providerName, ModelConfig config) {
|
||||||
// 如果没有传 providerName,使用默认 Provider
|
// 如果没有传 providerName,使用默认 Provider
|
||||||
if (providerName == null || providerName.isEmpty()) {
|
if (providerName == null || providerName.isEmpty()) {
|
||||||
providerName = taskDefaultProviderMap.get(taskType).get(0);
|
providerName = taskDefaultProviderMap.get(taskType).get(0);
|
||||||
|
|||||||
@@ -2,9 +2,7 @@ package com.xiang.xservice.ai.core.strategy;
|
|||||||
|
|
||||||
import com.xiang.xservice.ai.core.entity.ModelConfig;
|
import com.xiang.xservice.ai.core.entity.ModelConfig;
|
||||||
import com.xiang.xservice.ai.core.enums.ModelStrategyEnum;
|
import com.xiang.xservice.ai.core.enums.ModelStrategyEnum;
|
||||||
import com.xiang.xservice.ai.core.provider.BaseProvider;
|
|
||||||
import com.xiang.xservice.ai.core.route.TaskRouter;
|
import com.xiang.xservice.ai.core.route.TaskRouter;
|
||||||
import dev.langchain4j.model.chat.ChatModel;
|
|
||||||
import dev.langchain4j.model.chat.StreamingChatModel;
|
import dev.langchain4j.model.chat.StreamingChatModel;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
@@ -22,6 +20,6 @@ public class ChatStrategy implements BaseStrategy {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public StreamingChatModel createProvider(String provider, ModelConfig config) {
|
public StreamingChatModel createProvider(String provider, ModelConfig config) {
|
||||||
return taskRouter.route(ModelStrategyEnum.CHAT, provider, config);
|
return taskRouter.routeStream(ModelStrategyEnum.CHAT, provider, config);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user