feat(agent): 集成OpenAI和阿里云通义千问模型支持

- 在AgentMetaService中添加日志记录功能,监控MCP工具状态
- 配置类AiChatModelConfiguration集成OpenAI、智谱AI和Ollama模型支持
- 实现API密钥脱敏处理保护敏感信息
- 应用配置中默认启用OpenAI作为首选AI提供商
- 开发环境配置集成阿里云通义千问兼容模式接口
This commit is contained in:
will
2026-08-12 22:09:23 +08:00
parent bf5a3209d9
commit 19f9a42dd1
4 changed files with 23 additions and 6 deletions
@@ -3,6 +3,7 @@ package com.will.coreAgent.agent.service;
import com.will.coreAgent.agent.dto.AgentDefinition;
import com.will.coreAgent.agent.dto.AgentMetaResult;
import com.will.coreAgent.framework.mcp.McpContextService;
import lombok.extern.slf4j.Slf4j;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
@@ -10,6 +11,7 @@ import org.springframework.stereotype.Service;
import java.util.List;
@Slf4j
@Service
@RequiredArgsConstructor
@ConditionalOnProperty(prefix = "will.agent", name = "enabled", havingValue = "true")
@@ -31,6 +33,8 @@ public class AgentMetaService {
public AgentMetaResult getMeta() {
List<String> toolNames = mcpContextService.listToolNames();
log.info("[Agent][Meta] provider={}, model={}, mcpEnabled={}, mcpToolCount={}",
provider, resolveModel(), mcpContextService.isEnabled(), toolNames.size());
return AgentMetaResult.builder()
.enabled(true)
.provider(provider)
@@ -1,5 +1,6 @@
package com.will.coreAgent.framework.config;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.ollama.OllamaChatModel;
import org.springframework.ai.ollama.api.OllamaApi;
@@ -17,6 +18,7 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;
@Slf4j
@Configuration
@ConditionalOnProperty(prefix = "will.agent", name = "enabled", havingValue = "true")
public class AiChatModelConfiguration {
@@ -29,8 +31,9 @@ public class AiChatModelConfiguration {
@Value("${spring.ai.openai.base-url:https://api.openai.com}") String baseUrl,
@Value("${spring.ai.openai.chat.options.model:gpt-4o-mini}") String model) {
if (!StringUtils.hasText(apiKey)) {
throw new IllegalStateException("will.agent.provider=openai 时需配置 spring.ai.openai.api-key");
throw new IllegalStateException("will.agent.provider=openai 鏃堕渶閰嶇疆 spring.ai.openai.api-key");
}
log.info("[Agent][AI] provider=openai, baseUrl={}, model={}, apiKey={}", baseUrl, model, maskKey(apiKey));
return OpenAiChatModel.builder()
.openAiApi(OpenAiApi.builder().baseUrl(baseUrl).apiKey(apiKey).build())
.defaultOptions(OpenAiChatOptions.builder().model(model).temperature(0.2).build())
@@ -45,8 +48,9 @@ public class AiChatModelConfiguration {
@Value("${spring.ai.zhipuai.base-url:https://open.bigmodel.cn/api/paas}") String baseUrl,
@Value("${spring.ai.zhipuai.chat.options.model:glm-4-flash}") String model) {
if (!StringUtils.hasText(apiKey)) {
throw new IllegalStateException("will.agent.provider=zhipu 时需配置 spring.ai.zhipuai.api-key");
throw new IllegalStateException("will.agent.provider=zhipu 鏃堕渶閰嶇疆 spring.ai.zhipuai.api-key");
}
log.info("[Agent][AI] provider=zhipu, baseUrl={}, model={}, apiKey={}", baseUrl, model, maskKey(apiKey));
return new ZhiPuAiChatModel(
new ZhiPuAiApi(baseUrl, apiKey),
ZhiPuAiChatOptions.builder().model(model).temperature(0.2).build());
@@ -58,9 +62,18 @@ public class AiChatModelConfiguration {
public ChatModel ollamaChatModel(
@Value("${spring.ai.ollama.base-url:http://localhost:11434}") String baseUrl,
@Value("${spring.ai.ollama.chat.options.model:qwen2.5:7b}") String model) {
log.info("[Agent][AI] provider=ollama, baseUrl={}, model={}", baseUrl, model);
return OllamaChatModel.builder()
.ollamaApi(OllamaApi.builder().baseUrl(baseUrl).build())
.defaultOptions(OllamaOptions.builder().model(model).temperature(0.2).build())
.build();
}
private String maskKey(String apiKey) {
String value = apiKey == null ? "" : apiKey.trim();
if (value.length() <= 10) {
return "***";
}
return value.substring(0, 6) + "***" + value.substring(value.length() - 4);
}
}
@@ -7,11 +7,11 @@ spring:
model: ${OLLAMA_MODEL:qwen2.5:7b}
temperature: ${OLLAMA_TEMPERATURE:0.2}
openai:
api-key: ${OPENAI_API_KEY:}
base-url: ${OPENAI_BASE_URL:https://api.openai.com}
api-key: ${OPENAI_API_KEY:sk-ws-H.EEIXXEX.plGC.MEUCIQDLPhhJgB1hx9zwmr-mQUEXojCqYtyH5Gr2pTdE3pBhWwIgDUoIySiYVOGbFBz0PdEWsgaNj3m8kEBm_9z-bmVhc0c}
base-url: ${OPENAI_BASE_URL:https://dashscope.aliyuncs.com/compatible-mode}
chat:
options:
model: ${OPENAI_MODEL:gpt-4o-mini}
model: ${OPENAI_MODEL:qwen3.7-plus}
temperature: ${OPENAI_TEMPERATURE:0.2}
zhipuai:
api-key: ${ZHIPUAI_API_KEY:7980536e3ea04550b037db126899bb4f.QhE9ardDCIgT3SG3}
+1 -1
View File
@@ -5,7 +5,7 @@ spring:
will:
agent:
enabled: true
provider: ${WILL_AGENT_PROVIDER:zhipu}
provider: ${WILL_AGENT_PROVIDER:openai}
chat:
max-messages: ${WILL_AGENT_CHAT_MAX_MESSAGES:30}
mcp: