新增 Tool calling,让 AI 调用你的 java 实现你的业务联动

This commit is contained in:
thinkgem
2025-03-29 11:08:35 +08:00
parent 52c62be658
commit 2931cc52bb
3 changed files with 80 additions and 24 deletions

View File

@@ -5,6 +5,7 @@
package com.jeesite.modules.cms.ai.config;
import com.jeesite.common.datasource.DataSourceHolder;
import com.jeesite.modules.cms.ai.tools.CmsAiTools;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
@@ -22,6 +23,31 @@ import java.sql.SQLException;
@Configuration
public class CmsAiChatConfig {
/**
* 聊天对话客户端
* @author ThinkGem
*/
@Bean
public ChatClient chatClient(ChatClient.Builder builder) {
return builder
.defaultSystem("""
## 人物设定
你是我的知识库AI助手你把我当作朋友耐心真诚地回复我提出的相关问题。
你需要遵循以下原则,与关注者进行友善而有价值的沟通。
## 表达方式:
1. 使用简体中文回答我的问题。
2. 使用幽默有趣的方式与我沟通。
3. 增加互动,如 “您的看法如何?”
""")
.defaultTools(new CmsAiTools())
.build();
}
// @Bean
// public BatchingStrategy batchingStrategy() {
// return new TokenCountBatchingStrategy(EncodingType.CL100K_BASE, Integer.MAX_VALUE, 0.1);
// }
/**
* PG向量库数据源
* @author ThinkGem
@@ -35,20 +61,4 @@ public class CmsAiChatConfig {
return new JdbcTemplate(dataSource);
}
/**
* 聊天对话客户端
* @author ThinkGem
*/
@Bean
public ChatClient chatClient(ChatClient.Builder builder) {
return builder
.defaultSystem("你是我的知识库AI助手请帮我解答我提出的相关问题。")
.build();
}
// @Bean
// public BatchingStrategy batchingStrategy() {
// return new TokenCountBatchingStrategy(EncodingType.CL100K_BASE, Integer.MAX_VALUE, 0.1);
// }
}

View File

@@ -0,0 +1,40 @@
/**
* 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.tools;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import java.time.LocalDateTime;
/**
* AI 工具调用、Tool calling需选择支持 Tools 的模型)
* @author ThinkGem
*/
public class CmsAiTools {
private final Logger logger = LoggerFactory.getLogger(CmsAiTools.class);
/**
* 未联网搜索的时候,可获取到服务器时间
*/
@Tool(description = "当前时间,当前日期,几点了")
public String getCurrentDateTime() {
String dateTime = "当前日期时间:" + LocalDateTime.now();
logger.info(dateTime + " ============== ");
return dateTime;
}
/**
* 你可以询问:打开客厅的灯,关闭卧室的灯(需创建新对话)
*/
@Tool(description = "房间里的灯打开或关闭")
public void turnLight(@ToolParam(description = "房间") String roomName, @ToolParam(description = "开关") boolean on) {
String message = roomName + " 房间里的灯被 " + (on ? "打开" : "关闭");
logger.info(message + " ============== ");
}
}

View File

@@ -9,12 +9,15 @@ spring:
api-key: ${SFLOW_APP_KEY}
#base-url: https://ai.gitee.com
#api-key: ${GITEE_APP_KEY}
#base-url: https://dashscope.aliyuncs.com/compatible-mode
#api-key: ${BAILIAN_APP_KEY}
# 聊天对话模型
chat:
enabled: true
enabled: false
options:
model: deepseek-ai/DeepSeek-R1-Distill-Qwen-7B
#model: DeepSeek-R1-Distill-Qwen-14B
#model: deepseek-r1-distill-llama-8b
max-tokens: 1024
temperature: 0.6
top-p: 0.7
@@ -22,28 +25,30 @@ spring:
logprobs: true
# 向量库知识库模型(注意:不同的模型维度不同)
embedding:
enabled: true
enabled: false
options:
model: BAAI/bge-m3
#model: bge-large-zh-v1.5
dimensions: 512
#model: text-embedding-v3
#dimensions: 1024
# 本地大模型配置(使用该模型,请开启 enabled 参数)
ollama:
base-url: http://localhost:11434
# 聊天对话模型
chat:
enabled: false
enabled: true
options:
#model: qwen2.5
model: deepseek-r1:7b
model: qwen2.5
#model: deepseek-r1:7b
max-tokens: 1024
temperature: 0.6
top-p: 0.7
frequency-penalty: 0
# 向量库知识库模型(注意:不同的模型维度不同)
embedding:
enabled: false
enabled: true
# 维度 dimensions 设置为 384
#model: all-minilm:33m
# 维度 dimensions 设置为 768
@@ -57,10 +62,11 @@ spring:
# Chroma 向量数据库
chroma:
client:
host: http://localhost
host: http://testserver
port: 8000
initialize-schema: true
collection-name: vector_store
#collection-name: vector_store
collection-name: vector_store_1024
# # Postgresql 向量数据库PG 连接配置,见下文,需要手动建表)
# pgvector: