Compare commits
3 Commits
35dca0a435
...
feat/langc
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5267d29d97 | ||
|
|
24d01bc3e1 | ||
|
|
5dbd0b3a36 |
@@ -2,6 +2,8 @@ package com.xiang.xsa.xservice.ai.server;
|
||||
|
||||
import com.xiang.xservice.ai.agent.BaseAgent;
|
||||
import com.xiang.xservice.ai.core.enums.ModelTypeEnum;
|
||||
import com.xiang.xservice.ai.pojo.enums.AgentEnums;
|
||||
import com.xiang.xservice.ai.service.AgentService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
@@ -11,11 +13,12 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
@RequiredArgsConstructor
|
||||
public class ChatController {
|
||||
|
||||
private final BaseAgent baseAgent;
|
||||
private final AgentService agentService;
|
||||
|
||||
@GetMapping("/chat")
|
||||
public String chatDemo(@RequestParam("question") String question, @RequestParam("memoryId") Long memoryId, @RequestParam("userId") Long userId) {
|
||||
baseAgent.chat(ModelTypeEnum.OPEN_AI, memoryId, userId, question);
|
||||
BaseAgent agent = agentService.createAgent(AgentEnums.SIMPLE_CHAT_AGENT);
|
||||
agent.chat(ModelTypeEnum.OPEN_AI, memoryId, userId, question);
|
||||
return "321";
|
||||
}
|
||||
|
||||
|
||||
@@ -12,4 +12,9 @@ ai:
|
||||
apiKey: sk-70cb426d7d1e4b54b4ffe71022e7d815
|
||||
modelName: qwen3.5-plus
|
||||
baseUrl: https://dashscope.aliyuncs.com/compatible-mode/v1
|
||||
ollama:
|
||||
configs:
|
||||
qwen3:
|
||||
modelName: qwen3
|
||||
baseUrl: http://192.168.1.12:11434/api/chat
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.xiang.xservice.ai.agent;
|
||||
|
||||
import com.xiang.xservice.ai.core.enums.ModelTypeEnum;
|
||||
import com.xiang.xservice.ai.pojo.enums.AgentEnums;
|
||||
|
||||
public interface BaseAgent {
|
||||
/**
|
||||
@@ -11,4 +12,6 @@ public interface BaseAgent {
|
||||
* @param message
|
||||
*/
|
||||
void chat(ModelTypeEnum modelType, Long memoryId, Long userId, String message);
|
||||
|
||||
AgentEnums agent();
|
||||
}
|
||||
|
||||
@@ -7,16 +7,14 @@ import com.xiang.xservice.ai.core.assistant.Assistant;
|
||||
import com.xiang.xservice.ai.core.entity.ModelConfig;
|
||||
import com.xiang.xservice.ai.core.enums.ModelStrategyEnum;
|
||||
import com.xiang.xservice.ai.core.enums.ModelTypeEnum;
|
||||
import com.xiang.xservice.ai.core.handler.MyStreamingHandler;
|
||||
import com.xiang.xservice.ai.core.route.TaskRouter;
|
||||
import com.xiang.xservice.ai.core.storage.DbPersistentStore;
|
||||
import com.xiang.xservice.ai.core.storage.MemoryPersistentStore;
|
||||
import com.xiang.xservice.ai.pojo.enums.AgentEnums;
|
||||
import com.xiang.xservice.ai.repository.manage.IAiSimpleChatMessageManage;
|
||||
import dev.langchain4j.data.message.SystemMessage;
|
||||
import dev.langchain4j.data.message.UserMessage;
|
||||
import dev.langchain4j.memory.chat.ChatMemoryProvider;
|
||||
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
|
||||
import dev.langchain4j.model.chat.ChatModel;
|
||||
import dev.langchain4j.model.chat.StreamingChatModel;
|
||||
import dev.langchain4j.service.AiServices;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
@@ -51,7 +49,7 @@ public class SimpleChatAgent implements BaseAgent {
|
||||
.modelName(openAIConfig.getModelName())
|
||||
.temperature(openAIConfig.getTemperature())
|
||||
.build();
|
||||
StreamingChatModel chat = router.route(ModelStrategyEnum.CHAT, modelType.getModelType(), modelConfig);
|
||||
StreamingChatModel chat = router.routeStream(ModelStrategyEnum.CHAT, modelType.getModelType(), modelConfig);
|
||||
if (chat == null) {
|
||||
throw new RuntimeException("chat model route failed");
|
||||
}
|
||||
@@ -69,4 +67,9 @@ public class SimpleChatAgent implements BaseAgent {
|
||||
.ignoreErrors()
|
||||
.start();
|
||||
}
|
||||
|
||||
@Override
|
||||
public AgentEnums agent() {
|
||||
return AgentEnums.SIMPLE_CHAT_AGENT;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.xiang.xservice.ai.agent;
|
||||
|
||||
import com.xiang.xservice.ai.core.enums.ModelTypeEnum;
|
||||
import com.xiang.xservice.ai.pojo.enums.AgentEnums;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class StockAnalysisAgent implements BaseAgent{
|
||||
@Override
|
||||
public void chat(ModelTypeEnum modelType, Long memoryId, Long userId, String message) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public AgentEnums agent() {
|
||||
return AgentEnums.STOCK_ANALYZER_AGENT;
|
||||
}
|
||||
}
|
||||
@@ -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.enums.ModelStrategyEnum;
|
||||
import com.xiang.xservice.ai.core.enums.ModelTypeEnum;
|
||||
import dev.langchain4j.model.chat.ChatModel;
|
||||
import dev.langchain4j.model.chat.StreamingChatModel;
|
||||
|
||||
@@ -14,11 +13,18 @@ public interface BaseProvider {
|
||||
*/
|
||||
String providerName();
|
||||
/**
|
||||
* 创建model
|
||||
* 创建流式model
|
||||
* @param config model配置文件
|
||||
* @return ChatModel
|
||||
*/
|
||||
StreamingChatModel build(ModelConfig config);
|
||||
|
||||
/**
|
||||
* 同步聊天模型
|
||||
* @param config
|
||||
* @return
|
||||
*/
|
||||
ChatModel buildChatModel(ModelConfig config);
|
||||
/**
|
||||
* 用于标记这个 Provider 是否适合某个 TaskType
|
||||
* @param taskType ModelStrategyEnum
|
||||
|
||||
@@ -26,6 +26,15 @@ public class OllamaLlmProvider implements BaseProvider{
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ChatModel buildChatModel(ModelConfig config) {
|
||||
return OllamaChatModel.builder()
|
||||
.baseUrl(config.getBaseUrl())
|
||||
.modelName(config.getModelName())
|
||||
.temperature(config.getTemperature())
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean support(ModelStrategyEnum taskType) {
|
||||
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.enums.ModelStrategyEnum;
|
||||
import com.xiang.xservice.ai.core.enums.ModelTypeEnum;
|
||||
import com.xiang.xservice.ai.core.storage.MemoryPersistentStore;
|
||||
import dev.langchain4j.memory.chat.ChatMemoryProvider;
|
||||
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
|
||||
import dev.langchain4j.model.chat.ChatModel;
|
||||
import dev.langchain4j.model.chat.StreamingChatModel;
|
||||
import dev.langchain4j.model.openai.OpenAiChatModel;
|
||||
import dev.langchain4j.model.openai.OpenAiStreamingChatModel;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.time.Duration;
|
||||
|
||||
|
||||
@Component
|
||||
public class OpenAILlmProvider implements BaseProvider {
|
||||
@@ -33,6 +34,18 @@ public class OpenAILlmProvider implements BaseProvider {
|
||||
.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
|
||||
public boolean support(ModelStrategyEnum taskType) {
|
||||
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.enums.ModelStrategyEnum;
|
||||
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.StreamingChatModel;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
@@ -29,7 +27,7 @@ public class ModelRouter {
|
||||
/**
|
||||
* 根据 providerName 选择 Provider,并用 config 构建模型
|
||||
*/
|
||||
public StreamingChatModel route(String providerName, ModelConfig config) {
|
||||
public StreamingChatModel routeStream(String providerName, ModelConfig config) {
|
||||
BaseProvider provider = providerMap.get(providerName);
|
||||
if (provider == null) {
|
||||
throw new RuntimeException("Provider " + providerName + " not found");
|
||||
@@ -37,6 +35,14 @@ public class ModelRouter {
|
||||
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 名称
|
||||
*/
|
||||
|
||||
@@ -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.enums.ModelStrategyEnum;
|
||||
import com.xiang.xservice.ai.core.provider.BaseProvider;
|
||||
import dev.langchain4j.model.chat.ChatModel;
|
||||
import dev.langchain4j.model.chat.StreamingChatModel;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@@ -29,7 +30,17 @@ public class TaskRouter {
|
||||
/**
|
||||
* 第一层路由 + 第二层 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
|
||||
if (providerName == null || providerName.isEmpty()) {
|
||||
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.enums.ModelStrategyEnum;
|
||||
import com.xiang.xservice.ai.core.provider.BaseProvider;
|
||||
import com.xiang.xservice.ai.core.route.TaskRouter;
|
||||
import dev.langchain4j.model.chat.ChatModel;
|
||||
import dev.langchain4j.model.chat.StreamingChatModel;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Component;
|
||||
@@ -22,6 +20,6 @@ public class ChatStrategy implements BaseStrategy {
|
||||
|
||||
@Override
|
||||
public StreamingChatModel createProvider(String provider, ModelConfig config) {
|
||||
return taskRouter.route(ModelStrategyEnum.CHAT, provider, config);
|
||||
return taskRouter.routeStream(ModelStrategyEnum.CHAT, provider, config);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.xiang.xservice.ai.pojo.enums;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
public enum AgentEnums {
|
||||
SIMPLE_CHAT_AGENT("SimpleChatAgent"),
|
||||
STOCK_ANALYZER_AGENT("StockAnalysisAgent"),
|
||||
;
|
||||
private final String name;
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.xiang.xservice.ai.service;
|
||||
|
||||
import com.google.common.collect.Maps;
|
||||
import com.xiang.xservice.ai.agent.BaseAgent;
|
||||
import com.xiang.xservice.ai.pojo.enums.AgentEnums;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class AgentService {
|
||||
|
||||
private final static Map<AgentEnums, BaseAgent> agents = Maps.newHashMap();
|
||||
|
||||
public AgentService(List<BaseAgent> agentList) {
|
||||
agents.putAll(agentList.stream()
|
||||
.collect(Collectors.toMap(BaseAgent::agent, Function.identity())));
|
||||
}
|
||||
|
||||
public BaseAgent createAgent(AgentEnums name) {
|
||||
BaseAgent agent = agents.get(name);
|
||||
if (Objects.isNull(agent)) throw new RuntimeException("Agent not found: " + name);
|
||||
return agent;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user