新增 AI MCP 服务端和客户端调用,展示远程工具调用示例
This commit is contained in:
@@ -2,16 +2,18 @@
|
||||
* Copyright (c) 2013-Now http://jeesite.com All rights reserved.
|
||||
* No deletion without permission, or be held responsible to law.
|
||||
*/
|
||||
package com.jeesite.modules.cms.ai.config;
|
||||
package com.jeesite.modules.ai.cms.config;
|
||||
|
||||
import com.jeesite.common.datasource.DataSourceHolder;
|
||||
import com.jeesite.common.lang.StringUtils;
|
||||
import com.jeesite.modules.cms.ai.properties.CmsAiProperties;
|
||||
import com.jeesite.modules.cms.ai.service.CacheChatMemoryRepository;
|
||||
import com.jeesite.modules.cms.ai.tools.CmsAiTools;
|
||||
import com.jeesite.modules.ai.cms.properties.AiCmsProperties;
|
||||
import com.jeesite.modules.ai.cms.service.CacheChatMemoryRepository;
|
||||
import com.jeesite.modules.ai.tools.TestAiTools;
|
||||
import com.jeesite.modules.ai.tools.UserAITools;
|
||||
import org.springframework.ai.chat.client.ChatClient;
|
||||
import org.springframework.ai.chat.memory.ChatMemory;
|
||||
import org.springframework.ai.chat.memory.MessageWindowChatMemory;
|
||||
import org.springframework.ai.mcp.SyncMcpToolCallbackProvider;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
@@ -24,24 +26,41 @@ import org.springframework.jdbc.core.JdbcTemplate;
|
||||
* @author ThinkGem
|
||||
*/
|
||||
@Configuration
|
||||
@EnableConfigurationProperties(CmsAiProperties.class)
|
||||
public class CmsAiChatConfig {
|
||||
@EnableConfigurationProperties(AiCmsProperties.class)
|
||||
public class AiCmsChatConfig {
|
||||
|
||||
/**
|
||||
* 聊天对话客户端
|
||||
* 聊天对话客户端(使用本地 Tools)
|
||||
* @author ThinkGem
|
||||
*/
|
||||
@Bean
|
||||
public ChatClient chatClient(ChatClient.Builder builder, CmsAiProperties properties) {
|
||||
@Bean("chatClient")
|
||||
@ConditionalOnProperty(name = "spring.ai.mcp.client.enabled", havingValue = "false", matchIfMissing = true)
|
||||
public ChatClient chatClient(ChatClient.Builder builder, AiCmsProperties properties,
|
||||
TestAiTools testAiTools, UserAITools userAITools) {
|
||||
if (StringUtils.isNotBlank(properties.getDefaultSystem())) {
|
||||
builder.defaultSystem(properties.getDefaultSystem());
|
||||
}
|
||||
if (properties.getToolCalls()) {
|
||||
builder.defaultTools(new CmsAiTools());
|
||||
if (properties.getTools().getEnabled()) {
|
||||
builder.defaultTools(testAiTools, userAITools);
|
||||
}
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* 聊天对话客户端(使用 MCP Tools)
|
||||
* @author ThinkGem
|
||||
*/
|
||||
@Bean("chatClient")
|
||||
@ConditionalOnProperty(name = "spring.ai.mcp.client.enabled", havingValue = "true", matchIfMissing = false)
|
||||
public ChatClient chatClientMcp(ChatClient.Builder builder, AiCmsProperties properties,
|
||||
SyncMcpToolCallbackProvider syncMcpToolCallbackProvider) {
|
||||
if (StringUtils.isNotBlank(properties.getDefaultSystem())) {
|
||||
builder.defaultSystem(properties.getDefaultSystem());
|
||||
}
|
||||
builder.defaultToolCallbacks(syncMcpToolCallbackProvider.getToolCallbacks());
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* 聊天对话数据存储
|
||||
* @author ThinkGem
|
||||
@@ -7,10 +7,16 @@ spring:
|
||||
model:
|
||||
chat: openai
|
||||
embedding: ${spring.ai.model.chat}
|
||||
image: ${spring.ai.model.chat}
|
||||
audio: ${spring.ai.model.chat}
|
||||
embedding.text: ${spring.ai.model.chat}
|
||||
embedding.multimodal: ${spring.ai.model.chat}
|
||||
audio.transcription: none
|
||||
audio.speech: none
|
||||
moderation: none
|
||||
image: none
|
||||
|
||||
# 在线大模型【请在 pom.xml 中打开 openai 的注释,并注释上其它模型】
|
||||
# ========= 聊天对话 相关配置 =========
|
||||
|
||||
# 云端模型【请在 pom.xml 中打开 openai 的注释,并注释上其它模型】
|
||||
openai:
|
||||
|
||||
# 聊天对话模型使用阿里百炼
|
||||
@@ -88,7 +94,7 @@ spring:
|
||||
# 聊天对话模型
|
||||
chat:
|
||||
options:
|
||||
model: qwen2.5
|
||||
model: qwen3:8b
|
||||
#model: deepseek-r1:7b
|
||||
max-tokens: 1024
|
||||
temperature: 0.6
|
||||
@@ -98,11 +104,11 @@ spring:
|
||||
embedding:
|
||||
# 维度 dimensions 设置为 384
|
||||
#model: all-minilm:33m
|
||||
# 维度 dimensions 设置为 768
|
||||
#model: nomic-embed-text
|
||||
# 维度 dimensions 设置为 1024
|
||||
model: bge-m3
|
||||
|
||||
# ========= 向量数据库 相关配置 =========
|
||||
|
||||
# 向量数据库配置
|
||||
vectorstore:
|
||||
|
||||
@@ -153,8 +159,31 @@ spring:
|
||||
index-type: HNSW
|
||||
metric-type: COSINE
|
||||
|
||||
# 是否启用工具调用【例子详见 CmsAiTools.java 】
|
||||
tool-calls: false
|
||||
# ========= 本地工具调用 相关配置 =========
|
||||
|
||||
# 是否启用 Tool calling 工具调用【例子详见 TestAiTools.java、UserAiTools.java 】
|
||||
tools:
|
||||
enabled: false
|
||||
|
||||
# ========= MPC 远程工具调用 相关配置 =========
|
||||
|
||||
# https://docs.spring.io/spring-ai/reference/api/mcp/mcp-client-boot-starter-docs.html
|
||||
mcp:
|
||||
client:
|
||||
enabled: false
|
||||
name: jeesite-mcp-client
|
||||
version: 1.0.0
|
||||
request-timeout: 30s
|
||||
type: SYNC
|
||||
sse:
|
||||
connections:
|
||||
jeesite:
|
||||
url: http://127.0.0.1:8981
|
||||
sse-endpoint: /api/v1/sse
|
||||
toolcallback:
|
||||
enabled: true
|
||||
|
||||
# ========= 默认提示词、默认回答模版 =========
|
||||
|
||||
# 默认系统提示词
|
||||
default-system: |
|
||||
@@ -166,7 +195,6 @@ spring:
|
||||
{query}
|
||||
请根据知识库和提供的历史信息作答。如果知识库中没有答案,请自我发挥。
|
||||
以下是知识库信息:{question_answer_context}
|
||||
|
||||
|
||||
# ========= Postgresql 向量数据库数据源 =========
|
||||
|
||||
@@ -192,6 +220,8 @@ spring:
|
||||
# username: elastic
|
||||
# password: elastic
|
||||
|
||||
# ========= 其他配置选项 =========
|
||||
|
||||
# 对话消息存缓存,可自定义存数据库
|
||||
j2cache:
|
||||
caffeine:
|
||||
@@ -199,7 +229,3 @@ j2cache:
|
||||
# 对话消息的超期时间,默认 30天,根据需要可以设置更久。
|
||||
cmsChatCache: 100000, 30d
|
||||
cmsChatMsgCache: 100000, 30d
|
||||
|
||||
#logging:
|
||||
# level:
|
||||
# org.springframework: debug
|
||||
@@ -23,7 +23,7 @@ public class TestAiTools {
|
||||
/**
|
||||
* 获取服务器时间
|
||||
*/
|
||||
@Tool(name="获取服务器时间", description = "获取当前的日期和时间,格式为 yyyy-MM-dd HH:mm:ss。")
|
||||
@Tool(name="当前服务器时间", description = "当前服务器日期时间,格式为 yyyy-MM-dd HH:mm:ss。")
|
||||
public String getCurrentDateTime() {
|
||||
String dateTime = "当前日期时间:" + DateUtils.getDateTime();
|
||||
logger.info("当前日期时间 ============== {}", dateTime);
|
||||
|
||||
@@ -13,10 +13,10 @@
|
||||
<inceptionYear>2013-Now</inceptionYear>
|
||||
|
||||
<modules>
|
||||
<module>core</module>
|
||||
<module>ai</module>
|
||||
<module>app</module>
|
||||
<module>cms</module>
|
||||
<module>cms-ai</module>
|
||||
<module>core</module>
|
||||
<module>static</module>
|
||||
<module>test</module>
|
||||
</modules>
|
||||
|
||||
Reference in New Issue
Block a user